Home | History | Annotate | Line # | Download | only in bfd
elf64-ppc.c revision 1.1.1.9
      1 /* PowerPC64-specific support for 64-bit ELF.
      2    Copyright (C) 1999-2020 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 /* All users of this file have bfd_octets_per_byte (abfd, sec) == 1.  */
     39 #define OCTETS_PER_BYTE(ABFD, SEC) 1
     40 
     41 static bfd_reloc_status_type ppc64_elf_ha_reloc
     42   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     43 static bfd_reloc_status_type ppc64_elf_branch_reloc
     44   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     45 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
     46   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     47 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
     48   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     49 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
     50   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     51 static bfd_reloc_status_type ppc64_elf_toc_reloc
     52   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     53 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
     54   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     55 static bfd_reloc_status_type ppc64_elf_toc64_reloc
     56   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     57 static bfd_reloc_status_type ppc64_elf_prefix_reloc
     58   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     59 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
     60   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     61 static bfd_vma opd_entry_value
     62   (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
     63 
     64 #define TARGET_LITTLE_SYM	powerpc_elf64_le_vec
     65 #define TARGET_LITTLE_NAME	"elf64-powerpcle"
     66 #define TARGET_BIG_SYM		powerpc_elf64_vec
     67 #define TARGET_BIG_NAME		"elf64-powerpc"
     68 #define ELF_ARCH		bfd_arch_powerpc
     69 #define ELF_TARGET_ID		PPC64_ELF_DATA
     70 #define ELF_MACHINE_CODE	EM_PPC64
     71 #define ELF_MAXPAGESIZE		0x10000
     72 #define ELF_COMMONPAGESIZE	0x1000
     73 #define ELF_RELROPAGESIZE	ELF_MAXPAGESIZE
     74 #define elf_info_to_howto	ppc64_elf_info_to_howto
     75 
     76 #define elf_backend_want_got_sym 0
     77 #define elf_backend_want_plt_sym 0
     78 #define elf_backend_plt_alignment 3
     79 #define elf_backend_plt_not_loaded 1
     80 #define elf_backend_got_header_size 8
     81 #define elf_backend_want_dynrelro 1
     82 #define elf_backend_can_gc_sections 1
     83 #define elf_backend_can_refcount 1
     84 #define elf_backend_rela_normal 1
     85 #define elf_backend_dtrel_excludes_plt 1
     86 #define elf_backend_default_execstack 0
     87 
     88 #define bfd_elf64_mkobject		      ppc64_elf_mkobject
     89 #define bfd_elf64_bfd_reloc_type_lookup	      ppc64_elf_reloc_type_lookup
     90 #define bfd_elf64_bfd_reloc_name_lookup	      ppc64_elf_reloc_name_lookup
     91 #define bfd_elf64_bfd_merge_private_bfd_data  ppc64_elf_merge_private_bfd_data
     92 #define bfd_elf64_bfd_print_private_bfd_data  ppc64_elf_print_private_bfd_data
     93 #define bfd_elf64_new_section_hook	      ppc64_elf_new_section_hook
     94 #define bfd_elf64_bfd_link_hash_table_create  ppc64_elf_link_hash_table_create
     95 #define bfd_elf64_get_synthetic_symtab	      ppc64_elf_get_synthetic_symtab
     96 #define bfd_elf64_bfd_link_just_syms	      ppc64_elf_link_just_syms
     97 #define bfd_elf64_bfd_gc_sections	      ppc64_elf_gc_sections
     98 
     99 #define elf_backend_object_p		      ppc64_elf_object_p
    100 #define elf_backend_grok_prstatus	      ppc64_elf_grok_prstatus
    101 #define elf_backend_grok_psinfo		      ppc64_elf_grok_psinfo
    102 #define elf_backend_write_core_note	      ppc64_elf_write_core_note
    103 #define elf_backend_create_dynamic_sections   _bfd_elf_create_dynamic_sections
    104 #define elf_backend_copy_indirect_symbol      ppc64_elf_copy_indirect_symbol
    105 #define elf_backend_add_symbol_hook	      ppc64_elf_add_symbol_hook
    106 #define elf_backend_check_directives	      ppc64_elf_before_check_relocs
    107 #define elf_backend_notice_as_needed	      ppc64_elf_notice_as_needed
    108 #define elf_backend_archive_symbol_lookup     ppc64_elf_archive_symbol_lookup
    109 #define elf_backend_check_relocs	      ppc64_elf_check_relocs
    110 #define elf_backend_relocs_compatible	      _bfd_elf_relocs_compatible
    111 #define elf_backend_gc_keep		      ppc64_elf_gc_keep
    112 #define elf_backend_gc_mark_dynamic_ref       ppc64_elf_gc_mark_dynamic_ref
    113 #define elf_backend_gc_mark_hook	      ppc64_elf_gc_mark_hook
    114 #define elf_backend_adjust_dynamic_symbol     ppc64_elf_adjust_dynamic_symbol
    115 #define elf_backend_hide_symbol		      ppc64_elf_hide_symbol
    116 #define elf_backend_maybe_function_sym	      ppc64_elf_maybe_function_sym
    117 #define elf_backend_always_size_sections      ppc64_elf_func_desc_adjust
    118 #define elf_backend_size_dynamic_sections     ppc64_elf_size_dynamic_sections
    119 #define elf_backend_hash_symbol		      ppc64_elf_hash_symbol
    120 #define elf_backend_init_index_section	      _bfd_elf_init_2_index_sections
    121 #define elf_backend_action_discarded	      ppc64_elf_action_discarded
    122 #define elf_backend_relocate_section	      ppc64_elf_relocate_section
    123 #define elf_backend_finish_dynamic_symbol     ppc64_elf_finish_dynamic_symbol
    124 #define elf_backend_reloc_type_class	      ppc64_elf_reloc_type_class
    125 #define elf_backend_finish_dynamic_sections   ppc64_elf_finish_dynamic_sections
    126 #define elf_backend_link_output_symbol_hook   ppc64_elf_output_symbol_hook
    127 #define elf_backend_special_sections	      ppc64_elf_special_sections
    128 #define elf_backend_section_flags	      ppc64_elf_section_flags
    129 #define elf_backend_merge_symbol_attribute    ppc64_elf_merge_symbol_attribute
    130 #define elf_backend_merge_symbol	      ppc64_elf_merge_symbol
    131 #define elf_backend_get_reloc_section	      bfd_get_section_by_name
    132 
    133 /* The name of the dynamic interpreter.  This is put in the .interp
    134    section.  */
    135 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
    136 
    137 /* The size in bytes of an entry in the procedure linkage table.  */
    138 #define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
    139 #define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
    140 
    141 /* The initial size of the plt reserved for the dynamic linker.  */
    142 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
    143 
    144 /* Offsets to some stack save slots.  */
    145 #define STK_LR 16
    146 #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
    147 /* This one is dodgy.  ELFv2 does not have a linker word, so use the
    148    CR save slot.  Used only by optimised __tls_get_addr call stub,
    149    relying on __tls_get_addr_opt not saving CR..  */
    150 #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
    151 
    152 /* TOC base pointers offset from start of TOC.  */
    153 #define TOC_BASE_OFF	0x8000
    154 /* TOC base alignment.  */
    155 #define TOC_BASE_ALIGN	256
    156 
    157 /* Offset of tp and dtp pointers from start of TLS block.  */
    158 #define TP_OFFSET	0x7000
    159 #define DTP_OFFSET	0x8000
    160 
    161 /* .plt call stub instructions.  The normal stub is like this, but
    162    sometimes the .plt entry crosses a 64k boundary and we need to
    163    insert an addi to adjust r11.  */
    164 #define STD_R2_0R1	0xf8410000	/* std	 %r2,0+40(%r1)	     */
    165 #define ADDIS_R11_R2	0x3d620000	/* addis %r11,%r2,xxx@ha     */
    166 #define LD_R12_0R11	0xe98b0000	/* ld	 %r12,xxx+0@l(%r11)  */
    167 #define MTCTR_R12	0x7d8903a6	/* mtctr %r12		     */
    168 #define LD_R2_0R11	0xe84b0000	/* ld	 %r2,xxx+8@l(%r11)   */
    169 #define LD_R11_0R11	0xe96b0000	/* ld	 %r11,xxx+16@l(%r11) */
    170 #define BCTR		0x4e800420	/* bctr			     */
    171 
    172 #define ADDI_R11_R11	0x396b0000	/* addi %r11,%r11,off@l	 */
    173 #define ADDI_R12_R11	0x398b0000	/* addi %r12,%r11,off@l	 */
    174 #define ADDI_R12_R12	0x398c0000	/* addi %r12,%r12,off@l	 */
    175 #define ADDIS_R2_R2	0x3c420000	/* addis %r2,%r2,off@ha	 */
    176 #define ADDI_R2_R2	0x38420000	/* addi	 %r2,%r2,off@l	 */
    177 
    178 #define XOR_R2_R12_R12	0x7d826278	/* xor	 %r2,%r12,%r12	 */
    179 #define ADD_R11_R11_R2	0x7d6b1214	/* add	 %r11,%r11,%r2	 */
    180 #define XOR_R11_R12_R12	0x7d8b6278	/* xor	 %r11,%r12,%r12	 */
    181 #define ADD_R2_R2_R11	0x7c425a14	/* add	 %r2,%r2,%r11	 */
    182 #define CMPLDI_R2_0	0x28220000	/* cmpldi %r2,0		 */
    183 #define BNECTR		0x4ca20420	/* bnectr+		 */
    184 #define BNECTR_P4	0x4ce20420	/* bnectr+		 */
    185 
    186 #define LD_R12_0R2	0xe9820000	/* ld	 %r12,xxx+0(%r2) */
    187 #define LD_R11_0R2	0xe9620000	/* ld	 %r11,xxx+0(%r2) */
    188 #define LD_R2_0R2	0xe8420000	/* ld	 %r2,xxx+0(%r2)	 */
    189 
    190 #define LD_R2_0R1	0xe8410000	/* ld	 %r2,0(%r1)	 */
    191 #define LD_R2_0R12	0xe84c0000	/* ld	 %r2,0(%r12)	 */
    192 #define ADD_R2_R2_R12	0x7c426214	/* add	 %r2,%r2,%r12	 */
    193 
    194 #define LI_R11_0	0x39600000	/* li    %r11,0		*/
    195 #define LIS_R2		0x3c400000	/* lis %r2,xxx@ha	  */
    196 #define LIS_R11		0x3d600000	/* lis %r11,xxx@ha	  */
    197 #define LIS_R12		0x3d800000	/* lis %r12,xxx@ha	  */
    198 #define ADDIS_R2_R12	0x3c4c0000	/* addis %r2,%r12,xxx@ha  */
    199 #define ADDIS_R12_R2	0x3d820000	/* addis %r12,%r2,xxx@ha  */
    200 #define ADDIS_R12_R11	0x3d8b0000	/* addis %r12,%r11,xxx@ha */
    201 #define ADDIS_R12_R12	0x3d8c0000	/* addis %r12,%r12,xxx@ha */
    202 #define ORIS_R12_R12_0	0x658c0000	/* oris  %r12,%r12,xxx@hi */
    203 #define ORI_R11_R11_0	0x616b0000	/* ori   %r11,%r11,xxx@l  */
    204 #define ORI_R12_R12_0	0x618c0000	/* ori   %r12,%r12,xxx@l  */
    205 #define LD_R12_0R12	0xe98c0000	/* ld	 %r12,xxx@l(%r12) */
    206 #define SLDI_R11_R11_34	0x796b1746	/* sldi  %r11,%r11,34     */
    207 #define SLDI_R12_R12_32	0x799c07c6	/* sldi  %r12,%r12,32     */
    208 #define LDX_R12_R11_R12 0x7d8b602a	/* ldx   %r12,%r11,%r12   */
    209 #define ADD_R12_R11_R12 0x7d8b6214	/* add   %r12,%r11,%r12   */
    210 #define PADDI_R12_PC	0x0610000039800000ULL
    211 #define PLD_R12_PC	0x04100000e5800000ULL
    212 #define PNOP		0x0700000000000000ULL
    213 
    214 /* __glink_PLTresolve stub instructions.  We enter with the index in R0.  */
    215 #define GLINK_PLTRESOLVE_SIZE(htab)			\
    216   (8u + (htab->opd_abi ? 11 * 4 : 14 * 4))
    217 					/* 0:				*/
    218 					/*  .quad plt0-1f		*/
    219 					/* __glink:			*/
    220 #define MFLR_R12	0x7d8802a6	/*  mflr %12			*/
    221 #define BCL_20_31	0x429f0005	/*  bcl 20,31,1f		*/
    222 					/* 1:				*/
    223 #define MFLR_R11	0x7d6802a6	/*  mflr %11			*/
    224 					/*  ld %2,(0b-1b)(%11)		*/
    225 #define MTLR_R12	0x7d8803a6	/*  mtlr %12			*/
    226 #define ADD_R11_R2_R11	0x7d625a14	/*  add %11,%2,%11		*/
    227 					/*  ld %12,0(%11)		*/
    228 					/*  ld %2,8(%11)		*/
    229 					/*  mtctr %12			*/
    230 					/*  ld %11,16(%11)		*/
    231 					/*  bctr			*/
    232 #define MFLR_R0		0x7c0802a6	/*  mflr %r0			*/
    233 #define MTLR_R0		0x7c0803a6	/*  mtlr %r0			*/
    234 #define SUB_R12_R12_R11	0x7d8b6050	/*  subf %r12,%r11,%r12		*/
    235 #define ADDI_R0_R12	0x380c0000	/*  addi %r0,%r12,0		*/
    236 #define SRDI_R0_R0_2	0x7800f082	/*  rldicl %r0,%r0,62,2		*/
    237 
    238 /* Pad with this.  */
    239 #define NOP		0x60000000
    240 
    241 /* Some other nops.  */
    242 #define CROR_151515	0x4def7b82
    243 #define CROR_313131	0x4ffffb82
    244 
    245 /* .glink entries for the first 32k functions are two instructions.  */
    246 #define LI_R0_0		0x38000000	/* li    %r0,0		*/
    247 #define B_DOT		0x48000000	/* b     .		*/
    248 
    249 /* After that, we need two instructions to load the index, followed by
    250    a branch.  */
    251 #define LIS_R0_0	0x3c000000	/* lis   %r0,0		*/
    252 #define ORI_R0_R0_0	0x60000000	/* ori	 %r0,%r0,0	*/
    253 
    254 /* Instructions used by the save and restore reg functions.  */
    255 #define STD_R0_0R1	0xf8010000	/* std   %r0,0(%r1)	*/
    256 #define STD_R0_0R12	0xf80c0000	/* std   %r0,0(%r12)	*/
    257 #define LD_R0_0R1	0xe8010000	/* ld    %r0,0(%r1)	*/
    258 #define LD_R0_0R12	0xe80c0000	/* ld    %r0,0(%r12)	*/
    259 #define STFD_FR0_0R1	0xd8010000	/* stfd  %fr0,0(%r1)	*/
    260 #define LFD_FR0_0R1	0xc8010000	/* lfd   %fr0,0(%r1)	*/
    261 #define LI_R12_0	0x39800000	/* li    %r12,0		*/
    262 #define STVX_VR0_R12_R0	0x7c0c01ce	/* stvx  %v0,%r12,%r0	*/
    263 #define LVX_VR0_R12_R0	0x7c0c00ce	/* lvx   %v0,%r12,%r0	*/
    264 #define MTLR_R0		0x7c0803a6	/* mtlr  %r0		*/
    265 #define BLR		0x4e800020	/* blr			*/
    266 
    267 /* Since .opd is an array of descriptors and each entry will end up
    268    with identical R_PPC64_RELATIVE relocs, there is really no need to
    269    propagate .opd relocs;  The dynamic linker should be taught to
    270    relocate .opd without reloc entries.  */
    271 #ifndef NO_OPD_RELOCS
    272 #define NO_OPD_RELOCS 0
    273 #endif
    274 
    275 #ifndef ARRAY_SIZE
    276 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
    277 #endif
    278 
    279 static inline int
    280 abiversion (bfd *abfd)
    281 {
    282   return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
    283 }
    284 
    285 static inline void
    286 set_abiversion (bfd *abfd, int ver)
    287 {
    288   elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
    289   elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
    290 }
    291 
    292 /* Relocation HOWTO's.  */
    294 /* Like other ELF RELA targets that don't apply multiple
    295    field-altering relocations to the same localation, src_mask is
    296    always zero and pcrel_offset is the same as pc_relative.
    297    PowerPC can always use a zero bitpos, even when the field is not at
    298    the LSB.  For example, a REL24 could use rightshift=2, bisize=24
    299    and bitpos=2 which matches the ABI description, or as we do here,
    300    rightshift=0, bitsize=26 and bitpos=0.  */
    301 #define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
    302 	    complain, special_func)				\
    303   HOWTO (type, rightshift, size, bitsize, pc_relative, 0,	\
    304 	 complain_overflow_ ## complain, special_func,		\
    305 	 #type, FALSE, 0, mask, pc_relative)
    306 
    307 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
    308 
    309 static reloc_howto_type ppc64_elf_howto_raw[] =
    310 {
    311   /* This reloc does nothing.  */
    312   HOW (R_PPC64_NONE, 3, 0, 0, 0, FALSE, dont,
    313        bfd_elf_generic_reloc),
    314 
    315   /* A standard 32 bit relocation.  */
    316   HOW (R_PPC64_ADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
    317        bfd_elf_generic_reloc),
    318 
    319   /* An absolute 26 bit branch; the lower two bits must be zero.
    320      FIXME: we don't check that, we just clear them.  */
    321   HOW (R_PPC64_ADDR24, 2, 26, 0x03fffffc, 0, FALSE, bitfield,
    322        bfd_elf_generic_reloc),
    323 
    324   /* A standard 16 bit relocation.  */
    325   HOW (R_PPC64_ADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
    326        bfd_elf_generic_reloc),
    327 
    328   /* A 16 bit relocation without overflow.  */
    329   HOW (R_PPC64_ADDR16_LO, 1, 16, 0xffff, 0, FALSE, dont,
    330        bfd_elf_generic_reloc),
    331 
    332   /* Bits 16-31 of an address.  */
    333   HOW (R_PPC64_ADDR16_HI, 1, 16, 0xffff, 16, FALSE, signed,
    334        bfd_elf_generic_reloc),
    335 
    336   /* Bits 16-31 of an address, plus 1 if the contents of the low 16
    337      bits, treated as a signed number, is negative.  */
    338   HOW (R_PPC64_ADDR16_HA, 1, 16, 0xffff, 16, FALSE, signed,
    339        ppc64_elf_ha_reloc),
    340 
    341   /* An absolute 16 bit branch; the lower two bits must be zero.
    342      FIXME: we don't check that, we just clear them.  */
    343   HOW (R_PPC64_ADDR14, 2, 16, 0x0000fffc, 0, FALSE, signed,
    344        ppc64_elf_branch_reloc),
    345 
    346   /* An absolute 16 bit branch, for which bit 10 should be set to
    347      indicate that the branch is expected to be taken.  The lower two
    348      bits must be zero.  */
    349   HOW (R_PPC64_ADDR14_BRTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
    350        ppc64_elf_brtaken_reloc),
    351 
    352   /* An absolute 16 bit branch, for which bit 10 should be set to
    353      indicate that the branch is not expected to be taken.  The lower
    354      two bits must be zero.  */
    355   HOW (R_PPC64_ADDR14_BRNTAKEN, 2, 16, 0x0000fffc, 0, FALSE, signed,
    356        ppc64_elf_brtaken_reloc),
    357 
    358   /* A relative 26 bit branch; the lower two bits must be zero.  */
    359   HOW (R_PPC64_REL24, 2, 26, 0x03fffffc, 0, TRUE, signed,
    360        ppc64_elf_branch_reloc),
    361 
    362   /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer.  */
    363   HOW (R_PPC64_REL24_NOTOC, 2, 26, 0x03fffffc, 0, TRUE, signed,
    364        ppc64_elf_branch_reloc),
    365 
    366   /* A relative 16 bit branch; the lower two bits must be zero.  */
    367   HOW (R_PPC64_REL14, 2, 16, 0x0000fffc, 0, TRUE, signed,
    368        ppc64_elf_branch_reloc),
    369 
    370   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
    371      the branch is expected to be taken.  The lower two bits must be
    372      zero.  */
    373   HOW (R_PPC64_REL14_BRTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
    374        ppc64_elf_brtaken_reloc),
    375 
    376   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
    377      the branch is not expected to be taken.  The lower two bits must
    378      be zero.  */
    379   HOW (R_PPC64_REL14_BRNTAKEN, 2, 16, 0x0000fffc, 0, TRUE, signed,
    380        ppc64_elf_brtaken_reloc),
    381 
    382   /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
    383      symbol.  */
    384   HOW (R_PPC64_GOT16, 1, 16, 0xffff, 0, FALSE, signed,
    385        ppc64_elf_unhandled_reloc),
    386 
    387   /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
    388      the symbol.  */
    389   HOW (R_PPC64_GOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
    390        ppc64_elf_unhandled_reloc),
    391 
    392   /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
    393      the symbol.  */
    394   HOW (R_PPC64_GOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
    395        ppc64_elf_unhandled_reloc),
    396 
    397   /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
    398      the symbol.  */
    399   HOW (R_PPC64_GOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
    400        ppc64_elf_unhandled_reloc),
    401 
    402   /* This is used only by the dynamic linker.  The symbol should exist
    403      both in the object being run and in some shared library.  The
    404      dynamic linker copies the data addressed by the symbol from the
    405      shared library into the object, because the object being
    406      run has to have the data at some particular address.  */
    407   HOW (R_PPC64_COPY, 0, 0, 0, 0, FALSE, dont,
    408        ppc64_elf_unhandled_reloc),
    409 
    410   /* Like R_PPC64_ADDR64, but used when setting global offset table
    411      entries.  */
    412   HOW (R_PPC64_GLOB_DAT, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
    413        ppc64_elf_unhandled_reloc),
    414 
    415   /* Created by the link editor.  Marks a procedure linkage table
    416      entry for a symbol.  */
    417   HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, FALSE, dont,
    418        ppc64_elf_unhandled_reloc),
    419 
    420   /* Used only by the dynamic linker.  When the object is run, this
    421      doubleword64 is set to the load address of the object, plus the
    422      addend.  */
    423   HOW (R_PPC64_RELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
    424        bfd_elf_generic_reloc),
    425 
    426   /* Like R_PPC64_ADDR32, but may be unaligned.  */
    427   HOW (R_PPC64_UADDR32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
    428        bfd_elf_generic_reloc),
    429 
    430   /* Like R_PPC64_ADDR16, but may be unaligned.  */
    431   HOW (R_PPC64_UADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
    432        bfd_elf_generic_reloc),
    433 
    434   /* 32-bit PC relative.  */
    435   HOW (R_PPC64_REL32, 2, 32, 0xffffffff, 0, TRUE, signed,
    436        bfd_elf_generic_reloc),
    437 
    438   /* 32-bit relocation to the symbol's procedure linkage table.  */
    439   HOW (R_PPC64_PLT32, 2, 32, 0xffffffff, 0, FALSE, bitfield,
    440        ppc64_elf_unhandled_reloc),
    441 
    442   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
    443      FIXME: R_PPC64_PLTREL32 not supported.  */
    444   HOW (R_PPC64_PLTREL32, 2, 32, 0xffffffff, 0, TRUE, signed,
    445        ppc64_elf_unhandled_reloc),
    446 
    447   /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
    448      the symbol.  */
    449   HOW (R_PPC64_PLT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
    450        ppc64_elf_unhandled_reloc),
    451 
    452   /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
    453      the symbol.  */
    454   HOW (R_PPC64_PLT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
    455        ppc64_elf_unhandled_reloc),
    456 
    457   /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
    458      the symbol.  */
    459   HOW (R_PPC64_PLT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
    460        ppc64_elf_unhandled_reloc),
    461 
    462   /* 16-bit section relative relocation.  */
    463   HOW (R_PPC64_SECTOFF, 1, 16, 0xffff, 0, FALSE, signed,
    464        ppc64_elf_sectoff_reloc),
    465 
    466   /* Like R_PPC64_SECTOFF, but no overflow warning.  */
    467   HOW (R_PPC64_SECTOFF_LO, 1, 16, 0xffff, 0, FALSE, dont,
    468        ppc64_elf_sectoff_reloc),
    469 
    470   /* 16-bit upper half section relative relocation.  */
    471   HOW (R_PPC64_SECTOFF_HI, 1, 16, 0xffff, 16, FALSE, signed,
    472        ppc64_elf_sectoff_reloc),
    473 
    474   /* 16-bit upper half adjusted section relative relocation.  */
    475   HOW (R_PPC64_SECTOFF_HA, 1, 16, 0xffff, 16, FALSE, signed,
    476        ppc64_elf_sectoff_ha_reloc),
    477 
    478   /* Like R_PPC64_REL24 without touching the two least significant bits.  */
    479   HOW (R_PPC64_REL30, 2, 30, 0xfffffffc, 2, TRUE, dont,
    480        bfd_elf_generic_reloc),
    481 
    482   /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
    483 
    484   /* A standard 64-bit relocation.  */
    485   HOW (R_PPC64_ADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
    486        bfd_elf_generic_reloc),
    487 
    488   /* The bits 32-47 of an address.  */
    489   HOW (R_PPC64_ADDR16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
    490        bfd_elf_generic_reloc),
    491 
    492   /* The bits 32-47 of an address, plus 1 if the contents of the low
    493      16 bits, treated as a signed number, is negative.  */
    494   HOW (R_PPC64_ADDR16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
    495        ppc64_elf_ha_reloc),
    496 
    497   /* The bits 48-63 of an address.  */
    498   HOW (R_PPC64_ADDR16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
    499        bfd_elf_generic_reloc),
    500 
    501   /* The bits 48-63 of an address, plus 1 if the contents of the low
    502      16 bits, treated as a signed number, is negative.  */
    503   HOW (R_PPC64_ADDR16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
    504        ppc64_elf_ha_reloc),
    505 
    506   /* Like ADDR64, but may be unaligned.  */
    507   HOW (R_PPC64_UADDR64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
    508        bfd_elf_generic_reloc),
    509 
    510   /* 64-bit relative relocation.  */
    511   HOW (R_PPC64_REL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
    512        bfd_elf_generic_reloc),
    513 
    514   /* 64-bit relocation to the symbol's procedure linkage table.  */
    515   HOW (R_PPC64_PLT64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
    516        ppc64_elf_unhandled_reloc),
    517 
    518   /* 64-bit PC relative relocation to the symbol's procedure linkage
    519      table.  */
    520   /* FIXME: R_PPC64_PLTREL64 not supported.  */
    521   HOW (R_PPC64_PLTREL64, 4, 64, 0xffffffffffffffffULL, 0, TRUE, dont,
    522        ppc64_elf_unhandled_reloc),
    523 
    524   /* 16 bit TOC-relative relocation.  */
    525   /* R_PPC64_TOC16	  47	   half16*	S + A - .TOC.  */
    526   HOW (R_PPC64_TOC16, 1, 16, 0xffff, 0, FALSE, signed,
    527        ppc64_elf_toc_reloc),
    528 
    529   /* 16 bit TOC-relative relocation without overflow.  */
    530   /* R_PPC64_TOC16_LO	  48	   half16	 #lo (S + A - .TOC.)  */
    531   HOW (R_PPC64_TOC16_LO, 1, 16, 0xffff, 0, FALSE, dont,
    532        ppc64_elf_toc_reloc),
    533 
    534   /* 16 bit TOC-relative relocation, high 16 bits.  */
    535   /* R_PPC64_TOC16_HI	  49	   half16	 #hi (S + A - .TOC.)  */
    536   HOW (R_PPC64_TOC16_HI, 1, 16, 0xffff, 16, FALSE, signed,
    537        ppc64_elf_toc_reloc),
    538 
    539   /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
    540      contents of the low 16 bits, treated as a signed number, is
    541      negative.  */
    542   /* R_PPC64_TOC16_HA	  50	   half16	 #ha (S + A - .TOC.)  */
    543   HOW (R_PPC64_TOC16_HA, 1, 16, 0xffff, 16, FALSE, signed,
    544        ppc64_elf_toc_ha_reloc),
    545 
    546   /* 64-bit relocation; insert value of TOC base (.TOC.).  */
    547   /* R_PPC64_TOC		  51	   doubleword64	 .TOC.  */
    548   HOW (R_PPC64_TOC, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
    549        ppc64_elf_toc64_reloc),
    550 
    551   /* Like R_PPC64_GOT16, but also informs the link editor that the
    552      value to relocate may (!) refer to a PLT entry which the link
    553      editor (a) may replace with the symbol value.  If the link editor
    554      is unable to fully resolve the symbol, it may (b) create a PLT
    555      entry and store the address to the new PLT entry in the GOT.
    556      This permits lazy resolution of function symbols at run time.
    557      The link editor may also skip all of this and just (c) emit a
    558      R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
    559   /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
    560     HOW (R_PPC64_PLTGOT16, 1, 16, 0xffff, 0, FALSE,signed,
    561 	  ppc64_elf_unhandled_reloc),
    562 
    563   /* Like R_PPC64_PLTGOT16, but without overflow.  */
    564   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
    565   HOW (R_PPC64_PLTGOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
    566        ppc64_elf_unhandled_reloc),
    567 
    568   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
    569   /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
    570   HOW (R_PPC64_PLTGOT16_HI, 1, 16, 0xffff, 16, FALSE, signed,
    571        ppc64_elf_unhandled_reloc),
    572 
    573   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
    574      1 if the contents of the low 16 bits, treated as a signed number,
    575      is negative.  */
    576   /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
    577   HOW (R_PPC64_PLTGOT16_HA, 1, 16, 0xffff, 16, FALSE, signed,
    578        ppc64_elf_unhandled_reloc),
    579 
    580   /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
    581   HOW (R_PPC64_ADDR16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
    582        bfd_elf_generic_reloc),
    583 
    584   /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
    585   HOW (R_PPC64_ADDR16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
    586        bfd_elf_generic_reloc),
    587 
    588   /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
    589   HOW (R_PPC64_GOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
    590        ppc64_elf_unhandled_reloc),
    591 
    592   /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
    593   HOW (R_PPC64_GOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
    594        ppc64_elf_unhandled_reloc),
    595 
    596   /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
    597   HOW (R_PPC64_PLT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
    598        ppc64_elf_unhandled_reloc),
    599 
    600   /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
    601   HOW (R_PPC64_SECTOFF_DS, 1, 16, 0xfffc, 0, FALSE, signed,
    602        ppc64_elf_sectoff_reloc),
    603 
    604   /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
    605   HOW (R_PPC64_SECTOFF_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
    606        ppc64_elf_sectoff_reloc),
    607 
    608   /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
    609   HOW (R_PPC64_TOC16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
    610        ppc64_elf_toc_reloc),
    611 
    612   /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
    613   HOW (R_PPC64_TOC16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
    614        ppc64_elf_toc_reloc),
    615 
    616   /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
    617   /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
    618   HOW (R_PPC64_PLTGOT16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
    619        ppc64_elf_unhandled_reloc),
    620 
    621   /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
    622   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
    623   HOW (R_PPC64_PLTGOT16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
    624        ppc64_elf_unhandled_reloc),
    625 
    626   /* Marker relocs for TLS.  */
    627   HOW (R_PPC64_TLS, 2, 32, 0, 0, FALSE, dont,
    628        bfd_elf_generic_reloc),
    629 
    630   HOW (R_PPC64_TLSGD, 2, 32, 0, 0, FALSE, dont,
    631        bfd_elf_generic_reloc),
    632 
    633   HOW (R_PPC64_TLSLD, 2, 32, 0, 0, FALSE, dont,
    634        bfd_elf_generic_reloc),
    635 
    636   /* Marker reloc for optimizing r2 save in prologue rather than on
    637      each plt call stub.  */
    638   HOW (R_PPC64_TOCSAVE, 2, 32, 0, 0, FALSE, dont,
    639        bfd_elf_generic_reloc),
    640 
    641   /* Marker relocs on inline plt call instructions.  */
    642   HOW (R_PPC64_PLTSEQ, 2, 32, 0, 0, FALSE, dont,
    643        bfd_elf_generic_reloc),
    644 
    645   HOW (R_PPC64_PLTCALL, 2, 32, 0, 0, FALSE, dont,
    646        bfd_elf_generic_reloc),
    647 
    648   /* Computes the load module index of the load module that contains the
    649      definition of its TLS sym.  */
    650   HOW (R_PPC64_DTPMOD64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
    651        ppc64_elf_unhandled_reloc),
    652 
    653   /* Computes a dtv-relative displacement, the difference between the value
    654      of sym+add and the base address of the thread-local storage block that
    655      contains the definition of sym, minus 0x8000.  */
    656   HOW (R_PPC64_DTPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
    657        ppc64_elf_unhandled_reloc),
    658 
    659   /* A 16 bit dtprel reloc.  */
    660   HOW (R_PPC64_DTPREL16, 1, 16, 0xffff, 0, FALSE, signed,
    661        ppc64_elf_unhandled_reloc),
    662 
    663   /* Like DTPREL16, but no overflow.  */
    664   HOW (R_PPC64_DTPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
    665        ppc64_elf_unhandled_reloc),
    666 
    667   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
    668   HOW (R_PPC64_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
    669        ppc64_elf_unhandled_reloc),
    670 
    671   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
    672   HOW (R_PPC64_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
    673        ppc64_elf_unhandled_reloc),
    674 
    675   /* Like DTPREL16_HI, but next higher group of 16 bits.  */
    676   HOW (R_PPC64_DTPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
    677        ppc64_elf_unhandled_reloc),
    678 
    679   /* Like DTPREL16_HIGHER, but adjust for low 16 bits.  */
    680   HOW (R_PPC64_DTPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
    681        ppc64_elf_unhandled_reloc),
    682 
    683   /* Like DTPREL16_HIGHER, but next higher group of 16 bits.  */
    684   HOW (R_PPC64_DTPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
    685        ppc64_elf_unhandled_reloc),
    686 
    687   /* Like DTPREL16_HIGHEST, but adjust for low 16 bits.  */
    688   HOW (R_PPC64_DTPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
    689        ppc64_elf_unhandled_reloc),
    690 
    691   /* Like DTPREL16, but for insns with a DS field.  */
    692   HOW (R_PPC64_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
    693        ppc64_elf_unhandled_reloc),
    694 
    695   /* Like DTPREL16_DS, but no overflow.  */
    696   HOW (R_PPC64_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
    697        ppc64_elf_unhandled_reloc),
    698 
    699   /* Computes a tp-relative displacement, the difference between the value of
    700      sym+add and the value of the thread pointer (r13).  */
    701   HOW (R_PPC64_TPREL64, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
    702        ppc64_elf_unhandled_reloc),
    703 
    704   /* A 16 bit tprel reloc.  */
    705   HOW (R_PPC64_TPREL16, 1, 16, 0xffff, 0, FALSE, signed,
    706        ppc64_elf_unhandled_reloc),
    707 
    708   /* Like TPREL16, but no overflow.  */
    709   HOW (R_PPC64_TPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
    710        ppc64_elf_unhandled_reloc),
    711 
    712   /* Like TPREL16_LO, but next higher group of 16 bits.  */
    713   HOW (R_PPC64_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
    714        ppc64_elf_unhandled_reloc),
    715 
    716   /* Like TPREL16_HI, but adjust for low 16 bits.  */
    717   HOW (R_PPC64_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
    718        ppc64_elf_unhandled_reloc),
    719 
    720   /* Like TPREL16_HI, but next higher group of 16 bits.  */
    721   HOW (R_PPC64_TPREL16_HIGHER, 1, 16, 0xffff, 32, FALSE, dont,
    722        ppc64_elf_unhandled_reloc),
    723 
    724   /* Like TPREL16_HIGHER, but adjust for low 16 bits.  */
    725   HOW (R_PPC64_TPREL16_HIGHERA, 1, 16, 0xffff, 32, FALSE, dont,
    726        ppc64_elf_unhandled_reloc),
    727 
    728   /* Like TPREL16_HIGHER, but next higher group of 16 bits.  */
    729   HOW (R_PPC64_TPREL16_HIGHEST, 1, 16, 0xffff, 48, FALSE, dont,
    730        ppc64_elf_unhandled_reloc),
    731 
    732   /* Like TPREL16_HIGHEST, but adjust for low 16 bits.  */
    733   HOW (R_PPC64_TPREL16_HIGHESTA, 1, 16, 0xffff, 48, FALSE, dont,
    734        ppc64_elf_unhandled_reloc),
    735 
    736   /* Like TPREL16, but for insns with a DS field.  */
    737   HOW (R_PPC64_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
    738        ppc64_elf_unhandled_reloc),
    739 
    740   /* Like TPREL16_DS, but no overflow.  */
    741   HOW (R_PPC64_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
    742        ppc64_elf_unhandled_reloc),
    743 
    744   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
    745      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
    746      to the first entry relative to the TOC base (r2).  */
    747   HOW (R_PPC64_GOT_TLSGD16, 1, 16, 0xffff, 0, FALSE, signed,
    748        ppc64_elf_unhandled_reloc),
    749 
    750   /* Like GOT_TLSGD16, but no overflow.  */
    751   HOW (R_PPC64_GOT_TLSGD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
    752        ppc64_elf_unhandled_reloc),
    753 
    754   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
    755   HOW (R_PPC64_GOT_TLSGD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
    756        ppc64_elf_unhandled_reloc),
    757 
    758   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
    759   HOW (R_PPC64_GOT_TLSGD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
    760        ppc64_elf_unhandled_reloc),
    761 
    762   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
    763      with values (sym+add)@dtpmod and zero, and computes the offset to the
    764      first entry relative to the TOC base (r2).  */
    765   HOW (R_PPC64_GOT_TLSLD16, 1, 16, 0xffff, 0, FALSE, signed,
    766        ppc64_elf_unhandled_reloc),
    767 
    768   /* Like GOT_TLSLD16, but no overflow.  */
    769   HOW (R_PPC64_GOT_TLSLD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
    770        ppc64_elf_unhandled_reloc),
    771 
    772   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
    773   HOW (R_PPC64_GOT_TLSLD16_HI, 1, 16, 0xffff, 16, FALSE, signed,
    774        ppc64_elf_unhandled_reloc),
    775 
    776   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
    777   HOW (R_PPC64_GOT_TLSLD16_HA, 1, 16, 0xffff, 16, FALSE, signed,
    778        ppc64_elf_unhandled_reloc),
    779 
    780   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
    781      the offset to the entry relative to the TOC base (r2).  */
    782   HOW (R_PPC64_GOT_DTPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
    783        ppc64_elf_unhandled_reloc),
    784 
    785   /* Like GOT_DTPREL16_DS, but no overflow.  */
    786   HOW (R_PPC64_GOT_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
    787        ppc64_elf_unhandled_reloc),
    788 
    789   /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits.  */
    790   HOW (R_PPC64_GOT_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
    791        ppc64_elf_unhandled_reloc),
    792 
    793   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
    794   HOW (R_PPC64_GOT_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
    795        ppc64_elf_unhandled_reloc),
    796 
    797   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
    798      offset to the entry relative to the TOC base (r2).  */
    799   HOW (R_PPC64_GOT_TPREL16_DS, 1, 16, 0xfffc, 0, FALSE, signed,
    800        ppc64_elf_unhandled_reloc),
    801 
    802   /* Like GOT_TPREL16_DS, but no overflow.  */
    803   HOW (R_PPC64_GOT_TPREL16_LO_DS, 1, 16, 0xfffc, 0, FALSE, dont,
    804        ppc64_elf_unhandled_reloc),
    805 
    806   /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits.  */
    807   HOW (R_PPC64_GOT_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, signed,
    808        ppc64_elf_unhandled_reloc),
    809 
    810   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
    811   HOW (R_PPC64_GOT_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, signed,
    812        ppc64_elf_unhandled_reloc),
    813 
    814   HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, FALSE, dont,
    815        ppc64_elf_unhandled_reloc),
    816 
    817   HOW (R_PPC64_IRELATIVE, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
    818        bfd_elf_generic_reloc),
    819 
    820   /* A 16 bit relative relocation.  */
    821   HOW (R_PPC64_REL16, 1, 16, 0xffff, 0, TRUE, signed,
    822        bfd_elf_generic_reloc),
    823 
    824   /* A 16 bit relative relocation without overflow.  */
    825   HOW (R_PPC64_REL16_LO, 1, 16, 0xffff, 0, TRUE, dont,
    826        bfd_elf_generic_reloc),
    827 
    828   /* The high order 16 bits of a relative address.  */
    829   HOW (R_PPC64_REL16_HI, 1, 16, 0xffff, 16, TRUE, signed,
    830        bfd_elf_generic_reloc),
    831 
    832   /* The high order 16 bits of a relative address, plus 1 if the contents of
    833      the low 16 bits, treated as a signed number, is negative.  */
    834   HOW (R_PPC64_REL16_HA, 1, 16, 0xffff, 16, TRUE, signed,
    835        ppc64_elf_ha_reloc),
    836 
    837   HOW (R_PPC64_REL16_HIGH, 1, 16, 0xffff, 16, TRUE, dont,
    838        bfd_elf_generic_reloc),
    839 
    840   HOW (R_PPC64_REL16_HIGHA, 1, 16, 0xffff, 16, TRUE, dont,
    841        ppc64_elf_ha_reloc),
    842 
    843   HOW (R_PPC64_REL16_HIGHER, 1, 16, 0xffff, 32, TRUE, dont,
    844        bfd_elf_generic_reloc),
    845 
    846   HOW (R_PPC64_REL16_HIGHERA, 1, 16, 0xffff, 32, TRUE, dont,
    847        ppc64_elf_ha_reloc),
    848 
    849   HOW (R_PPC64_REL16_HIGHEST, 1, 16, 0xffff, 48, TRUE, dont,
    850        bfd_elf_generic_reloc),
    851 
    852   HOW (R_PPC64_REL16_HIGHESTA, 1, 16, 0xffff, 48, TRUE, dont,
    853        ppc64_elf_ha_reloc),
    854 
    855   /* Like R_PPC64_REL16_HA but for split field in addpcis.  */
    856   HOW (R_PPC64_REL16DX_HA, 2, 16, 0x1fffc1, 16, TRUE, signed,
    857        ppc64_elf_ha_reloc),
    858 
    859   /* A split-field reloc for addpcis, non-relative (gas internal use only).  */
    860   HOW (R_PPC64_16DX_HA, 2, 16, 0x1fffc1, 16, FALSE, signed,
    861        ppc64_elf_ha_reloc),
    862 
    863   /* Like R_PPC64_ADDR16_HI, but no overflow.  */
    864   HOW (R_PPC64_ADDR16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
    865        bfd_elf_generic_reloc),
    866 
    867   /* Like R_PPC64_ADDR16_HA, but no overflow.  */
    868   HOW (R_PPC64_ADDR16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
    869        ppc64_elf_ha_reloc),
    870 
    871   /* Like R_PPC64_DTPREL16_HI, but no overflow.  */
    872   HOW (R_PPC64_DTPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
    873        ppc64_elf_unhandled_reloc),
    874 
    875   /* Like R_PPC64_DTPREL16_HA, but no overflow.  */
    876   HOW (R_PPC64_DTPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
    877        ppc64_elf_unhandled_reloc),
    878 
    879   /* Like R_PPC64_TPREL16_HI, but no overflow.  */
    880   HOW (R_PPC64_TPREL16_HIGH, 1, 16, 0xffff, 16, FALSE, dont,
    881        ppc64_elf_unhandled_reloc),
    882 
    883   /* Like R_PPC64_TPREL16_HA, but no overflow.  */
    884   HOW (R_PPC64_TPREL16_HIGHA, 1, 16, 0xffff, 16, FALSE, dont,
    885        ppc64_elf_unhandled_reloc),
    886 
    887   /* Marker reloc on ELFv2 large-model function entry.  */
    888   HOW (R_PPC64_ENTRY, 2, 32, 0, 0, FALSE, dont,
    889        bfd_elf_generic_reloc),
    890 
    891   /* Like ADDR64, but use local entry point of function.  */
    892   HOW (R_PPC64_ADDR64_LOCAL, 4, 64, 0xffffffffffffffffULL, 0, FALSE, dont,
    893        bfd_elf_generic_reloc),
    894 
    895   HOW (R_PPC64_PLTSEQ_NOTOC, 2, 32, 0, 0, FALSE, dont,
    896        bfd_elf_generic_reloc),
    897 
    898   HOW (R_PPC64_PLTCALL_NOTOC, 2, 32, 0, 0, FALSE, dont,
    899        bfd_elf_generic_reloc),
    900 
    901   HOW (R_PPC64_PCREL_OPT, 2, 32, 0, 0, FALSE, dont,
    902        bfd_elf_generic_reloc),
    903 
    904   HOW (R_PPC64_D34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
    905        ppc64_elf_prefix_reloc),
    906 
    907   HOW (R_PPC64_D34_LO, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, dont,
    908        ppc64_elf_prefix_reloc),
    909 
    910   HOW (R_PPC64_D34_HI30, 4, 34, 0x3ffff0000ffffULL, 34, FALSE, dont,
    911        ppc64_elf_prefix_reloc),
    912 
    913   HOW (R_PPC64_D34_HA30, 4, 34, 0x3ffff0000ffffULL, 34, FALSE, dont,
    914        ppc64_elf_prefix_reloc),
    915 
    916   HOW (R_PPC64_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
    917        ppc64_elf_prefix_reloc),
    918 
    919   HOW (R_PPC64_GOT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
    920        ppc64_elf_unhandled_reloc),
    921 
    922   HOW (R_PPC64_PLT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
    923        ppc64_elf_unhandled_reloc),
    924 
    925   HOW (R_PPC64_PLT_PCREL34_NOTOC, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
    926        ppc64_elf_unhandled_reloc),
    927 
    928   HOW (R_PPC64_TPREL34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
    929        ppc64_elf_unhandled_reloc),
    930 
    931   HOW (R_PPC64_DTPREL34, 4, 34, 0x3ffff0000ffffULL, 0, FALSE, signed,
    932        ppc64_elf_unhandled_reloc),
    933 
    934   HOW (R_PPC64_GOT_TLSGD_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
    935        ppc64_elf_unhandled_reloc),
    936 
    937   HOW (R_PPC64_GOT_TLSLD_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
    938        ppc64_elf_unhandled_reloc),
    939 
    940   HOW (R_PPC64_GOT_TPREL_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
    941        ppc64_elf_unhandled_reloc),
    942 
    943   HOW (R_PPC64_GOT_DTPREL_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, TRUE, signed,
    944        ppc64_elf_unhandled_reloc),
    945 
    946   HOW (R_PPC64_ADDR16_HIGHER34, 1, 16, 0xffff, 34, FALSE, dont,
    947        bfd_elf_generic_reloc),
    948 
    949   HOW (R_PPC64_ADDR16_HIGHERA34, 1, 16, 0xffff, 34, FALSE, dont,
    950        ppc64_elf_ha_reloc),
    951 
    952   HOW (R_PPC64_ADDR16_HIGHEST34, 1, 16, 0xffff, 50, FALSE, dont,
    953        bfd_elf_generic_reloc),
    954 
    955   HOW (R_PPC64_ADDR16_HIGHESTA34, 1, 16, 0xffff, 50, FALSE, dont,
    956        ppc64_elf_ha_reloc),
    957 
    958   HOW (R_PPC64_REL16_HIGHER34, 1, 16, 0xffff, 34, TRUE, dont,
    959        bfd_elf_generic_reloc),
    960 
    961   HOW (R_PPC64_REL16_HIGHERA34, 1, 16, 0xffff, 34, TRUE, dont,
    962        ppc64_elf_ha_reloc),
    963 
    964   HOW (R_PPC64_REL16_HIGHEST34, 1, 16, 0xffff, 50, TRUE, dont,
    965        bfd_elf_generic_reloc),
    966 
    967   HOW (R_PPC64_REL16_HIGHESTA34, 1, 16, 0xffff, 50, TRUE, dont,
    968        ppc64_elf_ha_reloc),
    969 
    970   HOW (R_PPC64_D28, 4, 28, 0xfff0000ffffULL, 0, FALSE, signed,
    971        ppc64_elf_prefix_reloc),
    972 
    973   HOW (R_PPC64_PCREL28, 4, 28, 0xfff0000ffffULL, 0, TRUE, signed,
    974        ppc64_elf_prefix_reloc),
    975 
    976   /* GNU extension to record C++ vtable hierarchy.  */
    977   HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, FALSE, dont,
    978        NULL),
    979 
    980   /* GNU extension to record C++ vtable member usage.  */
    981   HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, FALSE, dont,
    982        NULL),
    983 };
    984 
    985 
    986 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
    988    be done.  */
    989 
    990 static void
    991 ppc_howto_init (void)
    992 {
    993   unsigned int i, type;
    994 
    995   for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
    996     {
    997       type = ppc64_elf_howto_raw[i].type;
    998       BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
    999       ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
   1000     }
   1001 }
   1002 
   1003 static reloc_howto_type *
   1004 ppc64_elf_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
   1005 {
   1006   enum elf_ppc64_reloc_type r = R_PPC64_NONE;
   1007 
   1008   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
   1009     /* Initialize howto table if needed.  */
   1010     ppc_howto_init ();
   1011 
   1012   switch (code)
   1013     {
   1014     default:
   1015       /* xgettext:c-format */
   1016       _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
   1017 			  (int) code);
   1018       bfd_set_error (bfd_error_bad_value);
   1019       return NULL;
   1020 
   1021     case BFD_RELOC_NONE:			r = R_PPC64_NONE;
   1022       break;
   1023     case BFD_RELOC_32:				r = R_PPC64_ADDR32;
   1024       break;
   1025     case BFD_RELOC_PPC_BA26:			r = R_PPC64_ADDR24;
   1026       break;
   1027     case BFD_RELOC_16:				r = R_PPC64_ADDR16;
   1028       break;
   1029     case BFD_RELOC_LO16:			r = R_PPC64_ADDR16_LO;
   1030       break;
   1031     case BFD_RELOC_HI16:			r = R_PPC64_ADDR16_HI;
   1032       break;
   1033     case BFD_RELOC_PPC64_ADDR16_HIGH:		r = R_PPC64_ADDR16_HIGH;
   1034       break;
   1035     case BFD_RELOC_HI16_S:			r = R_PPC64_ADDR16_HA;
   1036       break;
   1037     case BFD_RELOC_PPC64_ADDR16_HIGHA:		r = R_PPC64_ADDR16_HIGHA;
   1038       break;
   1039     case BFD_RELOC_PPC_BA16:			r = R_PPC64_ADDR14;
   1040       break;
   1041     case BFD_RELOC_PPC_BA16_BRTAKEN:		r = R_PPC64_ADDR14_BRTAKEN;
   1042       break;
   1043     case BFD_RELOC_PPC_BA16_BRNTAKEN:		r = R_PPC64_ADDR14_BRNTAKEN;
   1044       break;
   1045     case BFD_RELOC_PPC_B26:			r = R_PPC64_REL24;
   1046       break;
   1047     case BFD_RELOC_PPC64_REL24_NOTOC:		r = R_PPC64_REL24_NOTOC;
   1048       break;
   1049     case BFD_RELOC_PPC_B16:			r = R_PPC64_REL14;
   1050       break;
   1051     case BFD_RELOC_PPC_B16_BRTAKEN:		r = R_PPC64_REL14_BRTAKEN;
   1052       break;
   1053     case BFD_RELOC_PPC_B16_BRNTAKEN:		r = R_PPC64_REL14_BRNTAKEN;
   1054       break;
   1055     case BFD_RELOC_16_GOTOFF:			r = R_PPC64_GOT16;
   1056       break;
   1057     case BFD_RELOC_LO16_GOTOFF:			r = R_PPC64_GOT16_LO;
   1058       break;
   1059     case BFD_RELOC_HI16_GOTOFF:			r = R_PPC64_GOT16_HI;
   1060       break;
   1061     case BFD_RELOC_HI16_S_GOTOFF:		r = R_PPC64_GOT16_HA;
   1062       break;
   1063     case BFD_RELOC_PPC_COPY:			r = R_PPC64_COPY;
   1064       break;
   1065     case BFD_RELOC_PPC_GLOB_DAT:		r = R_PPC64_GLOB_DAT;
   1066       break;
   1067     case BFD_RELOC_32_PCREL:			r = R_PPC64_REL32;
   1068       break;
   1069     case BFD_RELOC_32_PLTOFF:			r = R_PPC64_PLT32;
   1070       break;
   1071     case BFD_RELOC_32_PLT_PCREL:		r = R_PPC64_PLTREL32;
   1072       break;
   1073     case BFD_RELOC_LO16_PLTOFF:			r = R_PPC64_PLT16_LO;
   1074       break;
   1075     case BFD_RELOC_HI16_PLTOFF:			r = R_PPC64_PLT16_HI;
   1076       break;
   1077     case BFD_RELOC_HI16_S_PLTOFF:		r = R_PPC64_PLT16_HA;
   1078       break;
   1079     case BFD_RELOC_16_BASEREL:			r = R_PPC64_SECTOFF;
   1080       break;
   1081     case BFD_RELOC_LO16_BASEREL:		r = R_PPC64_SECTOFF_LO;
   1082       break;
   1083     case BFD_RELOC_HI16_BASEREL:		r = R_PPC64_SECTOFF_HI;
   1084       break;
   1085     case BFD_RELOC_HI16_S_BASEREL:		r = R_PPC64_SECTOFF_HA;
   1086       break;
   1087     case BFD_RELOC_CTOR:			r = R_PPC64_ADDR64;
   1088       break;
   1089     case BFD_RELOC_64:				r = R_PPC64_ADDR64;
   1090       break;
   1091     case BFD_RELOC_PPC64_HIGHER:		r = R_PPC64_ADDR16_HIGHER;
   1092       break;
   1093     case BFD_RELOC_PPC64_HIGHER_S:		r = R_PPC64_ADDR16_HIGHERA;
   1094       break;
   1095     case BFD_RELOC_PPC64_HIGHEST:		r = R_PPC64_ADDR16_HIGHEST;
   1096       break;
   1097     case BFD_RELOC_PPC64_HIGHEST_S:		r = R_PPC64_ADDR16_HIGHESTA;
   1098       break;
   1099     case BFD_RELOC_64_PCREL:			r = R_PPC64_REL64;
   1100       break;
   1101     case BFD_RELOC_64_PLTOFF:			r = R_PPC64_PLT64;
   1102       break;
   1103     case BFD_RELOC_64_PLT_PCREL:		r = R_PPC64_PLTREL64;
   1104       break;
   1105     case BFD_RELOC_PPC_TOC16:			r = R_PPC64_TOC16;
   1106       break;
   1107     case BFD_RELOC_PPC64_TOC16_LO:		r = R_PPC64_TOC16_LO;
   1108       break;
   1109     case BFD_RELOC_PPC64_TOC16_HI:		r = R_PPC64_TOC16_HI;
   1110       break;
   1111     case BFD_RELOC_PPC64_TOC16_HA:		r = R_PPC64_TOC16_HA;
   1112       break;
   1113     case BFD_RELOC_PPC64_TOC:			r = R_PPC64_TOC;
   1114       break;
   1115     case BFD_RELOC_PPC64_PLTGOT16:		r = R_PPC64_PLTGOT16;
   1116       break;
   1117     case BFD_RELOC_PPC64_PLTGOT16_LO:		r = R_PPC64_PLTGOT16_LO;
   1118       break;
   1119     case BFD_RELOC_PPC64_PLTGOT16_HI:		r = R_PPC64_PLTGOT16_HI;
   1120       break;
   1121     case BFD_RELOC_PPC64_PLTGOT16_HA:		r = R_PPC64_PLTGOT16_HA;
   1122       break;
   1123     case BFD_RELOC_PPC64_ADDR16_DS:		r = R_PPC64_ADDR16_DS;
   1124       break;
   1125     case BFD_RELOC_PPC64_ADDR16_LO_DS:		r = R_PPC64_ADDR16_LO_DS;
   1126       break;
   1127     case BFD_RELOC_PPC64_GOT16_DS:		r = R_PPC64_GOT16_DS;
   1128       break;
   1129     case BFD_RELOC_PPC64_GOT16_LO_DS:		r = R_PPC64_GOT16_LO_DS;
   1130       break;
   1131     case BFD_RELOC_PPC64_PLT16_LO_DS:		r = R_PPC64_PLT16_LO_DS;
   1132       break;
   1133     case BFD_RELOC_PPC64_SECTOFF_DS:		r = R_PPC64_SECTOFF_DS;
   1134       break;
   1135     case BFD_RELOC_PPC64_SECTOFF_LO_DS:		r = R_PPC64_SECTOFF_LO_DS;
   1136       break;
   1137     case BFD_RELOC_PPC64_TOC16_DS:		r = R_PPC64_TOC16_DS;
   1138       break;
   1139     case BFD_RELOC_PPC64_TOC16_LO_DS:		r = R_PPC64_TOC16_LO_DS;
   1140       break;
   1141     case BFD_RELOC_PPC64_PLTGOT16_DS:		r = R_PPC64_PLTGOT16_DS;
   1142       break;
   1143     case BFD_RELOC_PPC64_PLTGOT16_LO_DS:	r = R_PPC64_PLTGOT16_LO_DS;
   1144       break;
   1145     case BFD_RELOC_PPC64_TLS_PCREL:
   1146     case BFD_RELOC_PPC_TLS:			r = R_PPC64_TLS;
   1147       break;
   1148     case BFD_RELOC_PPC_TLSGD:			r = R_PPC64_TLSGD;
   1149       break;
   1150     case BFD_RELOC_PPC_TLSLD:			r = R_PPC64_TLSLD;
   1151       break;
   1152     case BFD_RELOC_PPC_DTPMOD:			r = R_PPC64_DTPMOD64;
   1153       break;
   1154     case BFD_RELOC_PPC_TPREL16:			r = R_PPC64_TPREL16;
   1155       break;
   1156     case BFD_RELOC_PPC_TPREL16_LO:		r = R_PPC64_TPREL16_LO;
   1157       break;
   1158     case BFD_RELOC_PPC_TPREL16_HI:		r = R_PPC64_TPREL16_HI;
   1159       break;
   1160     case BFD_RELOC_PPC64_TPREL16_HIGH:		r = R_PPC64_TPREL16_HIGH;
   1161       break;
   1162     case BFD_RELOC_PPC_TPREL16_HA:		r = R_PPC64_TPREL16_HA;
   1163       break;
   1164     case BFD_RELOC_PPC64_TPREL16_HIGHA:		r = R_PPC64_TPREL16_HIGHA;
   1165       break;
   1166     case BFD_RELOC_PPC_TPREL:			r = R_PPC64_TPREL64;
   1167       break;
   1168     case BFD_RELOC_PPC_DTPREL16:		r = R_PPC64_DTPREL16;
   1169       break;
   1170     case BFD_RELOC_PPC_DTPREL16_LO:		r = R_PPC64_DTPREL16_LO;
   1171       break;
   1172     case BFD_RELOC_PPC_DTPREL16_HI:		r = R_PPC64_DTPREL16_HI;
   1173       break;
   1174     case BFD_RELOC_PPC64_DTPREL16_HIGH:		r = R_PPC64_DTPREL16_HIGH;
   1175       break;
   1176     case BFD_RELOC_PPC_DTPREL16_HA:		r = R_PPC64_DTPREL16_HA;
   1177       break;
   1178     case BFD_RELOC_PPC64_DTPREL16_HIGHA:	r = R_PPC64_DTPREL16_HIGHA;
   1179       break;
   1180     case BFD_RELOC_PPC_DTPREL:			r = R_PPC64_DTPREL64;
   1181       break;
   1182     case BFD_RELOC_PPC_GOT_TLSGD16:		r = R_PPC64_GOT_TLSGD16;
   1183       break;
   1184     case BFD_RELOC_PPC_GOT_TLSGD16_LO:		r = R_PPC64_GOT_TLSGD16_LO;
   1185       break;
   1186     case BFD_RELOC_PPC_GOT_TLSGD16_HI:		r = R_PPC64_GOT_TLSGD16_HI;
   1187       break;
   1188     case BFD_RELOC_PPC_GOT_TLSGD16_HA:		r = R_PPC64_GOT_TLSGD16_HA;
   1189       break;
   1190     case BFD_RELOC_PPC_GOT_TLSLD16:		r = R_PPC64_GOT_TLSLD16;
   1191       break;
   1192     case BFD_RELOC_PPC_GOT_TLSLD16_LO:		r = R_PPC64_GOT_TLSLD16_LO;
   1193       break;
   1194     case BFD_RELOC_PPC_GOT_TLSLD16_HI:		r = R_PPC64_GOT_TLSLD16_HI;
   1195       break;
   1196     case BFD_RELOC_PPC_GOT_TLSLD16_HA:		r = R_PPC64_GOT_TLSLD16_HA;
   1197       break;
   1198     case BFD_RELOC_PPC_GOT_TPREL16:		r = R_PPC64_GOT_TPREL16_DS;
   1199       break;
   1200     case BFD_RELOC_PPC_GOT_TPREL16_LO:		r = R_PPC64_GOT_TPREL16_LO_DS;
   1201       break;
   1202     case BFD_RELOC_PPC_GOT_TPREL16_HI:		r = R_PPC64_GOT_TPREL16_HI;
   1203       break;
   1204     case BFD_RELOC_PPC_GOT_TPREL16_HA:		r = R_PPC64_GOT_TPREL16_HA;
   1205       break;
   1206     case BFD_RELOC_PPC_GOT_DTPREL16:		r = R_PPC64_GOT_DTPREL16_DS;
   1207       break;
   1208     case BFD_RELOC_PPC_GOT_DTPREL16_LO:		r = R_PPC64_GOT_DTPREL16_LO_DS;
   1209       break;
   1210     case BFD_RELOC_PPC_GOT_DTPREL16_HI:		r = R_PPC64_GOT_DTPREL16_HI;
   1211       break;
   1212     case BFD_RELOC_PPC_GOT_DTPREL16_HA:		r = R_PPC64_GOT_DTPREL16_HA;
   1213       break;
   1214     case BFD_RELOC_PPC64_TPREL16_DS:		r = R_PPC64_TPREL16_DS;
   1215       break;
   1216     case BFD_RELOC_PPC64_TPREL16_LO_DS:		r = R_PPC64_TPREL16_LO_DS;
   1217       break;
   1218     case BFD_RELOC_PPC64_TPREL16_HIGHER:	r = R_PPC64_TPREL16_HIGHER;
   1219       break;
   1220     case BFD_RELOC_PPC64_TPREL16_HIGHERA:	r = R_PPC64_TPREL16_HIGHERA;
   1221       break;
   1222     case BFD_RELOC_PPC64_TPREL16_HIGHEST:	r = R_PPC64_TPREL16_HIGHEST;
   1223       break;
   1224     case BFD_RELOC_PPC64_TPREL16_HIGHESTA:	r = R_PPC64_TPREL16_HIGHESTA;
   1225       break;
   1226     case BFD_RELOC_PPC64_DTPREL16_DS:		r = R_PPC64_DTPREL16_DS;
   1227       break;
   1228     case BFD_RELOC_PPC64_DTPREL16_LO_DS:	r = R_PPC64_DTPREL16_LO_DS;
   1229       break;
   1230     case BFD_RELOC_PPC64_DTPREL16_HIGHER:	r = R_PPC64_DTPREL16_HIGHER;
   1231       break;
   1232     case BFD_RELOC_PPC64_DTPREL16_HIGHERA:	r = R_PPC64_DTPREL16_HIGHERA;
   1233       break;
   1234     case BFD_RELOC_PPC64_DTPREL16_HIGHEST:	r = R_PPC64_DTPREL16_HIGHEST;
   1235       break;
   1236     case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:	r = R_PPC64_DTPREL16_HIGHESTA;
   1237       break;
   1238     case BFD_RELOC_16_PCREL:			r = R_PPC64_REL16;
   1239       break;
   1240     case BFD_RELOC_LO16_PCREL:			r = R_PPC64_REL16_LO;
   1241       break;
   1242     case BFD_RELOC_HI16_PCREL:			r = R_PPC64_REL16_HI;
   1243       break;
   1244     case BFD_RELOC_HI16_S_PCREL:		r = R_PPC64_REL16_HA;
   1245       break;
   1246     case BFD_RELOC_PPC64_REL16_HIGH:		r = R_PPC64_REL16_HIGH;
   1247       break;
   1248     case BFD_RELOC_PPC64_REL16_HIGHA:		r = R_PPC64_REL16_HIGHA;
   1249       break;
   1250     case BFD_RELOC_PPC64_REL16_HIGHER:		r = R_PPC64_REL16_HIGHER;
   1251       break;
   1252     case BFD_RELOC_PPC64_REL16_HIGHERA:		r = R_PPC64_REL16_HIGHERA;
   1253       break;
   1254     case BFD_RELOC_PPC64_REL16_HIGHEST:		r = R_PPC64_REL16_HIGHEST;
   1255       break;
   1256     case BFD_RELOC_PPC64_REL16_HIGHESTA:	r = R_PPC64_REL16_HIGHESTA;
   1257       break;
   1258     case BFD_RELOC_PPC_16DX_HA:			r = R_PPC64_16DX_HA;
   1259       break;
   1260     case BFD_RELOC_PPC_REL16DX_HA:		r = R_PPC64_REL16DX_HA;
   1261       break;
   1262     case BFD_RELOC_PPC64_ENTRY:			r = R_PPC64_ENTRY;
   1263       break;
   1264     case BFD_RELOC_PPC64_ADDR64_LOCAL:		r = R_PPC64_ADDR64_LOCAL;
   1265       break;
   1266     case BFD_RELOC_PPC64_D34:			r = R_PPC64_D34;
   1267       break;
   1268     case BFD_RELOC_PPC64_D34_LO:		r = R_PPC64_D34_LO;
   1269       break;
   1270     case BFD_RELOC_PPC64_D34_HI30:		r = R_PPC64_D34_HI30;
   1271       break;
   1272     case BFD_RELOC_PPC64_D34_HA30:		r = R_PPC64_D34_HA30;
   1273       break;
   1274     case BFD_RELOC_PPC64_PCREL34:		r = R_PPC64_PCREL34;
   1275       break;
   1276     case BFD_RELOC_PPC64_GOT_PCREL34:		r = R_PPC64_GOT_PCREL34;
   1277       break;
   1278     case BFD_RELOC_PPC64_PLT_PCREL34:		r = R_PPC64_PLT_PCREL34;
   1279       break;
   1280     case BFD_RELOC_PPC64_TPREL34:		r = R_PPC64_TPREL34;
   1281       break;
   1282     case BFD_RELOC_PPC64_DTPREL34:		r = R_PPC64_DTPREL34;
   1283       break;
   1284     case BFD_RELOC_PPC64_GOT_TLSGD_PCREL34:	r = R_PPC64_GOT_TLSGD_PCREL34;
   1285       break;
   1286     case BFD_RELOC_PPC64_GOT_TLSLD_PCREL34:	r = R_PPC64_GOT_TLSLD_PCREL34;
   1287       break;
   1288     case BFD_RELOC_PPC64_GOT_TPREL_PCREL34:	r = R_PPC64_GOT_TPREL_PCREL34;
   1289       break;
   1290     case BFD_RELOC_PPC64_GOT_DTPREL_PCREL34:	r = R_PPC64_GOT_DTPREL_PCREL34;
   1291       break;
   1292     case BFD_RELOC_PPC64_ADDR16_HIGHER34:	r = R_PPC64_ADDR16_HIGHER34;
   1293       break;
   1294     case BFD_RELOC_PPC64_ADDR16_HIGHERA34:	r = R_PPC64_ADDR16_HIGHERA34;
   1295       break;
   1296     case BFD_RELOC_PPC64_ADDR16_HIGHEST34:	r = R_PPC64_ADDR16_HIGHEST34;
   1297       break;
   1298     case BFD_RELOC_PPC64_ADDR16_HIGHESTA34:	r = R_PPC64_ADDR16_HIGHESTA34;
   1299       break;
   1300     case BFD_RELOC_PPC64_REL16_HIGHER34:	r = R_PPC64_REL16_HIGHER34;
   1301       break;
   1302     case BFD_RELOC_PPC64_REL16_HIGHERA34:	r = R_PPC64_REL16_HIGHERA34;
   1303       break;
   1304     case BFD_RELOC_PPC64_REL16_HIGHEST34:	r = R_PPC64_REL16_HIGHEST34;
   1305       break;
   1306     case BFD_RELOC_PPC64_REL16_HIGHESTA34:	r = R_PPC64_REL16_HIGHESTA34;
   1307       break;
   1308     case BFD_RELOC_PPC64_D28:			r = R_PPC64_D28;
   1309       break;
   1310     case BFD_RELOC_PPC64_PCREL28:		r = R_PPC64_PCREL28;
   1311       break;
   1312     case BFD_RELOC_VTABLE_INHERIT:		r = R_PPC64_GNU_VTINHERIT;
   1313       break;
   1314     case BFD_RELOC_VTABLE_ENTRY:		r = R_PPC64_GNU_VTENTRY;
   1315       break;
   1316     }
   1317 
   1318   return ppc64_elf_howto_table[r];
   1319 };
   1320 
   1321 static reloc_howto_type *
   1322 ppc64_elf_reloc_name_lookup (bfd *abfd, const char *r_name)
   1323 {
   1324   unsigned int i;
   1325   static char *compat_map[][2] = {
   1326     { "R_PPC64_GOT_TLSGD34", "R_PPC64_GOT_TLSGD_PCREL34" },
   1327     { "R_PPC64_GOT_TLSLD34", "R_PPC64_GOT_TLSLD_PCREL34" },
   1328     { "R_PPC64_GOT_TPREL34", "R_PPC64_GOT_TPREL_PCREL34" },
   1329     { "R_PPC64_GOT_DTPREL34", "R_PPC64_GOT_DTPREL_PCREL34" }
   1330   };
   1331 
   1332   for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
   1333     if (ppc64_elf_howto_raw[i].name != NULL
   1334 	&& strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
   1335       return &ppc64_elf_howto_raw[i];
   1336 
   1337   /* Handle old names of relocations in case they were used by
   1338      .reloc directives.
   1339      FIXME: Remove this soon.  Mapping the reloc names is very likely
   1340      completely unnecessary.  */
   1341   for (i = 0; i < ARRAY_SIZE (compat_map); i++)
   1342     if (strcasecmp (compat_map[i][0], r_name) == 0)
   1343       {
   1344 	_bfd_error_handler (_("warning: %s should be used rather than %s"),
   1345 			    compat_map[i][1], compat_map[i][0]);
   1346 	return ppc64_elf_reloc_name_lookup (abfd, compat_map[i][1]);
   1347       }
   1348 
   1349   return NULL;
   1350 }
   1351 
   1352 /* Set the howto pointer for a PowerPC ELF reloc.  */
   1353 
   1354 static bfd_boolean
   1355 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
   1356 			 Elf_Internal_Rela *dst)
   1357 {
   1358   unsigned int type;
   1359 
   1360   /* Initialize howto table if needed.  */
   1361   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
   1362     ppc_howto_init ();
   1363 
   1364   type = ELF64_R_TYPE (dst->r_info);
   1365   if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
   1366     {
   1367       /* xgettext:c-format */
   1368       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
   1369 			  abfd, type);
   1370       bfd_set_error (bfd_error_bad_value);
   1371       return FALSE;
   1372     }
   1373   cache_ptr->howto = ppc64_elf_howto_table[type];
   1374   if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
   1375     {
   1376       /* xgettext:c-format */
   1377       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
   1378 			  abfd, type);
   1379       bfd_set_error (bfd_error_bad_value);
   1380       return FALSE;
   1381     }
   1382 
   1383   return TRUE;
   1384 }
   1385 
   1386 /* Handle the R_PPC64_ADDR16_HA and similar relocs.  */
   1387 
   1388 static bfd_reloc_status_type
   1389 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   1390 		    void *data, asection *input_section,
   1391 		    bfd *output_bfd, char **error_message)
   1392 {
   1393   enum elf_ppc64_reloc_type r_type;
   1394   long insn;
   1395   bfd_size_type octets;
   1396   bfd_vma value;
   1397 
   1398   /* If this is a relocatable link (output_bfd test tells us), just
   1399      call the generic function.  Any adjustment will be done at final
   1400      link time.  */
   1401   if (output_bfd != NULL)
   1402     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   1403 				  input_section, output_bfd, error_message);
   1404 
   1405   /* Adjust the addend for sign extension of the low 16 (or 34) bits.
   1406      We won't actually be using the low bits, so trashing them
   1407      doesn't matter.  */
   1408   r_type = reloc_entry->howto->type;
   1409   if (r_type == R_PPC64_ADDR16_HIGHERA34
   1410       || r_type == R_PPC64_ADDR16_HIGHESTA34
   1411       || r_type == R_PPC64_REL16_HIGHERA34
   1412       || r_type == R_PPC64_REL16_HIGHESTA34)
   1413     reloc_entry->addend += 1ULL << 33;
   1414   else
   1415     reloc_entry->addend += 1U << 15;
   1416   if (r_type != R_PPC64_REL16DX_HA)
   1417     return bfd_reloc_continue;
   1418 
   1419   value = 0;
   1420   if (!bfd_is_com_section (symbol->section))
   1421     value = symbol->value;
   1422   value += (reloc_entry->addend
   1423 	    + symbol->section->output_offset
   1424 	    + symbol->section->output_section->vma);
   1425   value -= (reloc_entry->address
   1426 	    + input_section->output_offset
   1427 	    + input_section->output_section->vma);
   1428   value = (bfd_signed_vma) value >> 16;
   1429 
   1430   octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
   1431   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
   1432   insn &= ~0x1fffc1;
   1433   insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
   1434   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
   1435   if (value + 0x8000 > 0xffff)
   1436     return bfd_reloc_overflow;
   1437   return bfd_reloc_ok;
   1438 }
   1439 
   1440 static bfd_reloc_status_type
   1441 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   1442 			void *data, asection *input_section,
   1443 			bfd *output_bfd, char **error_message)
   1444 {
   1445   if (output_bfd != NULL)
   1446     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   1447 				  input_section, output_bfd, error_message);
   1448 
   1449   if (strcmp (symbol->section->name, ".opd") == 0
   1450       && (symbol->section->owner->flags & DYNAMIC) == 0)
   1451     {
   1452       bfd_vma dest = opd_entry_value (symbol->section,
   1453 				      symbol->value + reloc_entry->addend,
   1454 				      NULL, NULL, FALSE);
   1455       if (dest != (bfd_vma) -1)
   1456 	reloc_entry->addend = dest - (symbol->value
   1457 				      + symbol->section->output_section->vma
   1458 				      + symbol->section->output_offset);
   1459     }
   1460   else
   1461     {
   1462       elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
   1463 
   1464       if (symbol->section->owner != abfd
   1465 	  && symbol->section->owner != NULL
   1466 	  && abiversion (symbol->section->owner) >= 2)
   1467 	{
   1468 	  unsigned int i;
   1469 
   1470 	  for (i = 0; i < symbol->section->owner->symcount; ++i)
   1471 	    {
   1472 	      asymbol *symdef = symbol->section->owner->outsymbols[i];
   1473 
   1474 	      if (strcmp (symdef->name, symbol->name) == 0)
   1475 		{
   1476 		  elfsym = (elf_symbol_type *) symdef;
   1477 		  break;
   1478 		}
   1479 	    }
   1480 	}
   1481       reloc_entry->addend
   1482 	+= PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
   1483     }
   1484   return bfd_reloc_continue;
   1485 }
   1486 
   1487 static bfd_reloc_status_type
   1488 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   1489 			 void *data, asection *input_section,
   1490 			 bfd *output_bfd, char **error_message)
   1491 {
   1492   long insn;
   1493   enum elf_ppc64_reloc_type r_type;
   1494   bfd_size_type octets;
   1495   /* Assume 'at' branch hints.  */
   1496   bfd_boolean is_isa_v2 = TRUE;
   1497 
   1498   /* If this is a relocatable link (output_bfd test tells us), just
   1499      call the generic function.  Any adjustment will be done at final
   1500      link time.  */
   1501   if (output_bfd != NULL)
   1502     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   1503 				  input_section, output_bfd, error_message);
   1504 
   1505   octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
   1506   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
   1507   insn &= ~(0x01 << 21);
   1508   r_type = reloc_entry->howto->type;
   1509   if (r_type == R_PPC64_ADDR14_BRTAKEN
   1510       || r_type == R_PPC64_REL14_BRTAKEN)
   1511     insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
   1512 
   1513   if (is_isa_v2)
   1514     {
   1515       /* Set 'a' bit.  This is 0b00010 in BO field for branch
   1516 	 on CR(BI) insns (BO == 001at or 011at), and 0b01000
   1517 	 for branch on CTR insns (BO == 1a00t or 1a01t).  */
   1518       if ((insn & (0x14 << 21)) == (0x04 << 21))
   1519 	insn |= 0x02 << 21;
   1520       else if ((insn & (0x14 << 21)) == (0x10 << 21))
   1521 	insn |= 0x08 << 21;
   1522       else
   1523 	goto out;
   1524     }
   1525   else
   1526     {
   1527       bfd_vma target = 0;
   1528       bfd_vma from;
   1529 
   1530       if (!bfd_is_com_section (symbol->section))
   1531 	target = symbol->value;
   1532       target += symbol->section->output_section->vma;
   1533       target += symbol->section->output_offset;
   1534       target += reloc_entry->addend;
   1535 
   1536       from = (reloc_entry->address
   1537 	      + input_section->output_offset
   1538 	      + input_section->output_section->vma);
   1539 
   1540       /* Invert 'y' bit if not the default.  */
   1541       if ((bfd_signed_vma) (target - from) < 0)
   1542 	insn ^= 0x01 << 21;
   1543     }
   1544   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
   1545  out:
   1546   return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
   1547 				 input_section, output_bfd, error_message);
   1548 }
   1549 
   1550 static bfd_reloc_status_type
   1551 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   1552 			 void *data, asection *input_section,
   1553 			 bfd *output_bfd, char **error_message)
   1554 {
   1555   /* If this is a relocatable link (output_bfd test tells us), just
   1556      call the generic function.  Any adjustment will be done at final
   1557      link time.  */
   1558   if (output_bfd != NULL)
   1559     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   1560 				  input_section, output_bfd, error_message);
   1561 
   1562   /* Subtract the symbol section base address.  */
   1563   reloc_entry->addend -= symbol->section->output_section->vma;
   1564   return bfd_reloc_continue;
   1565 }
   1566 
   1567 static bfd_reloc_status_type
   1568 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   1569 			    void *data, asection *input_section,
   1570 			    bfd *output_bfd, char **error_message)
   1571 {
   1572   /* If this is a relocatable link (output_bfd test tells us), just
   1573      call the generic function.  Any adjustment will be done at final
   1574      link time.  */
   1575   if (output_bfd != NULL)
   1576     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   1577 				  input_section, output_bfd, error_message);
   1578 
   1579   /* Subtract the symbol section base address.  */
   1580   reloc_entry->addend -= symbol->section->output_section->vma;
   1581 
   1582   /* Adjust the addend for sign extension of the low 16 bits.  */
   1583   reloc_entry->addend += 0x8000;
   1584   return bfd_reloc_continue;
   1585 }
   1586 
   1587 static bfd_reloc_status_type
   1588 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   1589 		     void *data, asection *input_section,
   1590 		     bfd *output_bfd, char **error_message)
   1591 {
   1592   bfd_vma TOCstart;
   1593 
   1594   /* If this is a relocatable link (output_bfd test tells us), just
   1595      call the generic function.  Any adjustment will be done at final
   1596      link time.  */
   1597   if (output_bfd != NULL)
   1598     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   1599 				  input_section, output_bfd, error_message);
   1600 
   1601   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
   1602   if (TOCstart == 0)
   1603     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
   1604 
   1605   /* Subtract the TOC base address.  */
   1606   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
   1607   return bfd_reloc_continue;
   1608 }
   1609 
   1610 static bfd_reloc_status_type
   1611 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   1612 			void *data, asection *input_section,
   1613 			bfd *output_bfd, char **error_message)
   1614 {
   1615   bfd_vma TOCstart;
   1616 
   1617   /* If this is a relocatable link (output_bfd test tells us), just
   1618      call the generic function.  Any adjustment will be done at final
   1619      link time.  */
   1620   if (output_bfd != NULL)
   1621     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   1622 				  input_section, output_bfd, error_message);
   1623 
   1624   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
   1625   if (TOCstart == 0)
   1626     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
   1627 
   1628   /* Subtract the TOC base address.  */
   1629   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
   1630 
   1631   /* Adjust the addend for sign extension of the low 16 bits.  */
   1632   reloc_entry->addend += 0x8000;
   1633   return bfd_reloc_continue;
   1634 }
   1635 
   1636 static bfd_reloc_status_type
   1637 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   1638 		       void *data, asection *input_section,
   1639 		       bfd *output_bfd, char **error_message)
   1640 {
   1641   bfd_vma TOCstart;
   1642   bfd_size_type octets;
   1643 
   1644   /* If this is a relocatable link (output_bfd test tells us), just
   1645      call the generic function.  Any adjustment will be done at final
   1646      link time.  */
   1647   if (output_bfd != NULL)
   1648     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   1649 				  input_section, output_bfd, error_message);
   1650 
   1651   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
   1652   if (TOCstart == 0)
   1653     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
   1654 
   1655   octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
   1656   bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
   1657   return bfd_reloc_ok;
   1658 }
   1659 
   1660 static bfd_reloc_status_type
   1661 ppc64_elf_prefix_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   1662 			void *data, asection *input_section,
   1663 			bfd *output_bfd, char **error_message)
   1664 {
   1665   uint64_t insn;
   1666   bfd_vma targ;
   1667 
   1668   if (output_bfd != NULL)
   1669     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   1670 				  input_section, output_bfd, error_message);
   1671 
   1672   insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
   1673   insn <<= 32;
   1674   insn |= bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address + 4);
   1675 
   1676   targ = (symbol->section->output_section->vma
   1677 	  + symbol->section->output_offset
   1678 	  + reloc_entry->addend);
   1679   if (!bfd_is_com_section (symbol->section))
   1680     targ += symbol->value;
   1681   if (reloc_entry->howto->type == R_PPC64_D34_HA30)
   1682     targ += 1ULL << 33;
   1683   if (reloc_entry->howto->pc_relative)
   1684     {
   1685       bfd_vma from = (reloc_entry->address
   1686 		      + input_section->output_offset
   1687 		      + input_section->output_section->vma);
   1688       targ -=from;
   1689     }
   1690   targ >>= reloc_entry->howto->rightshift;
   1691   insn &= ~reloc_entry->howto->dst_mask;
   1692   insn |= ((targ << 16) | (targ & 0xffff)) & reloc_entry->howto->dst_mask;
   1693   bfd_put_32 (abfd, insn >> 32, (bfd_byte *) data + reloc_entry->address);
   1694   bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address + 4);
   1695   if (reloc_entry->howto->complain_on_overflow == complain_overflow_signed
   1696       && (targ + (1ULL << (reloc_entry->howto->bitsize - 1))
   1697 	  >= 1ULL << reloc_entry->howto->bitsize))
   1698     return bfd_reloc_overflow;
   1699   return bfd_reloc_ok;
   1700 }
   1701 
   1702 static bfd_reloc_status_type
   1703 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   1704 			   void *data, asection *input_section,
   1705 			   bfd *output_bfd, char **error_message)
   1706 {
   1707   /* If this is a relocatable link (output_bfd test tells us), just
   1708      call the generic function.  Any adjustment will be done at final
   1709      link time.  */
   1710   if (output_bfd != NULL)
   1711     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   1712 				  input_section, output_bfd, error_message);
   1713 
   1714   if (error_message != NULL)
   1715     {
   1716       static char buf[60];
   1717       sprintf (buf, "generic linker can't handle %s",
   1718 	       reloc_entry->howto->name);
   1719       *error_message = buf;
   1720     }
   1721   return bfd_reloc_dangerous;
   1722 }
   1723 
   1724 /* Track GOT entries needed for a given symbol.  We might need more
   1725    than one got entry per symbol.  */
   1726 struct got_entry
   1727 {
   1728   struct got_entry *next;
   1729 
   1730   /* The symbol addend that we'll be placing in the GOT.  */
   1731   bfd_vma addend;
   1732 
   1733   /* Unlike other ELF targets, we use separate GOT entries for the same
   1734      symbol referenced from different input files.  This is to support
   1735      automatic multiple TOC/GOT sections, where the TOC base can vary
   1736      from one input file to another.  After partitioning into TOC groups
   1737      we merge entries within the group.
   1738 
   1739      Point to the BFD owning this GOT entry.  */
   1740   bfd *owner;
   1741 
   1742   /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
   1743      TLS_TPREL or TLS_DTPREL for tls entries.  */
   1744   unsigned char tls_type;
   1745 
   1746   /* Non-zero if got.ent points to real entry.  */
   1747   unsigned char is_indirect;
   1748 
   1749   /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
   1750   union
   1751   {
   1752     bfd_signed_vma refcount;
   1753     bfd_vma offset;
   1754     struct got_entry *ent;
   1755   } got;
   1756 };
   1757 
   1758 /* The same for PLT.  */
   1759 struct plt_entry
   1760 {
   1761   struct plt_entry *next;
   1762 
   1763   bfd_vma addend;
   1764 
   1765   union
   1766   {
   1767     bfd_signed_vma refcount;
   1768     bfd_vma offset;
   1769   } plt;
   1770 };
   1771 
   1772 struct ppc64_elf_obj_tdata
   1773 {
   1774   struct elf_obj_tdata elf;
   1775 
   1776   /* Shortcuts to dynamic linker sections.  */
   1777   asection *got;
   1778   asection *relgot;
   1779 
   1780   /* Used during garbage collection.  We attach global symbols defined
   1781      on removed .opd entries to this section so that the sym is removed.  */
   1782   asection *deleted_section;
   1783 
   1784   /* TLS local dynamic got entry handling.  Support for multiple GOT
   1785      sections means we potentially need one of these for each input bfd.  */
   1786   struct got_entry tlsld_got;
   1787 
   1788   union
   1789   {
   1790     /* A copy of relocs before they are modified for --emit-relocs.  */
   1791     Elf_Internal_Rela *relocs;
   1792 
   1793     /* Section contents.  */
   1794     bfd_byte *contents;
   1795   } opd;
   1796 
   1797   /* Nonzero if this bfd has small toc/got relocs, ie. that expect
   1798      the reloc to be in the range -32768 to 32767.  */
   1799   unsigned int has_small_toc_reloc : 1;
   1800 
   1801   /* Set if toc/got ha relocs detected not using r2, or lo reloc
   1802      instruction not one we handle.  */
   1803   unsigned int unexpected_toc_insn : 1;
   1804 
   1805   /* Set if PLT/GOT/TOC relocs that can be optimised are present in
   1806      this file.  */
   1807   unsigned int has_optrel : 1;
   1808 };
   1809 
   1810 #define ppc64_elf_tdata(bfd) \
   1811   ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
   1812 
   1813 #define ppc64_tlsld_got(bfd) \
   1814   (&ppc64_elf_tdata (bfd)->tlsld_got)
   1815 
   1816 #define is_ppc64_elf(bfd) \
   1817   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
   1818    && elf_object_id (bfd) == PPC64_ELF_DATA)
   1819 
   1820 /* Override the generic function because we store some extras.  */
   1821 
   1822 static bfd_boolean
   1823 ppc64_elf_mkobject (bfd *abfd)
   1824 {
   1825   return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
   1826 				  PPC64_ELF_DATA);
   1827 }
   1828 
   1829 /* Fix bad default arch selected for a 64 bit input bfd when the
   1830    default is 32 bit.  Also select arch based on apuinfo.  */
   1831 
   1832 static bfd_boolean
   1833 ppc64_elf_object_p (bfd *abfd)
   1834 {
   1835   if (!abfd->arch_info->the_default)
   1836     return TRUE;
   1837 
   1838   if (abfd->arch_info->bits_per_word == 32)
   1839     {
   1840       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
   1841 
   1842       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
   1843 	{
   1844 	  /* Relies on arch after 32 bit default being 64 bit default.  */
   1845 	  abfd->arch_info = abfd->arch_info->next;
   1846 	  BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
   1847 	}
   1848     }
   1849   return _bfd_elf_ppc_set_arch (abfd);
   1850 }
   1851 
   1852 /* Support for core dump NOTE sections.  */
   1853 
   1854 static bfd_boolean
   1855 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
   1856 {
   1857   size_t offset, size;
   1858 
   1859   if (note->descsz != 504)
   1860     return FALSE;
   1861 
   1862   /* pr_cursig */
   1863   elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
   1864 
   1865   /* pr_pid */
   1866   elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
   1867 
   1868   /* pr_reg */
   1869   offset = 112;
   1870   size = 384;
   1871 
   1872   /* Make a ".reg/999" section.  */
   1873   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
   1874 					  size, note->descpos + offset);
   1875 }
   1876 
   1877 static bfd_boolean
   1878 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
   1879 {
   1880   if (note->descsz != 136)
   1881     return FALSE;
   1882 
   1883   elf_tdata (abfd)->core->pid
   1884     = bfd_get_32 (abfd, note->descdata + 24);
   1885   elf_tdata (abfd)->core->program
   1886     = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
   1887   elf_tdata (abfd)->core->command
   1888     = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
   1889 
   1890   return TRUE;
   1891 }
   1892 
   1893 static char *
   1894 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
   1895 			   ...)
   1896 {
   1897   switch (note_type)
   1898     {
   1899     default:
   1900       return NULL;
   1901 
   1902     case NT_PRPSINFO:
   1903       {
   1904 	char data[136] ATTRIBUTE_NONSTRING;
   1905 	va_list ap;
   1906 
   1907 	va_start (ap, note_type);
   1908 	memset (data, 0, sizeof (data));
   1909 	strncpy (data + 40, va_arg (ap, const char *), 16);
   1910 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
   1911 	DIAGNOSTIC_PUSH;
   1912 	/* GCC 8.0 and 8.1 warn about 80 equals destination size with
   1913 	   -Wstringop-truncation:
   1914 	   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
   1915 	 */
   1916 	DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
   1917 #endif
   1918 	strncpy (data + 56, va_arg (ap, const char *), 80);
   1919 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
   1920 	DIAGNOSTIC_POP;
   1921 #endif
   1922 	va_end (ap);
   1923 	return elfcore_write_note (abfd, buf, bufsiz,
   1924 				   "CORE", note_type, data, sizeof (data));
   1925       }
   1926 
   1927     case NT_PRSTATUS:
   1928       {
   1929 	char data[504];
   1930 	va_list ap;
   1931 	long pid;
   1932 	int cursig;
   1933 	const void *greg;
   1934 
   1935 	va_start (ap, note_type);
   1936 	memset (data, 0, 112);
   1937 	pid = va_arg (ap, long);
   1938 	bfd_put_32 (abfd, pid, data + 32);
   1939 	cursig = va_arg (ap, int);
   1940 	bfd_put_16 (abfd, cursig, data + 12);
   1941 	greg = va_arg (ap, const void *);
   1942 	memcpy (data + 112, greg, 384);
   1943 	memset (data + 496, 0, 8);
   1944 	va_end (ap);
   1945 	return elfcore_write_note (abfd, buf, bufsiz,
   1946 				   "CORE", note_type, data, sizeof (data));
   1947       }
   1948     }
   1949 }
   1950 
   1951 /* Add extra PPC sections.  */
   1952 
   1953 static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
   1954 {
   1955   { STRING_COMMA_LEN (".plt"),	  0, SHT_NOBITS,   0 },
   1956   { STRING_COMMA_LEN (".sbss"),	 -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
   1957   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   1958   { STRING_COMMA_LEN (".toc"),	  0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   1959   { STRING_COMMA_LEN (".toc1"),	  0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   1960   { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
   1961   { NULL,		      0,  0, 0,		   0 }
   1962 };
   1963 
   1964 enum _ppc64_sec_type {
   1965   sec_normal = 0,
   1966   sec_opd = 1,
   1967   sec_toc = 2
   1968 };
   1969 
   1970 struct _ppc64_elf_section_data
   1971 {
   1972   struct bfd_elf_section_data elf;
   1973 
   1974   union
   1975   {
   1976     /* An array with one entry for each opd function descriptor,
   1977        and some spares since opd entries may be either 16 or 24 bytes.  */
   1978 #define OPD_NDX(OFF) ((OFF) >> 4)
   1979     struct _opd_sec_data
   1980     {
   1981       /* Points to the function code section for local opd entries.  */
   1982       asection **func_sec;
   1983 
   1984       /* After editing .opd, adjust references to opd local syms.  */
   1985       long *adjust;
   1986     } opd;
   1987 
   1988     /* An array for toc sections, indexed by offset/8.  */
   1989     struct _toc_sec_data
   1990     {
   1991       /* Specifies the relocation symbol index used at a given toc offset.  */
   1992       unsigned *symndx;
   1993 
   1994       /* And the relocation addend.  */
   1995       bfd_vma *add;
   1996     } toc;
   1997   } u;
   1998 
   1999   enum _ppc64_sec_type sec_type:2;
   2000 
   2001   /* Flag set when small branches are detected.  Used to
   2002      select suitable defaults for the stub group size.  */
   2003   unsigned int has_14bit_branch:1;
   2004 
   2005   /* Flag set when PLTCALL relocs are detected.  */
   2006   unsigned int has_pltcall:1;
   2007 
   2008   /* Flag set when section has PLT/GOT/TOC relocations that can be
   2009      optimised.  */
   2010   unsigned int has_optrel:1;
   2011 };
   2012 
   2013 #define ppc64_elf_section_data(sec) \
   2014   ((struct _ppc64_elf_section_data *) elf_section_data (sec))
   2015 
   2016 static bfd_boolean
   2017 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
   2018 {
   2019   if (!sec->used_by_bfd)
   2020     {
   2021       struct _ppc64_elf_section_data *sdata;
   2022       size_t amt = sizeof (*sdata);
   2023 
   2024       sdata = bfd_zalloc (abfd, amt);
   2025       if (sdata == NULL)
   2026 	return FALSE;
   2027       sec->used_by_bfd = sdata;
   2028     }
   2029 
   2030   return _bfd_elf_new_section_hook (abfd, sec);
   2031 }
   2032 
   2033 static bfd_boolean
   2034 ppc64_elf_section_flags (const Elf_Internal_Shdr *hdr)
   2035 {
   2036   const char *name = hdr->bfd_section->name;
   2037 
   2038   if (strncmp (name, ".sbss", 5) == 0
   2039       || strncmp (name, ".sdata", 6) == 0)
   2040     hdr->bfd_section->flags |= SEC_SMALL_DATA;
   2041 
   2042   return TRUE;
   2043 }
   2044 
   2045 static struct _opd_sec_data *
   2046 get_opd_info (asection * sec)
   2047 {
   2048   if (sec != NULL
   2049       && ppc64_elf_section_data (sec) != NULL
   2050       && ppc64_elf_section_data (sec)->sec_type == sec_opd)
   2051     return &ppc64_elf_section_data (sec)->u.opd;
   2052   return NULL;
   2053 }
   2054 
   2055 /* Parameters for the qsort hook.  */
   2057 static bfd_boolean synthetic_relocatable;
   2058 static asection *synthetic_opd;
   2059 
   2060 /* qsort comparison function for ppc64_elf_get_synthetic_symtab.  */
   2061 
   2062 static int
   2063 compare_symbols (const void *ap, const void *bp)
   2064 {
   2065   const asymbol *a = *(const asymbol **) ap;
   2066   const asymbol *b = *(const asymbol **) bp;
   2067 
   2068   /* Section symbols first.  */
   2069   if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
   2070     return -1;
   2071   if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
   2072     return 1;
   2073 
   2074   /* then .opd symbols.  */
   2075   if (synthetic_opd != NULL)
   2076     {
   2077       if (strcmp (a->section->name, ".opd") == 0
   2078 	  && strcmp (b->section->name, ".opd") != 0)
   2079 	return -1;
   2080       if (strcmp (a->section->name, ".opd") != 0
   2081 	  && strcmp (b->section->name, ".opd") == 0)
   2082 	return 1;
   2083     }
   2084 
   2085   /* then other code symbols.  */
   2086   if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   2087        == (SEC_CODE | SEC_ALLOC))
   2088       && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   2089 	  != (SEC_CODE | SEC_ALLOC)))
   2090     return -1;
   2091 
   2092   if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   2093        != (SEC_CODE | SEC_ALLOC))
   2094       && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   2095 	  == (SEC_CODE | SEC_ALLOC)))
   2096     return 1;
   2097 
   2098   if (synthetic_relocatable)
   2099     {
   2100       if (a->section->id < b->section->id)
   2101 	return -1;
   2102 
   2103       if (a->section->id > b->section->id)
   2104 	return 1;
   2105     }
   2106 
   2107   if (a->value + a->section->vma < b->value + b->section->vma)
   2108     return -1;
   2109 
   2110   if (a->value + a->section->vma > b->value + b->section->vma)
   2111     return 1;
   2112 
   2113   /* For syms with the same value, prefer strong dynamic global function
   2114      syms over other syms.  */
   2115   if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
   2116     return -1;
   2117 
   2118   if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
   2119     return 1;
   2120 
   2121   if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
   2122     return -1;
   2123 
   2124   if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
   2125     return 1;
   2126 
   2127   if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
   2128     return -1;
   2129 
   2130   if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
   2131     return 1;
   2132 
   2133   if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
   2134     return -1;
   2135 
   2136   if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
   2137     return 1;
   2138 
   2139   /* Finally, sort on where the symbol is in memory.  The symbols will
   2140      be in at most two malloc'd blocks, one for static syms, one for
   2141      dynamic syms, and we distinguish the two blocks above by testing
   2142      BSF_DYNAMIC.  Since we are sorting the symbol pointers which were
   2143      originally in the same order as the symbols (and we're not
   2144      sorting the symbols themselves), this ensures a stable sort.  */
   2145   if (a < b)
   2146     return -1;
   2147   if (a > b)
   2148     return 1;
   2149   return 0;
   2150 }
   2151 
   2152 /* Search SYMS for a symbol of the given VALUE.  */
   2153 
   2154 static asymbol *
   2155 sym_exists_at (asymbol **syms, size_t lo, size_t hi, unsigned int id,
   2156 	       bfd_vma value)
   2157 {
   2158   size_t mid;
   2159 
   2160   if (id == (unsigned) -1)
   2161     {
   2162       while (lo < hi)
   2163 	{
   2164 	  mid = (lo + hi) >> 1;
   2165 	  if (syms[mid]->value + syms[mid]->section->vma < value)
   2166 	    lo = mid + 1;
   2167 	  else if (syms[mid]->value + syms[mid]->section->vma > value)
   2168 	    hi = mid;
   2169 	  else
   2170 	    return syms[mid];
   2171 	}
   2172     }
   2173   else
   2174     {
   2175       while (lo < hi)
   2176 	{
   2177 	  mid = (lo + hi) >> 1;
   2178 	  if (syms[mid]->section->id < id)
   2179 	    lo = mid + 1;
   2180 	  else if (syms[mid]->section->id > id)
   2181 	    hi = mid;
   2182 	  else if (syms[mid]->value < value)
   2183 	    lo = mid + 1;
   2184 	  else if (syms[mid]->value > value)
   2185 	    hi = mid;
   2186 	  else
   2187 	    return syms[mid];
   2188 	}
   2189     }
   2190   return NULL;
   2191 }
   2192 
   2193 static bfd_boolean
   2194 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
   2195 {
   2196   bfd_vma vma = *(bfd_vma *) ptr;
   2197   return ((section->flags & SEC_ALLOC) != 0
   2198 	  && section->vma <= vma
   2199 	  && vma < section->vma + section->size);
   2200 }
   2201 
   2202 /* Create synthetic symbols, effectively restoring "dot-symbol" function
   2203    entry syms.  Also generate @plt symbols for the glink branch table.
   2204    Returns count of synthetic symbols in RET or -1 on error.  */
   2205 
   2206 static long
   2207 ppc64_elf_get_synthetic_symtab (bfd *abfd,
   2208 				long static_count, asymbol **static_syms,
   2209 				long dyn_count, asymbol **dyn_syms,
   2210 				asymbol **ret)
   2211 {
   2212   asymbol *s;
   2213   size_t i, j, count;
   2214   char *names;
   2215   size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
   2216   asection *opd = NULL;
   2217   bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
   2218   asymbol **syms;
   2219   int abi = abiversion (abfd);
   2220 
   2221   *ret = NULL;
   2222 
   2223   if (abi < 2)
   2224     {
   2225       opd = bfd_get_section_by_name (abfd, ".opd");
   2226       if (opd == NULL && abi == 1)
   2227 	return 0;
   2228     }
   2229 
   2230   syms = NULL;
   2231   codesecsym = 0;
   2232   codesecsymend = 0;
   2233   secsymend = 0;
   2234   opdsymend = 0;
   2235   symcount = 0;
   2236   if (opd != NULL)
   2237     {
   2238       symcount = static_count;
   2239       if (!relocatable)
   2240 	symcount += dyn_count;
   2241       if (symcount == 0)
   2242 	return 0;
   2243 
   2244       syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
   2245       if (syms == NULL)
   2246 	return -1;
   2247 
   2248       if (!relocatable && static_count != 0 && dyn_count != 0)
   2249 	{
   2250 	  /* Use both symbol tables.  */
   2251 	  memcpy (syms, static_syms, static_count * sizeof (*syms));
   2252 	  memcpy (syms + static_count, dyn_syms,
   2253 		  (dyn_count + 1) * sizeof (*syms));
   2254 	}
   2255       else if (!relocatable && static_count == 0)
   2256 	memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
   2257       else
   2258 	memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
   2259 
   2260       /* Trim uninteresting symbols.  Interesting symbols are section,
   2261 	 function, and notype symbols.  */
   2262       for (i = 0, j = 0; i < symcount; ++i)
   2263 	if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
   2264 			       | BSF_RELC | BSF_SRELC)) == 0)
   2265 	  syms[j++] = syms[i];
   2266       symcount = j;
   2267 
   2268       synthetic_relocatable = relocatable;
   2269       synthetic_opd = opd;
   2270       qsort (syms, symcount, sizeof (*syms), compare_symbols);
   2271 
   2272       if (!relocatable && symcount > 1)
   2273 	{
   2274 	  /* Trim duplicate syms, since we may have merged the normal
   2275 	     and dynamic symbols.  Actually, we only care about syms
   2276 	     that have different values, so trim any with the same
   2277 	     value.  Don't consider ifunc and ifunc resolver symbols
   2278 	     duplicates however, because GDB wants to know whether a
   2279 	     text symbol is an ifunc resolver.  */
   2280 	  for (i = 1, j = 1; i < symcount; ++i)
   2281 	    {
   2282 	      const asymbol *s0 = syms[i - 1];
   2283 	      const asymbol *s1 = syms[i];
   2284 
   2285 	      if ((s0->value + s0->section->vma
   2286 		   != s1->value + s1->section->vma)
   2287 		  || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
   2288 		      != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
   2289 		syms[j++] = syms[i];
   2290 	    }
   2291 	  symcount = j;
   2292 	}
   2293 
   2294       i = 0;
   2295       /* Note that here and in compare_symbols we can't compare opd and
   2296 	 sym->section directly.  With separate debug info files, the
   2297 	 symbols will be extracted from the debug file while abfd passed
   2298 	 to this function is the real binary.  */
   2299       if (strcmp (syms[i]->section->name, ".opd") == 0)
   2300 	++i;
   2301       codesecsym = i;
   2302 
   2303       for (; i < symcount; ++i)
   2304 	if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
   2305 					 | SEC_THREAD_LOCAL))
   2306 	     != (SEC_CODE | SEC_ALLOC))
   2307 	    || (syms[i]->flags & BSF_SECTION_SYM) == 0)
   2308 	  break;
   2309       codesecsymend = i;
   2310 
   2311       for (; i < symcount; ++i)
   2312 	if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
   2313 	  break;
   2314       secsymend = i;
   2315 
   2316       for (; i < symcount; ++i)
   2317 	if (strcmp (syms[i]->section->name, ".opd") != 0)
   2318 	  break;
   2319       opdsymend = i;
   2320 
   2321       for (; i < symcount; ++i)
   2322 	if (((syms[i]->section->flags
   2323 	      & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
   2324 	    != (SEC_CODE | SEC_ALLOC))
   2325 	  break;
   2326       symcount = i;
   2327     }
   2328   count = 0;
   2329 
   2330   if (relocatable)
   2331     {
   2332       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
   2333       arelent *r;
   2334       size_t size;
   2335       size_t relcount;
   2336 
   2337       if (opdsymend == secsymend)
   2338 	goto done;
   2339 
   2340       slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   2341       relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
   2342       if (relcount == 0)
   2343 	goto done;
   2344 
   2345       if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
   2346 	{
   2347 	  count = -1;
   2348 	  goto done;
   2349 	}
   2350 
   2351       size = 0;
   2352       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
   2353 	{
   2354 	  asymbol *sym;
   2355 
   2356 	  while (r < opd->relocation + relcount
   2357 		 && r->address < syms[i]->value + opd->vma)
   2358 	    ++r;
   2359 
   2360 	  if (r == opd->relocation + relcount)
   2361 	    break;
   2362 
   2363 	  if (r->address != syms[i]->value + opd->vma)
   2364 	    continue;
   2365 
   2366 	  if (r->howto->type != R_PPC64_ADDR64)
   2367 	    continue;
   2368 
   2369 	  sym = *r->sym_ptr_ptr;
   2370 	  if (!sym_exists_at (syms, opdsymend, symcount,
   2371 			      sym->section->id, sym->value + r->addend))
   2372 	    {
   2373 	      ++count;
   2374 	      size += sizeof (asymbol);
   2375 	      size += strlen (syms[i]->name) + 2;
   2376 	    }
   2377 	}
   2378 
   2379       if (size == 0)
   2380 	goto done;
   2381       s = *ret = bfd_malloc (size);
   2382       if (s == NULL)
   2383 	{
   2384 	  count = -1;
   2385 	  goto done;
   2386 	}
   2387 
   2388       names = (char *) (s + count);
   2389 
   2390       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
   2391 	{
   2392 	  asymbol *sym;
   2393 
   2394 	  while (r < opd->relocation + relcount
   2395 		 && r->address < syms[i]->value + opd->vma)
   2396 	    ++r;
   2397 
   2398 	  if (r == opd->relocation + relcount)
   2399 	    break;
   2400 
   2401 	  if (r->address != syms[i]->value + opd->vma)
   2402 	    continue;
   2403 
   2404 	  if (r->howto->type != R_PPC64_ADDR64)
   2405 	    continue;
   2406 
   2407 	  sym = *r->sym_ptr_ptr;
   2408 	  if (!sym_exists_at (syms, opdsymend, symcount,
   2409 			      sym->section->id, sym->value + r->addend))
   2410 	    {
   2411 	      size_t len;
   2412 
   2413 	      *s = *syms[i];
   2414 	      s->flags |= BSF_SYNTHETIC;
   2415 	      s->section = sym->section;
   2416 	      s->value = sym->value + r->addend;
   2417 	      s->name = names;
   2418 	      *names++ = '.';
   2419 	      len = strlen (syms[i]->name);
   2420 	      memcpy (names, syms[i]->name, len + 1);
   2421 	      names += len + 1;
   2422 	      /* Have udata.p point back to the original symbol this
   2423 		 synthetic symbol was derived from.  */
   2424 	      s->udata.p = syms[i];
   2425 	      s++;
   2426 	    }
   2427 	}
   2428     }
   2429   else
   2430     {
   2431       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
   2432       bfd_byte *contents = NULL;
   2433       size_t size;
   2434       size_t plt_count = 0;
   2435       bfd_vma glink_vma = 0, resolv_vma = 0;
   2436       asection *dynamic, *glink = NULL, *relplt = NULL;
   2437       arelent *p;
   2438 
   2439       if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
   2440 	{
   2441 	free_contents_and_exit_err:
   2442 	  count = -1;
   2443 	free_contents_and_exit:
   2444 	  free (contents);
   2445 	  goto done;
   2446 	}
   2447 
   2448       size = 0;
   2449       for (i = secsymend; i < opdsymend; ++i)
   2450 	{
   2451 	  bfd_vma ent;
   2452 
   2453 	  /* Ignore bogus symbols.  */
   2454 	  if (syms[i]->value > opd->size - 8)
   2455 	    continue;
   2456 
   2457 	  ent = bfd_get_64 (abfd, contents + syms[i]->value);
   2458 	  if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
   2459 	    {
   2460 	      ++count;
   2461 	      size += sizeof (asymbol);
   2462 	      size += strlen (syms[i]->name) + 2;
   2463 	    }
   2464 	}
   2465 
   2466       /* Get start of .glink stubs from DT_PPC64_GLINK.  */
   2467       if (dyn_count != 0
   2468 	  && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
   2469 	{
   2470 	  bfd_byte *dynbuf, *extdyn, *extdynend;
   2471 	  size_t extdynsize;
   2472 	  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
   2473 
   2474 	  if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
   2475 	    goto free_contents_and_exit_err;
   2476 
   2477 	  extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
   2478 	  swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
   2479 
   2480 	  extdyn = dynbuf;
   2481 	  extdynend = extdyn + dynamic->size;
   2482 	  for (; extdyn < extdynend; extdyn += extdynsize)
   2483 	    {
   2484 	      Elf_Internal_Dyn dyn;
   2485 	      (*swap_dyn_in) (abfd, extdyn, &dyn);
   2486 
   2487 	      if (dyn.d_tag == DT_NULL)
   2488 		break;
   2489 
   2490 	      if (dyn.d_tag == DT_PPC64_GLINK)
   2491 		{
   2492 		  /* The first glink stub starts at DT_PPC64_GLINK plus 32.
   2493 		     See comment in ppc64_elf_finish_dynamic_sections. */
   2494 		  glink_vma = dyn.d_un.d_val + 8 * 4;
   2495 		  /* The .glink section usually does not survive the final
   2496 		     link; search for the section (usually .text) where the
   2497 		     glink stubs now reside.  */
   2498 		  glink = bfd_sections_find_if (abfd, section_covers_vma,
   2499 						&glink_vma);
   2500 		  break;
   2501 		}
   2502 	    }
   2503 
   2504 	  free (dynbuf);
   2505 	}
   2506 
   2507       if (glink != NULL)
   2508 	{
   2509 	  /* Determine __glink trampoline by reading the relative branch
   2510 	     from the first glink stub.  */
   2511 	  bfd_byte buf[4];
   2512 	  unsigned int off = 0;
   2513 
   2514 	  while (bfd_get_section_contents (abfd, glink, buf,
   2515 					   glink_vma + off - glink->vma, 4))
   2516 	    {
   2517 	      unsigned int insn = bfd_get_32 (abfd, buf);
   2518 	      insn ^= B_DOT;
   2519 	      if ((insn & ~0x3fffffc) == 0)
   2520 		{
   2521 		  resolv_vma
   2522 		    = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
   2523 		  break;
   2524 		}
   2525 	      off += 4;
   2526 	      if (off > 4)
   2527 		break;
   2528 	    }
   2529 
   2530 	  if (resolv_vma)
   2531 	    size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
   2532 
   2533 	  relplt = bfd_get_section_by_name (abfd, ".rela.plt");
   2534 	  if (relplt != NULL)
   2535 	    {
   2536 	      slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   2537 	      if (!(*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
   2538 		goto free_contents_and_exit_err;
   2539 
   2540 	      plt_count = relplt->size / sizeof (Elf64_External_Rela);
   2541 	      size += plt_count * sizeof (asymbol);
   2542 
   2543 	      p = relplt->relocation;
   2544 	      for (i = 0; i < plt_count; i++, p++)
   2545 		{
   2546 		  size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
   2547 		  if (p->addend != 0)
   2548 		    size += sizeof ("+0x") - 1 + 16;
   2549 		}
   2550 	    }
   2551 	}
   2552 
   2553       if (size == 0)
   2554 	goto free_contents_and_exit;
   2555       s = *ret = bfd_malloc (size);
   2556       if (s == NULL)
   2557 	goto free_contents_and_exit_err;
   2558 
   2559       names = (char *) (s + count + plt_count + (resolv_vma != 0));
   2560 
   2561       for (i = secsymend; i < opdsymend; ++i)
   2562 	{
   2563 	  bfd_vma ent;
   2564 
   2565 	  if (syms[i]->value > opd->size - 8)
   2566 	    continue;
   2567 
   2568 	  ent = bfd_get_64 (abfd, contents + syms[i]->value);
   2569 	  if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
   2570 	    {
   2571 	      size_t lo, hi;
   2572 	      size_t len;
   2573 	      asection *sec = abfd->sections;
   2574 
   2575 	      *s = *syms[i];
   2576 	      lo = codesecsym;
   2577 	      hi = codesecsymend;
   2578 	      while (lo < hi)
   2579 		{
   2580 		  size_t mid = (lo + hi) >> 1;
   2581 		  if (syms[mid]->section->vma < ent)
   2582 		    lo = mid + 1;
   2583 		  else if (syms[mid]->section->vma > ent)
   2584 		    hi = mid;
   2585 		  else
   2586 		    {
   2587 		      sec = syms[mid]->section;
   2588 		      break;
   2589 		    }
   2590 		}
   2591 
   2592 	      if (lo >= hi && lo > codesecsym)
   2593 		sec = syms[lo - 1]->section;
   2594 
   2595 	      for (; sec != NULL; sec = sec->next)
   2596 		{
   2597 		  if (sec->vma > ent)
   2598 		    break;
   2599 		  /* SEC_LOAD may not be set if SEC is from a separate debug
   2600 		     info file.  */
   2601 		  if ((sec->flags & SEC_ALLOC) == 0)
   2602 		    break;
   2603 		  if ((sec->flags & SEC_CODE) != 0)
   2604 		    s->section = sec;
   2605 		}
   2606 	      s->flags |= BSF_SYNTHETIC;
   2607 	      s->value = ent - s->section->vma;
   2608 	      s->name = names;
   2609 	      *names++ = '.';
   2610 	      len = strlen (syms[i]->name);
   2611 	      memcpy (names, syms[i]->name, len + 1);
   2612 	      names += len + 1;
   2613 	      /* Have udata.p point back to the original symbol this
   2614 		 synthetic symbol was derived from.  */
   2615 	      s->udata.p = syms[i];
   2616 	      s++;
   2617 	    }
   2618 	}
   2619       free (contents);
   2620 
   2621       if (glink != NULL && relplt != NULL)
   2622 	{
   2623 	  if (resolv_vma)
   2624 	    {
   2625 	      /* Add a symbol for the main glink trampoline.  */
   2626 	      memset (s, 0, sizeof *s);
   2627 	      s->the_bfd = abfd;
   2628 	      s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
   2629 	      s->section = glink;
   2630 	      s->value = resolv_vma - glink->vma;
   2631 	      s->name = names;
   2632 	      memcpy (names, "__glink_PLTresolve",
   2633 		      sizeof ("__glink_PLTresolve"));
   2634 	      names += sizeof ("__glink_PLTresolve");
   2635 	      s++;
   2636 	      count++;
   2637 	    }
   2638 
   2639 	  /* FIXME: It would be very much nicer to put sym@plt on the
   2640 	     stub rather than on the glink branch table entry.  The
   2641 	     objdump disassembler would then use a sensible symbol
   2642 	     name on plt calls.  The difficulty in doing so is
   2643 	     a) finding the stubs, and,
   2644 	     b) matching stubs against plt entries, and,
   2645 	     c) there can be multiple stubs for a given plt entry.
   2646 
   2647 	     Solving (a) could be done by code scanning, but older
   2648 	     ppc64 binaries used different stubs to current code.
   2649 	     (b) is the tricky one since you need to known the toc
   2650 	     pointer for at least one function that uses a pic stub to
   2651 	     be able to calculate the plt address referenced.
   2652 	     (c) means gdb would need to set multiple breakpoints (or
   2653 	     find the glink branch itself) when setting breakpoints
   2654 	     for pending shared library loads.  */
   2655 	  p = relplt->relocation;
   2656 	  for (i = 0; i < plt_count; i++, p++)
   2657 	    {
   2658 	      size_t len;
   2659 
   2660 	      *s = **p->sym_ptr_ptr;
   2661 	      /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
   2662 		 we are defining a symbol, ensure one of them is set.  */
   2663 	      if ((s->flags & BSF_LOCAL) == 0)
   2664 		s->flags |= BSF_GLOBAL;
   2665 	      s->flags |= BSF_SYNTHETIC;
   2666 	      s->section = glink;
   2667 	      s->value = glink_vma - glink->vma;
   2668 	      s->name = names;
   2669 	      s->udata.p = NULL;
   2670 	      len = strlen ((*p->sym_ptr_ptr)->name);
   2671 	      memcpy (names, (*p->sym_ptr_ptr)->name, len);
   2672 	      names += len;
   2673 	      if (p->addend != 0)
   2674 		{
   2675 		  memcpy (names, "+0x", sizeof ("+0x") - 1);
   2676 		  names += sizeof ("+0x") - 1;
   2677 		  bfd_sprintf_vma (abfd, names, p->addend);
   2678 		  names += strlen (names);
   2679 		}
   2680 	      memcpy (names, "@plt", sizeof ("@plt"));
   2681 	      names += sizeof ("@plt");
   2682 	      s++;
   2683 	      if (abi < 2)
   2684 		{
   2685 		  glink_vma += 8;
   2686 		  if (i >= 0x8000)
   2687 		    glink_vma += 4;
   2688 		}
   2689 	      else
   2690 		glink_vma += 4;
   2691 	    }
   2692 	  count += plt_count;
   2693 	}
   2694     }
   2695 
   2696  done:
   2697   free (syms);
   2698   return count;
   2699 }
   2700 
   2701 /* The following functions are specific to the ELF linker, while
   2703    functions above are used generally.  Those named ppc64_elf_* are
   2704    called by the main ELF linker code.  They appear in this file more
   2705    or less in the order in which they are called.  eg.
   2706    ppc64_elf_check_relocs is called early in the link process,
   2707    ppc64_elf_finish_dynamic_sections is one of the last functions
   2708    called.
   2709 
   2710    PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
   2711    functions have both a function code symbol and a function descriptor
   2712    symbol.  A call to foo in a relocatable object file looks like:
   2713 
   2714    .		.text
   2715    .	x:
   2716    .		bl	.foo
   2717    .		nop
   2718 
   2719    The function definition in another object file might be:
   2720 
   2721    .		.section .opd
   2722    .	foo:	.quad	.foo
   2723    .		.quad	.TOC.@tocbase
   2724    .		.quad	0
   2725    .
   2726    .		.text
   2727    .	.foo:	blr
   2728 
   2729    When the linker resolves the call during a static link, the branch
   2730    unsurprisingly just goes to .foo and the .opd information is unused.
   2731    If the function definition is in a shared library, things are a little
   2732    different:  The call goes via a plt call stub, the opd information gets
   2733    copied to the plt, and the linker patches the nop.
   2734 
   2735    .	x:
   2736    .		bl	.foo_stub
   2737    .		ld	2,40(1)
   2738    .
   2739    .
   2740    .	.foo_stub:
   2741    .		std	2,40(1)			# in practice, the call stub
   2742    .		addis	11,2,Lfoo@toc@ha	# is slightly optimized, but
   2743    .		addi	11,11,Lfoo@toc@l	# this is the general idea
   2744    .		ld	12,0(11)
   2745    .		ld	2,8(11)
   2746    .		mtctr	12
   2747    .		ld	11,16(11)
   2748    .		bctr
   2749    .
   2750    .		.section .plt
   2751    .	Lfoo:	reloc (R_PPC64_JMP_SLOT, foo)
   2752 
   2753    The "reloc ()" notation is supposed to indicate that the linker emits
   2754    an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
   2755    copying.
   2756 
   2757    What are the difficulties here?  Well, firstly, the relocations
   2758    examined by the linker in check_relocs are against the function code
   2759    sym .foo, while the dynamic relocation in the plt is emitted against
   2760    the function descriptor symbol, foo.  Somewhere along the line, we need
   2761    to carefully copy dynamic link information from one symbol to the other.
   2762    Secondly, the generic part of the elf linker will make .foo a dynamic
   2763    symbol as is normal for most other backends.  We need foo dynamic
   2764    instead, at least for an application final link.  However, when
   2765    creating a shared library containing foo, we need to have both symbols
   2766    dynamic so that references to .foo are satisfied during the early
   2767    stages of linking.  Otherwise the linker might decide to pull in a
   2768    definition from some other object, eg. a static library.
   2769 
   2770    Update: As of August 2004, we support a new convention.  Function
   2771    calls may use the function descriptor symbol, ie. "bl foo".  This
   2772    behaves exactly as "bl .foo".  */
   2773 
   2774 /* Of those relocs that might be copied as dynamic relocs, this
   2775    function selects those that must be copied when linking a shared
   2776    library or PIE, even when the symbol is local.  */
   2777 
   2778 static int
   2779 must_be_dyn_reloc (struct bfd_link_info *info,
   2780 		   enum elf_ppc64_reloc_type r_type)
   2781 {
   2782   switch (r_type)
   2783     {
   2784     default:
   2785       /* Only relative relocs can be resolved when the object load
   2786 	 address isn't fixed.  DTPREL64 is excluded because the
   2787 	 dynamic linker needs to differentiate global dynamic from
   2788 	 local dynamic __tls_index pairs when PPC64_OPT_TLS is set.  */
   2789       return 1;
   2790 
   2791     case R_PPC64_REL32:
   2792     case R_PPC64_REL64:
   2793     case R_PPC64_REL30:
   2794     case R_PPC64_TOC16:
   2795     case R_PPC64_TOC16_DS:
   2796     case R_PPC64_TOC16_LO:
   2797     case R_PPC64_TOC16_HI:
   2798     case R_PPC64_TOC16_HA:
   2799     case R_PPC64_TOC16_LO_DS:
   2800       return 0;
   2801 
   2802     case R_PPC64_TPREL16:
   2803     case R_PPC64_TPREL16_LO:
   2804     case R_PPC64_TPREL16_HI:
   2805     case R_PPC64_TPREL16_HA:
   2806     case R_PPC64_TPREL16_DS:
   2807     case R_PPC64_TPREL16_LO_DS:
   2808     case R_PPC64_TPREL16_HIGH:
   2809     case R_PPC64_TPREL16_HIGHA:
   2810     case R_PPC64_TPREL16_HIGHER:
   2811     case R_PPC64_TPREL16_HIGHERA:
   2812     case R_PPC64_TPREL16_HIGHEST:
   2813     case R_PPC64_TPREL16_HIGHESTA:
   2814     case R_PPC64_TPREL64:
   2815     case R_PPC64_TPREL34:
   2816       /* These relocations are relative but in a shared library the
   2817 	 linker doesn't know the thread pointer base.  */
   2818       return bfd_link_dll (info);
   2819     }
   2820 }
   2821 
   2822 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
   2823    copying dynamic variables from a shared lib into an app's .dynbss
   2824    section, and instead use a dynamic relocation to point into the
   2825    shared lib.  With code that gcc generates it is vital that this be
   2826    enabled;  In the PowerPC64 ELFv1 ABI the address of a function is
   2827    actually the address of a function descriptor which resides in the
   2828    .opd section.  gcc uses the descriptor directly rather than going
   2829    via the GOT as some other ABIs do, which means that initialized
   2830    function pointers reference the descriptor.  Thus, a function
   2831    pointer initialized to the address of a function in a shared
   2832    library will either require a .dynbss copy and a copy reloc, or a
   2833    dynamic reloc.  Using a .dynbss copy redefines the function
   2834    descriptor symbol to point to the copy.  This presents a problem as
   2835    a PLT entry for that function is also initialized from the function
   2836    descriptor symbol and the copy may not be initialized first.  */
   2837 #define ELIMINATE_COPY_RELOCS 1
   2838 
   2839 /* Section name for stubs is the associated section name plus this
   2840    string.  */
   2841 #define STUB_SUFFIX ".stub"
   2842 
   2843 /* Linker stubs.
   2844    ppc_stub_long_branch:
   2845    Used when a 14 bit branch (or even a 24 bit branch) can't reach its
   2846    destination, but a 24 bit branch in a stub section will reach.
   2847    .	b	dest
   2848 
   2849    ppc_stub_plt_branch:
   2850    Similar to the above, but a 24 bit branch in the stub section won't
   2851    reach its destination.
   2852    .	addis	%r12,%r2,xxx@toc@ha
   2853    .	ld	%r12,xxx@toc@l(%r12)
   2854    .	mtctr	%r12
   2855    .	bctr
   2856 
   2857    ppc_stub_plt_call:
   2858    Used to call a function in a shared library.  If it so happens that
   2859    the plt entry referenced crosses a 64k boundary, then an extra
   2860    "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
   2861    ppc_stub_plt_call_r2save starts with "std %r2,40(%r1)".
   2862    .	addis	%r11,%r2,xxx@toc@ha
   2863    .	ld	%r12,xxx+0@toc@l(%r11)
   2864    .	mtctr	%r12
   2865    .	ld	%r2,xxx+8@toc@l(%r11)
   2866    .	ld	%r11,xxx+16@toc@l(%r11)
   2867    .	bctr
   2868 
   2869    ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
   2870    code to adjust the value and save r2 to support multiple toc sections.
   2871    A ppc_stub_long_branch with an r2 offset looks like:
   2872    .	std	%r2,40(%r1)
   2873    .	addis	%r2,%r2,off@ha
   2874    .	addi	%r2,%r2,off@l
   2875    .	b	dest
   2876 
   2877    A ppc_stub_plt_branch with an r2 offset looks like:
   2878    .	std	%r2,40(%r1)
   2879    .	addis	%r12,%r2,xxx@toc@ha
   2880    .	ld	%r12,xxx@toc@l(%r12)
   2881    .	addis	%r2,%r2,off@ha
   2882    .	addi	%r2,%r2,off@l
   2883    .	mtctr	%r12
   2884    .	bctr
   2885 
   2886    All of the above stubs are shown as their ELFv1 variants.  ELFv2
   2887    variants exist too, simpler for plt calls since a new toc pointer
   2888    and static chain are not loaded by the stub.  In addition, ELFv2
   2889    has some more complex stubs to handle calls marked with NOTOC
   2890    relocs from functions where r2 is not a valid toc pointer.  These
   2891    come in two flavours, the ones shown below, and _both variants that
   2892    start with "std %r2,24(%r1)" to save r2 in the unlikely event that
   2893    one call is from a function where r2 is used as the toc pointer but
   2894    needs a toc adjusting stub for small-model multi-toc, and another
   2895    call is from a function where r2 is not valid.
   2896    ppc_stub_long_branch_notoc:
   2897    .	mflr	%r12
   2898    .	bcl	20,31,1f
   2899    .  1:
   2900    .	mflr	%r11
   2901    .	mtlr	%r12
   2902    .	addis	%r12,%r11,dest-1b@ha
   2903    .	addi	%r12,%r12,dest-1b@l
   2904    .	b	dest
   2905 
   2906    ppc_stub_plt_branch_notoc:
   2907    .	mflr	%r12
   2908    .	bcl	20,31,1f
   2909    .  1:
   2910    .	mflr	%r11
   2911    .	mtlr	%r12
   2912    .	lis	%r12,xxx-1b@highest
   2913    .	ori	%r12,%r12,xxx-1b@higher
   2914    .	sldi	%r12,%r12,32
   2915    .	oris	%r12,%r12,xxx-1b@high
   2916    .	ori	%r12,%r12,xxx-1b@l
   2917    .	add	%r12,%r11,%r12
   2918    .	mtctr	%r12
   2919    .	bctr
   2920 
   2921    ppc_stub_plt_call_notoc:
   2922    .	mflr	%r12
   2923    .	bcl	20,31,1f
   2924    .  1:
   2925    .	mflr	%r11
   2926    .	mtlr	%r12
   2927    .	lis	%r12,xxx-1b@highest
   2928    .	ori	%r12,%r12,xxx-1b@higher
   2929    .	sldi	%r12,%r12,32
   2930    .	oris	%r12,%r12,xxx-1b@high
   2931    .	ori	%r12,%r12,xxx-1b@l
   2932    .	ldx	%r12,%r11,%r12
   2933    .	mtctr	%r12
   2934    .	bctr
   2935 
   2936    There are also ELFv1 power10 variants of these stubs.
   2937    ppc_stub_long_branch_notoc:
   2938    .	pla	%r12,dest@pcrel
   2939    .	b	dest
   2940    ppc_stub_plt_branch_notoc:
   2941    .	lis	%r11,(dest-1f)@highesta34
   2942    .	ori	%r11,%r11,(dest-1f)@highera34
   2943    .	sldi	%r11,%r11,34
   2944    . 1: pla	%r12,dest@pcrel
   2945    .	add	%r12,%r11,%r12
   2946    .	mtctr	%r12
   2947    .	bctr
   2948    ppc_stub_plt_call_notoc:
   2949    .	lis	%r11,(xxx-1f)@highesta34
   2950    .	ori	%r11,%r11,(xxx-1f)@highera34
   2951    .	sldi	%r11,%r11,34
   2952    . 1: pla	%r12,xxx@pcrel
   2953    .	ldx	%r12,%r11,%r12
   2954    .	mtctr	%r12
   2955    .	bctr
   2956 
   2957    In cases where the high instructions would add zero, they are
   2958    omitted and following instructions modified in some cases.
   2959    For example, a power10 ppc_stub_plt_call_notoc might simplify down
   2960    to
   2961    .	pld	%r12,xxx@pcrel
   2962    .	mtctr	%r12
   2963    .	bctr
   2964 
   2965    For a given stub group (a set of sections all using the same toc
   2966    pointer value) there will be just one stub type used for any
   2967    particular function symbol.  For example, if printf is called from
   2968    code with the tocsave optimization (ie. r2 saved in function
   2969    prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
   2970    and from other code without the tocsave optimization requiring a
   2971    ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
   2972    type will be created.  Calls with the tocsave optimization will
   2973    enter this stub after the instruction saving r2.  A similar
   2974    situation exists when calls are marked with R_PPC64_REL24_NOTOC
   2975    relocations.  These require a ppc_stub_plt_call_notoc linkage stub
   2976    to call an external function like printf.  If other calls to printf
   2977    require a ppc_stub_plt_call linkage stub then a single
   2978    ppc_stub_plt_call_notoc linkage stub will be used for both types of
   2979    call.  If other calls to printf require a ppc_stub_plt_call_r2save
   2980    linkage stub then a single ppc_stub_plt_call_both linkage stub will
   2981    be created and calls not requiring r2 to be saved will enter the
   2982    stub after the r2 save instruction.  There is an analogous
   2983    hierarchy of long branch and plt branch stubs for local call
   2984    linkage.  */
   2985 
   2986 enum ppc_stub_type
   2987 {
   2988   ppc_stub_none,
   2989   ppc_stub_long_branch,
   2990   ppc_stub_long_branch_r2off,
   2991   ppc_stub_long_branch_notoc,
   2992   ppc_stub_long_branch_both, /* r2off and notoc variants both needed.  */
   2993   ppc_stub_plt_branch,
   2994   ppc_stub_plt_branch_r2off,
   2995   ppc_stub_plt_branch_notoc,
   2996   ppc_stub_plt_branch_both,
   2997   ppc_stub_plt_call,
   2998   ppc_stub_plt_call_r2save,
   2999   ppc_stub_plt_call_notoc,
   3000   ppc_stub_plt_call_both,
   3001   ppc_stub_global_entry,
   3002   ppc_stub_save_res
   3003 };
   3004 
   3005 /* Information on stub grouping.  */
   3006 struct map_stub
   3007 {
   3008   /* The stub section.  */
   3009   asection *stub_sec;
   3010   /* This is the section to which stubs in the group will be attached.  */
   3011   asection *link_sec;
   3012   /* Next group.  */
   3013   struct map_stub *next;
   3014   /* Whether to emit a copy of register save/restore functions in this
   3015      group.  */
   3016   int needs_save_res;
   3017   /* Current offset within stubs after the insn restoring lr in a
   3018      _notoc or _both stub using bcl for pc-relative addressing, or
   3019      after the insn restoring lr in a __tls_get_addr_opt plt stub.  */
   3020   unsigned int lr_restore;
   3021   /* Accumulated size of EH info emitted to describe return address
   3022      if stubs modify lr.  Does not include 17 byte FDE header.  */
   3023   unsigned int eh_size;
   3024   /* Offset in glink_eh_frame to the start of EH info for this group.  */
   3025   unsigned int eh_base;
   3026 };
   3027 
   3028 struct ppc_stub_hash_entry
   3029 {
   3030   /* Base hash table entry structure.  */
   3031   struct bfd_hash_entry root;
   3032 
   3033   enum ppc_stub_type stub_type;
   3034 
   3035   /* Group information.  */
   3036   struct map_stub *group;
   3037 
   3038   /* Offset within stub_sec of the beginning of this stub.  */
   3039   bfd_vma stub_offset;
   3040 
   3041   /* Given the symbol's value and its section we can determine its final
   3042      value when building the stubs (so the stub knows where to jump.  */
   3043   bfd_vma target_value;
   3044   asection *target_section;
   3045 
   3046   /* The symbol table entry, if any, that this was derived from.  */
   3047   struct ppc_link_hash_entry *h;
   3048   struct plt_entry *plt_ent;
   3049 
   3050   /* Symbol type.  */
   3051   unsigned char symtype;
   3052 
   3053   /* Symbol st_other.  */
   3054   unsigned char other;
   3055 };
   3056 
   3057 struct ppc_branch_hash_entry
   3058 {
   3059   /* Base hash table entry structure.  */
   3060   struct bfd_hash_entry root;
   3061 
   3062   /* Offset within branch lookup table.  */
   3063   unsigned int offset;
   3064 
   3065   /* Generation marker.  */
   3066   unsigned int iter;
   3067 };
   3068 
   3069 /* Used to track dynamic relocations for local symbols.  */
   3070 struct ppc_dyn_relocs
   3071 {
   3072   struct ppc_dyn_relocs *next;
   3073 
   3074   /* The input section of the reloc.  */
   3075   asection *sec;
   3076 
   3077   /* Total number of relocs copied for the input section.  */
   3078   unsigned int count : 31;
   3079 
   3080   /* Whether this entry is for STT_GNU_IFUNC symbols.  */
   3081   unsigned int ifunc : 1;
   3082 };
   3083 
   3084 struct ppc_link_hash_entry
   3085 {
   3086   struct elf_link_hash_entry elf;
   3087 
   3088   union
   3089   {
   3090     /* A pointer to the most recently used stub hash entry against this
   3091        symbol.  */
   3092     struct ppc_stub_hash_entry *stub_cache;
   3093 
   3094     /* A pointer to the next symbol starting with a '.'  */
   3095     struct ppc_link_hash_entry *next_dot_sym;
   3096   } u;
   3097 
   3098   /* Link between function code and descriptor symbols.  */
   3099   struct ppc_link_hash_entry *oh;
   3100 
   3101   /* Flag function code and descriptor symbols.  */
   3102   unsigned int is_func:1;
   3103   unsigned int is_func_descriptor:1;
   3104   unsigned int fake:1;
   3105 
   3106   /* Whether global opd/toc sym has been adjusted or not.
   3107      After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
   3108      should be set for all globals defined in any opd/toc section.  */
   3109   unsigned int adjust_done:1;
   3110 
   3111   /* Set if this is an out-of-line register save/restore function,
   3112      with non-standard calling convention.  */
   3113   unsigned int save_res:1;
   3114 
   3115   /* Set if a duplicate symbol with non-zero localentry is detected,
   3116      even when the duplicate symbol does not provide a definition.  */
   3117   unsigned int non_zero_localentry:1;
   3118 
   3119   /* Contexts in which symbol is used in the GOT (or TOC).
   3120      Bits are or'd into the mask as the corresponding relocs are
   3121      encountered during check_relocs, with TLS_TLS being set when any
   3122      of the other TLS bits are set.  tls_optimize clears bits when
   3123      optimizing to indicate the corresponding GOT entry type is not
   3124      needed.  If set, TLS_TLS is never cleared.  tls_optimize may also
   3125      set TLS_GDIE when a GD reloc turns into an IE one.
   3126      These flags are also kept for local symbols.  */
   3127 #define TLS_TLS		 1	/* Any TLS reloc.  */
   3128 #define TLS_GD		 2	/* GD reloc. */
   3129 #define TLS_LD		 4	/* LD reloc. */
   3130 #define TLS_TPREL	 8	/* TPREL reloc, => IE. */
   3131 #define TLS_DTPREL	16	/* DTPREL reloc, => LD. */
   3132 #define TLS_MARK	32	/* __tls_get_addr call marked. */
   3133 #define TLS_GDIE	64	/* GOT TPREL reloc resulting from GD->IE. */
   3134 #define TLS_EXPLICIT   256	/* TOC section TLS reloc, not stored. */
   3135   unsigned char tls_mask;
   3136 
   3137   /* The above field is also used to mark function symbols.  In which
   3138      case TLS_TLS will be 0.  */
   3139 #define PLT_IFUNC	 2	/* STT_GNU_IFUNC.  */
   3140 #define PLT_KEEP	 4	/* inline plt call requires plt entry.  */
   3141 #define NON_GOT        256	/* local symbol plt, not stored.  */
   3142 };
   3143 
   3144 static inline struct ppc_link_hash_entry *
   3145 ppc_elf_hash_entry (struct elf_link_hash_entry *ent)
   3146 {
   3147   return (struct ppc_link_hash_entry *) ent;
   3148 }
   3149 
   3150 /* ppc64 ELF linker hash table.  */
   3151 
   3152 struct ppc_link_hash_table
   3153 {
   3154   struct elf_link_hash_table elf;
   3155 
   3156   /* The stub hash table.  */
   3157   struct bfd_hash_table stub_hash_table;
   3158 
   3159   /* Another hash table for plt_branch stubs.  */
   3160   struct bfd_hash_table branch_hash_table;
   3161 
   3162   /* Hash table for function prologue tocsave.  */
   3163   htab_t tocsave_htab;
   3164 
   3165   /* Various options and other info passed from the linker.  */
   3166   struct ppc64_elf_params *params;
   3167 
   3168   /* The size of sec_info below.  */
   3169   unsigned int sec_info_arr_size;
   3170 
   3171   /* Per-section array of extra section info.  Done this way rather
   3172      than as part of ppc64_elf_section_data so we have the info for
   3173      non-ppc64 sections.  */
   3174   struct
   3175   {
   3176     /* Along with elf_gp, specifies the TOC pointer used by this section.  */
   3177     bfd_vma toc_off;
   3178 
   3179     union
   3180     {
   3181       /* The section group that this section belongs to.  */
   3182       struct map_stub *group;
   3183       /* A temp section list pointer.  */
   3184       asection *list;
   3185     } u;
   3186   } *sec_info;
   3187 
   3188   /* Linked list of groups.  */
   3189   struct map_stub *group;
   3190 
   3191   /* Temp used when calculating TOC pointers.  */
   3192   bfd_vma toc_curr;
   3193   bfd *toc_bfd;
   3194   asection *toc_first_sec;
   3195 
   3196   /* Used when adding symbols.  */
   3197   struct ppc_link_hash_entry *dot_syms;
   3198 
   3199   /* Shortcuts to get to dynamic linker sections.  */
   3200   asection *glink;
   3201   asection *global_entry;
   3202   asection *sfpr;
   3203   asection *pltlocal;
   3204   asection *relpltlocal;
   3205   asection *brlt;
   3206   asection *relbrlt;
   3207   asection *glink_eh_frame;
   3208 
   3209   /* Shortcut to .__tls_get_addr and __tls_get_addr.  */
   3210   struct ppc_link_hash_entry *tls_get_addr;
   3211   struct ppc_link_hash_entry *tls_get_addr_fd;
   3212   struct ppc_link_hash_entry *tga_desc;
   3213   struct ppc_link_hash_entry *tga_desc_fd;
   3214   struct map_stub *tga_group;
   3215 
   3216   /* The size of reliplt used by got entry relocs.  */
   3217   bfd_size_type got_reli_size;
   3218 
   3219   /* Statistics.  */
   3220   unsigned long stub_count[ppc_stub_global_entry];
   3221 
   3222   /* Number of stubs against global syms.  */
   3223   unsigned long stub_globals;
   3224 
   3225   /* Set if we're linking code with function descriptors.  */
   3226   unsigned int opd_abi:1;
   3227 
   3228   /* Support for multiple toc sections.  */
   3229   unsigned int do_multi_toc:1;
   3230   unsigned int multi_toc_needed:1;
   3231   unsigned int second_toc_pass:1;
   3232   unsigned int do_toc_opt:1;
   3233 
   3234   /* Set if tls optimization is enabled.  */
   3235   unsigned int do_tls_opt:1;
   3236 
   3237   /* Set if inline plt calls should be converted to direct calls.  */
   3238   unsigned int can_convert_all_inline_plt:1;
   3239 
   3240   /* Set on error.  */
   3241   unsigned int stub_error:1;
   3242 
   3243   /* Whether func_desc_adjust needs to be run over symbols.  */
   3244   unsigned int need_func_desc_adj:1;
   3245 
   3246   /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized.  */
   3247   unsigned int has_plt_localentry0:1;
   3248 
   3249   /* Whether calls are made via the PLT from NOTOC functions.  */
   3250   unsigned int notoc_plt:1;
   3251 
   3252   /* Whether any code linked seems to be Power10.  */
   3253   unsigned int has_power10_relocs:1;
   3254 
   3255   /* Incremented every time we size stubs.  */
   3256   unsigned int stub_iteration;
   3257 };
   3258 
   3259 /* Rename some of the generic section flags to better document how they
   3260    are used here.  */
   3261 
   3262 /* Nonzero if this section has TLS related relocations.  */
   3263 #define has_tls_reloc sec_flg0
   3264 
   3265 /* Nonzero if this section has a call to __tls_get_addr lacking marker
   3266    relocations.  */
   3267 #define nomark_tls_get_addr sec_flg1
   3268 
   3269 /* Nonzero if this section has any toc or got relocs.  */
   3270 #define has_toc_reloc sec_flg2
   3271 
   3272 /* Nonzero if this section has a call to another section that uses
   3273    the toc or got.  */
   3274 #define makes_toc_func_call sec_flg3
   3275 
   3276 /* Recursion protection when determining above flag.  */
   3277 #define call_check_in_progress sec_flg4
   3278 #define call_check_done sec_flg5
   3279 
   3280 /* Get the ppc64 ELF linker hash table from a link_info structure.  */
   3281 
   3282 #define ppc_hash_table(p) \
   3283   ((is_elf_hash_table ((p)->hash)					\
   3284     && elf_hash_table_id (elf_hash_table (p)) == PPC64_ELF_DATA)	\
   3285    ? (struct ppc_link_hash_table *) (p)->hash : NULL)
   3286 
   3287 #define ppc_stub_hash_lookup(table, string, create, copy) \
   3288   ((struct ppc_stub_hash_entry *) \
   3289    bfd_hash_lookup ((table), (string), (create), (copy)))
   3290 
   3291 #define ppc_branch_hash_lookup(table, string, create, copy) \
   3292   ((struct ppc_branch_hash_entry *) \
   3293    bfd_hash_lookup ((table), (string), (create), (copy)))
   3294 
   3295 /* Create an entry in the stub hash table.  */
   3296 
   3297 static struct bfd_hash_entry *
   3298 stub_hash_newfunc (struct bfd_hash_entry *entry,
   3299 		   struct bfd_hash_table *table,
   3300 		   const char *string)
   3301 {
   3302   /* Allocate the structure if it has not already been allocated by a
   3303      subclass.  */
   3304   if (entry == NULL)
   3305     {
   3306       entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
   3307       if (entry == NULL)
   3308 	return entry;
   3309     }
   3310 
   3311   /* Call the allocation method of the superclass.  */
   3312   entry = bfd_hash_newfunc (entry, table, string);
   3313   if (entry != NULL)
   3314     {
   3315       struct ppc_stub_hash_entry *eh;
   3316 
   3317       /* Initialize the local fields.  */
   3318       eh = (struct ppc_stub_hash_entry *) entry;
   3319       eh->stub_type = ppc_stub_none;
   3320       eh->group = NULL;
   3321       eh->stub_offset = 0;
   3322       eh->target_value = 0;
   3323       eh->target_section = NULL;
   3324       eh->h = NULL;
   3325       eh->plt_ent = NULL;
   3326       eh->other = 0;
   3327     }
   3328 
   3329   return entry;
   3330 }
   3331 
   3332 /* Create an entry in the branch hash table.  */
   3333 
   3334 static struct bfd_hash_entry *
   3335 branch_hash_newfunc (struct bfd_hash_entry *entry,
   3336 		     struct bfd_hash_table *table,
   3337 		     const char *string)
   3338 {
   3339   /* Allocate the structure if it has not already been allocated by a
   3340      subclass.  */
   3341   if (entry == NULL)
   3342     {
   3343       entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
   3344       if (entry == NULL)
   3345 	return entry;
   3346     }
   3347 
   3348   /* Call the allocation method of the superclass.  */
   3349   entry = bfd_hash_newfunc (entry, table, string);
   3350   if (entry != NULL)
   3351     {
   3352       struct ppc_branch_hash_entry *eh;
   3353 
   3354       /* Initialize the local fields.  */
   3355       eh = (struct ppc_branch_hash_entry *) entry;
   3356       eh->offset = 0;
   3357       eh->iter = 0;
   3358     }
   3359 
   3360   return entry;
   3361 }
   3362 
   3363 /* Create an entry in a ppc64 ELF linker hash table.  */
   3364 
   3365 static struct bfd_hash_entry *
   3366 link_hash_newfunc (struct bfd_hash_entry *entry,
   3367 		   struct bfd_hash_table *table,
   3368 		   const char *string)
   3369 {
   3370   /* Allocate the structure if it has not already been allocated by a
   3371      subclass.  */
   3372   if (entry == NULL)
   3373     {
   3374       entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
   3375       if (entry == NULL)
   3376 	return entry;
   3377     }
   3378 
   3379   /* Call the allocation method of the superclass.  */
   3380   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
   3381   if (entry != NULL)
   3382     {
   3383       struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
   3384 
   3385       memset (&eh->u.stub_cache, 0,
   3386 	      (sizeof (struct ppc_link_hash_entry)
   3387 	       - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
   3388 
   3389       /* When making function calls, old ABI code references function entry
   3390 	 points (dot symbols), while new ABI code references the function
   3391 	 descriptor symbol.  We need to make any combination of reference and
   3392 	 definition work together, without breaking archive linking.
   3393 
   3394 	 For a defined function "foo" and an undefined call to "bar":
   3395 	 An old object defines "foo" and ".foo", references ".bar" (possibly
   3396 	 "bar" too).
   3397 	 A new object defines "foo" and references "bar".
   3398 
   3399 	 A new object thus has no problem with its undefined symbols being
   3400 	 satisfied by definitions in an old object.  On the other hand, the
   3401 	 old object won't have ".bar" satisfied by a new object.
   3402 
   3403 	 Keep a list of newly added dot-symbols.  */
   3404 
   3405       if (string[0] == '.')
   3406 	{
   3407 	  struct ppc_link_hash_table *htab;
   3408 
   3409 	  htab = (struct ppc_link_hash_table *) table;
   3410 	  eh->u.next_dot_sym = htab->dot_syms;
   3411 	  htab->dot_syms = eh;
   3412 	}
   3413     }
   3414 
   3415   return entry;
   3416 }
   3417 
   3418 struct tocsave_entry
   3419 {
   3420   asection *sec;
   3421   bfd_vma offset;
   3422 };
   3423 
   3424 static hashval_t
   3425 tocsave_htab_hash (const void *p)
   3426 {
   3427   const struct tocsave_entry *e = (const struct tocsave_entry *) p;
   3428   return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
   3429 }
   3430 
   3431 static int
   3432 tocsave_htab_eq (const void *p1, const void *p2)
   3433 {
   3434   const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
   3435   const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
   3436   return e1->sec == e2->sec && e1->offset == e2->offset;
   3437 }
   3438 
   3439 /* Destroy a ppc64 ELF linker hash table.  */
   3440 
   3441 static void
   3442 ppc64_elf_link_hash_table_free (bfd *obfd)
   3443 {
   3444   struct ppc_link_hash_table *htab;
   3445 
   3446   htab = (struct ppc_link_hash_table *) obfd->link.hash;
   3447   if (htab->tocsave_htab)
   3448     htab_delete (htab->tocsave_htab);
   3449   bfd_hash_table_free (&htab->branch_hash_table);
   3450   bfd_hash_table_free (&htab->stub_hash_table);
   3451   _bfd_elf_link_hash_table_free (obfd);
   3452 }
   3453 
   3454 /* Create a ppc64 ELF linker hash table.  */
   3455 
   3456 static struct bfd_link_hash_table *
   3457 ppc64_elf_link_hash_table_create (bfd *abfd)
   3458 {
   3459   struct ppc_link_hash_table *htab;
   3460   size_t amt = sizeof (struct ppc_link_hash_table);
   3461 
   3462   htab = bfd_zmalloc (amt);
   3463   if (htab == NULL)
   3464     return NULL;
   3465 
   3466   if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
   3467 				      sizeof (struct ppc_link_hash_entry),
   3468 				      PPC64_ELF_DATA))
   3469     {
   3470       free (htab);
   3471       return NULL;
   3472     }
   3473 
   3474   /* Init the stub hash table too.  */
   3475   if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
   3476 			    sizeof (struct ppc_stub_hash_entry)))
   3477     {
   3478       _bfd_elf_link_hash_table_free (abfd);
   3479       return NULL;
   3480     }
   3481 
   3482   /* And the branch hash table.  */
   3483   if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
   3484 			    sizeof (struct ppc_branch_hash_entry)))
   3485     {
   3486       bfd_hash_table_free (&htab->stub_hash_table);
   3487       _bfd_elf_link_hash_table_free (abfd);
   3488       return NULL;
   3489     }
   3490 
   3491   htab->tocsave_htab = htab_try_create (1024,
   3492 					tocsave_htab_hash,
   3493 					tocsave_htab_eq,
   3494 					NULL);
   3495   if (htab->tocsave_htab == NULL)
   3496     {
   3497       ppc64_elf_link_hash_table_free (abfd);
   3498       return NULL;
   3499     }
   3500   htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
   3501 
   3502   /* Initializing two fields of the union is just cosmetic.  We really
   3503      only care about glist, but when compiled on a 32-bit host the
   3504      bfd_vma fields are larger.  Setting the bfd_vma to zero makes
   3505      debugger inspection of these fields look nicer.  */
   3506   htab->elf.init_got_refcount.refcount = 0;
   3507   htab->elf.init_got_refcount.glist = NULL;
   3508   htab->elf.init_plt_refcount.refcount = 0;
   3509   htab->elf.init_plt_refcount.glist = NULL;
   3510   htab->elf.init_got_offset.offset = 0;
   3511   htab->elf.init_got_offset.glist = NULL;
   3512   htab->elf.init_plt_offset.offset = 0;
   3513   htab->elf.init_plt_offset.glist = NULL;
   3514 
   3515   return &htab->elf.root;
   3516 }
   3517 
   3518 /* Create sections for linker generated code.  */
   3519 
   3520 static bfd_boolean
   3521 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
   3522 {
   3523   struct ppc_link_hash_table *htab;
   3524   flagword flags;
   3525 
   3526   htab = ppc_hash_table (info);
   3527 
   3528   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
   3529 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   3530   if (htab->params->save_restore_funcs)
   3531     {
   3532       /* Create .sfpr for code to save and restore fp regs.  */
   3533       htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
   3534 						       flags);
   3535       if (htab->sfpr == NULL
   3536 	  || !bfd_set_section_alignment (htab->sfpr, 2))
   3537 	return FALSE;
   3538     }
   3539 
   3540   if (bfd_link_relocatable (info))
   3541     return TRUE;
   3542 
   3543   /* Create .glink for lazy dynamic linking support.  */
   3544   htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
   3545 						    flags);
   3546   if (htab->glink == NULL
   3547       || !bfd_set_section_alignment (htab->glink, 3))
   3548     return FALSE;
   3549 
   3550   /* The part of .glink used by global entry stubs, separate so that
   3551      it can be aligned appropriately without affecting htab->glink.  */
   3552   htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
   3553 							   flags);
   3554   if (htab->global_entry == NULL
   3555       || !bfd_set_section_alignment (htab->global_entry, 2))
   3556     return FALSE;
   3557 
   3558   if (!info->no_ld_generated_unwind_info)
   3559     {
   3560       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
   3561 	       | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   3562       htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
   3563 								 ".eh_frame",
   3564 								 flags);
   3565       if (htab->glink_eh_frame == NULL
   3566 	  || !bfd_set_section_alignment (htab->glink_eh_frame, 2))
   3567 	return FALSE;
   3568     }
   3569 
   3570   flags = SEC_ALLOC | SEC_LINKER_CREATED;
   3571   htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
   3572   if (htab->elf.iplt == NULL
   3573       || !bfd_set_section_alignment (htab->elf.iplt, 3))
   3574     return FALSE;
   3575 
   3576   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
   3577 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   3578   htab->elf.irelplt
   3579     = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
   3580   if (htab->elf.irelplt == NULL
   3581       || !bfd_set_section_alignment (htab->elf.irelplt, 3))
   3582     return FALSE;
   3583 
   3584   /* Create branch lookup table for plt_branch stubs.  */
   3585   flags = (SEC_ALLOC | SEC_LOAD
   3586 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   3587   htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
   3588 						   flags);
   3589   if (htab->brlt == NULL
   3590       || !bfd_set_section_alignment (htab->brlt, 3))
   3591     return FALSE;
   3592 
   3593   /* Local plt entries, put in .branch_lt but a separate section for
   3594      convenience.  */
   3595   htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
   3596 						       flags);
   3597   if (htab->pltlocal == NULL
   3598       || !bfd_set_section_alignment (htab->pltlocal, 3))
   3599     return FALSE;
   3600 
   3601   if (!bfd_link_pic (info))
   3602     return TRUE;
   3603 
   3604   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
   3605 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   3606   htab->relbrlt
   3607     = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
   3608   if (htab->relbrlt == NULL
   3609       || !bfd_set_section_alignment (htab->relbrlt, 3))
   3610     return FALSE;
   3611 
   3612   htab->relpltlocal
   3613     = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
   3614   if (htab->relpltlocal == NULL
   3615       || !bfd_set_section_alignment (htab->relpltlocal, 3))
   3616     return FALSE;
   3617 
   3618   return TRUE;
   3619 }
   3620 
   3621 /* Satisfy the ELF linker by filling in some fields in our fake bfd.  */
   3622 
   3623 bfd_boolean
   3624 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
   3625 			 struct ppc64_elf_params *params)
   3626 {
   3627   struct ppc_link_hash_table *htab;
   3628 
   3629   elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
   3630 
   3631 /* Always hook our dynamic sections into the first bfd, which is the
   3632    linker created stub bfd.  This ensures that the GOT header is at
   3633    the start of the output TOC section.  */
   3634   htab = ppc_hash_table (info);
   3635   htab->elf.dynobj = params->stub_bfd;
   3636   htab->params = params;
   3637 
   3638   return create_linkage_sections (htab->elf.dynobj, info);
   3639 }
   3640 
   3641 /* Build a name for an entry in the stub hash table.  */
   3642 
   3643 static char *
   3644 ppc_stub_name (const asection *input_section,
   3645 	       const asection *sym_sec,
   3646 	       const struct ppc_link_hash_entry *h,
   3647 	       const Elf_Internal_Rela *rel)
   3648 {
   3649   char *stub_name;
   3650   ssize_t len;
   3651 
   3652   /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
   3653      offsets from a sym as a branch target?  In fact, we could
   3654      probably assume the addend is always zero.  */
   3655   BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
   3656 
   3657   if (h)
   3658     {
   3659       len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
   3660       stub_name = bfd_malloc (len);
   3661       if (stub_name == NULL)
   3662 	return stub_name;
   3663 
   3664       len = sprintf (stub_name, "%08x.%s+%x",
   3665 		     input_section->id & 0xffffffff,
   3666 		     h->elf.root.root.string,
   3667 		     (int) rel->r_addend & 0xffffffff);
   3668     }
   3669   else
   3670     {
   3671       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
   3672       stub_name = bfd_malloc (len);
   3673       if (stub_name == NULL)
   3674 	return stub_name;
   3675 
   3676       len = sprintf (stub_name, "%08x.%x:%x+%x",
   3677 		     input_section->id & 0xffffffff,
   3678 		     sym_sec->id & 0xffffffff,
   3679 		     (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
   3680 		     (int) rel->r_addend & 0xffffffff);
   3681     }
   3682   if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
   3683     stub_name[len - 2] = 0;
   3684   return stub_name;
   3685 }
   3686 
   3687 /* If mixing power10 with non-power10 code and --power10-stubs is not
   3688    specified (or is auto) then calls using @notoc relocations that
   3689    need a stub will utilize power10 instructions in the stub, and
   3690    calls without @notoc relocations will not use power10 instructions.
   3691    The two classes of stubs are stored in separate stub_hash_table
   3692    entries having the same key string.  The two entries will always be
   3693    adjacent on entry->root.next chain, even if hash table resizing
   3694    occurs.  This function selects the correct entry to use.  */
   3695 
   3696 static struct ppc_stub_hash_entry *
   3697 select_alt_stub (struct ppc_stub_hash_entry *entry, bfd_boolean notoc)
   3698 {
   3699   bfd_boolean have_notoc;
   3700 
   3701   have_notoc = (entry->stub_type == ppc_stub_plt_call_notoc
   3702 		|| entry->stub_type == ppc_stub_plt_branch_notoc
   3703 		|| entry->stub_type == ppc_stub_long_branch_notoc);
   3704 
   3705   if (have_notoc != notoc)
   3706     {
   3707       const char *stub_name = entry->root.string;
   3708 
   3709       entry = (struct ppc_stub_hash_entry *) entry->root.next;
   3710       if (entry != NULL
   3711 	  && entry->root.string != stub_name)
   3712 	entry = NULL;
   3713     }
   3714 
   3715   return entry;
   3716 }
   3717 
   3718 /* Look up an entry in the stub hash.  Stub entries are cached because
   3719    creating the stub name takes a bit of time.  */
   3720 
   3721 static struct ppc_stub_hash_entry *
   3722 ppc_get_stub_entry (const asection *input_section,
   3723 		    const asection *sym_sec,
   3724 		    struct ppc_link_hash_entry *h,
   3725 		    const Elf_Internal_Rela *rel,
   3726 		    struct ppc_link_hash_table *htab)
   3727 {
   3728   struct ppc_stub_hash_entry *stub_entry;
   3729   struct map_stub *group;
   3730 
   3731   /* If this input section is part of a group of sections sharing one
   3732      stub section, then use the id of the first section in the group.
   3733      Stub names need to include a section id, as there may well be
   3734      more than one stub used to reach say, printf, and we need to
   3735      distinguish between them.  */
   3736   group = htab->sec_info[input_section->id].u.group;
   3737   if (group == NULL)
   3738     return NULL;
   3739 
   3740   if (h != NULL && h->u.stub_cache != NULL
   3741       && h->u.stub_cache->h == h
   3742       && h->u.stub_cache->group == group)
   3743     {
   3744       stub_entry = h->u.stub_cache;
   3745     }
   3746   else
   3747     {
   3748       char *stub_name;
   3749 
   3750       stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
   3751       if (stub_name == NULL)
   3752 	return NULL;
   3753 
   3754       stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
   3755 					 stub_name, FALSE, FALSE);
   3756       if (h != NULL)
   3757 	h->u.stub_cache = stub_entry;
   3758 
   3759       free (stub_name);
   3760     }
   3761 
   3762   if (stub_entry != NULL && htab->params->power10_stubs == -1)
   3763     {
   3764       bfd_boolean notoc = ELF64_R_TYPE (rel->r_info) == R_PPC64_REL24_NOTOC;
   3765 
   3766       stub_entry = select_alt_stub (stub_entry, notoc);
   3767     }
   3768 
   3769   return stub_entry;
   3770 }
   3771 
   3772 /* Add a new stub entry to the stub hash.  Not all fields of the new
   3773    stub entry are initialised.  */
   3774 
   3775 static struct ppc_stub_hash_entry *
   3776 ppc_add_stub (const char *stub_name,
   3777 	      asection *section,
   3778 	      struct bfd_link_info *info)
   3779 {
   3780   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   3781   struct map_stub *group;
   3782   asection *link_sec;
   3783   asection *stub_sec;
   3784   struct ppc_stub_hash_entry *stub_entry;
   3785 
   3786   group = htab->sec_info[section->id].u.group;
   3787   link_sec = group->link_sec;
   3788   stub_sec = group->stub_sec;
   3789   if (stub_sec == NULL)
   3790     {
   3791       size_t namelen;
   3792       bfd_size_type len;
   3793       char *s_name;
   3794 
   3795       namelen = strlen (link_sec->name);
   3796       len = namelen + sizeof (STUB_SUFFIX);
   3797       s_name = bfd_alloc (htab->params->stub_bfd, len);
   3798       if (s_name == NULL)
   3799 	return NULL;
   3800 
   3801       memcpy (s_name, link_sec->name, namelen);
   3802       memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
   3803       stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
   3804       if (stub_sec == NULL)
   3805 	return NULL;
   3806       group->stub_sec = stub_sec;
   3807     }
   3808 
   3809   /* Enter this entry into the linker stub hash table.  */
   3810   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
   3811 				     TRUE, FALSE);
   3812   if (stub_entry == NULL)
   3813     {
   3814       /* xgettext:c-format */
   3815       _bfd_error_handler (_("%pB: cannot create stub entry %s"),
   3816 			  section->owner, stub_name);
   3817       return NULL;
   3818     }
   3819 
   3820   stub_entry->group = group;
   3821   stub_entry->stub_offset = 0;
   3822   return stub_entry;
   3823 }
   3824 
   3825 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
   3826    not already done.  */
   3827 
   3828 static bfd_boolean
   3829 create_got_section (bfd *abfd, struct bfd_link_info *info)
   3830 {
   3831   asection *got, *relgot;
   3832   flagword flags;
   3833   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   3834 
   3835   if (!is_ppc64_elf (abfd))
   3836     return FALSE;
   3837   if (htab == NULL)
   3838     return FALSE;
   3839 
   3840   if (!htab->elf.sgot
   3841       && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
   3842     return FALSE;
   3843 
   3844   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
   3845 	   | SEC_LINKER_CREATED);
   3846 
   3847   got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
   3848   if (!got
   3849       || !bfd_set_section_alignment (got, 3))
   3850     return FALSE;
   3851 
   3852   relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
   3853 					       flags | SEC_READONLY);
   3854   if (!relgot
   3855       || !bfd_set_section_alignment (relgot, 3))
   3856     return FALSE;
   3857 
   3858   ppc64_elf_tdata (abfd)->got = got;
   3859   ppc64_elf_tdata (abfd)->relgot = relgot;
   3860   return TRUE;
   3861 }
   3862 
   3863 /* Follow indirect and warning symbol links.  */
   3864 
   3865 static inline struct bfd_link_hash_entry *
   3866 follow_link (struct bfd_link_hash_entry *h)
   3867 {
   3868   while (h->type == bfd_link_hash_indirect
   3869 	 || h->type == bfd_link_hash_warning)
   3870     h = h->u.i.link;
   3871   return h;
   3872 }
   3873 
   3874 static inline struct elf_link_hash_entry *
   3875 elf_follow_link (struct elf_link_hash_entry *h)
   3876 {
   3877   return (struct elf_link_hash_entry *) follow_link (&h->root);
   3878 }
   3879 
   3880 static inline struct ppc_link_hash_entry *
   3881 ppc_follow_link (struct ppc_link_hash_entry *h)
   3882 {
   3883   return ppc_elf_hash_entry (elf_follow_link (&h->elf));
   3884 }
   3885 
   3886 /* Merge PLT info on FROM with that on TO.  */
   3887 
   3888 static void
   3889 move_plt_plist (struct ppc_link_hash_entry *from,
   3890 		struct ppc_link_hash_entry *to)
   3891 {
   3892   if (from->elf.plt.plist != NULL)
   3893     {
   3894       if (to->elf.plt.plist != NULL)
   3895 	{
   3896 	  struct plt_entry **entp;
   3897 	  struct plt_entry *ent;
   3898 
   3899 	  for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
   3900 	    {
   3901 	      struct plt_entry *dent;
   3902 
   3903 	      for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
   3904 		if (dent->addend == ent->addend)
   3905 		  {
   3906 		    dent->plt.refcount += ent->plt.refcount;
   3907 		    *entp = ent->next;
   3908 		    break;
   3909 		  }
   3910 	      if (dent == NULL)
   3911 		entp = &ent->next;
   3912 	    }
   3913 	  *entp = to->elf.plt.plist;
   3914 	}
   3915 
   3916       to->elf.plt.plist = from->elf.plt.plist;
   3917       from->elf.plt.plist = NULL;
   3918     }
   3919 }
   3920 
   3921 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
   3922 
   3923 static void
   3924 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
   3925 				struct elf_link_hash_entry *dir,
   3926 				struct elf_link_hash_entry *ind)
   3927 {
   3928   struct ppc_link_hash_entry *edir, *eind;
   3929 
   3930   edir = ppc_elf_hash_entry (dir);
   3931   eind = ppc_elf_hash_entry (ind);
   3932 
   3933   edir->is_func |= eind->is_func;
   3934   edir->is_func_descriptor |= eind->is_func_descriptor;
   3935   edir->tls_mask |= eind->tls_mask;
   3936   if (eind->oh != NULL)
   3937     edir->oh = ppc_follow_link (eind->oh);
   3938 
   3939   if (edir->elf.versioned != versioned_hidden)
   3940     edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
   3941   edir->elf.ref_regular |= eind->elf.ref_regular;
   3942   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
   3943   edir->elf.non_got_ref |= eind->elf.non_got_ref;
   3944   edir->elf.needs_plt |= eind->elf.needs_plt;
   3945   edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
   3946 
   3947   /* If we were called to copy over info for a weak sym, don't copy
   3948      dyn_relocs, plt/got info, or dynindx.  We used to copy dyn_relocs
   3949      in order to simplify readonly_dynrelocs and save a field in the
   3950      symbol hash entry, but that means dyn_relocs can't be used in any
   3951      tests about a specific symbol, or affect other symbol flags which
   3952      are then tested.  */
   3953   if (eind->elf.root.type != bfd_link_hash_indirect)
   3954     return;
   3955 
   3956   /* Copy over any dynamic relocs we may have on the indirect sym.  */
   3957   if (ind->dyn_relocs != NULL)
   3958     {
   3959       if (dir->dyn_relocs != NULL)
   3960 	{
   3961 	  struct elf_dyn_relocs **pp;
   3962 	  struct elf_dyn_relocs *p;
   3963 
   3964 	  /* Add reloc counts against the indirect sym to the direct sym
   3965 	     list.  Merge any entries against the same section.  */
   3966 	  for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
   3967 	    {
   3968 	      struct elf_dyn_relocs *q;
   3969 
   3970 	      for (q = dir->dyn_relocs; q != NULL; q = q->next)
   3971 		if (q->sec == p->sec)
   3972 		  {
   3973 		    q->pc_count += p->pc_count;
   3974 		    q->count += p->count;
   3975 		    *pp = p->next;
   3976 		    break;
   3977 		  }
   3978 	      if (q == NULL)
   3979 		pp = &p->next;
   3980 	    }
   3981 	  *pp = dir->dyn_relocs;
   3982 	}
   3983 
   3984       dir->dyn_relocs = ind->dyn_relocs;
   3985       ind->dyn_relocs = NULL;
   3986     }
   3987 
   3988   /* Copy over got entries that we may have already seen to the
   3989      symbol which just became indirect.  */
   3990   if (eind->elf.got.glist != NULL)
   3991     {
   3992       if (edir->elf.got.glist != NULL)
   3993 	{
   3994 	  struct got_entry **entp;
   3995 	  struct got_entry *ent;
   3996 
   3997 	  for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
   3998 	    {
   3999 	      struct got_entry *dent;
   4000 
   4001 	      for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
   4002 		if (dent->addend == ent->addend
   4003 		    && dent->owner == ent->owner
   4004 		    && dent->tls_type == ent->tls_type)
   4005 		  {
   4006 		    dent->got.refcount += ent->got.refcount;
   4007 		    *entp = ent->next;
   4008 		    break;
   4009 		  }
   4010 	      if (dent == NULL)
   4011 		entp = &ent->next;
   4012 	    }
   4013 	  *entp = edir->elf.got.glist;
   4014 	}
   4015 
   4016       edir->elf.got.glist = eind->elf.got.glist;
   4017       eind->elf.got.glist = NULL;
   4018     }
   4019 
   4020   /* And plt entries.  */
   4021   move_plt_plist (eind, edir);
   4022 
   4023   if (eind->elf.dynindx != -1)
   4024     {
   4025       if (edir->elf.dynindx != -1)
   4026 	_bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
   4027 				edir->elf.dynstr_index);
   4028       edir->elf.dynindx = eind->elf.dynindx;
   4029       edir->elf.dynstr_index = eind->elf.dynstr_index;
   4030       eind->elf.dynindx = -1;
   4031       eind->elf.dynstr_index = 0;
   4032     }
   4033 }
   4034 
   4035 /* Find the function descriptor hash entry from the given function code
   4036    hash entry FH.  Link the entries via their OH fields.  */
   4037 
   4038 static struct ppc_link_hash_entry *
   4039 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
   4040 {
   4041   struct ppc_link_hash_entry *fdh = fh->oh;
   4042 
   4043   if (fdh == NULL)
   4044     {
   4045       const char *fd_name = fh->elf.root.root.string + 1;
   4046 
   4047       fdh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, fd_name,
   4048 						      FALSE, FALSE, FALSE));
   4049       if (fdh == NULL)
   4050 	return fdh;
   4051 
   4052       fdh->is_func_descriptor = 1;
   4053       fdh->oh = fh;
   4054       fh->is_func = 1;
   4055       fh->oh = fdh;
   4056     }
   4057 
   4058   fdh = ppc_follow_link (fdh);
   4059   fdh->is_func_descriptor = 1;
   4060   fdh->oh = fh;
   4061   return fdh;
   4062 }
   4063 
   4064 /* Make a fake function descriptor sym for the undefined code sym FH.  */
   4065 
   4066 static struct ppc_link_hash_entry *
   4067 make_fdh (struct bfd_link_info *info,
   4068 	  struct ppc_link_hash_entry *fh)
   4069 {
   4070   bfd *abfd = fh->elf.root.u.undef.abfd;
   4071   struct bfd_link_hash_entry *bh = NULL;
   4072   struct ppc_link_hash_entry *fdh;
   4073   flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
   4074 		    ? BSF_WEAK
   4075 		    : BSF_GLOBAL);
   4076 
   4077   if (!_bfd_generic_link_add_one_symbol (info, abfd,
   4078 					 fh->elf.root.root.string + 1,
   4079 					 flags, bfd_und_section_ptr, 0,
   4080 					 NULL, FALSE, FALSE, &bh))
   4081     return NULL;
   4082 
   4083   fdh = (struct ppc_link_hash_entry *) bh;
   4084   fdh->elf.non_elf = 0;
   4085   fdh->fake = 1;
   4086   fdh->is_func_descriptor = 1;
   4087   fdh->oh = fh;
   4088   fh->is_func = 1;
   4089   fh->oh = fdh;
   4090   return fdh;
   4091 }
   4092 
   4093 /* Fix function descriptor symbols defined in .opd sections to be
   4094    function type.  */
   4095 
   4096 static bfd_boolean
   4097 ppc64_elf_add_symbol_hook (bfd *ibfd,
   4098 			   struct bfd_link_info *info,
   4099 			   Elf_Internal_Sym *isym,
   4100 			   const char **name,
   4101 			   flagword *flags ATTRIBUTE_UNUSED,
   4102 			   asection **sec,
   4103 			   bfd_vma *value)
   4104 {
   4105   if (*sec != NULL
   4106       && strcmp ((*sec)->name, ".opd") == 0)
   4107     {
   4108       asection *code_sec;
   4109 
   4110       if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
   4111 	    || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
   4112 	isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
   4113 
   4114       /* If the symbol is a function defined in .opd, and the function
   4115 	 code is in a discarded group, let it appear to be undefined.  */
   4116       if (!bfd_link_relocatable (info)
   4117 	  && (*sec)->reloc_count != 0
   4118 	  && opd_entry_value (*sec, *value, &code_sec, NULL,
   4119 			      FALSE) != (bfd_vma) -1
   4120 	  && discarded_section (code_sec))
   4121 	{
   4122 	  *sec = bfd_und_section_ptr;
   4123 	  isym->st_shndx = SHN_UNDEF;
   4124 	}
   4125     }
   4126   else if (*sec != NULL
   4127 	   && strcmp ((*sec)->name, ".toc") == 0
   4128 	   && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
   4129     {
   4130       struct ppc_link_hash_table *htab = ppc_hash_table (info);
   4131       if (htab != NULL)
   4132 	htab->params->object_in_toc = 1;
   4133     }
   4134 
   4135   if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
   4136     {
   4137       if (abiversion (ibfd) == 0)
   4138 	set_abiversion (ibfd, 2);
   4139       else if (abiversion (ibfd) == 1)
   4140 	{
   4141 	  _bfd_error_handler (_("symbol '%s' has invalid st_other"
   4142 				" for ABI version 1"), *name);
   4143 	  bfd_set_error (bfd_error_bad_value);
   4144 	  return FALSE;
   4145 	}
   4146     }
   4147 
   4148   return TRUE;
   4149 }
   4150 
   4151 /* Merge non-visibility st_other attributes: local entry point.  */
   4152 
   4153 static void
   4154 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
   4155 				  const Elf_Internal_Sym *isym,
   4156 				  bfd_boolean definition,
   4157 				  bfd_boolean dynamic)
   4158 {
   4159   if (definition && (!dynamic || !h->def_regular))
   4160     h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
   4161 		| ELF_ST_VISIBILITY (h->other));
   4162 }
   4163 
   4164 /* Hook called on merging a symbol.  We use this to clear "fake" since
   4165    we now have a real symbol.  */
   4166 
   4167 static bfd_boolean
   4168 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
   4169 			const Elf_Internal_Sym *isym,
   4170 			asection **psec ATTRIBUTE_UNUSED,
   4171 			bfd_boolean newdef ATTRIBUTE_UNUSED,
   4172 			bfd_boolean olddef ATTRIBUTE_UNUSED,
   4173 			bfd *oldbfd ATTRIBUTE_UNUSED,
   4174 			const asection *oldsec ATTRIBUTE_UNUSED)
   4175 {
   4176   ppc_elf_hash_entry (h)->fake = 0;
   4177   if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
   4178     ppc_elf_hash_entry (h)->non_zero_localentry = 1;
   4179   return TRUE;
   4180 }
   4181 
   4182 /* This function makes an old ABI object reference to ".bar" cause the
   4183    inclusion of a new ABI object archive that defines "bar".
   4184    NAME is a symbol defined in an archive.  Return a symbol in the hash
   4185    table that might be satisfied by the archive symbols.  */
   4186 
   4187 static struct elf_link_hash_entry *
   4188 ppc64_elf_archive_symbol_lookup (bfd *abfd,
   4189 				 struct bfd_link_info *info,
   4190 				 const char *name)
   4191 {
   4192   struct elf_link_hash_entry *h;
   4193   char *dot_name;
   4194   size_t len;
   4195 
   4196   h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
   4197   if (h != NULL
   4198       /* Don't return this sym if it is a fake function descriptor
   4199 	 created by add_symbol_adjust.  */
   4200       && !ppc_elf_hash_entry (h)->fake)
   4201     return h;
   4202 
   4203   if (name[0] == '.')
   4204     return h;
   4205 
   4206   len = strlen (name);
   4207   dot_name = bfd_alloc (abfd, len + 2);
   4208   if (dot_name == NULL)
   4209     return (struct elf_link_hash_entry *) -1;
   4210   dot_name[0] = '.';
   4211   memcpy (dot_name + 1, name, len + 1);
   4212   h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
   4213   bfd_release (abfd, dot_name);
   4214   if (h != NULL)
   4215     return h;
   4216 
   4217   if (strcmp (name, "__tls_get_addr_opt") == 0)
   4218     h = _bfd_elf_archive_symbol_lookup (abfd, info, "__tls_get_addr_desc");
   4219   return h;
   4220 }
   4221 
   4222 /* This function satisfies all old ABI object references to ".bar" if a
   4223    new ABI object defines "bar".  Well, at least, undefined dot symbols
   4224    are made weak.  This stops later archive searches from including an
   4225    object if we already have a function descriptor definition.  It also
   4226    prevents the linker complaining about undefined symbols.
   4227    We also check and correct mismatched symbol visibility here.  The
   4228    most restrictive visibility of the function descriptor and the
   4229    function entry symbol is used.  */
   4230 
   4231 static bfd_boolean
   4232 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
   4233 {
   4234   struct ppc_link_hash_table *htab;
   4235   struct ppc_link_hash_entry *fdh;
   4236 
   4237   if (eh->elf.root.type == bfd_link_hash_warning)
   4238     eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
   4239 
   4240   if (eh->elf.root.type == bfd_link_hash_indirect)
   4241     return TRUE;
   4242 
   4243   if (eh->elf.root.root.string[0] != '.')
   4244     abort ();
   4245 
   4246   htab = ppc_hash_table (info);
   4247   if (htab == NULL)
   4248     return FALSE;
   4249 
   4250   fdh = lookup_fdh (eh, htab);
   4251   if (fdh == NULL
   4252       && !bfd_link_relocatable (info)
   4253       && (eh->elf.root.type == bfd_link_hash_undefined
   4254 	  || eh->elf.root.type == bfd_link_hash_undefweak)
   4255       && eh->elf.ref_regular)
   4256     {
   4257       /* Make an undefined function descriptor sym, in order to
   4258 	 pull in an --as-needed shared lib.  Archives are handled
   4259 	 elsewhere.  */
   4260       fdh = make_fdh (info, eh);
   4261       if (fdh == NULL)
   4262 	return FALSE;
   4263     }
   4264 
   4265   if (fdh != NULL)
   4266     {
   4267       unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
   4268       unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
   4269 
   4270       /* Make both descriptor and entry symbol have the most
   4271 	 constraining visibility of either symbol.  */
   4272       if (entry_vis < descr_vis)
   4273 	fdh->elf.other += entry_vis - descr_vis;
   4274       else if (entry_vis > descr_vis)
   4275 	eh->elf.other += descr_vis - entry_vis;
   4276 
   4277       /* Propagate reference flags from entry symbol to function
   4278 	 descriptor symbol.  */
   4279       fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
   4280       fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
   4281       fdh->elf.ref_regular |= eh->elf.ref_regular;
   4282       fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
   4283 
   4284       if (!fdh->elf.forced_local
   4285 	  && fdh->elf.dynindx == -1
   4286 	  && fdh->elf.versioned != versioned_hidden
   4287 	  && (bfd_link_dll (info)
   4288 	      || fdh->elf.def_dynamic
   4289 	      || fdh->elf.ref_dynamic)
   4290 	  && (eh->elf.ref_regular
   4291 	      || eh->elf.def_regular))
   4292 	{
   4293 	  if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
   4294 	    return FALSE;
   4295 	}
   4296     }
   4297 
   4298   return TRUE;
   4299 }
   4300 
   4301 /* Set up opd section info and abiversion for IBFD, and process list
   4302    of dot-symbols we made in link_hash_newfunc.  */
   4303 
   4304 static bfd_boolean
   4305 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
   4306 {
   4307   struct ppc_link_hash_table *htab;
   4308   struct ppc_link_hash_entry **p, *eh;
   4309   asection *opd = bfd_get_section_by_name (ibfd, ".opd");
   4310 
   4311   if (opd != NULL && opd->size != 0)
   4312     {
   4313       BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
   4314       ppc64_elf_section_data (opd)->sec_type = sec_opd;
   4315 
   4316       if (abiversion (ibfd) == 0)
   4317 	set_abiversion (ibfd, 1);
   4318       else if (abiversion (ibfd) >= 2)
   4319 	{
   4320 	  /* xgettext:c-format */
   4321 	  _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
   4322 			      ibfd, abiversion (ibfd));
   4323 	  bfd_set_error (bfd_error_bad_value);
   4324 	  return FALSE;
   4325 	}
   4326     }
   4327 
   4328   if (is_ppc64_elf (info->output_bfd))
   4329     {
   4330       /* For input files without an explicit abiversion in e_flags
   4331 	 we should have flagged any with symbol st_other bits set
   4332 	 as ELFv1 and above flagged those with .opd as ELFv2.
   4333 	 Set the output abiversion if not yet set, and for any input
   4334 	 still ambiguous, take its abiversion from the output.
   4335 	 Differences in ABI are reported later.  */
   4336       if (abiversion (info->output_bfd) == 0)
   4337 	set_abiversion (info->output_bfd, abiversion (ibfd));
   4338       else if (abiversion (ibfd) == 0)
   4339 	set_abiversion (ibfd, abiversion (info->output_bfd));
   4340     }
   4341 
   4342   htab = ppc_hash_table (info);
   4343   if (htab == NULL)
   4344     return TRUE;
   4345 
   4346   if (opd != NULL && opd->size != 0
   4347       && (ibfd->flags & DYNAMIC) == 0
   4348       && (opd->flags & SEC_RELOC) != 0
   4349       && opd->reloc_count != 0
   4350       && !bfd_is_abs_section (opd->output_section)
   4351       && info->gc_sections)
   4352     {
   4353       /* Garbage collection needs some extra help with .opd sections.
   4354 	 We don't want to necessarily keep everything referenced by
   4355 	 relocs in .opd, as that would keep all functions.  Instead,
   4356 	 if we reference an .opd symbol (a function descriptor), we
   4357 	 want to keep the function code symbol's section.  This is
   4358 	 easy for global symbols, but for local syms we need to keep
   4359 	 information about the associated function section.  */
   4360       bfd_size_type amt;
   4361       asection **opd_sym_map;
   4362       Elf_Internal_Shdr *symtab_hdr;
   4363       Elf_Internal_Rela *relocs, *rel_end, *rel;
   4364 
   4365       amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
   4366       opd_sym_map = bfd_zalloc (ibfd, amt);
   4367       if (opd_sym_map == NULL)
   4368 	return FALSE;
   4369       ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
   4370       relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
   4371 					  info->keep_memory);
   4372       if (relocs == NULL)
   4373 	return FALSE;
   4374       symtab_hdr = &elf_symtab_hdr (ibfd);
   4375       rel_end = relocs + opd->reloc_count - 1;
   4376       for (rel = relocs; rel < rel_end; rel++)
   4377 	{
   4378 	  enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
   4379 	  unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
   4380 
   4381 	  if (r_type == R_PPC64_ADDR64
   4382 	      && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
   4383 	      && r_symndx < symtab_hdr->sh_info)
   4384 	    {
   4385 	      Elf_Internal_Sym *isym;
   4386 	      asection *s;
   4387 
   4388 	      isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, ibfd,
   4389 					    r_symndx);
   4390 	      if (isym == NULL)
   4391 		{
   4392 		  if (elf_section_data (opd)->relocs != relocs)
   4393 		    free (relocs);
   4394 		  return FALSE;
   4395 		}
   4396 
   4397 	      s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
   4398 	      if (s != NULL && s != opd)
   4399 		opd_sym_map[OPD_NDX (rel->r_offset)] = s;
   4400 	    }
   4401 	}
   4402       if (elf_section_data (opd)->relocs != relocs)
   4403 	free (relocs);
   4404     }
   4405 
   4406   p = &htab->dot_syms;
   4407   while ((eh = *p) != NULL)
   4408     {
   4409       *p = NULL;
   4410       if (&eh->elf == htab->elf.hgot)
   4411 	;
   4412       else if (htab->elf.hgot == NULL
   4413 	       && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
   4414 	htab->elf.hgot = &eh->elf;
   4415       else if (abiversion (ibfd) <= 1)
   4416 	{
   4417 	  htab->need_func_desc_adj = 1;
   4418 	  if (!add_symbol_adjust (eh, info))
   4419 	    return FALSE;
   4420 	}
   4421       p = &eh->u.next_dot_sym;
   4422     }
   4423   return TRUE;
   4424 }
   4425 
   4426 /* Undo hash table changes when an --as-needed input file is determined
   4427    not to be needed.  */
   4428 
   4429 static bfd_boolean
   4430 ppc64_elf_notice_as_needed (bfd *ibfd,
   4431 			    struct bfd_link_info *info,
   4432 			    enum notice_asneeded_action act)
   4433 {
   4434   if (act == notice_not_needed)
   4435     {
   4436       struct ppc_link_hash_table *htab = ppc_hash_table (info);
   4437 
   4438       if (htab == NULL)
   4439 	return FALSE;
   4440 
   4441       htab->dot_syms = NULL;
   4442     }
   4443   return _bfd_elf_notice_as_needed (ibfd, info, act);
   4444 }
   4445 
   4446 /* If --just-symbols against a final linked binary, then assume we need
   4447    toc adjusting stubs when calling functions defined there.  */
   4448 
   4449 static void
   4450 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
   4451 {
   4452   if ((sec->flags & SEC_CODE) != 0
   4453       && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
   4454       && is_ppc64_elf (sec->owner))
   4455     {
   4456       if (abiversion (sec->owner) >= 2
   4457 	  || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
   4458 	sec->has_toc_reloc = 1;
   4459     }
   4460   _bfd_elf_link_just_syms (sec, info);
   4461 }
   4462 
   4463 static struct plt_entry **
   4464 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
   4465 		       unsigned long r_symndx, bfd_vma r_addend, int tls_type)
   4466 {
   4467   struct got_entry **local_got_ents = elf_local_got_ents (abfd);
   4468   struct plt_entry **local_plt;
   4469   unsigned char *local_got_tls_masks;
   4470 
   4471   if (local_got_ents == NULL)
   4472     {
   4473       bfd_size_type size = symtab_hdr->sh_info;
   4474 
   4475       size *= (sizeof (*local_got_ents)
   4476 	       + sizeof (*local_plt)
   4477 	       + sizeof (*local_got_tls_masks));
   4478       local_got_ents = bfd_zalloc (abfd, size);
   4479       if (local_got_ents == NULL)
   4480 	return NULL;
   4481       elf_local_got_ents (abfd) = local_got_ents;
   4482     }
   4483 
   4484   if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
   4485     {
   4486       struct got_entry *ent;
   4487 
   4488       for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
   4489 	if (ent->addend == r_addend
   4490 	    && ent->owner == abfd
   4491 	    && ent->tls_type == tls_type)
   4492 	  break;
   4493       if (ent == NULL)
   4494 	{
   4495 	  size_t amt = sizeof (*ent);
   4496 	  ent = bfd_alloc (abfd, amt);
   4497 	  if (ent == NULL)
   4498 	    return FALSE;
   4499 	  ent->next = local_got_ents[r_symndx];
   4500 	  ent->addend = r_addend;
   4501 	  ent->owner = abfd;
   4502 	  ent->tls_type = tls_type;
   4503 	  ent->is_indirect = FALSE;
   4504 	  ent->got.refcount = 0;
   4505 	  local_got_ents[r_symndx] = ent;
   4506 	}
   4507       ent->got.refcount += 1;
   4508     }
   4509 
   4510   local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
   4511   local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
   4512   local_got_tls_masks[r_symndx] |= tls_type & 0xff;
   4513 
   4514   return local_plt + r_symndx;
   4515 }
   4516 
   4517 static bfd_boolean
   4518 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
   4519 {
   4520   struct plt_entry *ent;
   4521 
   4522   for (ent = *plist; ent != NULL; ent = ent->next)
   4523     if (ent->addend == addend)
   4524       break;
   4525   if (ent == NULL)
   4526     {
   4527       size_t amt = sizeof (*ent);
   4528       ent = bfd_alloc (abfd, amt);
   4529       if (ent == NULL)
   4530 	return FALSE;
   4531       ent->next = *plist;
   4532       ent->addend = addend;
   4533       ent->plt.refcount = 0;
   4534       *plist = ent;
   4535     }
   4536   ent->plt.refcount += 1;
   4537   return TRUE;
   4538 }
   4539 
   4540 static bfd_boolean
   4541 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
   4542 {
   4543   return (r_type == R_PPC64_REL24
   4544 	  || r_type == R_PPC64_REL24_NOTOC
   4545 	  || r_type == R_PPC64_REL14
   4546 	  || r_type == R_PPC64_REL14_BRTAKEN
   4547 	  || r_type == R_PPC64_REL14_BRNTAKEN
   4548 	  || r_type == R_PPC64_ADDR24
   4549 	  || r_type == R_PPC64_ADDR14
   4550 	  || r_type == R_PPC64_ADDR14_BRTAKEN
   4551 	  || r_type == R_PPC64_ADDR14_BRNTAKEN
   4552 	  || r_type == R_PPC64_PLTCALL
   4553 	  || r_type == R_PPC64_PLTCALL_NOTOC);
   4554 }
   4555 
   4556 /* Relocs on inline plt call sequence insns prior to the call.  */
   4557 
   4558 static bfd_boolean
   4559 is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
   4560 {
   4561   return (r_type == R_PPC64_PLT16_HA
   4562 	  || r_type == R_PPC64_PLT16_HI
   4563 	  || r_type == R_PPC64_PLT16_LO
   4564 	  || r_type == R_PPC64_PLT16_LO_DS
   4565 	  || r_type == R_PPC64_PLT_PCREL34
   4566 	  || r_type == R_PPC64_PLT_PCREL34_NOTOC
   4567 	  || r_type == R_PPC64_PLTSEQ
   4568 	  || r_type == R_PPC64_PLTSEQ_NOTOC);
   4569 }
   4570 
   4571 /* Look through the relocs for a section during the first phase, and
   4572    calculate needed space in the global offset table, procedure
   4573    linkage table, and dynamic reloc sections.  */
   4574 
   4575 static bfd_boolean
   4576 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
   4577 			asection *sec, const Elf_Internal_Rela *relocs)
   4578 {
   4579   struct ppc_link_hash_table *htab;
   4580   Elf_Internal_Shdr *symtab_hdr;
   4581   struct elf_link_hash_entry **sym_hashes;
   4582   const Elf_Internal_Rela *rel;
   4583   const Elf_Internal_Rela *rel_end;
   4584   asection *sreloc;
   4585   struct elf_link_hash_entry *tga, *dottga;
   4586   bfd_boolean is_opd;
   4587 
   4588   if (bfd_link_relocatable (info))
   4589     return TRUE;
   4590 
   4591   BFD_ASSERT (is_ppc64_elf (abfd));
   4592 
   4593   htab = ppc_hash_table (info);
   4594   if (htab == NULL)
   4595     return FALSE;
   4596 
   4597   tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
   4598 			      FALSE, FALSE, TRUE);
   4599   dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
   4600 				 FALSE, FALSE, TRUE);
   4601   symtab_hdr = &elf_symtab_hdr (abfd);
   4602   sym_hashes = elf_sym_hashes (abfd);
   4603   sreloc = NULL;
   4604   is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
   4605   rel_end = relocs + sec->reloc_count;
   4606   for (rel = relocs; rel < rel_end; rel++)
   4607     {
   4608       unsigned long r_symndx;
   4609       struct elf_link_hash_entry *h;
   4610       enum elf_ppc64_reloc_type r_type;
   4611       int tls_type;
   4612       struct _ppc64_elf_section_data *ppc64_sec;
   4613       struct plt_entry **ifunc, **plt_list;
   4614 
   4615       r_symndx = ELF64_R_SYM (rel->r_info);
   4616       if (r_symndx < symtab_hdr->sh_info)
   4617 	h = NULL;
   4618       else
   4619 	{
   4620 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   4621 	  h = elf_follow_link (h);
   4622 
   4623 	  if (h == htab->elf.hgot)
   4624 	    sec->has_toc_reloc = 1;
   4625 	}
   4626 
   4627       r_type = ELF64_R_TYPE (rel->r_info);
   4628       switch (r_type)
   4629 	{
   4630 	case R_PPC64_D34:
   4631 	case R_PPC64_D34_LO:
   4632 	case R_PPC64_D34_HI30:
   4633 	case R_PPC64_D34_HA30:
   4634 	case R_PPC64_D28:
   4635 	case R_PPC64_TPREL34:
   4636 	case R_PPC64_DTPREL34:
   4637 	case R_PPC64_PCREL34:
   4638 	case R_PPC64_GOT_PCREL34:
   4639 	case R_PPC64_GOT_TLSGD_PCREL34:
   4640 	case R_PPC64_GOT_TLSLD_PCREL34:
   4641 	case R_PPC64_GOT_TPREL_PCREL34:
   4642 	case R_PPC64_GOT_DTPREL_PCREL34:
   4643 	case R_PPC64_PLT_PCREL34:
   4644 	case R_PPC64_PLT_PCREL34_NOTOC:
   4645 	case R_PPC64_PCREL28:
   4646 	  htab->has_power10_relocs = 1;
   4647 	  break;
   4648 	default:
   4649 	  break;
   4650 	}
   4651 
   4652       switch (r_type)
   4653 	{
   4654 	case R_PPC64_PLT16_HA:
   4655 	case R_PPC64_GOT_TLSLD16_HA:
   4656 	case R_PPC64_GOT_TLSGD16_HA:
   4657 	case R_PPC64_GOT_TPREL16_HA:
   4658 	case R_PPC64_GOT_DTPREL16_HA:
   4659 	case R_PPC64_GOT16_HA:
   4660 	case R_PPC64_TOC16_HA:
   4661 	case R_PPC64_PLT16_LO:
   4662 	case R_PPC64_PLT16_LO_DS:
   4663 	case R_PPC64_GOT_TLSLD16_LO:
   4664 	case R_PPC64_GOT_TLSGD16_LO:
   4665 	case R_PPC64_GOT_TPREL16_LO_DS:
   4666 	case R_PPC64_GOT_DTPREL16_LO_DS:
   4667 	case R_PPC64_GOT16_LO:
   4668 	case R_PPC64_GOT16_LO_DS:
   4669 	case R_PPC64_TOC16_LO:
   4670 	case R_PPC64_TOC16_LO_DS:
   4671 	case R_PPC64_GOT_PCREL34:
   4672 	  ppc64_elf_tdata (abfd)->has_optrel = 1;
   4673 	  ppc64_elf_section_data (sec)->has_optrel = 1;
   4674 	  break;
   4675 	default:
   4676 	  break;
   4677 	}
   4678 
   4679       ifunc = NULL;
   4680       if (h != NULL)
   4681 	{
   4682 	  if (h->type == STT_GNU_IFUNC)
   4683 	    {
   4684 	      h->needs_plt = 1;
   4685 	      ifunc = &h->plt.plist;
   4686 	    }
   4687 	}
   4688       else
   4689 	{
   4690 	  Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
   4691 							  abfd, r_symndx);
   4692 	  if (isym == NULL)
   4693 	    return FALSE;
   4694 
   4695 	  if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
   4696 	    {
   4697 	      ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
   4698 					     rel->r_addend,
   4699 					     NON_GOT | PLT_IFUNC);
   4700 	      if (ifunc == NULL)
   4701 		return FALSE;
   4702 	    }
   4703 	}
   4704 
   4705       tls_type = 0;
   4706       switch (r_type)
   4707 	{
   4708 	case R_PPC64_TLSGD:
   4709 	case R_PPC64_TLSLD:
   4710 	  /* These special tls relocs tie a call to __tls_get_addr with
   4711 	     its parameter symbol.  */
   4712 	  if (h != NULL)
   4713 	    ppc_elf_hash_entry (h)->tls_mask |= TLS_TLS | TLS_MARK;
   4714 	  else
   4715 	    if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
   4716 					rel->r_addend,
   4717 					NON_GOT | TLS_TLS | TLS_MARK))
   4718 	      return FALSE;
   4719 	  sec->has_tls_reloc = 1;
   4720 	  break;
   4721 
   4722 	case R_PPC64_GOT_TLSLD16:
   4723 	case R_PPC64_GOT_TLSLD16_LO:
   4724 	case R_PPC64_GOT_TLSLD16_HI:
   4725 	case R_PPC64_GOT_TLSLD16_HA:
   4726 	case R_PPC64_GOT_TLSLD_PCREL34:
   4727 	  tls_type = TLS_TLS | TLS_LD;
   4728 	  goto dogottls;
   4729 
   4730 	case R_PPC64_GOT_TLSGD16:
   4731 	case R_PPC64_GOT_TLSGD16_LO:
   4732 	case R_PPC64_GOT_TLSGD16_HI:
   4733 	case R_PPC64_GOT_TLSGD16_HA:
   4734 	case R_PPC64_GOT_TLSGD_PCREL34:
   4735 	  tls_type = TLS_TLS | TLS_GD;
   4736 	  goto dogottls;
   4737 
   4738 	case R_PPC64_GOT_TPREL16_DS:
   4739 	case R_PPC64_GOT_TPREL16_LO_DS:
   4740 	case R_PPC64_GOT_TPREL16_HI:
   4741 	case R_PPC64_GOT_TPREL16_HA:
   4742 	case R_PPC64_GOT_TPREL_PCREL34:
   4743 	  if (bfd_link_dll (info))
   4744 	    info->flags |= DF_STATIC_TLS;
   4745 	  tls_type = TLS_TLS | TLS_TPREL;
   4746 	  goto dogottls;
   4747 
   4748 	case R_PPC64_GOT_DTPREL16_DS:
   4749 	case R_PPC64_GOT_DTPREL16_LO_DS:
   4750 	case R_PPC64_GOT_DTPREL16_HI:
   4751 	case R_PPC64_GOT_DTPREL16_HA:
   4752 	case R_PPC64_GOT_DTPREL_PCREL34:
   4753 	  tls_type = TLS_TLS | TLS_DTPREL;
   4754 	dogottls:
   4755 	  sec->has_tls_reloc = 1;
   4756 	  goto dogot;
   4757 
   4758 	case R_PPC64_GOT16:
   4759 	case R_PPC64_GOT16_LO:
   4760 	case R_PPC64_GOT16_HI:
   4761 	case R_PPC64_GOT16_HA:
   4762 	case R_PPC64_GOT16_DS:
   4763 	case R_PPC64_GOT16_LO_DS:
   4764 	case R_PPC64_GOT_PCREL34:
   4765 	dogot:
   4766 	  /* This symbol requires a global offset table entry.  */
   4767 	  sec->has_toc_reloc = 1;
   4768 	  if (r_type == R_PPC64_GOT_TLSLD16
   4769 	      || r_type == R_PPC64_GOT_TLSGD16
   4770 	      || r_type == R_PPC64_GOT_TPREL16_DS
   4771 	      || r_type == R_PPC64_GOT_DTPREL16_DS
   4772 	      || r_type == R_PPC64_GOT16
   4773 	      || r_type == R_PPC64_GOT16_DS)
   4774 	    {
   4775 	      htab->do_multi_toc = 1;
   4776 	      ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
   4777 	    }
   4778 
   4779 	  if (ppc64_elf_tdata (abfd)->got == NULL
   4780 	      && !create_got_section (abfd, info))
   4781 	    return FALSE;
   4782 
   4783 	  if (h != NULL)
   4784 	    {
   4785 	      struct ppc_link_hash_entry *eh;
   4786 	      struct got_entry *ent;
   4787 
   4788 	      eh = ppc_elf_hash_entry (h);
   4789 	      for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
   4790 		if (ent->addend == rel->r_addend
   4791 		    && ent->owner == abfd
   4792 		    && ent->tls_type == tls_type)
   4793 		  break;
   4794 	      if (ent == NULL)
   4795 		{
   4796 		  size_t amt = sizeof (*ent);
   4797 		  ent = bfd_alloc (abfd, amt);
   4798 		  if (ent == NULL)
   4799 		    return FALSE;
   4800 		  ent->next = eh->elf.got.glist;
   4801 		  ent->addend = rel->r_addend;
   4802 		  ent->owner = abfd;
   4803 		  ent->tls_type = tls_type;
   4804 		  ent->is_indirect = FALSE;
   4805 		  ent->got.refcount = 0;
   4806 		  eh->elf.got.glist = ent;
   4807 		}
   4808 	      ent->got.refcount += 1;
   4809 	      eh->tls_mask |= tls_type;
   4810 	    }
   4811 	  else
   4812 	    /* This is a global offset table entry for a local symbol.  */
   4813 	    if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
   4814 					rel->r_addend, tls_type))
   4815 	      return FALSE;
   4816 	  break;
   4817 
   4818 	case R_PPC64_PLT16_HA:
   4819 	case R_PPC64_PLT16_HI:
   4820 	case R_PPC64_PLT16_LO:
   4821 	case R_PPC64_PLT16_LO_DS:
   4822 	case R_PPC64_PLT_PCREL34:
   4823 	case R_PPC64_PLT_PCREL34_NOTOC:
   4824 	case R_PPC64_PLT32:
   4825 	case R_PPC64_PLT64:
   4826 	  /* This symbol requires a procedure linkage table entry.  */
   4827 	  plt_list = ifunc;
   4828 	  if (h != NULL)
   4829 	    {
   4830 	      h->needs_plt = 1;
   4831 	      if (h->root.root.string[0] == '.'
   4832 		  && h->root.root.string[1] != '\0')
   4833 		ppc_elf_hash_entry (h)->is_func = 1;
   4834 	      ppc_elf_hash_entry (h)->tls_mask |= PLT_KEEP;
   4835 	      plt_list = &h->plt.plist;
   4836 	    }
   4837 	  if (plt_list == NULL)
   4838 	    plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
   4839 					      rel->r_addend,
   4840 					      NON_GOT | PLT_KEEP);
   4841 	  if (!update_plt_info (abfd, plt_list, rel->r_addend))
   4842 	    return FALSE;
   4843 	  break;
   4844 
   4845 	  /* The following relocations don't need to propagate the
   4846 	     relocation if linking a shared object since they are
   4847 	     section relative.  */
   4848 	case R_PPC64_SECTOFF:
   4849 	case R_PPC64_SECTOFF_LO:
   4850 	case R_PPC64_SECTOFF_HI:
   4851 	case R_PPC64_SECTOFF_HA:
   4852 	case R_PPC64_SECTOFF_DS:
   4853 	case R_PPC64_SECTOFF_LO_DS:
   4854 	case R_PPC64_DTPREL16:
   4855 	case R_PPC64_DTPREL16_LO:
   4856 	case R_PPC64_DTPREL16_HI:
   4857 	case R_PPC64_DTPREL16_HA:
   4858 	case R_PPC64_DTPREL16_DS:
   4859 	case R_PPC64_DTPREL16_LO_DS:
   4860 	case R_PPC64_DTPREL16_HIGH:
   4861 	case R_PPC64_DTPREL16_HIGHA:
   4862 	case R_PPC64_DTPREL16_HIGHER:
   4863 	case R_PPC64_DTPREL16_HIGHERA:
   4864 	case R_PPC64_DTPREL16_HIGHEST:
   4865 	case R_PPC64_DTPREL16_HIGHESTA:
   4866 	  break;
   4867 
   4868 	  /* Nor do these.  */
   4869 	case R_PPC64_REL16:
   4870 	case R_PPC64_REL16_LO:
   4871 	case R_PPC64_REL16_HI:
   4872 	case R_PPC64_REL16_HA:
   4873 	case R_PPC64_REL16_HIGH:
   4874 	case R_PPC64_REL16_HIGHA:
   4875 	case R_PPC64_REL16_HIGHER:
   4876 	case R_PPC64_REL16_HIGHERA:
   4877 	case R_PPC64_REL16_HIGHEST:
   4878 	case R_PPC64_REL16_HIGHESTA:
   4879 	case R_PPC64_REL16_HIGHER34:
   4880 	case R_PPC64_REL16_HIGHERA34:
   4881 	case R_PPC64_REL16_HIGHEST34:
   4882 	case R_PPC64_REL16_HIGHESTA34:
   4883 	case R_PPC64_REL16DX_HA:
   4884 	  break;
   4885 
   4886 	  /* Not supported as a dynamic relocation.  */
   4887 	case R_PPC64_ADDR64_LOCAL:
   4888 	  if (bfd_link_pic (info))
   4889 	    {
   4890 	      if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
   4891 		ppc_howto_init ();
   4892 	      /* xgettext:c-format */
   4893 	      info->callbacks->einfo (_("%H: %s reloc unsupported "
   4894 					"in shared libraries and PIEs\n"),
   4895 				      abfd, sec, rel->r_offset,
   4896 				      ppc64_elf_howto_table[r_type]->name);
   4897 	      bfd_set_error (bfd_error_bad_value);
   4898 	      return FALSE;
   4899 	    }
   4900 	  break;
   4901 
   4902 	case R_PPC64_TOC16:
   4903 	case R_PPC64_TOC16_DS:
   4904 	  htab->do_multi_toc = 1;
   4905 	  ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
   4906 	  /* Fall through.  */
   4907 	case R_PPC64_TOC16_LO:
   4908 	case R_PPC64_TOC16_HI:
   4909 	case R_PPC64_TOC16_HA:
   4910 	case R_PPC64_TOC16_LO_DS:
   4911 	  sec->has_toc_reloc = 1;
   4912 	  if (h != NULL && bfd_link_executable (info))
   4913 	    {
   4914 	      /* We may need a copy reloc.  */
   4915 	      h->non_got_ref = 1;
   4916 	      /* Strongly prefer a copy reloc over a dynamic reloc.
   4917 		 glibc ld.so as of 2019-08 will error out if one of
   4918 		 these relocations is emitted.  */
   4919 	      h->needs_copy = 1;
   4920 	      goto dodyn;
   4921 	    }
   4922 	  break;
   4923 
   4924 	  /* Marker reloc.  */
   4925 	case R_PPC64_ENTRY:
   4926 	  break;
   4927 
   4928 	  /* This relocation describes the C++ object vtable hierarchy.
   4929 	     Reconstruct it for later use during GC.  */
   4930 	case R_PPC64_GNU_VTINHERIT:
   4931 	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
   4932 	    return FALSE;
   4933 	  break;
   4934 
   4935 	  /* This relocation describes which C++ vtable entries are actually
   4936 	     used.  Record for later use during GC.  */
   4937 	case R_PPC64_GNU_VTENTRY:
   4938 	  if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
   4939 	    return FALSE;
   4940 	  break;
   4941 
   4942 	case R_PPC64_REL14:
   4943 	case R_PPC64_REL14_BRTAKEN:
   4944 	case R_PPC64_REL14_BRNTAKEN:
   4945 	  {
   4946 	    asection *dest = NULL;
   4947 
   4948 	    /* Heuristic: If jumping outside our section, chances are
   4949 	       we are going to need a stub.  */
   4950 	    if (h != NULL)
   4951 	      {
   4952 		/* If the sym is weak it may be overridden later, so
   4953 		   don't assume we know where a weak sym lives.  */
   4954 		if (h->root.type == bfd_link_hash_defined)
   4955 		  dest = h->root.u.def.section;
   4956 	      }
   4957 	    else
   4958 	      {
   4959 		Elf_Internal_Sym *isym;
   4960 
   4961 		isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
   4962 					      abfd, r_symndx);
   4963 		if (isym == NULL)
   4964 		  return FALSE;
   4965 
   4966 		dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
   4967 	      }
   4968 
   4969 	    if (dest != sec)
   4970 	      ppc64_elf_section_data (sec)->has_14bit_branch = 1;
   4971 	  }
   4972 	  goto rel24;
   4973 
   4974 	case R_PPC64_PLTCALL:
   4975 	case R_PPC64_PLTCALL_NOTOC:
   4976 	  ppc64_elf_section_data (sec)->has_pltcall = 1;
   4977 	  /* Fall through.  */
   4978 
   4979 	case R_PPC64_REL24:
   4980 	case R_PPC64_REL24_NOTOC:
   4981 	rel24:
   4982 	  plt_list = ifunc;
   4983 	  if (h != NULL)
   4984 	    {
   4985 	      h->needs_plt = 1;
   4986 	      if (h->root.root.string[0] == '.'
   4987 		  && h->root.root.string[1] != '\0')
   4988 		ppc_elf_hash_entry (h)->is_func = 1;
   4989 
   4990 	      if (h == tga || h == dottga)
   4991 		{
   4992 		  sec->has_tls_reloc = 1;
   4993 		  if (rel != relocs
   4994 		      && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
   4995 			  || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
   4996 		    /* We have a new-style __tls_get_addr call with
   4997 		       a marker reloc.  */
   4998 		    ;
   4999 		  else
   5000 		    /* Mark this section as having an old-style call.  */
   5001 		    sec->nomark_tls_get_addr = 1;
   5002 		}
   5003 	      plt_list = &h->plt.plist;
   5004 	    }
   5005 
   5006 	  /* We may need a .plt entry if the function this reloc
   5007 	     refers to is in a shared lib.  */
   5008 	  if (plt_list
   5009 	      && !update_plt_info (abfd, plt_list, rel->r_addend))
   5010 	    return FALSE;
   5011 	  break;
   5012 
   5013 	case R_PPC64_ADDR14:
   5014 	case R_PPC64_ADDR14_BRNTAKEN:
   5015 	case R_PPC64_ADDR14_BRTAKEN:
   5016 	case R_PPC64_ADDR24:
   5017 	  goto dodyn;
   5018 
   5019 	case R_PPC64_TPREL64:
   5020 	  tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
   5021 	  if (bfd_link_dll (info))
   5022 	    info->flags |= DF_STATIC_TLS;
   5023 	  goto dotlstoc;
   5024 
   5025 	case R_PPC64_DTPMOD64:
   5026 	  if (rel + 1 < rel_end
   5027 	      && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
   5028 	      && rel[1].r_offset == rel->r_offset + 8)
   5029 	    tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
   5030 	  else
   5031 	    tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
   5032 	  goto dotlstoc;
   5033 
   5034 	case R_PPC64_DTPREL64:
   5035 	  tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
   5036 	  if (rel != relocs
   5037 	      && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
   5038 	      && rel[-1].r_offset == rel->r_offset - 8)
   5039 	    /* This is the second reloc of a dtpmod, dtprel pair.
   5040 	       Don't mark with TLS_DTPREL.  */
   5041 	    goto dodyn;
   5042 
   5043 	dotlstoc:
   5044 	  sec->has_tls_reloc = 1;
   5045 	  if (h != NULL)
   5046 	    ppc_elf_hash_entry (h)->tls_mask |= tls_type & 0xff;
   5047 	  else
   5048 	    if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
   5049 					rel->r_addend, tls_type))
   5050 	      return FALSE;
   5051 
   5052 	  ppc64_sec = ppc64_elf_section_data (sec);
   5053 	  if (ppc64_sec->sec_type != sec_toc)
   5054 	    {
   5055 	      bfd_size_type amt;
   5056 
   5057 	      /* One extra to simplify get_tls_mask.  */
   5058 	      amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
   5059 	      ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
   5060 	      if (ppc64_sec->u.toc.symndx == NULL)
   5061 		return FALSE;
   5062 	      amt = sec->size * sizeof (bfd_vma) / 8;
   5063 	      ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
   5064 	      if (ppc64_sec->u.toc.add == NULL)
   5065 		return FALSE;
   5066 	      BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
   5067 	      ppc64_sec->sec_type = sec_toc;
   5068 	    }
   5069 	  BFD_ASSERT (rel->r_offset % 8 == 0);
   5070 	  ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
   5071 	  ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
   5072 
   5073 	  /* Mark the second slot of a GD or LD entry.
   5074 	     -1 to indicate GD and -2 to indicate LD.  */
   5075 	  if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
   5076 	    ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
   5077 	  else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
   5078 	    ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
   5079 	  goto dodyn;
   5080 
   5081 	case R_PPC64_TPREL16_HI:
   5082 	case R_PPC64_TPREL16_HA:
   5083 	case R_PPC64_TPREL16_HIGH:
   5084 	case R_PPC64_TPREL16_HIGHA:
   5085 	case R_PPC64_TPREL16_HIGHER:
   5086 	case R_PPC64_TPREL16_HIGHERA:
   5087 	case R_PPC64_TPREL16_HIGHEST:
   5088 	case R_PPC64_TPREL16_HIGHESTA:
   5089 	  sec->has_tls_reloc = 1;
   5090 	  /* Fall through.  */
   5091 	case R_PPC64_TPREL34:
   5092 	case R_PPC64_TPREL16:
   5093 	case R_PPC64_TPREL16_DS:
   5094 	case R_PPC64_TPREL16_LO:
   5095 	case R_PPC64_TPREL16_LO_DS:
   5096 	  if (bfd_link_dll (info))
   5097 	    info->flags |= DF_STATIC_TLS;
   5098 	  goto dodyn;
   5099 
   5100 	case R_PPC64_ADDR64:
   5101 	  if (is_opd
   5102 	      && rel + 1 < rel_end
   5103 	      && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
   5104 	    {
   5105 	      if (h != NULL)
   5106 		ppc_elf_hash_entry (h)->is_func = 1;
   5107 	    }
   5108 	  /* Fall through.  */
   5109 
   5110 	case R_PPC64_ADDR16:
   5111 	case R_PPC64_ADDR16_DS:
   5112 	case R_PPC64_ADDR16_HA:
   5113 	case R_PPC64_ADDR16_HI:
   5114 	case R_PPC64_ADDR16_HIGH:
   5115 	case R_PPC64_ADDR16_HIGHA:
   5116 	case R_PPC64_ADDR16_HIGHER:
   5117 	case R_PPC64_ADDR16_HIGHERA:
   5118 	case R_PPC64_ADDR16_HIGHEST:
   5119 	case R_PPC64_ADDR16_HIGHESTA:
   5120 	case R_PPC64_ADDR16_LO:
   5121 	case R_PPC64_ADDR16_LO_DS:
   5122 	case R_PPC64_D34:
   5123 	case R_PPC64_D34_LO:
   5124 	case R_PPC64_D34_HI30:
   5125 	case R_PPC64_D34_HA30:
   5126 	case R_PPC64_ADDR16_HIGHER34:
   5127 	case R_PPC64_ADDR16_HIGHERA34:
   5128 	case R_PPC64_ADDR16_HIGHEST34:
   5129 	case R_PPC64_ADDR16_HIGHESTA34:
   5130 	case R_PPC64_D28:
   5131 	  if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
   5132 	      && rel->r_addend == 0)
   5133 	    {
   5134 	      /* We may need a .plt entry if this reloc refers to a
   5135 		 function in a shared lib.  */
   5136 	      if (!update_plt_info (abfd, &h->plt.plist, 0))
   5137 		return FALSE;
   5138 	      h->pointer_equality_needed = 1;
   5139 	    }
   5140 	  /* Fall through.  */
   5141 
   5142 	case R_PPC64_REL30:
   5143 	case R_PPC64_REL32:
   5144 	case R_PPC64_REL64:
   5145 	case R_PPC64_ADDR32:
   5146 	case R_PPC64_UADDR16:
   5147 	case R_PPC64_UADDR32:
   5148 	case R_PPC64_UADDR64:
   5149 	case R_PPC64_TOC:
   5150 	  if (h != NULL && bfd_link_executable (info))
   5151 	    /* We may need a copy reloc.  */
   5152 	    h->non_got_ref = 1;
   5153 
   5154 	  /* Don't propagate .opd relocs.  */
   5155 	  if (NO_OPD_RELOCS && is_opd)
   5156 	    break;
   5157 
   5158 	  /* If we are creating a shared library, and this is a reloc
   5159 	     against a global symbol, or a non PC relative reloc
   5160 	     against a local symbol, then we need to copy the reloc
   5161 	     into the shared library.  However, if we are linking with
   5162 	     -Bsymbolic, we do not need to copy a reloc against a
   5163 	     global symbol which is defined in an object we are
   5164 	     including in the link (i.e., DEF_REGULAR is set).  At
   5165 	     this point we have not seen all the input files, so it is
   5166 	     possible that DEF_REGULAR is not set now but will be set
   5167 	     later (it is never cleared).  In case of a weak definition,
   5168 	     DEF_REGULAR may be cleared later by a strong definition in
   5169 	     a shared library.  We account for that possibility below by
   5170 	     storing information in the dyn_relocs field of the hash
   5171 	     table entry.  A similar situation occurs when creating
   5172 	     shared libraries and symbol visibility changes render the
   5173 	     symbol local.
   5174 
   5175 	     If on the other hand, we are creating an executable, we
   5176 	     may need to keep relocations for symbols satisfied by a
   5177 	     dynamic library if we manage to avoid copy relocs for the
   5178 	     symbol.  */
   5179 	dodyn:
   5180 	  if ((h != NULL
   5181 	       && (h->root.type == bfd_link_hash_defweak
   5182 		   || !h->def_regular))
   5183 	      || (h != NULL
   5184 		  && !bfd_link_executable (info)
   5185 		  && !SYMBOLIC_BIND (info, h))
   5186 	      || (bfd_link_pic (info)
   5187 		  && must_be_dyn_reloc (info, r_type))
   5188 	      || (!bfd_link_pic (info)
   5189 		  && ifunc != NULL))
   5190 	    {
   5191 	      /* We must copy these reloc types into the output file.
   5192 		 Create a reloc section in dynobj and make room for
   5193 		 this reloc.  */
   5194 	      if (sreloc == NULL)
   5195 		{
   5196 		  sreloc = _bfd_elf_make_dynamic_reloc_section
   5197 		    (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
   5198 
   5199 		  if (sreloc == NULL)
   5200 		    return FALSE;
   5201 		}
   5202 
   5203 	      /* If this is a global symbol, we count the number of
   5204 		 relocations we need for this symbol.  */
   5205 	      if (h != NULL)
   5206 		{
   5207 		  struct elf_dyn_relocs *p;
   5208 		  struct elf_dyn_relocs **head;
   5209 
   5210 		  head = &h->dyn_relocs;
   5211 		  p = *head;
   5212 		  if (p == NULL || p->sec != sec)
   5213 		    {
   5214 		      p = bfd_alloc (htab->elf.dynobj, sizeof *p);
   5215 		      if (p == NULL)
   5216 			return FALSE;
   5217 		      p->next = *head;
   5218 		      *head = p;
   5219 		      p->sec = sec;
   5220 		      p->count = 0;
   5221 		      p->pc_count = 0;
   5222 		    }
   5223 		  p->count += 1;
   5224 		  if (!must_be_dyn_reloc (info, r_type))
   5225 		    p->pc_count += 1;
   5226 		}
   5227 	      else
   5228 		{
   5229 		  /* Track dynamic relocs needed for local syms too.
   5230 		     We really need local syms available to do this
   5231 		     easily.  Oh well.  */
   5232 		  struct ppc_dyn_relocs *p;
   5233 		  struct ppc_dyn_relocs **head;
   5234 		  bfd_boolean is_ifunc;
   5235 		  asection *s;
   5236 		  void *vpp;
   5237 		  Elf_Internal_Sym *isym;
   5238 
   5239 		  isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
   5240 						abfd, r_symndx);
   5241 		  if (isym == NULL)
   5242 		    return FALSE;
   5243 
   5244 		  s = bfd_section_from_elf_index (abfd, isym->st_shndx);
   5245 		  if (s == NULL)
   5246 		    s = sec;
   5247 
   5248 		  vpp = &elf_section_data (s)->local_dynrel;
   5249 		  head = (struct ppc_dyn_relocs **) vpp;
   5250 		  is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
   5251 		  p = *head;
   5252 		  if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
   5253 		    p = p->next;
   5254 		  if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
   5255 		    {
   5256 		      p = bfd_alloc (htab->elf.dynobj, sizeof *p);
   5257 		      if (p == NULL)
   5258 			return FALSE;
   5259 		      p->next = *head;
   5260 		      *head = p;
   5261 		      p->sec = sec;
   5262 		      p->ifunc = is_ifunc;
   5263 		      p->count = 0;
   5264 		    }
   5265 		  p->count += 1;
   5266 		}
   5267 	    }
   5268 	  break;
   5269 
   5270 	default:
   5271 	  break;
   5272 	}
   5273     }
   5274 
   5275   return TRUE;
   5276 }
   5277 
   5278 /* Merge backend specific data from an object file to the output
   5279    object file when linking.  */
   5280 
   5281 static bfd_boolean
   5282 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
   5283 {
   5284   bfd *obfd = info->output_bfd;
   5285   unsigned long iflags, oflags;
   5286 
   5287   if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
   5288     return TRUE;
   5289 
   5290   if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
   5291     return TRUE;
   5292 
   5293   if (!_bfd_generic_verify_endian_match (ibfd, info))
   5294     return FALSE;
   5295 
   5296   iflags = elf_elfheader (ibfd)->e_flags;
   5297   oflags = elf_elfheader (obfd)->e_flags;
   5298 
   5299   if (iflags & ~EF_PPC64_ABI)
   5300     {
   5301       _bfd_error_handler
   5302 	/* xgettext:c-format */
   5303 	(_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
   5304       bfd_set_error (bfd_error_bad_value);
   5305       return FALSE;
   5306     }
   5307   else if (iflags != oflags && iflags != 0)
   5308     {
   5309       _bfd_error_handler
   5310 	/* xgettext:c-format */
   5311 	(_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
   5312 	 ibfd, iflags, oflags);
   5313       bfd_set_error (bfd_error_bad_value);
   5314       return FALSE;
   5315     }
   5316 
   5317   if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
   5318     return FALSE;
   5319 
   5320   /* Merge Tag_compatibility attributes and any common GNU ones.  */
   5321   return _bfd_elf_merge_object_attributes (ibfd, info);
   5322 }
   5323 
   5324 static bfd_boolean
   5325 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
   5326 {
   5327   /* Print normal ELF private data.  */
   5328   _bfd_elf_print_private_bfd_data (abfd, ptr);
   5329 
   5330   if (elf_elfheader (abfd)->e_flags != 0)
   5331     {
   5332       FILE *file = ptr;
   5333 
   5334       fprintf (file, _("private flags = 0x%lx:"),
   5335 	       elf_elfheader (abfd)->e_flags);
   5336 
   5337       if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
   5338 	fprintf (file, _(" [abiv%ld]"),
   5339 		 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
   5340       fputc ('\n', file);
   5341     }
   5342 
   5343   return TRUE;
   5344 }
   5345 
   5346 /* OFFSET in OPD_SEC specifies a function descriptor.  Return the address
   5347    of the code entry point, and its section, which must be in the same
   5348    object as OPD_SEC.  Returns (bfd_vma) -1 on error.  */
   5349 
   5350 static bfd_vma
   5351 opd_entry_value (asection *opd_sec,
   5352 		 bfd_vma offset,
   5353 		 asection **code_sec,
   5354 		 bfd_vma *code_off,
   5355 		 bfd_boolean in_code_sec)
   5356 {
   5357   bfd *opd_bfd = opd_sec->owner;
   5358   Elf_Internal_Rela *relocs;
   5359   Elf_Internal_Rela *lo, *hi, *look;
   5360   bfd_vma val;
   5361 
   5362   /* No relocs implies we are linking a --just-symbols object, or looking
   5363      at a final linked executable with addr2line or somesuch.  */
   5364   if (opd_sec->reloc_count == 0)
   5365     {
   5366       bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
   5367 
   5368       if (contents == NULL)
   5369 	{
   5370 	  if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
   5371 	    return (bfd_vma) -1;
   5372 	  ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
   5373 	}
   5374 
   5375       /* PR 17512: file: 64b9dfbb.  */
   5376       if (offset + 7 >= opd_sec->size || offset + 7 < offset)
   5377 	return (bfd_vma) -1;
   5378 
   5379       val = bfd_get_64 (opd_bfd, contents + offset);
   5380       if (code_sec != NULL)
   5381 	{
   5382 	  asection *sec, *likely = NULL;
   5383 
   5384 	  if (in_code_sec)
   5385 	    {
   5386 	      sec = *code_sec;
   5387 	      if (sec->vma <= val
   5388 		  && val < sec->vma + sec->size)
   5389 		likely = sec;
   5390 	      else
   5391 		val = -1;
   5392 	    }
   5393 	  else
   5394 	    for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
   5395 	      if (sec->vma <= val
   5396 		  && (sec->flags & SEC_LOAD) != 0
   5397 		  && (sec->flags & SEC_ALLOC) != 0)
   5398 		likely = sec;
   5399 	  if (likely != NULL)
   5400 	    {
   5401 	      *code_sec = likely;
   5402 	      if (code_off != NULL)
   5403 		*code_off = val - likely->vma;
   5404 	    }
   5405 	}
   5406       return val;
   5407     }
   5408 
   5409   BFD_ASSERT (is_ppc64_elf (opd_bfd));
   5410 
   5411   relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
   5412   if (relocs == NULL)
   5413     relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
   5414   /* PR 17512: file: df8e1fd6.  */
   5415   if (relocs == NULL)
   5416     return (bfd_vma) -1;
   5417 
   5418   /* Go find the opd reloc at the sym address.  */
   5419   lo = relocs;
   5420   hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
   5421   val = (bfd_vma) -1;
   5422   while (lo < hi)
   5423     {
   5424       look = lo + (hi - lo) / 2;
   5425       if (look->r_offset < offset)
   5426 	lo = look + 1;
   5427       else if (look->r_offset > offset)
   5428 	hi = look;
   5429       else
   5430 	{
   5431 	  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
   5432 
   5433 	  if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
   5434 	      && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
   5435 	    {
   5436 	      unsigned long symndx = ELF64_R_SYM (look->r_info);
   5437 	      asection *sec = NULL;
   5438 
   5439 	      if (symndx >= symtab_hdr->sh_info
   5440 		  && elf_sym_hashes (opd_bfd) != NULL)
   5441 		{
   5442 		  struct elf_link_hash_entry **sym_hashes;
   5443 		  struct elf_link_hash_entry *rh;
   5444 
   5445 		  sym_hashes = elf_sym_hashes (opd_bfd);
   5446 		  rh = sym_hashes[symndx - symtab_hdr->sh_info];
   5447 		  if (rh != NULL)
   5448 		    {
   5449 		      rh = elf_follow_link (rh);
   5450 		      if (rh->root.type != bfd_link_hash_defined
   5451 			  && rh->root.type != bfd_link_hash_defweak)
   5452 			break;
   5453 		      if (rh->root.u.def.section->owner == opd_bfd)
   5454 			{
   5455 			  val = rh->root.u.def.value;
   5456 			  sec = rh->root.u.def.section;
   5457 			}
   5458 		    }
   5459 		}
   5460 
   5461 	      if (sec == NULL)
   5462 		{
   5463 		  Elf_Internal_Sym *sym;
   5464 
   5465 		  if (symndx < symtab_hdr->sh_info)
   5466 		    {
   5467 		      sym = (Elf_Internal_Sym *) symtab_hdr->contents;
   5468 		      if (sym == NULL)
   5469 			{
   5470 			  size_t symcnt = symtab_hdr->sh_info;
   5471 			  sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
   5472 						      symcnt, 0,
   5473 						      NULL, NULL, NULL);
   5474 			  if (sym == NULL)
   5475 			    break;
   5476 			  symtab_hdr->contents = (bfd_byte *) sym;
   5477 			}
   5478 		      sym += symndx;
   5479 		    }
   5480 		  else
   5481 		    {
   5482 		      sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
   5483 						  1, symndx,
   5484 						  NULL, NULL, NULL);
   5485 		      if (sym == NULL)
   5486 			break;
   5487 		    }
   5488 		  sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
   5489 		  if (sec == NULL)
   5490 		    break;
   5491 		  BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
   5492 		  val = sym->st_value;
   5493 		}
   5494 
   5495 	      val += look->r_addend;
   5496 	      if (code_off != NULL)
   5497 		*code_off = val;
   5498 	      if (code_sec != NULL)
   5499 		{
   5500 		  if (in_code_sec && *code_sec != sec)
   5501 		    return -1;
   5502 		  else
   5503 		    *code_sec = sec;
   5504 		}
   5505 	      if (sec->output_section != NULL)
   5506 		val += sec->output_section->vma + sec->output_offset;
   5507 	    }
   5508 	  break;
   5509 	}
   5510     }
   5511 
   5512   return val;
   5513 }
   5514 
   5515 /* If the ELF symbol SYM might be a function in SEC, return the
   5516    function size and set *CODE_OFF to the function's entry point,
   5517    otherwise return zero.  */
   5518 
   5519 static bfd_size_type
   5520 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
   5521 			      bfd_vma *code_off)
   5522 {
   5523   bfd_size_type size;
   5524 
   5525   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
   5526 		     | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
   5527     return 0;
   5528 
   5529   size = 0;
   5530   if (!(sym->flags & BSF_SYNTHETIC))
   5531     size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
   5532 
   5533   if (strcmp (sym->section->name, ".opd") == 0)
   5534     {
   5535       struct _opd_sec_data *opd = get_opd_info (sym->section);
   5536       bfd_vma symval = sym->value;
   5537 
   5538       if (opd != NULL
   5539 	  && opd->adjust != NULL
   5540 	  && elf_section_data (sym->section)->relocs != NULL)
   5541 	{
   5542 	  /* opd_entry_value will use cached relocs that have been
   5543 	     adjusted, but with raw symbols.  That means both local
   5544 	     and global symbols need adjusting.  */
   5545 	  long adjust = opd->adjust[OPD_NDX (symval)];
   5546 	  if (adjust == -1)
   5547 	    return 0;
   5548 	  symval += adjust;
   5549 	}
   5550 
   5551       if (opd_entry_value (sym->section, symval,
   5552 			   &sec, code_off, TRUE) == (bfd_vma) -1)
   5553 	return 0;
   5554       /* An old ABI binary with dot-syms has a size of 24 on the .opd
   5555 	 symbol.  This size has nothing to do with the code size of the
   5556 	 function, which is what we're supposed to return, but the
   5557 	 code size isn't available without looking up the dot-sym.
   5558 	 However, doing that would be a waste of time particularly
   5559 	 since elf_find_function will look at the dot-sym anyway.
   5560 	 Now, elf_find_function will keep the largest size of any
   5561 	 function sym found at the code address of interest, so return
   5562 	 1 here to avoid it incorrectly caching a larger function size
   5563 	 for a small function.  This does mean we return the wrong
   5564 	 size for a new-ABI function of size 24, but all that does is
   5565 	 disable caching for such functions.  */
   5566       if (size == 24)
   5567 	size = 1;
   5568     }
   5569   else
   5570     {
   5571       if (sym->section != sec)
   5572 	return 0;
   5573       *code_off = sym->value;
   5574     }
   5575   if (size == 0)
   5576     size = 1;
   5577   return size;
   5578 }
   5579 
   5580 /* Return true if symbol is a strong function defined in an ELFv2
   5581    object with st_other localentry bits of zero, ie. its local entry
   5582    point coincides with its global entry point.  */
   5583 
   5584 static bfd_boolean
   5585 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
   5586 {
   5587   return (h != NULL
   5588 	  && h->type == STT_FUNC
   5589 	  && h->root.type == bfd_link_hash_defined
   5590 	  && (STO_PPC64_LOCAL_MASK & h->other) == 0
   5591 	  && !ppc_elf_hash_entry (h)->non_zero_localentry
   5592 	  && is_ppc64_elf (h->root.u.def.section->owner)
   5593 	  && abiversion (h->root.u.def.section->owner) >= 2);
   5594 }
   5595 
   5596 /* Return true if symbol is defined in a regular object file.  */
   5597 
   5598 static bfd_boolean
   5599 is_static_defined (struct elf_link_hash_entry *h)
   5600 {
   5601   return ((h->root.type == bfd_link_hash_defined
   5602 	   || h->root.type == bfd_link_hash_defweak)
   5603 	  && h->root.u.def.section != NULL
   5604 	  && h->root.u.def.section->output_section != NULL);
   5605 }
   5606 
   5607 /* If FDH is a function descriptor symbol, return the associated code
   5608    entry symbol if it is defined.  Return NULL otherwise.  */
   5609 
   5610 static struct ppc_link_hash_entry *
   5611 defined_code_entry (struct ppc_link_hash_entry *fdh)
   5612 {
   5613   if (fdh->is_func_descriptor)
   5614     {
   5615       struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
   5616       if (fh->elf.root.type == bfd_link_hash_defined
   5617 	  || fh->elf.root.type == bfd_link_hash_defweak)
   5618 	return fh;
   5619     }
   5620   return NULL;
   5621 }
   5622 
   5623 /* If FH is a function code entry symbol, return the associated
   5624    function descriptor symbol if it is defined.  Return NULL otherwise.  */
   5625 
   5626 static struct ppc_link_hash_entry *
   5627 defined_func_desc (struct ppc_link_hash_entry *fh)
   5628 {
   5629   if (fh->oh != NULL
   5630       && fh->oh->is_func_descriptor)
   5631     {
   5632       struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
   5633       if (fdh->elf.root.type == bfd_link_hash_defined
   5634 	  || fdh->elf.root.type == bfd_link_hash_defweak)
   5635 	return fdh;
   5636     }
   5637   return NULL;
   5638 }
   5639 
   5640 /* Given H is a symbol that satisfies is_static_defined, return the
   5641    value in the output file.  */
   5642 
   5643 static bfd_vma
   5644 defined_sym_val (struct elf_link_hash_entry *h)
   5645 {
   5646   return (h->root.u.def.section->output_section->vma
   5647 	  + h->root.u.def.section->output_offset
   5648 	  + h->root.u.def.value);
   5649 }
   5650 
   5651 /* Return true if H matches __tls_get_addr or one of its variants.  */
   5652 
   5653 static bfd_boolean
   5654 is_tls_get_addr (struct elf_link_hash_entry *h,
   5655 		 struct ppc_link_hash_table *htab)
   5656 {
   5657   return (h == (struct elf_link_hash_entry *) htab->tls_get_addr_fd
   5658 	  || h == (struct elf_link_hash_entry *) htab->tga_desc_fd
   5659 	  || h == (struct elf_link_hash_entry *) htab->tls_get_addr
   5660 	  || h == (struct elf_link_hash_entry *) htab->tga_desc);
   5661 }
   5662 
   5663 static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
   5664 
   5665 /* Garbage collect sections, after first dealing with dot-symbols.  */
   5666 
   5667 static bfd_boolean
   5668 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
   5669 {
   5670   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   5671 
   5672   if (htab != NULL && htab->need_func_desc_adj)
   5673     {
   5674       elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
   5675       htab->need_func_desc_adj = 0;
   5676     }
   5677   return bfd_elf_gc_sections (abfd, info);
   5678 }
   5679 
   5680 /* Mark all our entry sym sections, both opd and code section.  */
   5681 
   5682 static void
   5683 ppc64_elf_gc_keep (struct bfd_link_info *info)
   5684 {
   5685   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   5686   struct bfd_sym_chain *sym;
   5687 
   5688   if (htab == NULL)
   5689     return;
   5690 
   5691   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
   5692     {
   5693       struct ppc_link_hash_entry *eh, *fh;
   5694       asection *sec;
   5695 
   5696       eh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym->name,
   5697 						     FALSE, FALSE, TRUE));
   5698       if (eh == NULL)
   5699 	continue;
   5700       if (eh->elf.root.type != bfd_link_hash_defined
   5701 	  && eh->elf.root.type != bfd_link_hash_defweak)
   5702 	continue;
   5703 
   5704       fh = defined_code_entry (eh);
   5705       if (fh != NULL)
   5706 	{
   5707 	  sec = fh->elf.root.u.def.section;
   5708 	  sec->flags |= SEC_KEEP;
   5709 	}
   5710       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
   5711 	       && opd_entry_value (eh->elf.root.u.def.section,
   5712 				   eh->elf.root.u.def.value,
   5713 				   &sec, NULL, FALSE) != (bfd_vma) -1)
   5714 	sec->flags |= SEC_KEEP;
   5715 
   5716       sec = eh->elf.root.u.def.section;
   5717       sec->flags |= SEC_KEEP;
   5718     }
   5719 }
   5720 
   5721 /* Mark sections containing dynamically referenced symbols.  When
   5722    building shared libraries, we must assume that any visible symbol is
   5723    referenced.  */
   5724 
   5725 static bfd_boolean
   5726 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
   5727 {
   5728   struct bfd_link_info *info = (struct bfd_link_info *) inf;
   5729   struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
   5730   struct ppc_link_hash_entry *fdh;
   5731   struct bfd_elf_dynamic_list *d = info->dynamic_list;
   5732 
   5733   /* Dynamic linking info is on the func descriptor sym.  */
   5734   fdh = defined_func_desc (eh);
   5735   if (fdh != NULL)
   5736     eh = fdh;
   5737 
   5738   if ((eh->elf.root.type == bfd_link_hash_defined
   5739        || eh->elf.root.type == bfd_link_hash_defweak)
   5740       && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
   5741 	  || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
   5742 	      && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
   5743 	      && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
   5744 	      && (!bfd_link_executable (info)
   5745 		  || info->gc_keep_exported
   5746 		  || info->export_dynamic
   5747 		  || (eh->elf.dynamic
   5748 		      && d != NULL
   5749 		      && (*d->match) (&d->head, NULL,
   5750 				      eh->elf.root.root.string)))
   5751 	      && (eh->elf.versioned >= versioned
   5752 		  || !bfd_hide_sym_by_version (info->version_info,
   5753 					       eh->elf.root.root.string)))))
   5754     {
   5755       asection *code_sec;
   5756       struct ppc_link_hash_entry *fh;
   5757 
   5758       eh->elf.root.u.def.section->flags |= SEC_KEEP;
   5759 
   5760       /* Function descriptor syms cause the associated
   5761 	 function code sym section to be marked.  */
   5762       fh = defined_code_entry (eh);
   5763       if (fh != NULL)
   5764 	{
   5765 	  code_sec = fh->elf.root.u.def.section;
   5766 	  code_sec->flags |= SEC_KEEP;
   5767 	}
   5768       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
   5769 	       && opd_entry_value (eh->elf.root.u.def.section,
   5770 				   eh->elf.root.u.def.value,
   5771 				   &code_sec, NULL, FALSE) != (bfd_vma) -1)
   5772 	code_sec->flags |= SEC_KEEP;
   5773     }
   5774 
   5775   return TRUE;
   5776 }
   5777 
   5778 /* Return the section that should be marked against GC for a given
   5779    relocation.  */
   5780 
   5781 static asection *
   5782 ppc64_elf_gc_mark_hook (asection *sec,
   5783 			struct bfd_link_info *info,
   5784 			Elf_Internal_Rela *rel,
   5785 			struct elf_link_hash_entry *h,
   5786 			Elf_Internal_Sym *sym)
   5787 {
   5788   asection *rsec;
   5789 
   5790   /* Syms return NULL if we're marking .opd, so we avoid marking all
   5791      function sections, as all functions are referenced in .opd.  */
   5792   rsec = NULL;
   5793   if (get_opd_info (sec) != NULL)
   5794     return rsec;
   5795 
   5796   if (h != NULL)
   5797     {
   5798       enum elf_ppc64_reloc_type r_type;
   5799       struct ppc_link_hash_entry *eh, *fh, *fdh;
   5800 
   5801       r_type = ELF64_R_TYPE (rel->r_info);
   5802       switch (r_type)
   5803 	{
   5804 	case R_PPC64_GNU_VTINHERIT:
   5805 	case R_PPC64_GNU_VTENTRY:
   5806 	  break;
   5807 
   5808 	default:
   5809 	  switch (h->root.type)
   5810 	    {
   5811 	    case bfd_link_hash_defined:
   5812 	    case bfd_link_hash_defweak:
   5813 	      eh = ppc_elf_hash_entry (h);
   5814 	      fdh = defined_func_desc (eh);
   5815 	      if (fdh != NULL)
   5816 		{
   5817 		  /* -mcall-aixdesc code references the dot-symbol on
   5818 		     a call reloc.  Mark the function descriptor too
   5819 		     against garbage collection.  */
   5820 		  fdh->elf.mark = 1;
   5821 		  if (fdh->elf.is_weakalias)
   5822 		    weakdef (&fdh->elf)->mark = 1;
   5823 		  eh = fdh;
   5824 		}
   5825 
   5826 	      /* Function descriptor syms cause the associated
   5827 		 function code sym section to be marked.  */
   5828 	      fh = defined_code_entry (eh);
   5829 	      if (fh != NULL)
   5830 		{
   5831 		  /* They also mark their opd section.  */
   5832 		  eh->elf.root.u.def.section->gc_mark = 1;
   5833 
   5834 		  rsec = fh->elf.root.u.def.section;
   5835 		}
   5836 	      else if (get_opd_info (eh->elf.root.u.def.section) != NULL
   5837 		       && opd_entry_value (eh->elf.root.u.def.section,
   5838 					   eh->elf.root.u.def.value,
   5839 					   &rsec, NULL, FALSE) != (bfd_vma) -1)
   5840 		eh->elf.root.u.def.section->gc_mark = 1;
   5841 	      else
   5842 		rsec = h->root.u.def.section;
   5843 	      break;
   5844 
   5845 	    case bfd_link_hash_common:
   5846 	      rsec = h->root.u.c.p->section;
   5847 	      break;
   5848 
   5849 	    default:
   5850 	      return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
   5851 	    }
   5852 	}
   5853     }
   5854   else
   5855     {
   5856       struct _opd_sec_data *opd;
   5857 
   5858       rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
   5859       opd = get_opd_info (rsec);
   5860       if (opd != NULL && opd->func_sec != NULL)
   5861 	{
   5862 	  rsec->gc_mark = 1;
   5863 
   5864 	  rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
   5865 	}
   5866     }
   5867 
   5868   return rsec;
   5869 }
   5870 
   5871 /* The maximum size of .sfpr.  */
   5872 #define SFPR_MAX (218*4)
   5873 
   5874 struct sfpr_def_parms
   5875 {
   5876   const char name[12];
   5877   unsigned char lo, hi;
   5878   bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
   5879   bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
   5880 };
   5881 
   5882 /* Auto-generate _save*, _rest* functions in .sfpr.
   5883    If STUB_SEC is non-null, define alias symbols in STUB_SEC
   5884    instead.  */
   5885 
   5886 static bfd_boolean
   5887 sfpr_define (struct bfd_link_info *info,
   5888 	     const struct sfpr_def_parms *parm,
   5889 	     asection *stub_sec)
   5890 {
   5891   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   5892   unsigned int i;
   5893   size_t len = strlen (parm->name);
   5894   bfd_boolean writing = FALSE;
   5895   char sym[16];
   5896 
   5897   if (htab == NULL)
   5898     return FALSE;
   5899 
   5900   memcpy (sym, parm->name, len);
   5901   sym[len + 2] = 0;
   5902 
   5903   for (i = parm->lo; i <= parm->hi; i++)
   5904     {
   5905       struct ppc_link_hash_entry *h;
   5906 
   5907       sym[len + 0] = i / 10 + '0';
   5908       sym[len + 1] = i % 10 + '0';
   5909       h = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym,
   5910 						    writing, TRUE, TRUE));
   5911       if (stub_sec != NULL)
   5912 	{
   5913 	  if (h != NULL
   5914 	      && h->elf.root.type == bfd_link_hash_defined
   5915 	      && h->elf.root.u.def.section == htab->sfpr)
   5916 	    {
   5917 	      struct elf_link_hash_entry *s;
   5918 	      char buf[32];
   5919 	      sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
   5920 	      s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
   5921 	      if (s == NULL)
   5922 		return FALSE;
   5923 	      if (s->root.type == bfd_link_hash_new)
   5924 		{
   5925 		  s->root.type = bfd_link_hash_defined;
   5926 		  s->root.u.def.section = stub_sec;
   5927 		  s->root.u.def.value = (stub_sec->size - htab->sfpr->size
   5928 					 + h->elf.root.u.def.value);
   5929 		  s->ref_regular = 1;
   5930 		  s->def_regular = 1;
   5931 		  s->ref_regular_nonweak = 1;
   5932 		  s->forced_local = 1;
   5933 		  s->non_elf = 0;
   5934 		  s->root.linker_def = 1;
   5935 		}
   5936 	    }
   5937 	  continue;
   5938 	}
   5939       if (h != NULL)
   5940 	{
   5941 	  h->save_res = 1;
   5942 	  if (!h->elf.def_regular)
   5943 	    {
   5944 	      h->elf.root.type = bfd_link_hash_defined;
   5945 	      h->elf.root.u.def.section = htab->sfpr;
   5946 	      h->elf.root.u.def.value = htab->sfpr->size;
   5947 	      h->elf.type = STT_FUNC;
   5948 	      h->elf.def_regular = 1;
   5949 	      h->elf.non_elf = 0;
   5950 	      _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
   5951 	      writing = TRUE;
   5952 	      if (htab->sfpr->contents == NULL)
   5953 		{
   5954 		  htab->sfpr->contents
   5955 		    = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
   5956 		  if (htab->sfpr->contents == NULL)
   5957 		    return FALSE;
   5958 		}
   5959 	    }
   5960 	}
   5961       if (writing)
   5962 	{
   5963 	  bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
   5964 	  if (i != parm->hi)
   5965 	    p = (*parm->write_ent) (htab->elf.dynobj, p, i);
   5966 	  else
   5967 	    p = (*parm->write_tail) (htab->elf.dynobj, p, i);
   5968 	  htab->sfpr->size = p - htab->sfpr->contents;
   5969 	}
   5970     }
   5971 
   5972   return TRUE;
   5973 }
   5974 
   5975 static bfd_byte *
   5976 savegpr0 (bfd *abfd, bfd_byte *p, int r)
   5977 {
   5978   bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   5979   return p + 4;
   5980 }
   5981 
   5982 static bfd_byte *
   5983 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
   5984 {
   5985   p = savegpr0 (abfd, p, r);
   5986   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
   5987   p = p + 4;
   5988   bfd_put_32 (abfd, BLR, p);
   5989   return p + 4;
   5990 }
   5991 
   5992 static bfd_byte *
   5993 restgpr0 (bfd *abfd, bfd_byte *p, int r)
   5994 {
   5995   bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   5996   return p + 4;
   5997 }
   5998 
   5999 static bfd_byte *
   6000 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
   6001 {
   6002   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
   6003   p = p + 4;
   6004   p = restgpr0 (abfd, p, r);
   6005   bfd_put_32 (abfd, MTLR_R0, p);
   6006   p = p + 4;
   6007   if (r == 29)
   6008     {
   6009       p = restgpr0 (abfd, p, 30);
   6010       p = restgpr0 (abfd, p, 31);
   6011     }
   6012   bfd_put_32 (abfd, BLR, p);
   6013   return p + 4;
   6014 }
   6015 
   6016 static bfd_byte *
   6017 savegpr1 (bfd *abfd, bfd_byte *p, int r)
   6018 {
   6019   bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6020   return p + 4;
   6021 }
   6022 
   6023 static bfd_byte *
   6024 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
   6025 {
   6026   p = savegpr1 (abfd, p, r);
   6027   bfd_put_32 (abfd, BLR, p);
   6028   return p + 4;
   6029 }
   6030 
   6031 static bfd_byte *
   6032 restgpr1 (bfd *abfd, bfd_byte *p, int r)
   6033 {
   6034   bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6035   return p + 4;
   6036 }
   6037 
   6038 static bfd_byte *
   6039 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
   6040 {
   6041   p = restgpr1 (abfd, p, r);
   6042   bfd_put_32 (abfd, BLR, p);
   6043   return p + 4;
   6044 }
   6045 
   6046 static bfd_byte *
   6047 savefpr (bfd *abfd, bfd_byte *p, int r)
   6048 {
   6049   bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6050   return p + 4;
   6051 }
   6052 
   6053 static bfd_byte *
   6054 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
   6055 {
   6056   p = savefpr (abfd, p, r);
   6057   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
   6058   p = p + 4;
   6059   bfd_put_32 (abfd, BLR, p);
   6060   return p + 4;
   6061 }
   6062 
   6063 static bfd_byte *
   6064 restfpr (bfd *abfd, bfd_byte *p, int r)
   6065 {
   6066   bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6067   return p + 4;
   6068 }
   6069 
   6070 static bfd_byte *
   6071 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
   6072 {
   6073   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
   6074   p = p + 4;
   6075   p = restfpr (abfd, p, r);
   6076   bfd_put_32 (abfd, MTLR_R0, p);
   6077   p = p + 4;
   6078   if (r == 29)
   6079     {
   6080       p = restfpr (abfd, p, 30);
   6081       p = restfpr (abfd, p, 31);
   6082     }
   6083   bfd_put_32 (abfd, BLR, p);
   6084   return p + 4;
   6085 }
   6086 
   6087 static bfd_byte *
   6088 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
   6089 {
   6090   p = savefpr (abfd, p, r);
   6091   bfd_put_32 (abfd, BLR, p);
   6092   return p + 4;
   6093 }
   6094 
   6095 static bfd_byte *
   6096 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
   6097 {
   6098   p = restfpr (abfd, p, r);
   6099   bfd_put_32 (abfd, BLR, p);
   6100   return p + 4;
   6101 }
   6102 
   6103 static bfd_byte *
   6104 savevr (bfd *abfd, bfd_byte *p, int r)
   6105 {
   6106   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
   6107   p = p + 4;
   6108   bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
   6109   return p + 4;
   6110 }
   6111 
   6112 static bfd_byte *
   6113 savevr_tail (bfd *abfd, bfd_byte *p, int r)
   6114 {
   6115   p = savevr (abfd, p, r);
   6116   bfd_put_32 (abfd, BLR, p);
   6117   return p + 4;
   6118 }
   6119 
   6120 static bfd_byte *
   6121 restvr (bfd *abfd, bfd_byte *p, int r)
   6122 {
   6123   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
   6124   p = p + 4;
   6125   bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
   6126   return p + 4;
   6127 }
   6128 
   6129 static bfd_byte *
   6130 restvr_tail (bfd *abfd, bfd_byte *p, int r)
   6131 {
   6132   p = restvr (abfd, p, r);
   6133   bfd_put_32 (abfd, BLR, p);
   6134   return p + 4;
   6135 }
   6136 
   6137 #define STDU_R1_0R1	0xf8210001
   6138 #define ADDI_R1_R1	0x38210000
   6139 
   6140 /* Emit prologue of wrapper preserving regs around a call to
   6141    __tls_get_addr_opt.  */
   6142 
   6143 static bfd_byte *
   6144 tls_get_addr_prologue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
   6145 {
   6146   unsigned int i;
   6147 
   6148   bfd_put_32 (obfd, MFLR_R0, p);
   6149   p += 4;
   6150   bfd_put_32 (obfd, STD_R0_0R1 + 16, p);
   6151   p += 4;
   6152 
   6153   if (htab->opd_abi)
   6154     {
   6155       for (i = 4; i < 12; i++)
   6156 	{
   6157 	  bfd_put_32 (obfd,
   6158 		      STD_R0_0R1 | i << 21 | (-(13 - i) * 8 & 0xffff), p);
   6159 	  p += 4;
   6160 	}
   6161       bfd_put_32 (obfd, STDU_R1_0R1 | (-128 & 0xffff), p);
   6162       p += 4;
   6163     }
   6164   else
   6165     {
   6166       for (i = 4; i < 12; i++)
   6167 	{
   6168 	  bfd_put_32 (obfd,
   6169 		      STD_R0_0R1 | i << 21 | (-(12 - i) * 8 & 0xffff), p);
   6170 	  p += 4;
   6171 	}
   6172       bfd_put_32 (obfd, STDU_R1_0R1 | (-96 & 0xffff), p);
   6173       p += 4;
   6174     }
   6175   return p;
   6176 }
   6177 
   6178 /* Emit epilogue of wrapper preserving regs around a call to
   6179    __tls_get_addr_opt.  */
   6180 
   6181 static bfd_byte *
   6182 tls_get_addr_epilogue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
   6183 {
   6184   unsigned int i;
   6185 
   6186   if (htab->opd_abi)
   6187     {
   6188       for (i = 4; i < 12; i++)
   6189 	{
   6190 	  bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (128 - (13 - i) * 8), p);
   6191 	  p += 4;
   6192 	}
   6193       bfd_put_32 (obfd, ADDI_R1_R1 | 128, p);
   6194       p += 4;
   6195     }
   6196   else
   6197     {
   6198       for (i = 4; i < 12; i++)
   6199 	{
   6200 	  bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (96 - (12 - i) * 8), p);
   6201 	  p += 4;
   6202 	}
   6203       bfd_put_32 (obfd, ADDI_R1_R1 | 96, p);
   6204       p += 4;
   6205     }
   6206   bfd_put_32 (obfd, LD_R0_0R1 | 16, p);
   6207   p += 4;
   6208   bfd_put_32 (obfd, MTLR_R0, p);
   6209   p += 4;
   6210   bfd_put_32 (obfd, BLR, p);
   6211   p += 4;
   6212   return p;
   6213 }
   6214 
   6215 /* Called via elf_link_hash_traverse to transfer dynamic linking
   6216    information on function code symbol entries to their corresponding
   6217    function descriptor symbol entries.  */
   6218 
   6219 static bfd_boolean
   6220 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
   6221 {
   6222   struct bfd_link_info *info;
   6223   struct ppc_link_hash_table *htab;
   6224   struct ppc_link_hash_entry *fh;
   6225   struct ppc_link_hash_entry *fdh;
   6226   bfd_boolean force_local;
   6227 
   6228   fh = ppc_elf_hash_entry (h);
   6229   if (fh->elf.root.type == bfd_link_hash_indirect)
   6230     return TRUE;
   6231 
   6232   if (!fh->is_func)
   6233     return TRUE;
   6234 
   6235   if (fh->elf.root.root.string[0] != '.'
   6236       || fh->elf.root.root.string[1] == '\0')
   6237     return TRUE;
   6238 
   6239   info = inf;
   6240   htab = ppc_hash_table (info);
   6241   if (htab == NULL)
   6242     return FALSE;
   6243 
   6244   /* Find the corresponding function descriptor symbol.  */
   6245   fdh = lookup_fdh (fh, htab);
   6246 
   6247   /* Resolve undefined references to dot-symbols as the value
   6248      in the function descriptor, if we have one in a regular object.
   6249      This is to satisfy cases like ".quad .foo".  Calls to functions
   6250      in dynamic objects are handled elsewhere.  */
   6251   if ((fh->elf.root.type == bfd_link_hash_undefined
   6252        || fh->elf.root.type == bfd_link_hash_undefweak)
   6253       && (fdh->elf.root.type == bfd_link_hash_defined
   6254 	  || fdh->elf.root.type == bfd_link_hash_defweak)
   6255       && get_opd_info (fdh->elf.root.u.def.section) != NULL
   6256       && opd_entry_value (fdh->elf.root.u.def.section,
   6257 			  fdh->elf.root.u.def.value,
   6258 			  &fh->elf.root.u.def.section,
   6259 			  &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
   6260     {
   6261       fh->elf.root.type = fdh->elf.root.type;
   6262       fh->elf.forced_local = 1;
   6263       fh->elf.def_regular = fdh->elf.def_regular;
   6264       fh->elf.def_dynamic = fdh->elf.def_dynamic;
   6265     }
   6266 
   6267   if (!fh->elf.dynamic)
   6268     {
   6269       struct plt_entry *ent;
   6270 
   6271       for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
   6272 	if (ent->plt.refcount > 0)
   6273 	  break;
   6274       if (ent == NULL)
   6275 	return TRUE;
   6276     }
   6277 
   6278   /* Create a descriptor as undefined if necessary.  */
   6279   if (fdh == NULL
   6280       && !bfd_link_executable (info)
   6281       && (fh->elf.root.type == bfd_link_hash_undefined
   6282 	  || fh->elf.root.type == bfd_link_hash_undefweak))
   6283     {
   6284       fdh = make_fdh (info, fh);
   6285       if (fdh == NULL)
   6286 	return FALSE;
   6287     }
   6288 
   6289   /* We can't support overriding of symbols on a fake descriptor.  */
   6290   if (fdh != NULL
   6291       && fdh->fake
   6292       && (fh->elf.root.type == bfd_link_hash_defined
   6293 	  || fh->elf.root.type == bfd_link_hash_defweak))
   6294     _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
   6295 
   6296   /* Transfer dynamic linking information to the function descriptor.  */
   6297   if (fdh != NULL)
   6298     {
   6299       fdh->elf.ref_regular |= fh->elf.ref_regular;
   6300       fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
   6301       fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
   6302       fdh->elf.non_got_ref |= fh->elf.non_got_ref;
   6303       fdh->elf.dynamic |= fh->elf.dynamic;
   6304       fdh->elf.needs_plt |= (fh->elf.needs_plt
   6305 			     || fh->elf.type == STT_FUNC
   6306 			     || fh->elf.type == STT_GNU_IFUNC);
   6307       move_plt_plist (fh, fdh);
   6308 
   6309       if (!fdh->elf.forced_local
   6310 	  && fh->elf.dynindx != -1)
   6311 	if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
   6312 	  return FALSE;
   6313     }
   6314 
   6315   /* Now that the info is on the function descriptor, clear the
   6316      function code sym info.  Any function code syms for which we
   6317      don't have a definition in a regular file, we force local.
   6318      This prevents a shared library from exporting syms that have
   6319      been imported from another library.  Function code syms that
   6320      are really in the library we must leave global to prevent the
   6321      linker dragging in a definition from a static library.  */
   6322   force_local = (!fh->elf.def_regular
   6323 		 || fdh == NULL
   6324 		 || !fdh->elf.def_regular
   6325 		 || fdh->elf.forced_local);
   6326   _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
   6327 
   6328   return TRUE;
   6329 }
   6330 
   6331 static const struct sfpr_def_parms save_res_funcs[] =
   6332   {
   6333     { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
   6334     { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
   6335     { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
   6336     { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
   6337     { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
   6338     { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
   6339     { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
   6340     { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
   6341     { "._savef", 14, 31, savefpr, savefpr1_tail },
   6342     { "._restf", 14, 31, restfpr, restfpr1_tail },
   6343     { "_savevr_", 20, 31, savevr, savevr_tail },
   6344     { "_restvr_", 20, 31, restvr, restvr_tail }
   6345   };
   6346 
   6347 /* Called near the start of bfd_elf_size_dynamic_sections.  We use
   6348    this hook to a) provide some gcc support functions, and b) transfer
   6349    dynamic linking information gathered so far on function code symbol
   6350    entries, to their corresponding function descriptor symbol entries.  */
   6351 
   6352 static bfd_boolean
   6353 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
   6354 			    struct bfd_link_info *info)
   6355 {
   6356   struct ppc_link_hash_table *htab;
   6357 
   6358   htab = ppc_hash_table (info);
   6359   if (htab == NULL)
   6360     return FALSE;
   6361 
   6362   /* Provide any missing _save* and _rest* functions.  */
   6363   if (htab->sfpr != NULL)
   6364     {
   6365       unsigned int i;
   6366 
   6367       htab->sfpr->size = 0;
   6368       for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
   6369 	if (!sfpr_define (info, &save_res_funcs[i], NULL))
   6370 	  return FALSE;
   6371       if (htab->sfpr->size == 0)
   6372 	htab->sfpr->flags |= SEC_EXCLUDE;
   6373     }
   6374 
   6375   if (bfd_link_relocatable (info))
   6376     return TRUE;
   6377 
   6378   if (htab->elf.hgot != NULL)
   6379     {
   6380       _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
   6381       /* Make .TOC. defined so as to prevent it being made dynamic.
   6382 	 The wrong value here is fixed later in ppc64_elf_set_toc.  */
   6383       if (!htab->elf.hgot->def_regular
   6384 	  || htab->elf.hgot->root.type != bfd_link_hash_defined)
   6385 	{
   6386 	  htab->elf.hgot->root.type = bfd_link_hash_defined;
   6387 	  htab->elf.hgot->root.u.def.value = 0;
   6388 	  htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
   6389 	  htab->elf.hgot->def_regular = 1;
   6390 	  htab->elf.hgot->root.linker_def = 1;
   6391 	}
   6392       htab->elf.hgot->type = STT_OBJECT;
   6393       htab->elf.hgot->other
   6394 	= (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
   6395     }
   6396 
   6397   if (htab->need_func_desc_adj)
   6398     {
   6399       elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
   6400       htab->need_func_desc_adj = 0;
   6401     }
   6402 
   6403   return TRUE;
   6404 }
   6405 
   6406 /* Return true if we have dynamic relocs against H or any of its weak
   6407    aliases, that apply to read-only sections.  Cannot be used after
   6408    size_dynamic_sections.  */
   6409 
   6410 static bfd_boolean
   6411 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
   6412 {
   6413   struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
   6414   do
   6415     {
   6416       if (_bfd_elf_readonly_dynrelocs (&eh->elf))
   6417 	return TRUE;
   6418       eh = ppc_elf_hash_entry (eh->elf.u.alias);
   6419     }
   6420   while (eh != NULL && &eh->elf != h);
   6421 
   6422   return FALSE;
   6423 }
   6424 
   6425 /* Return whether EH has pc-relative dynamic relocs.  */
   6426 
   6427 static bfd_boolean
   6428 pc_dynrelocs (struct ppc_link_hash_entry *eh)
   6429 {
   6430   struct elf_dyn_relocs *p;
   6431 
   6432   for (p = eh->elf.dyn_relocs; p != NULL; p = p->next)
   6433     if (p->pc_count != 0)
   6434       return TRUE;
   6435   return FALSE;
   6436 }
   6437 
   6438 /* Return true if a global entry stub will be created for H.  Valid
   6439    for ELFv2 before plt entries have been allocated.  */
   6440 
   6441 static bfd_boolean
   6442 global_entry_stub (struct elf_link_hash_entry *h)
   6443 {
   6444   struct plt_entry *pent;
   6445 
   6446   if (!h->pointer_equality_needed
   6447       || h->def_regular)
   6448     return FALSE;
   6449 
   6450   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
   6451     if (pent->plt.refcount > 0
   6452 	&& pent->addend == 0)
   6453       return TRUE;
   6454 
   6455   return FALSE;
   6456 }
   6457 
   6458 /* Adjust a symbol defined by a dynamic object and referenced by a
   6459    regular object.  The current definition is in some section of the
   6460    dynamic object, but we're not including those sections.  We have to
   6461    change the definition to something the rest of the link can
   6462    understand.  */
   6463 
   6464 static bfd_boolean
   6465 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
   6466 				 struct elf_link_hash_entry *h)
   6467 {
   6468   struct ppc_link_hash_table *htab;
   6469   asection *s, *srel;
   6470 
   6471   htab = ppc_hash_table (info);
   6472   if (htab == NULL)
   6473     return FALSE;
   6474 
   6475   /* Deal with function syms.  */
   6476   if (h->type == STT_FUNC
   6477       || h->type == STT_GNU_IFUNC
   6478       || h->needs_plt)
   6479     {
   6480       bfd_boolean local = (ppc_elf_hash_entry (h)->save_res
   6481 			   || SYMBOL_CALLS_LOCAL (info, h)
   6482 			   || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
   6483       /* Discard dyn_relocs when non-pic if we've decided that a
   6484 	 function symbol is local and not an ifunc.  We keep dynamic
   6485 	 relocs for ifuncs when local rather than always emitting a
   6486 	 plt call stub for them and defining the symbol on the call
   6487 	 stub.  We can't do that for ELFv1 anyway (a function symbol
   6488 	 is defined on a descriptor, not code) and it can be faster at
   6489 	 run-time due to not needing to bounce through a stub.  The
   6490 	 dyn_relocs for ifuncs will be applied even in a static
   6491 	 executable.  */
   6492       if (!bfd_link_pic (info)
   6493 	  && h->type != STT_GNU_IFUNC
   6494 	  && local)
   6495 	h->dyn_relocs = NULL;
   6496 
   6497       /* Clear procedure linkage table information for any symbol that
   6498 	 won't need a .plt entry.  */
   6499       struct plt_entry *ent;
   6500       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
   6501 	if (ent->plt.refcount > 0)
   6502 	  break;
   6503       if (ent == NULL
   6504 	  || (h->type != STT_GNU_IFUNC
   6505 	      && local
   6506 	      && (htab->can_convert_all_inline_plt
   6507 		  || (ppc_elf_hash_entry (h)->tls_mask
   6508 		      & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
   6509 	{
   6510 	  h->plt.plist = NULL;
   6511 	  h->needs_plt = 0;
   6512 	  h->pointer_equality_needed = 0;
   6513 	}
   6514       else if (abiversion (info->output_bfd) >= 2)
   6515 	{
   6516 	  /* Taking a function's address in a read/write section
   6517 	     doesn't require us to define the function symbol in the
   6518 	     executable on a global entry stub.  A dynamic reloc can
   6519 	     be used instead.  The reason we prefer a few more dynamic
   6520 	     relocs is that calling via a global entry stub costs a
   6521 	     few more instructions, and pointer_equality_needed causes
   6522 	     extra work in ld.so when resolving these symbols.  */
   6523 	  if (global_entry_stub (h))
   6524 	    {
   6525 	      if (!_bfd_elf_readonly_dynrelocs (h))
   6526 		{
   6527 		  h->pointer_equality_needed = 0;
   6528 		  /* If we haven't seen a branch reloc and the symbol
   6529 		     isn't an ifunc then we don't need a plt entry.  */
   6530 		  if (!h->needs_plt)
   6531 		    h->plt.plist = NULL;
   6532 		}
   6533 	      else if (!bfd_link_pic (info))
   6534 		/* We are going to be defining the function symbol on the
   6535 		   plt stub, so no dyn_relocs needed when non-pic.  */
   6536 		h->dyn_relocs = NULL;
   6537 	    }
   6538 
   6539 	  /* ELFv2 function symbols can't have copy relocs.  */
   6540 	  return TRUE;
   6541 	}
   6542       else if (!h->needs_plt
   6543 	       && !_bfd_elf_readonly_dynrelocs (h))
   6544 	{
   6545 	  /* If we haven't seen a branch reloc and the symbol isn't an
   6546 	     ifunc then we don't need a plt entry.  */
   6547 	  h->plt.plist = NULL;
   6548 	  h->pointer_equality_needed = 0;
   6549 	  return TRUE;
   6550 	}
   6551     }
   6552   else
   6553     h->plt.plist = NULL;
   6554 
   6555   /* If this is a weak symbol, and there is a real definition, the
   6556      processor independent code will have arranged for us to see the
   6557      real definition first, and we can just use the same value.  */
   6558   if (h->is_weakalias)
   6559     {
   6560       struct elf_link_hash_entry *def = weakdef (h);
   6561       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
   6562       h->root.u.def.section = def->root.u.def.section;
   6563       h->root.u.def.value = def->root.u.def.value;
   6564       if (def->root.u.def.section == htab->elf.sdynbss
   6565 	  || def->root.u.def.section == htab->elf.sdynrelro)
   6566 	h->dyn_relocs = NULL;
   6567       return TRUE;
   6568     }
   6569 
   6570   /* If we are creating a shared library, we must presume that the
   6571      only references to the symbol are via the global offset table.
   6572      For such cases we need not do anything here; the relocations will
   6573      be handled correctly by relocate_section.  */
   6574   if (!bfd_link_executable (info))
   6575     return TRUE;
   6576 
   6577   /* If there are no references to this symbol that do not use the
   6578      GOT, we don't need to generate a copy reloc.  */
   6579   if (!h->non_got_ref)
   6580     return TRUE;
   6581 
   6582   /* Don't generate a copy reloc for symbols defined in the executable.  */
   6583   if (!h->def_dynamic || !h->ref_regular || h->def_regular
   6584 
   6585       /* If -z nocopyreloc was given, don't generate them either.  */
   6586       || info->nocopyreloc
   6587 
   6588       /* If we don't find any dynamic relocs in read-only sections, then
   6589 	 we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
   6590       || (ELIMINATE_COPY_RELOCS
   6591 	  && !h->needs_copy
   6592 	  && !alias_readonly_dynrelocs (h))
   6593 
   6594       /* Protected variables do not work with .dynbss.  The copy in
   6595 	 .dynbss won't be used by the shared library with the protected
   6596 	 definition for the variable.  Text relocations are preferable
   6597 	 to an incorrect program.  */
   6598       || h->protected_def)
   6599     return TRUE;
   6600 
   6601   if (h->type == STT_FUNC
   6602       || h->type == STT_GNU_IFUNC)
   6603     {
   6604       /* .dynbss copies of function symbols only work if we have
   6605 	 ELFv1 dot-symbols.  ELFv1 compilers since 2004 default to not
   6606 	 use dot-symbols and set the function symbol size to the text
   6607 	 size of the function rather than the size of the descriptor.
   6608 	 That's wrong for copying a descriptor.  */
   6609       if (ppc_elf_hash_entry (h)->oh == NULL
   6610 	  || !(h->size == 24 || h->size == 16))
   6611 	return TRUE;
   6612 
   6613       /* We should never get here, but unfortunately there are old
   6614 	 versions of gcc (circa gcc-3.2) that improperly for the
   6615 	 ELFv1 ABI put initialized function pointers, vtable refs and
   6616 	 suchlike in read-only sections.  Allow them to proceed, but
   6617 	 warn that this might break at runtime.  */
   6618       info->callbacks->einfo
   6619 	(_("%P: copy reloc against `%pT' requires lazy plt linking; "
   6620 	   "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
   6621 	 h->root.root.string);
   6622     }
   6623 
   6624   /* This is a reference to a symbol defined by a dynamic object which
   6625      is not a function.  */
   6626 
   6627   /* We must allocate the symbol in our .dynbss section, which will
   6628      become part of the .bss section of the executable.  There will be
   6629      an entry for this symbol in the .dynsym section.  The dynamic
   6630      object will contain position independent code, so all references
   6631      from the dynamic object to this symbol will go through the global
   6632      offset table.  The dynamic linker will use the .dynsym entry to
   6633      determine the address it must put in the global offset table, so
   6634      both the dynamic object and the regular object will refer to the
   6635      same memory location for the variable.  */
   6636   if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
   6637     {
   6638       s = htab->elf.sdynrelro;
   6639       srel = htab->elf.sreldynrelro;
   6640     }
   6641   else
   6642     {
   6643       s = htab->elf.sdynbss;
   6644       srel = htab->elf.srelbss;
   6645     }
   6646   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
   6647     {
   6648       /* We must generate a R_PPC64_COPY reloc to tell the dynamic
   6649 	 linker to copy the initial value out of the dynamic object
   6650 	 and into the runtime process image.  */
   6651       srel->size += sizeof (Elf64_External_Rela);
   6652       h->needs_copy = 1;
   6653     }
   6654 
   6655   /* We no longer want dyn_relocs.  */
   6656   h->dyn_relocs = NULL;
   6657   return _bfd_elf_adjust_dynamic_copy (info, h, s);
   6658 }
   6659 
   6660 /* If given a function descriptor symbol, hide both the function code
   6661    sym and the descriptor.  */
   6662 static void
   6663 ppc64_elf_hide_symbol (struct bfd_link_info *info,
   6664 		       struct elf_link_hash_entry *h,
   6665 		       bfd_boolean force_local)
   6666 {
   6667   struct ppc_link_hash_entry *eh;
   6668   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
   6669 
   6670   if (ppc_hash_table (info) == NULL)
   6671     return;
   6672 
   6673   eh = ppc_elf_hash_entry (h);
   6674   if (eh->is_func_descriptor)
   6675     {
   6676       struct ppc_link_hash_entry *fh = eh->oh;
   6677 
   6678       if (fh == NULL)
   6679 	{
   6680 	  const char *p, *q;
   6681 	  struct elf_link_hash_table *htab = elf_hash_table (info);
   6682 	  char save;
   6683 
   6684 	  /* We aren't supposed to use alloca in BFD because on
   6685 	     systems which do not have alloca the version in libiberty
   6686 	     calls xmalloc, which might cause the program to crash
   6687 	     when it runs out of memory.  This function doesn't have a
   6688 	     return status, so there's no way to gracefully return an
   6689 	     error.  So cheat.  We know that string[-1] can be safely
   6690 	     accessed;  It's either a string in an ELF string table,
   6691 	     or allocated in an objalloc structure.  */
   6692 
   6693 	  p = eh->elf.root.root.string - 1;
   6694 	  save = *p;
   6695 	  *(char *) p = '.';
   6696 	  fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, FALSE,
   6697 							 FALSE, FALSE));
   6698 	  *(char *) p = save;
   6699 
   6700 	  /* Unfortunately, if it so happens that the string we were
   6701 	     looking for was allocated immediately before this string,
   6702 	     then we overwrote the string terminator.  That's the only
   6703 	     reason the lookup should fail.  */
   6704 	  if (fh == NULL)
   6705 	    {
   6706 	      q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
   6707 	      while (q >= eh->elf.root.root.string && *q == *p)
   6708 		--q, --p;
   6709 	      if (q < eh->elf.root.root.string && *p == '.')
   6710 		fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, FALSE,
   6711 							       FALSE, FALSE));
   6712 	    }
   6713 	  if (fh != NULL)
   6714 	    {
   6715 	      eh->oh = fh;
   6716 	      fh->oh = eh;
   6717 	    }
   6718 	}
   6719       if (fh != NULL)
   6720 	_bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
   6721     }
   6722 }
   6723 
   6724 static bfd_boolean
   6725 get_sym_h (struct elf_link_hash_entry **hp,
   6726 	   Elf_Internal_Sym **symp,
   6727 	   asection **symsecp,
   6728 	   unsigned char **tls_maskp,
   6729 	   Elf_Internal_Sym **locsymsp,
   6730 	   unsigned long r_symndx,
   6731 	   bfd *ibfd)
   6732 {
   6733   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
   6734 
   6735   if (r_symndx >= symtab_hdr->sh_info)
   6736     {
   6737       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
   6738       struct elf_link_hash_entry *h;
   6739 
   6740       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   6741       h = elf_follow_link (h);
   6742 
   6743       if (hp != NULL)
   6744 	*hp = h;
   6745 
   6746       if (symp != NULL)
   6747 	*symp = NULL;
   6748 
   6749       if (symsecp != NULL)
   6750 	{
   6751 	  asection *symsec = NULL;
   6752 	  if (h->root.type == bfd_link_hash_defined
   6753 	      || h->root.type == bfd_link_hash_defweak)
   6754 	    symsec = h->root.u.def.section;
   6755 	  *symsecp = symsec;
   6756 	}
   6757 
   6758       if (tls_maskp != NULL)
   6759 	*tls_maskp = &ppc_elf_hash_entry (h)->tls_mask;
   6760     }
   6761   else
   6762     {
   6763       Elf_Internal_Sym *sym;
   6764       Elf_Internal_Sym *locsyms = *locsymsp;
   6765 
   6766       if (locsyms == NULL)
   6767 	{
   6768 	  locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
   6769 	  if (locsyms == NULL)
   6770 	    locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
   6771 					    symtab_hdr->sh_info,
   6772 					    0, NULL, NULL, NULL);
   6773 	  if (locsyms == NULL)
   6774 	    return FALSE;
   6775 	  *locsymsp = locsyms;
   6776 	}
   6777       sym = locsyms + r_symndx;
   6778 
   6779       if (hp != NULL)
   6780 	*hp = NULL;
   6781 
   6782       if (symp != NULL)
   6783 	*symp = sym;
   6784 
   6785       if (symsecp != NULL)
   6786 	*symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
   6787 
   6788       if (tls_maskp != NULL)
   6789 	{
   6790 	  struct got_entry **lgot_ents;
   6791 	  unsigned char *tls_mask;
   6792 
   6793 	  tls_mask = NULL;
   6794 	  lgot_ents = elf_local_got_ents (ibfd);
   6795 	  if (lgot_ents != NULL)
   6796 	    {
   6797 	      struct plt_entry **local_plt = (struct plt_entry **)
   6798 		(lgot_ents + symtab_hdr->sh_info);
   6799 	      unsigned char *lgot_masks = (unsigned char *)
   6800 		(local_plt + symtab_hdr->sh_info);
   6801 	      tls_mask = &lgot_masks[r_symndx];
   6802 	    }
   6803 	  *tls_maskp = tls_mask;
   6804 	}
   6805     }
   6806   return TRUE;
   6807 }
   6808 
   6809 /* Returns TLS_MASKP for the given REL symbol.  Function return is 0 on
   6810    error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
   6811    type suitable for optimization, and 1 otherwise.  */
   6812 
   6813 static int
   6814 get_tls_mask (unsigned char **tls_maskp,
   6815 	      unsigned long *toc_symndx,
   6816 	      bfd_vma *toc_addend,
   6817 	      Elf_Internal_Sym **locsymsp,
   6818 	      const Elf_Internal_Rela *rel,
   6819 	      bfd *ibfd)
   6820 {
   6821   unsigned long r_symndx;
   6822   int next_r;
   6823   struct elf_link_hash_entry *h;
   6824   Elf_Internal_Sym *sym;
   6825   asection *sec;
   6826   bfd_vma off;
   6827 
   6828   r_symndx = ELF64_R_SYM (rel->r_info);
   6829   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
   6830     return 0;
   6831 
   6832   if ((*tls_maskp != NULL
   6833        && (**tls_maskp & TLS_TLS) != 0
   6834        && **tls_maskp != (TLS_TLS | TLS_MARK))
   6835       || sec == NULL
   6836       || ppc64_elf_section_data (sec) == NULL
   6837       || ppc64_elf_section_data (sec)->sec_type != sec_toc)
   6838     return 1;
   6839 
   6840   /* Look inside a TOC section too.  */
   6841   if (h != NULL)
   6842     {
   6843       BFD_ASSERT (h->root.type == bfd_link_hash_defined);
   6844       off = h->root.u.def.value;
   6845     }
   6846   else
   6847     off = sym->st_value;
   6848   off += rel->r_addend;
   6849   BFD_ASSERT (off % 8 == 0);
   6850   r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
   6851   next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
   6852   if (toc_symndx != NULL)
   6853     *toc_symndx = r_symndx;
   6854   if (toc_addend != NULL)
   6855     *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
   6856   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
   6857     return 0;
   6858   if ((h == NULL || is_static_defined (h))
   6859       && (next_r == -1 || next_r == -2))
   6860     return 1 - next_r;
   6861   return 1;
   6862 }
   6863 
   6864 /* Find (or create) an entry in the tocsave hash table.  */
   6865 
   6866 static struct tocsave_entry *
   6867 tocsave_find (struct ppc_link_hash_table *htab,
   6868 	      enum insert_option insert,
   6869 	      Elf_Internal_Sym **local_syms,
   6870 	      const Elf_Internal_Rela *irela,
   6871 	      bfd *ibfd)
   6872 {
   6873   unsigned long r_indx;
   6874   struct elf_link_hash_entry *h;
   6875   Elf_Internal_Sym *sym;
   6876   struct tocsave_entry ent, *p;
   6877   hashval_t hash;
   6878   struct tocsave_entry **slot;
   6879 
   6880   r_indx = ELF64_R_SYM (irela->r_info);
   6881   if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
   6882     return NULL;
   6883   if (ent.sec == NULL || ent.sec->output_section == NULL)
   6884     {
   6885       _bfd_error_handler
   6886 	(_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
   6887       return NULL;
   6888     }
   6889 
   6890   if (h != NULL)
   6891     ent.offset = h->root.u.def.value;
   6892   else
   6893     ent.offset = sym->st_value;
   6894   ent.offset += irela->r_addend;
   6895 
   6896   hash = tocsave_htab_hash (&ent);
   6897   slot = ((struct tocsave_entry **)
   6898 	  htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
   6899   if (slot == NULL)
   6900     return NULL;
   6901 
   6902   if (*slot == NULL)
   6903     {
   6904       p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
   6905       if (p == NULL)
   6906 	return NULL;
   6907       *p = ent;
   6908       *slot = p;
   6909     }
   6910   return *slot;
   6911 }
   6912 
   6913 /* Adjust all global syms defined in opd sections.  In gcc generated
   6914    code for the old ABI, these will already have been done.  */
   6915 
   6916 static bfd_boolean
   6917 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
   6918 {
   6919   struct ppc_link_hash_entry *eh;
   6920   asection *sym_sec;
   6921   struct _opd_sec_data *opd;
   6922 
   6923   if (h->root.type == bfd_link_hash_indirect)
   6924     return TRUE;
   6925 
   6926   if (h->root.type != bfd_link_hash_defined
   6927       && h->root.type != bfd_link_hash_defweak)
   6928     return TRUE;
   6929 
   6930   eh = ppc_elf_hash_entry (h);
   6931   if (eh->adjust_done)
   6932     return TRUE;
   6933 
   6934   sym_sec = eh->elf.root.u.def.section;
   6935   opd = get_opd_info (sym_sec);
   6936   if (opd != NULL && opd->adjust != NULL)
   6937     {
   6938       long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
   6939       if (adjust == -1)
   6940 	{
   6941 	  /* This entry has been deleted.  */
   6942 	  asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
   6943 	  if (dsec == NULL)
   6944 	    {
   6945 	      for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
   6946 		if (discarded_section (dsec))
   6947 		  {
   6948 		    ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
   6949 		    break;
   6950 		  }
   6951 	    }
   6952 	  eh->elf.root.u.def.value = 0;
   6953 	  eh->elf.root.u.def.section = dsec;
   6954 	}
   6955       else
   6956 	eh->elf.root.u.def.value += adjust;
   6957       eh->adjust_done = 1;
   6958     }
   6959   return TRUE;
   6960 }
   6961 
   6962 /* Handles decrementing dynamic reloc counts for the reloc specified by
   6963    R_INFO in section SEC.  If LOCAL_SYMS is NULL, then H and SYM
   6964    have already been determined.  */
   6965 
   6966 static bfd_boolean
   6967 dec_dynrel_count (bfd_vma r_info,
   6968 		  asection *sec,
   6969 		  struct bfd_link_info *info,
   6970 		  Elf_Internal_Sym **local_syms,
   6971 		  struct elf_link_hash_entry *h,
   6972 		  Elf_Internal_Sym *sym)
   6973 {
   6974   enum elf_ppc64_reloc_type r_type;
   6975   asection *sym_sec = NULL;
   6976 
   6977   /* Can this reloc be dynamic?  This switch, and later tests here
   6978      should be kept in sync with the code in check_relocs.  */
   6979   r_type = ELF64_R_TYPE (r_info);
   6980   switch (r_type)
   6981     {
   6982     default:
   6983       return TRUE;
   6984 
   6985     case R_PPC64_TOC16:
   6986     case R_PPC64_TOC16_DS:
   6987     case R_PPC64_TOC16_LO:
   6988     case R_PPC64_TOC16_HI:
   6989     case R_PPC64_TOC16_HA:
   6990     case R_PPC64_TOC16_LO_DS:
   6991       if (h == NULL)
   6992 	return TRUE;
   6993       break;
   6994 
   6995     case R_PPC64_TPREL16:
   6996     case R_PPC64_TPREL16_LO:
   6997     case R_PPC64_TPREL16_HI:
   6998     case R_PPC64_TPREL16_HA:
   6999     case R_PPC64_TPREL16_DS:
   7000     case R_PPC64_TPREL16_LO_DS:
   7001     case R_PPC64_TPREL16_HIGH:
   7002     case R_PPC64_TPREL16_HIGHA:
   7003     case R_PPC64_TPREL16_HIGHER:
   7004     case R_PPC64_TPREL16_HIGHERA:
   7005     case R_PPC64_TPREL16_HIGHEST:
   7006     case R_PPC64_TPREL16_HIGHESTA:
   7007     case R_PPC64_TPREL64:
   7008     case R_PPC64_TPREL34:
   7009     case R_PPC64_DTPMOD64:
   7010     case R_PPC64_DTPREL64:
   7011     case R_PPC64_ADDR64:
   7012     case R_PPC64_REL30:
   7013     case R_PPC64_REL32:
   7014     case R_PPC64_REL64:
   7015     case R_PPC64_ADDR14:
   7016     case R_PPC64_ADDR14_BRNTAKEN:
   7017     case R_PPC64_ADDR14_BRTAKEN:
   7018     case R_PPC64_ADDR16:
   7019     case R_PPC64_ADDR16_DS:
   7020     case R_PPC64_ADDR16_HA:
   7021     case R_PPC64_ADDR16_HI:
   7022     case R_PPC64_ADDR16_HIGH:
   7023     case R_PPC64_ADDR16_HIGHA:
   7024     case R_PPC64_ADDR16_HIGHER:
   7025     case R_PPC64_ADDR16_HIGHERA:
   7026     case R_PPC64_ADDR16_HIGHEST:
   7027     case R_PPC64_ADDR16_HIGHESTA:
   7028     case R_PPC64_ADDR16_LO:
   7029     case R_PPC64_ADDR16_LO_DS:
   7030     case R_PPC64_ADDR24:
   7031     case R_PPC64_ADDR32:
   7032     case R_PPC64_UADDR16:
   7033     case R_PPC64_UADDR32:
   7034     case R_PPC64_UADDR64:
   7035     case R_PPC64_TOC:
   7036     case R_PPC64_D34:
   7037     case R_PPC64_D34_LO:
   7038     case R_PPC64_D34_HI30:
   7039     case R_PPC64_D34_HA30:
   7040     case R_PPC64_ADDR16_HIGHER34:
   7041     case R_PPC64_ADDR16_HIGHERA34:
   7042     case R_PPC64_ADDR16_HIGHEST34:
   7043     case R_PPC64_ADDR16_HIGHESTA34:
   7044     case R_PPC64_D28:
   7045       break;
   7046     }
   7047 
   7048   if (local_syms != NULL)
   7049     {
   7050       unsigned long r_symndx;
   7051       bfd *ibfd = sec->owner;
   7052 
   7053       r_symndx = ELF64_R_SYM (r_info);
   7054       if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
   7055 	return FALSE;
   7056     }
   7057 
   7058   if ((h != NULL
   7059        && (h->root.type == bfd_link_hash_defweak
   7060 	   || !h->def_regular))
   7061       || (h != NULL
   7062 	  && !bfd_link_executable (info)
   7063 	  && !SYMBOLIC_BIND (info, h))
   7064       || (bfd_link_pic (info)
   7065 	  && must_be_dyn_reloc (info, r_type))
   7066       || (!bfd_link_pic (info)
   7067 	  && (h != NULL
   7068 	      ? h->type == STT_GNU_IFUNC
   7069 	      : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
   7070     ;
   7071   else
   7072     return TRUE;
   7073 
   7074   if (h != NULL)
   7075     {
   7076       struct elf_dyn_relocs *p;
   7077       struct elf_dyn_relocs **pp;
   7078       pp = &h->dyn_relocs;
   7079 
   7080       /* elf_gc_sweep may have already removed all dyn relocs associated
   7081 	 with local syms for a given section.  Also, symbol flags are
   7082 	 changed by elf_gc_sweep_symbol, confusing the test above.  Don't
   7083 	 report a dynreloc miscount.  */
   7084       if (*pp == NULL && info->gc_sections)
   7085 	return TRUE;
   7086 
   7087       while ((p = *pp) != NULL)
   7088 	{
   7089 	  if (p->sec == sec)
   7090 	    {
   7091 	      if (!must_be_dyn_reloc (info, r_type))
   7092 		p->pc_count -= 1;
   7093 	      p->count -= 1;
   7094 	      if (p->count == 0)
   7095 		*pp = p->next;
   7096 	      return TRUE;
   7097 	    }
   7098 	  pp = &p->next;
   7099 	}
   7100     }
   7101   else
   7102     {
   7103       struct ppc_dyn_relocs *p;
   7104       struct ppc_dyn_relocs **pp;
   7105       void *vpp;
   7106       bfd_boolean is_ifunc;
   7107 
   7108       if (local_syms == NULL)
   7109 	sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
   7110       if (sym_sec == NULL)
   7111 	sym_sec = sec;
   7112 
   7113       vpp = &elf_section_data (sym_sec)->local_dynrel;
   7114       pp = (struct ppc_dyn_relocs **) vpp;
   7115 
   7116       if (*pp == NULL && info->gc_sections)
   7117 	return TRUE;
   7118 
   7119       is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
   7120       while ((p = *pp) != NULL)
   7121 	{
   7122 	  if (p->sec == sec && p->ifunc == is_ifunc)
   7123 	    {
   7124 	      p->count -= 1;
   7125 	      if (p->count == 0)
   7126 		*pp = p->next;
   7127 	      return TRUE;
   7128 	    }
   7129 	  pp = &p->next;
   7130 	}
   7131     }
   7132 
   7133   /* xgettext:c-format */
   7134   _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
   7135 		      sec->owner, sec);
   7136   bfd_set_error (bfd_error_bad_value);
   7137   return FALSE;
   7138 }
   7139 
   7140 /* Remove unused Official Procedure Descriptor entries.  Currently we
   7141    only remove those associated with functions in discarded link-once
   7142    sections, or weakly defined functions that have been overridden.  It
   7143    would be possible to remove many more entries for statically linked
   7144    applications.  */
   7145 
   7146 bfd_boolean
   7147 ppc64_elf_edit_opd (struct bfd_link_info *info)
   7148 {
   7149   bfd *ibfd;
   7150   bfd_boolean some_edited = FALSE;
   7151   asection *need_pad = NULL;
   7152   struct ppc_link_hash_table *htab;
   7153 
   7154   htab = ppc_hash_table (info);
   7155   if (htab == NULL)
   7156     return FALSE;
   7157 
   7158   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   7159     {
   7160       asection *sec;
   7161       Elf_Internal_Rela *relstart, *rel, *relend;
   7162       Elf_Internal_Shdr *symtab_hdr;
   7163       Elf_Internal_Sym *local_syms;
   7164       struct _opd_sec_data *opd;
   7165       bfd_boolean need_edit, add_aux_fields, broken;
   7166       bfd_size_type cnt_16b = 0;
   7167 
   7168       if (!is_ppc64_elf (ibfd))
   7169 	continue;
   7170 
   7171       sec = bfd_get_section_by_name (ibfd, ".opd");
   7172       if (sec == NULL || sec->size == 0)
   7173 	continue;
   7174 
   7175       if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   7176 	continue;
   7177 
   7178       if (sec->output_section == bfd_abs_section_ptr)
   7179 	continue;
   7180 
   7181       /* Look through the section relocs.  */
   7182       if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
   7183 	continue;
   7184 
   7185       local_syms = NULL;
   7186       symtab_hdr = &elf_symtab_hdr (ibfd);
   7187 
   7188       /* Read the relocations.  */
   7189       relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   7190 					    info->keep_memory);
   7191       if (relstart == NULL)
   7192 	return FALSE;
   7193 
   7194       /* First run through the relocs to check they are sane, and to
   7195 	 determine whether we need to edit this opd section.  */
   7196       need_edit = FALSE;
   7197       broken = FALSE;
   7198       need_pad = sec;
   7199       relend = relstart + sec->reloc_count;
   7200       for (rel = relstart; rel < relend; )
   7201 	{
   7202 	  enum elf_ppc64_reloc_type r_type;
   7203 	  unsigned long r_symndx;
   7204 	  asection *sym_sec;
   7205 	  struct elf_link_hash_entry *h;
   7206 	  Elf_Internal_Sym *sym;
   7207 	  bfd_vma offset;
   7208 
   7209 	  /* .opd contains an array of 16 or 24 byte entries.  We're
   7210 	     only interested in the reloc pointing to a function entry
   7211 	     point.  */
   7212 	  offset = rel->r_offset;
   7213 	  if (rel + 1 == relend
   7214 	      || rel[1].r_offset != offset + 8)
   7215 	    {
   7216 	      /* If someone messes with .opd alignment then after a
   7217 		 "ld -r" we might have padding in the middle of .opd.
   7218 		 Also, there's nothing to prevent someone putting
   7219 		 something silly in .opd with the assembler.  No .opd
   7220 		 optimization for them!  */
   7221 	    broken_opd:
   7222 	      _bfd_error_handler
   7223 		(_("%pB: .opd is not a regular array of opd entries"), ibfd);
   7224 	      broken = TRUE;
   7225 	      break;
   7226 	    }
   7227 
   7228 	  if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
   7229 	      || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
   7230 	    {
   7231 	      _bfd_error_handler
   7232 		/* xgettext:c-format */
   7233 		(_("%pB: unexpected reloc type %u in .opd section"),
   7234 		 ibfd, r_type);
   7235 	      broken = TRUE;
   7236 	      break;
   7237 	    }
   7238 
   7239 	  r_symndx = ELF64_R_SYM (rel->r_info);
   7240 	  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   7241 			  r_symndx, ibfd))
   7242 	    goto error_ret;
   7243 
   7244 	  if (sym_sec == NULL || sym_sec->owner == NULL)
   7245 	    {
   7246 	      const char *sym_name;
   7247 	      if (h != NULL)
   7248 		sym_name = h->root.root.string;
   7249 	      else
   7250 		sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
   7251 					     sym_sec);
   7252 
   7253 	      _bfd_error_handler
   7254 		/* xgettext:c-format */
   7255 		(_("%pB: undefined sym `%s' in .opd section"),
   7256 		 ibfd, sym_name);
   7257 	      broken = TRUE;
   7258 	      break;
   7259 	    }
   7260 
   7261 	  /* opd entries are always for functions defined in the
   7262 	     current input bfd.  If the symbol isn't defined in the
   7263 	     input bfd, then we won't be using the function in this
   7264 	     bfd;  It must be defined in a linkonce section in another
   7265 	     bfd, or is weak.  It's also possible that we are
   7266 	     discarding the function due to a linker script /DISCARD/,
   7267 	     which we test for via the output_section.  */
   7268 	  if (sym_sec->owner != ibfd
   7269 	      || sym_sec->output_section == bfd_abs_section_ptr)
   7270 	    need_edit = TRUE;
   7271 
   7272 	  rel += 2;
   7273 	  if (rel + 1 == relend
   7274 	      || (rel + 2 < relend
   7275 		  && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
   7276 	    ++rel;
   7277 
   7278 	  if (rel == relend)
   7279 	    {
   7280 	      if (sec->size == offset + 24)
   7281 		{
   7282 		  need_pad = NULL;
   7283 		  break;
   7284 		}
   7285 	      if (sec->size == offset + 16)
   7286 		{
   7287 		  cnt_16b++;
   7288 		  break;
   7289 		}
   7290 	      goto broken_opd;
   7291 	    }
   7292 	  else if (rel + 1 < relend
   7293 		   && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
   7294 		   && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
   7295 	    {
   7296 	      if (rel[0].r_offset == offset + 16)
   7297 		cnt_16b++;
   7298 	      else if (rel[0].r_offset != offset + 24)
   7299 		goto broken_opd;
   7300 	    }
   7301 	  else
   7302 	    goto broken_opd;
   7303 	}
   7304 
   7305       add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
   7306 
   7307       if (!broken && (need_edit || add_aux_fields))
   7308 	{
   7309 	  Elf_Internal_Rela *write_rel;
   7310 	  Elf_Internal_Shdr *rel_hdr;
   7311 	  bfd_byte *rptr, *wptr;
   7312 	  bfd_byte *new_contents;
   7313 	  bfd_size_type amt;
   7314 
   7315 	  new_contents = NULL;
   7316 	  amt = OPD_NDX (sec->size) * sizeof (long);
   7317 	  opd = &ppc64_elf_section_data (sec)->u.opd;
   7318 	  opd->adjust = bfd_zalloc (sec->owner, amt);
   7319 	  if (opd->adjust == NULL)
   7320 	    return FALSE;
   7321 
   7322 	  /* This seems a waste of time as input .opd sections are all
   7323 	     zeros as generated by gcc, but I suppose there's no reason
   7324 	     this will always be so.  We might start putting something in
   7325 	     the third word of .opd entries.  */
   7326 	  if ((sec->flags & SEC_IN_MEMORY) == 0)
   7327 	    {
   7328 	      bfd_byte *loc;
   7329 	      if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
   7330 		{
   7331 		  free (loc);
   7332 		error_ret:
   7333 		  if (symtab_hdr->contents != (unsigned char *) local_syms)
   7334 		    free (local_syms);
   7335 		  if (elf_section_data (sec)->relocs != relstart)
   7336 		    free (relstart);
   7337 		  return FALSE;
   7338 		}
   7339 	      sec->contents = loc;
   7340 	      sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
   7341 	    }
   7342 
   7343 	  elf_section_data (sec)->relocs = relstart;
   7344 
   7345 	  new_contents = sec->contents;
   7346 	  if (add_aux_fields)
   7347 	    {
   7348 	      new_contents = bfd_malloc (sec->size + cnt_16b * 8);
   7349 	      if (new_contents == NULL)
   7350 		return FALSE;
   7351 	      need_pad = NULL;
   7352 	    }
   7353 	  wptr = new_contents;
   7354 	  rptr = sec->contents;
   7355 	  write_rel = relstart;
   7356 	  for (rel = relstart; rel < relend; )
   7357 	    {
   7358 	      unsigned long r_symndx;
   7359 	      asection *sym_sec;
   7360 	      struct elf_link_hash_entry *h;
   7361 	      struct ppc_link_hash_entry *fdh = NULL;
   7362 	      Elf_Internal_Sym *sym;
   7363 	      long opd_ent_size;
   7364 	      Elf_Internal_Rela *next_rel;
   7365 	      bfd_boolean skip;
   7366 
   7367 	      r_symndx = ELF64_R_SYM (rel->r_info);
   7368 	      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   7369 			      r_symndx, ibfd))
   7370 		goto error_ret;
   7371 
   7372 	      next_rel = rel + 2;
   7373 	      if (next_rel + 1 == relend
   7374 		  || (next_rel + 2 < relend
   7375 		      && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
   7376 		++next_rel;
   7377 
   7378 	      /* See if the .opd entry is full 24 byte or
   7379 		 16 byte (with fd_aux entry overlapped with next
   7380 		 fd_func).  */
   7381 	      opd_ent_size = 24;
   7382 	      if (next_rel == relend)
   7383 		{
   7384 		  if (sec->size == rel->r_offset + 16)
   7385 		    opd_ent_size = 16;
   7386 		}
   7387 	      else if (next_rel->r_offset == rel->r_offset + 16)
   7388 		opd_ent_size = 16;
   7389 
   7390 	      if (h != NULL
   7391 		  && h->root.root.string[0] == '.')
   7392 		{
   7393 		  fdh = ppc_elf_hash_entry (h)->oh;
   7394 		  if (fdh != NULL)
   7395 		    {
   7396 		      fdh = ppc_follow_link (fdh);
   7397 		      if (fdh->elf.root.type != bfd_link_hash_defined
   7398 			  && fdh->elf.root.type != bfd_link_hash_defweak)
   7399 			fdh = NULL;
   7400 		    }
   7401 		}
   7402 
   7403 	      skip = (sym_sec->owner != ibfd
   7404 		      || sym_sec->output_section == bfd_abs_section_ptr);
   7405 	      if (skip)
   7406 		{
   7407 		  if (fdh != NULL && sym_sec->owner == ibfd)
   7408 		    {
   7409 		      /* Arrange for the function descriptor sym
   7410 			 to be dropped.  */
   7411 		      fdh->elf.root.u.def.value = 0;
   7412 		      fdh->elf.root.u.def.section = sym_sec;
   7413 		    }
   7414 		  opd->adjust[OPD_NDX (rel->r_offset)] = -1;
   7415 
   7416 		  if (NO_OPD_RELOCS || bfd_link_relocatable (info))
   7417 		    rel = next_rel;
   7418 		  else
   7419 		    while (1)
   7420 		      {
   7421 			if (!dec_dynrel_count (rel->r_info, sec, info,
   7422 					       NULL, h, sym))
   7423 			  goto error_ret;
   7424 
   7425 			if (++rel == next_rel)
   7426 			  break;
   7427 
   7428 			r_symndx = ELF64_R_SYM (rel->r_info);
   7429 			if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   7430 					r_symndx, ibfd))
   7431 			  goto error_ret;
   7432 		      }
   7433 		}
   7434 	      else
   7435 		{
   7436 		  /* We'll be keeping this opd entry.  */
   7437 		  long adjust;
   7438 
   7439 		  if (fdh != NULL)
   7440 		    {
   7441 		      /* Redefine the function descriptor symbol to
   7442 			 this location in the opd section.  It is
   7443 			 necessary to update the value here rather
   7444 			 than using an array of adjustments as we do
   7445 			 for local symbols, because various places
   7446 			 in the generic ELF code use the value
   7447 			 stored in u.def.value.  */
   7448 		      fdh->elf.root.u.def.value = wptr - new_contents;
   7449 		      fdh->adjust_done = 1;
   7450 		    }
   7451 
   7452 		  /* Local syms are a bit tricky.  We could
   7453 		     tweak them as they can be cached, but
   7454 		     we'd need to look through the local syms
   7455 		     for the function descriptor sym which we
   7456 		     don't have at the moment.  So keep an
   7457 		     array of adjustments.  */
   7458 		  adjust = (wptr - new_contents) - (rptr - sec->contents);
   7459 		  opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
   7460 
   7461 		  if (wptr != rptr)
   7462 		    memcpy (wptr, rptr, opd_ent_size);
   7463 		  wptr += opd_ent_size;
   7464 		  if (add_aux_fields && opd_ent_size == 16)
   7465 		    {
   7466 		      memset (wptr, '\0', 8);
   7467 		      wptr += 8;
   7468 		    }
   7469 
   7470 		  /* We need to adjust any reloc offsets to point to the
   7471 		     new opd entries.  */
   7472 		  for ( ; rel != next_rel; ++rel)
   7473 		    {
   7474 		      rel->r_offset += adjust;
   7475 		      if (write_rel != rel)
   7476 			memcpy (write_rel, rel, sizeof (*rel));
   7477 		      ++write_rel;
   7478 		    }
   7479 		}
   7480 
   7481 	      rptr += opd_ent_size;
   7482 	    }
   7483 
   7484 	  sec->size = wptr - new_contents;
   7485 	  sec->reloc_count = write_rel - relstart;
   7486 	  if (add_aux_fields)
   7487 	    {
   7488 	      free (sec->contents);
   7489 	      sec->contents = new_contents;
   7490 	    }
   7491 
   7492 	  /* Fudge the header size too, as this is used later in
   7493 	     elf_bfd_final_link if we are emitting relocs.  */
   7494 	  rel_hdr = _bfd_elf_single_rel_hdr (sec);
   7495 	  rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
   7496 	  some_edited = TRUE;
   7497 	}
   7498       else if (elf_section_data (sec)->relocs != relstart)
   7499 	free (relstart);
   7500 
   7501       if (local_syms != NULL
   7502 	  && symtab_hdr->contents != (unsigned char *) local_syms)
   7503 	{
   7504 	  if (!info->keep_memory)
   7505 	    free (local_syms);
   7506 	  else
   7507 	    symtab_hdr->contents = (unsigned char *) local_syms;
   7508 	}
   7509     }
   7510 
   7511   if (some_edited)
   7512     elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
   7513 
   7514   /* If we are doing a final link and the last .opd entry is just 16 byte
   7515      long, add a 8 byte padding after it.  */
   7516   if (need_pad != NULL && !bfd_link_relocatable (info))
   7517     {
   7518       bfd_byte *p;
   7519 
   7520       if ((need_pad->flags & SEC_IN_MEMORY) == 0)
   7521 	{
   7522 	  BFD_ASSERT (need_pad->size > 0);
   7523 
   7524 	  p = bfd_malloc (need_pad->size + 8);
   7525 	  if (p == NULL)
   7526 	    return FALSE;
   7527 
   7528 	  if (!bfd_get_section_contents (need_pad->owner, need_pad,
   7529 					 p, 0, need_pad->size))
   7530 	    return FALSE;
   7531 
   7532 	  need_pad->contents = p;
   7533 	  need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
   7534 	}
   7535       else
   7536 	{
   7537 	  p = bfd_realloc (need_pad->contents, need_pad->size + 8);
   7538 	  if (p == NULL)
   7539 	    return FALSE;
   7540 
   7541 	  need_pad->contents = p;
   7542 	}
   7543 
   7544       memset (need_pad->contents + need_pad->size, 0, 8);
   7545       need_pad->size += 8;
   7546     }
   7547 
   7548   return TRUE;
   7549 }
   7550 
   7551 /* Analyze inline PLT call relocations to see whether calls to locally
   7552    defined functions can be converted to direct calls.  */
   7553 
   7554 bfd_boolean
   7555 ppc64_elf_inline_plt (struct bfd_link_info *info)
   7556 {
   7557   struct ppc_link_hash_table *htab;
   7558   bfd *ibfd;
   7559   asection *sec;
   7560   bfd_vma low_vma, high_vma, limit;
   7561 
   7562   htab = ppc_hash_table (info);
   7563   if (htab == NULL)
   7564     return FALSE;
   7565 
   7566   /* A bl insn can reach -0x2000000 to 0x1fffffc.  The limit is
   7567      reduced somewhat to cater for possible stubs that might be added
   7568      between the call and its destination.  */
   7569   if (htab->params->group_size < 0)
   7570     {
   7571       limit = -htab->params->group_size;
   7572       if (limit == 1)
   7573 	limit = 0x1e00000;
   7574     }
   7575   else
   7576     {
   7577       limit = htab->params->group_size;
   7578       if (limit == 1)
   7579 	limit = 0x1c00000;
   7580     }
   7581 
   7582   low_vma = -1;
   7583   high_vma = 0;
   7584   for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
   7585     if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
   7586       {
   7587 	if (low_vma > sec->vma)
   7588 	  low_vma = sec->vma;
   7589 	if (high_vma < sec->vma + sec->size)
   7590 	  high_vma = sec->vma + sec->size;
   7591       }
   7592 
   7593   /* If a "bl" can reach anywhere in local code sections, then we can
   7594      convert all inline PLT sequences to direct calls when the symbol
   7595      is local.  */
   7596   if (high_vma - low_vma < limit)
   7597     {
   7598       htab->can_convert_all_inline_plt = 1;
   7599       return TRUE;
   7600     }
   7601 
   7602   /* Otherwise, go looking through relocs for cases where a direct
   7603      call won't reach.  Mark the symbol on any such reloc to disable
   7604      the optimization and keep the PLT entry as it seems likely that
   7605      this will be better than creating trampolines.  Note that this
   7606      will disable the optimization for all inline PLT calls to a
   7607      particular symbol, not just those that won't reach.  The
   7608      difficulty in doing a more precise optimization is that the
   7609      linker needs to make a decision depending on whether a
   7610      particular R_PPC64_PLTCALL insn can be turned into a direct
   7611      call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
   7612      the sequence, and there is nothing that ties those relocs
   7613      together except their symbol.  */
   7614 
   7615   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   7616     {
   7617       Elf_Internal_Shdr *symtab_hdr;
   7618       Elf_Internal_Sym *local_syms;
   7619 
   7620       if (!is_ppc64_elf (ibfd))
   7621 	continue;
   7622 
   7623       local_syms = NULL;
   7624       symtab_hdr = &elf_symtab_hdr (ibfd);
   7625 
   7626       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   7627 	if (ppc64_elf_section_data (sec)->has_pltcall
   7628 	    && !bfd_is_abs_section (sec->output_section))
   7629 	  {
   7630 	    Elf_Internal_Rela *relstart, *rel, *relend;
   7631 
   7632 	    /* Read the relocations.  */
   7633 	    relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   7634 						  info->keep_memory);
   7635 	    if (relstart == NULL)
   7636 	      return FALSE;
   7637 
   7638 	    relend = relstart + sec->reloc_count;
   7639 	    for (rel = relstart; rel < relend; rel++)
   7640 	      {
   7641 		enum elf_ppc64_reloc_type r_type;
   7642 		unsigned long r_symndx;
   7643 		asection *sym_sec;
   7644 		struct elf_link_hash_entry *h;
   7645 		Elf_Internal_Sym *sym;
   7646 		unsigned char *tls_maskp;
   7647 
   7648 		r_type = ELF64_R_TYPE (rel->r_info);
   7649 		if (r_type != R_PPC64_PLTCALL
   7650 		    && r_type != R_PPC64_PLTCALL_NOTOC)
   7651 		  continue;
   7652 
   7653 		r_symndx = ELF64_R_SYM (rel->r_info);
   7654 		if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
   7655 				r_symndx, ibfd))
   7656 		  {
   7657 		    if (elf_section_data (sec)->relocs != relstart)
   7658 		      free (relstart);
   7659 		    if (symtab_hdr->contents != (bfd_byte *) local_syms)
   7660 		      free (local_syms);
   7661 		    return FALSE;
   7662 		  }
   7663 
   7664 		if (sym_sec != NULL && sym_sec->output_section != NULL)
   7665 		  {
   7666 		    bfd_vma from, to;
   7667 		    if (h != NULL)
   7668 		      to = h->root.u.def.value;
   7669 		    else
   7670 		      to = sym->st_value;
   7671 		    to += (rel->r_addend
   7672 			   + sym_sec->output_offset
   7673 			   + sym_sec->output_section->vma);
   7674 		    from = (rel->r_offset
   7675 			    + sec->output_offset
   7676 			    + sec->output_section->vma);
   7677 		    if (to - from + limit < 2 * limit
   7678 			&& !(r_type == R_PPC64_PLTCALL_NOTOC
   7679 			     && (((h ? h->other : sym->st_other)
   7680 				  & STO_PPC64_LOCAL_MASK)
   7681 				 > 1 << STO_PPC64_LOCAL_BIT)))
   7682 		      *tls_maskp &= ~PLT_KEEP;
   7683 		  }
   7684 	      }
   7685 	    if (elf_section_data (sec)->relocs != relstart)
   7686 	      free (relstart);
   7687 	  }
   7688 
   7689       if (local_syms != NULL
   7690 	  && symtab_hdr->contents != (unsigned char *) local_syms)
   7691 	{
   7692 	  if (!info->keep_memory)
   7693 	    free (local_syms);
   7694 	  else
   7695 	    symtab_hdr->contents = (unsigned char *) local_syms;
   7696 	}
   7697     }
   7698 
   7699   return TRUE;
   7700 }
   7701 
   7702 /* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
   7703 
   7704 asection *
   7705 ppc64_elf_tls_setup (struct bfd_link_info *info)
   7706 {
   7707   struct ppc_link_hash_table *htab;
   7708   struct elf_link_hash_entry *tga, *tga_fd, *desc, *desc_fd;
   7709 
   7710   htab = ppc_hash_table (info);
   7711   if (htab == NULL)
   7712     return NULL;
   7713 
   7714   if (abiversion (info->output_bfd) == 1)
   7715     htab->opd_abi = 1;
   7716 
   7717   if (htab->params->no_multi_toc)
   7718     htab->do_multi_toc = 0;
   7719   else if (!htab->do_multi_toc)
   7720     htab->params->no_multi_toc = 1;
   7721 
   7722   /* Default to --no-plt-localentry, as this option can cause problems
   7723      with symbol interposition.  For example, glibc libpthread.so and
   7724      libc.so duplicate many pthread symbols, with a fallback
   7725      implementation in libc.so.  In some cases the fallback does more
   7726      work than the pthread implementation.  __pthread_condattr_destroy
   7727      is one such symbol: the libpthread.so implementation is
   7728      localentry:0 while the libc.so implementation is localentry:8.
   7729      An app that "cleverly" uses dlopen to only load necessary
   7730      libraries at runtime may omit loading libpthread.so when not
   7731      running multi-threaded, which then results in the libc.so
   7732      fallback symbols being used and ld.so complaining.  Now there
   7733      are workarounds in ld (see non_zero_localentry) to detect the
   7734      pthread situation, but that may not be the only case where
   7735      --plt-localentry can cause trouble.  */
   7736   if (htab->params->plt_localentry0 < 0)
   7737     htab->params->plt_localentry0 = 0;
   7738   if (htab->params->plt_localentry0
   7739       && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
   7740 			       FALSE, FALSE, FALSE) == NULL)
   7741     _bfd_error_handler
   7742       (_("warning: --plt-localentry is especially dangerous without "
   7743 	 "ld.so support to detect ABI violations"));
   7744 
   7745   tga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
   7746 			      FALSE, FALSE, TRUE);
   7747   htab->tls_get_addr = ppc_elf_hash_entry (tga);
   7748 
   7749   /* Move dynamic linking info to the function descriptor sym.  */
   7750   if (tga != NULL)
   7751     func_desc_adjust (tga, info);
   7752   tga_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
   7753 				 FALSE, FALSE, TRUE);
   7754   htab->tls_get_addr_fd = ppc_elf_hash_entry (tga_fd);
   7755 
   7756   desc = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_desc",
   7757 			       FALSE, FALSE, TRUE);
   7758   htab->tga_desc = ppc_elf_hash_entry (desc);
   7759   if (desc != NULL)
   7760     func_desc_adjust (desc, info);
   7761   desc_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_desc",
   7762 				  FALSE, FALSE, TRUE);
   7763   htab->tga_desc_fd = ppc_elf_hash_entry (desc_fd);
   7764 
   7765   if (htab->params->tls_get_addr_opt)
   7766     {
   7767       struct elf_link_hash_entry *opt, *opt_fd;
   7768 
   7769       opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
   7770 				  FALSE, FALSE, TRUE);
   7771       if (opt != NULL)
   7772 	func_desc_adjust (opt, info);
   7773       opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
   7774 				     FALSE, FALSE, TRUE);
   7775       if (opt_fd != NULL
   7776 	  && (opt_fd->root.type == bfd_link_hash_defined
   7777 	      || opt_fd->root.type == bfd_link_hash_defweak))
   7778 	{
   7779 	  /* If glibc supports an optimized __tls_get_addr call stub,
   7780 	     signalled by the presence of __tls_get_addr_opt, and we'll
   7781 	     be calling __tls_get_addr via a plt call stub, then
   7782 	     make __tls_get_addr point to __tls_get_addr_opt.  */
   7783 	  if (!(htab->elf.dynamic_sections_created
   7784 		&& tga_fd != NULL
   7785 		&& (tga_fd->type == STT_FUNC
   7786 		    || tga_fd->needs_plt)
   7787 		&& !(SYMBOL_CALLS_LOCAL (info, tga_fd)
   7788 		     || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd))))
   7789 	    tga_fd = NULL;
   7790 	  if (!(htab->elf.dynamic_sections_created
   7791 		&& desc_fd != NULL
   7792 		&& (desc_fd->type == STT_FUNC
   7793 		    || desc_fd->needs_plt)
   7794 		&& !(SYMBOL_CALLS_LOCAL (info, desc_fd)
   7795 		     || UNDEFWEAK_NO_DYNAMIC_RELOC (info, desc_fd))))
   7796 	    desc_fd = NULL;
   7797 
   7798 	  if (tga_fd != NULL || desc_fd != NULL)
   7799 	    {
   7800 	      struct plt_entry *ent = NULL;
   7801 
   7802 	      if (tga_fd != NULL)
   7803 		for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
   7804 		  if (ent->plt.refcount > 0)
   7805 		    break;
   7806 	      if (ent == NULL && desc_fd != NULL)
   7807 		for (ent = desc_fd->plt.plist; ent != NULL; ent = ent->next)
   7808 		  if (ent->plt.refcount > 0)
   7809 		    break;
   7810 	      if (ent != NULL)
   7811 		{
   7812 		  if (tga_fd != NULL)
   7813 		    {
   7814 		      tga_fd->root.type = bfd_link_hash_indirect;
   7815 		      tga_fd->root.u.i.link = &opt_fd->root;
   7816 		      tga_fd->root.u.i.warning = NULL;
   7817 		      ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
   7818 		    }
   7819 		  if (desc_fd != NULL)
   7820 		    {
   7821 		      desc_fd->root.type = bfd_link_hash_indirect;
   7822 		      desc_fd->root.u.i.link = &opt_fd->root;
   7823 		      desc_fd->root.u.i.warning = NULL;
   7824 		      ppc64_elf_copy_indirect_symbol (info, opt_fd, desc_fd);
   7825 		    }
   7826 		  opt_fd->mark = 1;
   7827 		  if (opt_fd->dynindx != -1)
   7828 		    {
   7829 		      /* Use __tls_get_addr_opt in dynamic relocations.  */
   7830 		      opt_fd->dynindx = -1;
   7831 		      _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
   7832 					      opt_fd->dynstr_index);
   7833 		      if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
   7834 			return NULL;
   7835 		    }
   7836 		  if (tga_fd != NULL)
   7837 		    {
   7838 		      htab->tls_get_addr_fd = ppc_elf_hash_entry (opt_fd);
   7839 		      tga = (struct elf_link_hash_entry *) htab->tls_get_addr;
   7840 		      if (opt != NULL && tga != NULL)
   7841 			{
   7842 			  tga->root.type = bfd_link_hash_indirect;
   7843 			  tga->root.u.i.link = &opt->root;
   7844 			  tga->root.u.i.warning = NULL;
   7845 			  ppc64_elf_copy_indirect_symbol (info, opt, tga);
   7846 			  opt->mark = 1;
   7847 			  _bfd_elf_link_hash_hide_symbol (info, opt,
   7848 							  tga->forced_local);
   7849 			  htab->tls_get_addr = ppc_elf_hash_entry (opt);
   7850 			}
   7851 		      htab->tls_get_addr_fd->oh = htab->tls_get_addr;
   7852 		      htab->tls_get_addr_fd->is_func_descriptor = 1;
   7853 		      if (htab->tls_get_addr != NULL)
   7854 			{
   7855 			  htab->tls_get_addr->oh = htab->tls_get_addr_fd;
   7856 			  htab->tls_get_addr->is_func = 1;
   7857 			}
   7858 		    }
   7859 		  if (desc_fd != NULL)
   7860 		    {
   7861 		      htab->tga_desc_fd = ppc_elf_hash_entry (opt_fd);
   7862 		      if (opt != NULL && desc != NULL)
   7863 			{
   7864 			  desc->root.type = bfd_link_hash_indirect;
   7865 			  desc->root.u.i.link = &opt->root;
   7866 			  desc->root.u.i.warning = NULL;
   7867 			  ppc64_elf_copy_indirect_symbol (info, opt, desc);
   7868 			  opt->mark = 1;
   7869 			  _bfd_elf_link_hash_hide_symbol (info, opt,
   7870 							  desc->forced_local);
   7871 			  htab->tga_desc = ppc_elf_hash_entry (opt);
   7872 			}
   7873 		      htab->tga_desc_fd->oh = htab->tga_desc;
   7874 		      htab->tga_desc_fd->is_func_descriptor = 1;
   7875 		      if (htab->tga_desc != NULL)
   7876 			{
   7877 			  htab->tga_desc->oh = htab->tga_desc_fd;
   7878 			  htab->tga_desc->is_func = 1;
   7879 			}
   7880 		    }
   7881 		}
   7882 	    }
   7883 	}
   7884       else if (htab->params->tls_get_addr_opt < 0)
   7885 	htab->params->tls_get_addr_opt = 0;
   7886     }
   7887 
   7888   if (htab->tga_desc_fd != NULL
   7889       && htab->params->tls_get_addr_opt
   7890       && htab->params->no_tls_get_addr_regsave == -1)
   7891     htab->params->no_tls_get_addr_regsave = 0;
   7892 
   7893   return _bfd_elf_tls_setup (info->output_bfd, info);
   7894 }
   7895 
   7896 /* Return TRUE iff REL is a branch reloc with a global symbol matching
   7897    any of HASH1, HASH2, HASH3, or HASH4.  */
   7898 
   7899 static bfd_boolean
   7900 branch_reloc_hash_match (const bfd *ibfd,
   7901 			 const Elf_Internal_Rela *rel,
   7902 			 const struct ppc_link_hash_entry *hash1,
   7903 			 const struct ppc_link_hash_entry *hash2,
   7904 			 const struct ppc_link_hash_entry *hash3,
   7905 			 const struct ppc_link_hash_entry *hash4)
   7906 {
   7907   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
   7908   enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
   7909   unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
   7910 
   7911   if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
   7912     {
   7913       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
   7914       struct elf_link_hash_entry *h;
   7915 
   7916       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   7917       h = elf_follow_link (h);
   7918       if (h == (struct elf_link_hash_entry *) hash1
   7919 	  || h == (struct elf_link_hash_entry *) hash2
   7920 	  || h == (struct elf_link_hash_entry *) hash3
   7921 	  || h == (struct elf_link_hash_entry *) hash4)
   7922 	return TRUE;
   7923     }
   7924   return FALSE;
   7925 }
   7926 
   7927 /* Run through all the TLS relocs looking for optimization
   7928    opportunities.  The linker has been hacked (see ppc64elf.em) to do
   7929    a preliminary section layout so that we know the TLS segment
   7930    offsets.  We can't optimize earlier because some optimizations need
   7931    to know the tp offset, and we need to optimize before allocating
   7932    dynamic relocations.  */
   7933 
   7934 bfd_boolean
   7935 ppc64_elf_tls_optimize (struct bfd_link_info *info)
   7936 {
   7937   bfd *ibfd;
   7938   asection *sec;
   7939   struct ppc_link_hash_table *htab;
   7940   unsigned char *toc_ref;
   7941   int pass;
   7942 
   7943   if (!bfd_link_executable (info))
   7944     return TRUE;
   7945 
   7946   htab = ppc_hash_table (info);
   7947   if (htab == NULL)
   7948     return FALSE;
   7949 
   7950   htab->do_tls_opt = 1;
   7951 
   7952   /* Make two passes over the relocs.  On the first pass, mark toc
   7953      entries involved with tls relocs, and check that tls relocs
   7954      involved in setting up a tls_get_addr call are indeed followed by
   7955      such a call.  If they are not, we can't do any tls optimization.
   7956      On the second pass twiddle tls_mask flags to notify
   7957      relocate_section that optimization can be done, and adjust got
   7958      and plt refcounts.  */
   7959   toc_ref = NULL;
   7960   for (pass = 0; pass < 2; ++pass)
   7961     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   7962       {
   7963 	Elf_Internal_Sym *locsyms = NULL;
   7964 	asection *toc = bfd_get_section_by_name (ibfd, ".toc");
   7965 
   7966 	for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   7967 	  if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
   7968 	    {
   7969 	      Elf_Internal_Rela *relstart, *rel, *relend;
   7970 	      bfd_boolean found_tls_get_addr_arg = 0;
   7971 
   7972 	      /* Read the relocations.  */
   7973 	      relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   7974 						    info->keep_memory);
   7975 	      if (relstart == NULL)
   7976 		{
   7977 		  free (toc_ref);
   7978 		  return FALSE;
   7979 		}
   7980 
   7981 	      relend = relstart + sec->reloc_count;
   7982 	      for (rel = relstart; rel < relend; rel++)
   7983 		{
   7984 		  enum elf_ppc64_reloc_type r_type;
   7985 		  unsigned long r_symndx;
   7986 		  struct elf_link_hash_entry *h;
   7987 		  Elf_Internal_Sym *sym;
   7988 		  asection *sym_sec;
   7989 		  unsigned char *tls_mask;
   7990 		  unsigned int tls_set, tls_clear, tls_type = 0;
   7991 		  bfd_vma value;
   7992 		  bfd_boolean ok_tprel, is_local;
   7993 		  long toc_ref_index = 0;
   7994 		  int expecting_tls_get_addr = 0;
   7995 		  bfd_boolean ret = FALSE;
   7996 
   7997 		  r_symndx = ELF64_R_SYM (rel->r_info);
   7998 		  if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
   7999 				  r_symndx, ibfd))
   8000 		    {
   8001 		    err_free_rel:
   8002 		      if (elf_section_data (sec)->relocs != relstart)
   8003 			free (relstart);
   8004 		      free (toc_ref);
   8005 		      if (elf_symtab_hdr (ibfd).contents
   8006 			  != (unsigned char *) locsyms)
   8007 			free (locsyms);
   8008 		      return ret;
   8009 		    }
   8010 
   8011 		  if (h != NULL)
   8012 		    {
   8013 		      if (h->root.type == bfd_link_hash_defined
   8014 			  || h->root.type == bfd_link_hash_defweak)
   8015 			value = h->root.u.def.value;
   8016 		      else if (h->root.type == bfd_link_hash_undefweak)
   8017 			value = 0;
   8018 		      else
   8019 			{
   8020 			  found_tls_get_addr_arg = 0;
   8021 			  continue;
   8022 			}
   8023 		    }
   8024 		  else
   8025 		    /* Symbols referenced by TLS relocs must be of type
   8026 		       STT_TLS.  So no need for .opd local sym adjust.  */
   8027 		    value = sym->st_value;
   8028 
   8029 		  ok_tprel = FALSE;
   8030 		  is_local = SYMBOL_REFERENCES_LOCAL (info, h);
   8031 		  if (is_local)
   8032 		    {
   8033 		      if (h != NULL
   8034 			  && h->root.type == bfd_link_hash_undefweak)
   8035 			ok_tprel = TRUE;
   8036 		      else if (sym_sec != NULL
   8037 			       && sym_sec->output_section != NULL)
   8038 			{
   8039 			  value += sym_sec->output_offset;
   8040 			  value += sym_sec->output_section->vma;
   8041 			  value -= htab->elf.tls_sec->vma + TP_OFFSET;
   8042 			  /* Note that even though the prefix insns
   8043 			     allow a 1<<33 offset we use the same test
   8044 			     as for addis;addi.  There may be a mix of
   8045 			     pcrel and non-pcrel code and the decision
   8046 			     to optimise is per symbol, not per TLS
   8047 			     sequence.  */
   8048 			  ok_tprel = value + 0x80008000ULL < 1ULL << 32;
   8049 			}
   8050 		    }
   8051 
   8052 		  r_type = ELF64_R_TYPE (rel->r_info);
   8053 		  /* If this section has old-style __tls_get_addr calls
   8054 		     without marker relocs, then check that each
   8055 		     __tls_get_addr call reloc is preceded by a reloc
   8056 		     that conceivably belongs to the __tls_get_addr arg
   8057 		     setup insn.  If we don't find matching arg setup
   8058 		     relocs, don't do any tls optimization.  */
   8059 		  if (pass == 0
   8060 		      && sec->nomark_tls_get_addr
   8061 		      && h != NULL
   8062 		      && is_tls_get_addr (h, htab)
   8063 		      && !found_tls_get_addr_arg
   8064 		      && is_branch_reloc (r_type))
   8065 		    {
   8066 		      info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
   8067 						"TLS optimization disabled\n"),
   8068 					      ibfd, sec, rel->r_offset);
   8069 		      ret = TRUE;
   8070 		      goto err_free_rel;
   8071 		    }
   8072 
   8073 		  found_tls_get_addr_arg = 0;
   8074 		  switch (r_type)
   8075 		    {
   8076 		    case R_PPC64_GOT_TLSLD16:
   8077 		    case R_PPC64_GOT_TLSLD16_LO:
   8078 		    case R_PPC64_GOT_TLSLD_PCREL34:
   8079 		      expecting_tls_get_addr = 1;
   8080 		      found_tls_get_addr_arg = 1;
   8081 		      /* Fall through.  */
   8082 
   8083 		    case R_PPC64_GOT_TLSLD16_HI:
   8084 		    case R_PPC64_GOT_TLSLD16_HA:
   8085 		      /* These relocs should never be against a symbol
   8086 			 defined in a shared lib.  Leave them alone if
   8087 			 that turns out to be the case.  */
   8088 		      if (!is_local)
   8089 			continue;
   8090 
   8091 		      /* LD -> LE */
   8092 		      tls_set = 0;
   8093 		      tls_clear = TLS_LD;
   8094 		      tls_type = TLS_TLS | TLS_LD;
   8095 		      break;
   8096 
   8097 		    case R_PPC64_GOT_TLSGD16:
   8098 		    case R_PPC64_GOT_TLSGD16_LO:
   8099 		    case R_PPC64_GOT_TLSGD_PCREL34:
   8100 		      expecting_tls_get_addr = 1;
   8101 		      found_tls_get_addr_arg = 1;
   8102 		      /* Fall through. */
   8103 
   8104 		    case R_PPC64_GOT_TLSGD16_HI:
   8105 		    case R_PPC64_GOT_TLSGD16_HA:
   8106 		      if (ok_tprel)
   8107 			/* GD -> LE */
   8108 			tls_set = 0;
   8109 		      else
   8110 			/* GD -> IE */
   8111 			tls_set = TLS_TLS | TLS_GDIE;
   8112 		      tls_clear = TLS_GD;
   8113 		      tls_type = TLS_TLS | TLS_GD;
   8114 		      break;
   8115 
   8116 		    case R_PPC64_GOT_TPREL_PCREL34:
   8117 		    case R_PPC64_GOT_TPREL16_DS:
   8118 		    case R_PPC64_GOT_TPREL16_LO_DS:
   8119 		    case R_PPC64_GOT_TPREL16_HI:
   8120 		    case R_PPC64_GOT_TPREL16_HA:
   8121 		      if (ok_tprel)
   8122 			{
   8123 			  /* IE -> LE */
   8124 			  tls_set = 0;
   8125 			  tls_clear = TLS_TPREL;
   8126 			  tls_type = TLS_TLS | TLS_TPREL;
   8127 			  break;
   8128 			}
   8129 		      continue;
   8130 
   8131 		    case R_PPC64_TLSLD:
   8132 		      if (!is_local)
   8133 			continue;
   8134 		      /* Fall through.  */
   8135 		    case R_PPC64_TLSGD:
   8136 		      if (rel + 1 < relend
   8137 			  && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
   8138 			{
   8139 			  if (pass != 0
   8140 			      && (ELF64_R_TYPE (rel[1].r_info)
   8141 				  != R_PPC64_PLTSEQ)
   8142 			      && (ELF64_R_TYPE (rel[1].r_info)
   8143 				  != R_PPC64_PLTSEQ_NOTOC))
   8144 			    {
   8145 			      r_symndx = ELF64_R_SYM (rel[1].r_info);
   8146 			      if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
   8147 					      r_symndx, ibfd))
   8148 				goto err_free_rel;
   8149 			      if (h != NULL)
   8150 				{
   8151 				  struct plt_entry *ent = NULL;
   8152 
   8153 				  for (ent = h->plt.plist;
   8154 				       ent != NULL;
   8155 				       ent = ent->next)
   8156 				    if (ent->addend == rel[1].r_addend)
   8157 				      break;
   8158 
   8159 				  if (ent != NULL
   8160 				      && ent->plt.refcount > 0)
   8161 				    ent->plt.refcount -= 1;
   8162 				}
   8163 			    }
   8164 			  continue;
   8165 			}
   8166 		      found_tls_get_addr_arg = 1;
   8167 		      /* Fall through.  */
   8168 
   8169 		    case R_PPC64_TLS:
   8170 		    case R_PPC64_TOC16:
   8171 		    case R_PPC64_TOC16_LO:
   8172 		      if (sym_sec == NULL || sym_sec != toc)
   8173 			continue;
   8174 
   8175 		      /* Mark this toc entry as referenced by a TLS
   8176 			 code sequence.  We can do that now in the
   8177 			 case of R_PPC64_TLS, and after checking for
   8178 			 tls_get_addr for the TOC16 relocs.  */
   8179 		      if (toc_ref == NULL)
   8180 			toc_ref
   8181 			  = bfd_zmalloc (toc->output_section->rawsize / 8);
   8182 		      if (toc_ref == NULL)
   8183 			goto err_free_rel;
   8184 
   8185 		      if (h != NULL)
   8186 			value = h->root.u.def.value;
   8187 		      else
   8188 			value = sym->st_value;
   8189 		      value += rel->r_addend;
   8190 		      if (value % 8 != 0)
   8191 			continue;
   8192 		      BFD_ASSERT (value < toc->size
   8193 				  && toc->output_offset % 8 == 0);
   8194 		      toc_ref_index = (value + toc->output_offset) / 8;
   8195 		      if (r_type == R_PPC64_TLS
   8196 			  || r_type == R_PPC64_TLSGD
   8197 			  || r_type == R_PPC64_TLSLD)
   8198 			{
   8199 			  toc_ref[toc_ref_index] = 1;
   8200 			  continue;
   8201 			}
   8202 
   8203 		      if (pass != 0 && toc_ref[toc_ref_index] == 0)
   8204 			continue;
   8205 
   8206 		      tls_set = 0;
   8207 		      tls_clear = 0;
   8208 		      expecting_tls_get_addr = 2;
   8209 		      break;
   8210 
   8211 		    case R_PPC64_TPREL64:
   8212 		      if (pass == 0
   8213 			  || sec != toc
   8214 			  || toc_ref == NULL
   8215 			  || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
   8216 			continue;
   8217 		      if (ok_tprel)
   8218 			{
   8219 			  /* IE -> LE */
   8220 			  tls_set = TLS_EXPLICIT;
   8221 			  tls_clear = TLS_TPREL;
   8222 			  break;
   8223 			}
   8224 		      continue;
   8225 
   8226 		    case R_PPC64_DTPMOD64:
   8227 		      if (pass == 0
   8228 			  || sec != toc
   8229 			  || toc_ref == NULL
   8230 			  || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
   8231 			continue;
   8232 		      if (rel + 1 < relend
   8233 			  && (rel[1].r_info
   8234 			      == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
   8235 			  && rel[1].r_offset == rel->r_offset + 8)
   8236 			{
   8237 			  if (ok_tprel)
   8238 			    /* GD -> LE */
   8239 			    tls_set = TLS_EXPLICIT | TLS_GD;
   8240 			  else
   8241 			    /* GD -> IE */
   8242 			    tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
   8243 			  tls_clear = TLS_GD;
   8244 			}
   8245 		      else
   8246 			{
   8247 			  if (!is_local)
   8248 			    continue;
   8249 
   8250 			  /* LD -> LE */
   8251 			  tls_set = TLS_EXPLICIT;
   8252 			  tls_clear = TLS_LD;
   8253 			}
   8254 		      break;
   8255 
   8256 		    case R_PPC64_TPREL16_HA:
   8257 		      if (pass == 0)
   8258 			{
   8259 			  unsigned char buf[4];
   8260 			  unsigned int insn;
   8261 			  bfd_vma off = rel->r_offset & ~3;
   8262 			  if (!bfd_get_section_contents (ibfd, sec, buf,
   8263 							 off, 4))
   8264 			    goto err_free_rel;
   8265 			  insn = bfd_get_32 (ibfd, buf);
   8266 			  /* addis rt,13,imm */
   8267 			  if ((insn & ((0x3fu << 26) | 0x1f << 16))
   8268 			      != ((15u << 26) | (13 << 16)))
   8269 			    {
   8270 			      /* xgettext:c-format */
   8271 			      info->callbacks->minfo
   8272 				(_("%H: warning: %s unexpected insn %#x.\n"),
   8273 				 ibfd, sec, off, "R_PPC64_TPREL16_HA", insn);
   8274 			      htab->do_tls_opt = 0;
   8275 			    }
   8276 			}
   8277 		      continue;
   8278 
   8279 		    case R_PPC64_TPREL16_HI:
   8280 		    case R_PPC64_TPREL16_HIGH:
   8281 		    case R_PPC64_TPREL16_HIGHA:
   8282 		    case R_PPC64_TPREL16_HIGHER:
   8283 		    case R_PPC64_TPREL16_HIGHERA:
   8284 		    case R_PPC64_TPREL16_HIGHEST:
   8285 		    case R_PPC64_TPREL16_HIGHESTA:
   8286 		      /* These can all be used in sequences along with
   8287 			 TPREL16_LO or TPREL16_LO_DS in ways we aren't
   8288 			 able to verify easily.  */
   8289 		      htab->do_tls_opt = 0;
   8290 		      continue;
   8291 
   8292 		    default:
   8293 		      continue;
   8294 		    }
   8295 
   8296 		  if (pass == 0)
   8297 		    {
   8298 		      if (!expecting_tls_get_addr
   8299 			  || !sec->nomark_tls_get_addr)
   8300 			continue;
   8301 
   8302 		      if (rel + 1 < relend
   8303 			  && branch_reloc_hash_match (ibfd, rel + 1,
   8304 						      htab->tls_get_addr_fd,
   8305 						      htab->tga_desc_fd,
   8306 						      htab->tls_get_addr,
   8307 						      htab->tga_desc))
   8308 			{
   8309 			  if (expecting_tls_get_addr == 2)
   8310 			    {
   8311 			      /* Check for toc tls entries.  */
   8312 			      unsigned char *toc_tls;
   8313 			      int retval;
   8314 
   8315 			      retval = get_tls_mask (&toc_tls, NULL, NULL,
   8316 						     &locsyms,
   8317 						     rel, ibfd);
   8318 			      if (retval == 0)
   8319 				goto err_free_rel;
   8320 			      if (toc_tls != NULL)
   8321 				{
   8322 				  if ((*toc_tls & TLS_TLS) != 0
   8323 				      && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
   8324 				    found_tls_get_addr_arg = 1;
   8325 				  if (retval > 1)
   8326 				    toc_ref[toc_ref_index] = 1;
   8327 				}
   8328 			    }
   8329 			  continue;
   8330 			}
   8331 
   8332 		      /* Uh oh, we didn't find the expected call.  We
   8333 			 could just mark this symbol to exclude it
   8334 			 from tls optimization but it's safer to skip
   8335 			 the entire optimization.  */
   8336 		      /* xgettext:c-format */
   8337 		      info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
   8338 						"TLS optimization disabled\n"),
   8339 					      ibfd, sec, rel->r_offset);
   8340 		      ret = TRUE;
   8341 		      goto err_free_rel;
   8342 		    }
   8343 
   8344 		  /* If we don't have old-style __tls_get_addr calls
   8345 		     without TLSGD/TLSLD marker relocs, and we haven't
   8346 		     found a new-style __tls_get_addr call with a
   8347 		     marker for this symbol, then we either have a
   8348 		     broken object file or an -mlongcall style
   8349 		     indirect call to __tls_get_addr without a marker.
   8350 		     Disable optimization in this case.  */
   8351 		  if ((tls_clear & (TLS_GD | TLS_LD)) != 0
   8352 		      && (tls_set & TLS_EXPLICIT) == 0
   8353 		      && !sec->nomark_tls_get_addr
   8354 		      && ((*tls_mask & (TLS_TLS | TLS_MARK))
   8355 			  != (TLS_TLS | TLS_MARK)))
   8356 		    continue;
   8357 
   8358 		  if (expecting_tls_get_addr == 1 + !sec->nomark_tls_get_addr)
   8359 		    {
   8360 		      struct plt_entry *ent = NULL;
   8361 
   8362 		      if (htab->tls_get_addr_fd != NULL)
   8363 			for (ent = htab->tls_get_addr_fd->elf.plt.plist;
   8364 			     ent != NULL;
   8365 			     ent = ent->next)
   8366 			  if (ent->addend == 0)
   8367 			    break;
   8368 
   8369 		      if (ent == NULL && htab->tga_desc_fd != NULL)
   8370 			for (ent = htab->tga_desc_fd->elf.plt.plist;
   8371 			     ent != NULL;
   8372 			     ent = ent->next)
   8373 			  if (ent->addend == 0)
   8374 			    break;
   8375 
   8376 		      if (ent == NULL && htab->tls_get_addr != NULL)
   8377 			for (ent = htab->tls_get_addr->elf.plt.plist;
   8378 			     ent != NULL;
   8379 			     ent = ent->next)
   8380 			  if (ent->addend == 0)
   8381 			    break;
   8382 
   8383 		      if (ent == NULL && htab->tga_desc != NULL)
   8384 			for (ent = htab->tga_desc->elf.plt.plist;
   8385 			     ent != NULL;
   8386 			     ent = ent->next)
   8387 			  if (ent->addend == 0)
   8388 			    break;
   8389 
   8390 		      if (ent != NULL
   8391 			  && ent->plt.refcount > 0)
   8392 			ent->plt.refcount -= 1;
   8393 		    }
   8394 
   8395 		  if (tls_clear == 0)
   8396 		    continue;
   8397 
   8398 		  if ((tls_set & TLS_EXPLICIT) == 0)
   8399 		    {
   8400 		      struct got_entry *ent;
   8401 
   8402 		      /* Adjust got entry for this reloc.  */
   8403 		      if (h != NULL)
   8404 			ent = h->got.glist;
   8405 		      else
   8406 			ent = elf_local_got_ents (ibfd)[r_symndx];
   8407 
   8408 		      for (; ent != NULL; ent = ent->next)
   8409 			if (ent->addend == rel->r_addend
   8410 			    && ent->owner == ibfd
   8411 			    && ent->tls_type == tls_type)
   8412 			  break;
   8413 		      if (ent == NULL)
   8414 			abort ();
   8415 
   8416 		      if (tls_set == 0)
   8417 			{
   8418 			  /* We managed to get rid of a got entry.  */
   8419 			  if (ent->got.refcount > 0)
   8420 			    ent->got.refcount -= 1;
   8421 			}
   8422 		    }
   8423 		  else
   8424 		    {
   8425 		      /* If we got rid of a DTPMOD/DTPREL reloc pair then
   8426 			 we'll lose one or two dyn relocs.  */
   8427 		      if (!dec_dynrel_count (rel->r_info, sec, info,
   8428 					     NULL, h, sym))
   8429 			return FALSE;
   8430 
   8431 		      if (tls_set == (TLS_EXPLICIT | TLS_GD))
   8432 			{
   8433 			  if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
   8434 						 NULL, h, sym))
   8435 			    return FALSE;
   8436 			}
   8437 		    }
   8438 
   8439 		  *tls_mask |= tls_set & 0xff;
   8440 		  *tls_mask &= ~tls_clear;
   8441 		}
   8442 
   8443 	      if (elf_section_data (sec)->relocs != relstart)
   8444 		free (relstart);
   8445 	    }
   8446 
   8447 	if (locsyms != NULL
   8448 	    && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
   8449 	  {
   8450 	    if (!info->keep_memory)
   8451 	      free (locsyms);
   8452 	    else
   8453 	      elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
   8454 	  }
   8455       }
   8456 
   8457   free (toc_ref);
   8458   return TRUE;
   8459 }
   8460 
   8461 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
   8462    the values of any global symbols in a toc section that has been
   8463    edited.  Globals in toc sections should be a rarity, so this function
   8464    sets a flag if any are found in toc sections other than the one just
   8465    edited, so that further hash table traversals can be avoided.  */
   8466 
   8467 struct adjust_toc_info
   8468 {
   8469   asection *toc;
   8470   unsigned long *skip;
   8471   bfd_boolean global_toc_syms;
   8472 };
   8473 
   8474 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
   8475 
   8476 static bfd_boolean
   8477 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
   8478 {
   8479   struct ppc_link_hash_entry *eh;
   8480   struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
   8481   unsigned long i;
   8482 
   8483   if (h->root.type != bfd_link_hash_defined
   8484       && h->root.type != bfd_link_hash_defweak)
   8485     return TRUE;
   8486 
   8487   eh = ppc_elf_hash_entry (h);
   8488   if (eh->adjust_done)
   8489     return TRUE;
   8490 
   8491   if (eh->elf.root.u.def.section == toc_inf->toc)
   8492     {
   8493       if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
   8494 	i = toc_inf->toc->rawsize >> 3;
   8495       else
   8496 	i = eh->elf.root.u.def.value >> 3;
   8497 
   8498       if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
   8499 	{
   8500 	  _bfd_error_handler
   8501 	    (_("%s defined on removed toc entry"), eh->elf.root.root.string);
   8502 	  do
   8503 	    ++i;
   8504 	  while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
   8505 	  eh->elf.root.u.def.value = (bfd_vma) i << 3;
   8506 	}
   8507 
   8508       eh->elf.root.u.def.value -= toc_inf->skip[i];
   8509       eh->adjust_done = 1;
   8510     }
   8511   else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
   8512     toc_inf->global_toc_syms = TRUE;
   8513 
   8514   return TRUE;
   8515 }
   8516 
   8517 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
   8518    on a _LO variety toc/got reloc.  */
   8519 
   8520 static bfd_boolean
   8521 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
   8522 {
   8523   return ((insn & (0x3fu << 26)) == 12u << 26 /* addic */
   8524 	  || (insn & (0x3fu << 26)) == 14u << 26 /* addi */
   8525 	  || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
   8526 	  || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
   8527 	  || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
   8528 	  || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
   8529 	  || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
   8530 	  || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
   8531 	  || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
   8532 	  || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
   8533 	  || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
   8534 	  || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
   8535 	  || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
   8536 	  || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
   8537 	  || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
   8538 	  || (insn & (0x3fu << 26)) == 56u << 26 /* lq,lfq */
   8539 	  || ((insn & (0x3fu << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
   8540 	      /* Exclude lfqu by testing reloc.  If relocs are ever
   8541 		 defined for the reduced D field in psq_lu then those
   8542 		 will need testing too.  */
   8543 	      && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
   8544 	  || ((insn & (0x3fu << 26)) == 58u << 26 /* ld,lwa */
   8545 	      && (insn & 1) == 0)
   8546 	  || (insn & (0x3fu << 26)) == 60u << 26 /* stfq */
   8547 	  || ((insn & (0x3fu << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
   8548 	      /* Exclude stfqu.  psq_stu as above for psq_lu.  */
   8549 	      && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
   8550 	  || ((insn & (0x3fu << 26)) == 62u << 26 /* std,stq */
   8551 	      && (insn & 1) == 0));
   8552 }
   8553 
   8554 /* PCREL_OPT in one instance flags to the linker that a pair of insns:
   8555      pld ra,symbol@got@pcrel
   8556      load/store rt,off(ra)
   8557    or
   8558      pla ra,symbol@pcrel
   8559      load/store rt,off(ra)
   8560    may be translated to
   8561      pload/pstore rt,symbol+off@pcrel
   8562      nop.
   8563    This function returns true if the optimization is possible, placing
   8564    the prefix insn in *PINSN1, a NOP in *PINSN2 and the offset in *POFF.
   8565 
   8566    On entry to this function, the linker has already determined that
   8567    the pld can be replaced with pla: *PINSN1 is that pla insn,
   8568    while *PINSN2 is the second instruction.  */
   8569 
   8570 static bfd_boolean
   8571 xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2, bfd_signed_vma *poff)
   8572 {
   8573   uint64_t insn1 = *pinsn1;
   8574   uint64_t insn2 = *pinsn2;
   8575   bfd_signed_vma off;
   8576 
   8577   if ((insn2 & (63ULL << 58)) == 1ULL << 58)
   8578     {
   8579       /* Check that regs match.  */
   8580       if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
   8581 	return FALSE;
   8582 
   8583       /* P8LS or PMLS form, non-pcrel.  */
   8584       if ((insn2 & (-1ULL << 50) & ~(1ULL << 56)) != (1ULL << 58))
   8585 	return FALSE;
   8586 
   8587       *pinsn1 = (insn2 & ~(31 << 16) & ~0x3ffff0000ffffULL) | (1ULL << 52);
   8588       *pinsn2 = PNOP;
   8589       off = ((insn2 >> 16) & 0x3ffff0000ULL) | (insn2 & 0xffff);
   8590       *poff = (off ^ 0x200000000ULL) - 0x200000000ULL;
   8591       return TRUE;
   8592     }
   8593 
   8594   insn2 >>= 32;
   8595 
   8596   /* Check that regs match.  */
   8597   if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
   8598     return FALSE;
   8599 
   8600   switch ((insn2 >> 26) & 63)
   8601     {
   8602     default:
   8603       return FALSE;
   8604 
   8605     case 32: /* lwz */
   8606     case 34: /* lbz */
   8607     case 36: /* stw */
   8608     case 38: /* stb */
   8609     case 40: /* lhz */
   8610     case 42: /* lha */
   8611     case 44: /* sth */
   8612     case 48: /* lfs */
   8613     case 50: /* lfd */
   8614     case 52: /* stfs */
   8615     case 54: /* stfd */
   8616       /* These are the PMLS cases, where we just need to tack a prefix
   8617 	 on the insn.  */
   8618       insn1 = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
   8619 	       | (insn2 & ((63ULL << 26) | (31ULL << 21))));
   8620       off = insn2 & 0xffff;
   8621       break;
   8622 
   8623     case 58: /* lwa, ld */
   8624       if ((insn2 & 1) != 0)
   8625 	return FALSE;
   8626       insn1 = ((1ULL << 58) | (1ULL << 52)
   8627 	       | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
   8628 	       | (insn2 & (31ULL << 21)));
   8629       off = insn2 & 0xfffc;
   8630       break;
   8631 
   8632     case 57: /* lxsd, lxssp */
   8633       if ((insn2 & 3) < 2)
   8634 	return FALSE;
   8635       insn1 = ((1ULL << 58) | (1ULL << 52)
   8636 	       | ((40ULL | (insn2 & 3)) << 26)
   8637 	       | (insn2 & (31ULL << 21)));
   8638       off = insn2 & 0xfffc;
   8639       break;
   8640 
   8641     case 61: /* stxsd, stxssp, lxv, stxv  */
   8642       if ((insn2 & 3) == 0)
   8643 	return FALSE;
   8644       else if ((insn2 & 3) >= 2)
   8645 	{
   8646 	  insn1 = ((1ULL << 58) | (1ULL << 52)
   8647 		   | ((44ULL | (insn2 & 3)) << 26)
   8648 		   | (insn2 & (31ULL << 21)));
   8649 	  off = insn2 & 0xfffc;
   8650 	}
   8651       else
   8652 	{
   8653 	  insn1 = ((1ULL << 58) | (1ULL << 52)
   8654 		   | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
   8655 		   | (insn2 & (31ULL << 21)));
   8656 	  off = insn2 & 0xfff0;
   8657 	}
   8658       break;
   8659 
   8660     case 56: /* lq */
   8661       insn1 = ((1ULL << 58) | (1ULL << 52)
   8662 	       | (insn2 & ((63ULL << 26) | (31ULL << 21))));
   8663       off = insn2 & 0xffff;
   8664       break;
   8665 
   8666     case 6: /* lxvp, stxvp */
   8667       if ((insn2 & 0xe) != 0)
   8668 	return FALSE;
   8669       insn1 = ((1ULL << 58) | (1ULL << 52)
   8670 	       | ((insn2 & 1) == 0 ? 58ULL << 26 : 62ULL << 26)
   8671 	       | (insn2 & (31ULL << 21)));
   8672       off = insn2 & 0xfff0;
   8673       break;
   8674 
   8675     case 62: /* std, stq */
   8676       if ((insn2 & 1) != 0)
   8677 	return FALSE;
   8678       insn1 = ((1ULL << 58) | (1ULL << 52)
   8679 	       | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
   8680 	       | (insn2 & (31ULL << 21)));
   8681       off = insn2 & 0xfffc;
   8682       break;
   8683     }
   8684 
   8685   *pinsn1 = insn1;
   8686   *pinsn2 = (uint64_t) NOP << 32;
   8687   *poff = (off ^ 0x8000) - 0x8000;
   8688   return TRUE;
   8689 }
   8690 
   8691 /* Examine all relocs referencing .toc sections in order to remove
   8692    unused .toc entries.  */
   8693 
   8694 bfd_boolean
   8695 ppc64_elf_edit_toc (struct bfd_link_info *info)
   8696 {
   8697   bfd *ibfd;
   8698   struct adjust_toc_info toc_inf;
   8699   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   8700 
   8701   htab->do_toc_opt = 1;
   8702   toc_inf.global_toc_syms = TRUE;
   8703   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   8704     {
   8705       asection *toc, *sec;
   8706       Elf_Internal_Shdr *symtab_hdr;
   8707       Elf_Internal_Sym *local_syms;
   8708       Elf_Internal_Rela *relstart, *rel, *toc_relocs;
   8709       unsigned long *skip, *drop;
   8710       unsigned char *used;
   8711       unsigned char *keep, last, some_unused;
   8712 
   8713       if (!is_ppc64_elf (ibfd))
   8714 	continue;
   8715 
   8716       toc = bfd_get_section_by_name (ibfd, ".toc");
   8717       if (toc == NULL
   8718 	  || toc->size == 0
   8719 	  || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
   8720 	  || discarded_section (toc))
   8721 	continue;
   8722 
   8723       toc_relocs = NULL;
   8724       local_syms = NULL;
   8725       symtab_hdr = &elf_symtab_hdr (ibfd);
   8726 
   8727       /* Look at sections dropped from the final link.  */
   8728       skip = NULL;
   8729       relstart = NULL;
   8730       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   8731 	{
   8732 	  if (sec->reloc_count == 0
   8733 	      || !discarded_section (sec)
   8734 	      || get_opd_info (sec)
   8735 	      || (sec->flags & SEC_ALLOC) == 0
   8736 	      || (sec->flags & SEC_DEBUGGING) != 0)
   8737 	    continue;
   8738 
   8739 	  relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
   8740 	  if (relstart == NULL)
   8741 	    goto error_ret;
   8742 
   8743 	  /* Run through the relocs to see which toc entries might be
   8744 	     unused.  */
   8745 	  for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
   8746 	    {
   8747 	      enum elf_ppc64_reloc_type r_type;
   8748 	      unsigned long r_symndx;
   8749 	      asection *sym_sec;
   8750 	      struct elf_link_hash_entry *h;
   8751 	      Elf_Internal_Sym *sym;
   8752 	      bfd_vma val;
   8753 
   8754 	      r_type = ELF64_R_TYPE (rel->r_info);
   8755 	      switch (r_type)
   8756 		{
   8757 		default:
   8758 		  continue;
   8759 
   8760 		case R_PPC64_TOC16:
   8761 		case R_PPC64_TOC16_LO:
   8762 		case R_PPC64_TOC16_HI:
   8763 		case R_PPC64_TOC16_HA:
   8764 		case R_PPC64_TOC16_DS:
   8765 		case R_PPC64_TOC16_LO_DS:
   8766 		  break;
   8767 		}
   8768 
   8769 	      r_symndx = ELF64_R_SYM (rel->r_info);
   8770 	      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   8771 			      r_symndx, ibfd))
   8772 		goto error_ret;
   8773 
   8774 	      if (sym_sec != toc)
   8775 		continue;
   8776 
   8777 	      if (h != NULL)
   8778 		val = h->root.u.def.value;
   8779 	      else
   8780 		val = sym->st_value;
   8781 	      val += rel->r_addend;
   8782 
   8783 	      if (val >= toc->size)
   8784 		continue;
   8785 
   8786 	      /* Anything in the toc ought to be aligned to 8 bytes.
   8787 		 If not, don't mark as unused.  */
   8788 	      if (val & 7)
   8789 		continue;
   8790 
   8791 	      if (skip == NULL)
   8792 		{
   8793 		  skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
   8794 		  if (skip == NULL)
   8795 		    goto error_ret;
   8796 		}
   8797 
   8798 	      skip[val >> 3] = ref_from_discarded;
   8799 	    }
   8800 
   8801 	  if (elf_section_data (sec)->relocs != relstart)
   8802 	    free (relstart);
   8803 	}
   8804 
   8805       /* For largetoc loads of address constants, we can convert
   8806 	 .  addis rx,2,addr@got@ha
   8807 	 .  ld ry,addr@got@l(rx)
   8808 	 to
   8809 	 .  addis rx,2,addr@toc@ha
   8810 	 .  addi ry,rx,addr@toc@l
   8811 	 when addr is within 2G of the toc pointer.  This then means
   8812 	 that the word storing "addr" in the toc is no longer needed.  */
   8813 
   8814       if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
   8815 	  && toc->output_section->rawsize < (bfd_vma) 1 << 31
   8816 	  && toc->reloc_count != 0)
   8817 	{
   8818 	  /* Read toc relocs.  */
   8819 	  toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
   8820 						  info->keep_memory);
   8821 	  if (toc_relocs == NULL)
   8822 	    goto error_ret;
   8823 
   8824 	  for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
   8825 	    {
   8826 	      enum elf_ppc64_reloc_type r_type;
   8827 	      unsigned long r_symndx;
   8828 	      asection *sym_sec;
   8829 	      struct elf_link_hash_entry *h;
   8830 	      Elf_Internal_Sym *sym;
   8831 	      bfd_vma val, addr;
   8832 
   8833 	      r_type = ELF64_R_TYPE (rel->r_info);
   8834 	      if (r_type != R_PPC64_ADDR64)
   8835 		continue;
   8836 
   8837 	      r_symndx = ELF64_R_SYM (rel->r_info);
   8838 	      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   8839 			      r_symndx, ibfd))
   8840 		goto error_ret;
   8841 
   8842 	      if (sym_sec == NULL
   8843 		  || sym_sec->output_section == NULL
   8844 		  || discarded_section (sym_sec))
   8845 		continue;
   8846 
   8847 	      if (!SYMBOL_REFERENCES_LOCAL (info, h))
   8848 		continue;
   8849 
   8850 	      if (h != NULL)
   8851 		{
   8852 		  if (h->type == STT_GNU_IFUNC)
   8853 		    continue;
   8854 		  val = h->root.u.def.value;
   8855 		}
   8856 	      else
   8857 		{
   8858 		  if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
   8859 		    continue;
   8860 		  val = sym->st_value;
   8861 		}
   8862 	      val += rel->r_addend;
   8863 	      val += sym_sec->output_section->vma + sym_sec->output_offset;
   8864 
   8865 	      /* We don't yet know the exact toc pointer value, but we
   8866 		 know it will be somewhere in the toc section.  Don't
   8867 		 optimize if the difference from any possible toc
   8868 		 pointer is outside [ff..f80008000, 7fff7fff].  */
   8869 	      addr = toc->output_section->vma + TOC_BASE_OFF;
   8870 	      if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
   8871 		continue;
   8872 
   8873 	      addr = toc->output_section->vma + toc->output_section->rawsize;
   8874 	      if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
   8875 		continue;
   8876 
   8877 	      if (skip == NULL)
   8878 		{
   8879 		  skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
   8880 		  if (skip == NULL)
   8881 		    goto error_ret;
   8882 		}
   8883 
   8884 	      skip[rel->r_offset >> 3]
   8885 		|= can_optimize | ((rel - toc_relocs) << 2);
   8886 	    }
   8887 	}
   8888 
   8889       if (skip == NULL)
   8890 	continue;
   8891 
   8892       used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
   8893       if (used == NULL)
   8894 	{
   8895 	error_ret:
   8896 	  if (symtab_hdr->contents != (unsigned char *) local_syms)
   8897 	    free (local_syms);
   8898 	  if (sec != NULL
   8899 	      && elf_section_data (sec)->relocs != relstart)
   8900 	    free (relstart);
   8901 	  if (elf_section_data (toc)->relocs != toc_relocs)
   8902 	    free (toc_relocs);
   8903 	  free (skip);
   8904 	  return FALSE;
   8905 	}
   8906 
   8907       /* Now check all kept sections that might reference the toc.
   8908 	 Check the toc itself last.  */
   8909       for (sec = (ibfd->sections == toc && toc->next ? toc->next
   8910 		  : ibfd->sections);
   8911 	   sec != NULL;
   8912 	   sec = (sec == toc ? NULL
   8913 		  : sec->next == NULL ? toc
   8914 		  : sec->next == toc && toc->next ? toc->next
   8915 		  : sec->next))
   8916 	{
   8917 	  int repeat;
   8918 
   8919 	  if (sec->reloc_count == 0
   8920 	      || discarded_section (sec)
   8921 	      || get_opd_info (sec)
   8922 	      || (sec->flags & SEC_ALLOC) == 0
   8923 	      || (sec->flags & SEC_DEBUGGING) != 0)
   8924 	    continue;
   8925 
   8926 	  relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   8927 						info->keep_memory);
   8928 	  if (relstart == NULL)
   8929 	    {
   8930 	      free (used);
   8931 	      goto error_ret;
   8932 	    }
   8933 
   8934 	  /* Mark toc entries referenced as used.  */
   8935 	  do
   8936 	    {
   8937 	      repeat = 0;
   8938 	      for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
   8939 		{
   8940 		  enum elf_ppc64_reloc_type r_type;
   8941 		  unsigned long r_symndx;
   8942 		  asection *sym_sec;
   8943 		  struct elf_link_hash_entry *h;
   8944 		  Elf_Internal_Sym *sym;
   8945 		  bfd_vma val;
   8946 
   8947 		  r_type = ELF64_R_TYPE (rel->r_info);
   8948 		  switch (r_type)
   8949 		    {
   8950 		    case R_PPC64_TOC16:
   8951 		    case R_PPC64_TOC16_LO:
   8952 		    case R_PPC64_TOC16_HI:
   8953 		    case R_PPC64_TOC16_HA:
   8954 		    case R_PPC64_TOC16_DS:
   8955 		    case R_PPC64_TOC16_LO_DS:
   8956 		      /* In case we're taking addresses of toc entries.  */
   8957 		    case R_PPC64_ADDR64:
   8958 		      break;
   8959 
   8960 		    default:
   8961 		      continue;
   8962 		    }
   8963 
   8964 		  r_symndx = ELF64_R_SYM (rel->r_info);
   8965 		  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   8966 				  r_symndx, ibfd))
   8967 		    {
   8968 		      free (used);
   8969 		      goto error_ret;
   8970 		    }
   8971 
   8972 		  if (sym_sec != toc)
   8973 		    continue;
   8974 
   8975 		  if (h != NULL)
   8976 		    val = h->root.u.def.value;
   8977 		  else
   8978 		    val = sym->st_value;
   8979 		  val += rel->r_addend;
   8980 
   8981 		  if (val >= toc->size)
   8982 		    continue;
   8983 
   8984 		  if ((skip[val >> 3] & can_optimize) != 0)
   8985 		    {
   8986 		      bfd_vma off;
   8987 		      unsigned char opc;
   8988 
   8989 		      switch (r_type)
   8990 			{
   8991 			case R_PPC64_TOC16_HA:
   8992 			  break;
   8993 
   8994 			case R_PPC64_TOC16_LO_DS:
   8995 			  off = rel->r_offset;
   8996 			  off += (bfd_big_endian (ibfd) ? -2 : 3);
   8997 			  if (!bfd_get_section_contents (ibfd, sec, &opc,
   8998 							 off, 1))
   8999 			    {
   9000 			      free (used);
   9001 			      goto error_ret;
   9002 			    }
   9003 			  if ((opc & (0x3f << 2)) == (58u << 2))
   9004 			    break;
   9005 			  /* Fall through.  */
   9006 
   9007 			default:
   9008 			  /* Wrong sort of reloc, or not a ld.  We may
   9009 			     as well clear ref_from_discarded too.  */
   9010 			  skip[val >> 3] = 0;
   9011 			}
   9012 		    }
   9013 
   9014 		  if (sec != toc)
   9015 		    used[val >> 3] = 1;
   9016 		  /* For the toc section, we only mark as used if this
   9017 		     entry itself isn't unused.  */
   9018 		  else if ((used[rel->r_offset >> 3]
   9019 			    || !(skip[rel->r_offset >> 3] & ref_from_discarded))
   9020 			   && !used[val >> 3])
   9021 		    {
   9022 		      /* Do all the relocs again, to catch reference
   9023 			 chains.  */
   9024 		      repeat = 1;
   9025 		      used[val >> 3] = 1;
   9026 		    }
   9027 		}
   9028 	    }
   9029 	  while (repeat);
   9030 
   9031 	  if (elf_section_data (sec)->relocs != relstart)
   9032 	    free (relstart);
   9033 	}
   9034 
   9035       /* Merge the used and skip arrays.  Assume that TOC
   9036 	 doublewords not appearing as either used or unused belong
   9037 	 to an entry more than one doubleword in size.  */
   9038       for (drop = skip, keep = used, last = 0, some_unused = 0;
   9039 	   drop < skip + (toc->size + 7) / 8;
   9040 	   ++drop, ++keep)
   9041 	{
   9042 	  if (*keep)
   9043 	    {
   9044 	      *drop &= ~ref_from_discarded;
   9045 	      if ((*drop & can_optimize) != 0)
   9046 		some_unused = 1;
   9047 	      last = 0;
   9048 	    }
   9049 	  else if ((*drop & ref_from_discarded) != 0)
   9050 	    {
   9051 	      some_unused = 1;
   9052 	      last = ref_from_discarded;
   9053 	    }
   9054 	  else
   9055 	    *drop = last;
   9056 	}
   9057 
   9058       free (used);
   9059 
   9060       if (some_unused)
   9061 	{
   9062 	  bfd_byte *contents, *src;
   9063 	  unsigned long off;
   9064 	  Elf_Internal_Sym *sym;
   9065 	  bfd_boolean local_toc_syms = FALSE;
   9066 
   9067 	  /* Shuffle the toc contents, and at the same time convert the
   9068 	     skip array from booleans into offsets.  */
   9069 	  if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
   9070 	    goto error_ret;
   9071 
   9072 	  elf_section_data (toc)->this_hdr.contents = contents;
   9073 
   9074 	  for (src = contents, off = 0, drop = skip;
   9075 	       src < contents + toc->size;
   9076 	       src += 8, ++drop)
   9077 	    {
   9078 	      if ((*drop & (can_optimize | ref_from_discarded)) != 0)
   9079 		off += 8;
   9080 	      else if (off != 0)
   9081 		{
   9082 		  *drop = off;
   9083 		  memcpy (src - off, src, 8);
   9084 		}
   9085 	    }
   9086 	  *drop = off;
   9087 	  toc->rawsize = toc->size;
   9088 	  toc->size = src - contents - off;
   9089 
   9090 	  /* Adjust addends for relocs against the toc section sym,
   9091 	     and optimize any accesses we can.  */
   9092 	  for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   9093 	    {
   9094 	      if (sec->reloc_count == 0
   9095 		  || discarded_section (sec))
   9096 		continue;
   9097 
   9098 	      relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   9099 						    info->keep_memory);
   9100 	      if (relstart == NULL)
   9101 		goto error_ret;
   9102 
   9103 	      for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
   9104 		{
   9105 		  enum elf_ppc64_reloc_type r_type;
   9106 		  unsigned long r_symndx;
   9107 		  asection *sym_sec;
   9108 		  struct elf_link_hash_entry *h;
   9109 		  bfd_vma val;
   9110 
   9111 		  r_type = ELF64_R_TYPE (rel->r_info);
   9112 		  switch (r_type)
   9113 		    {
   9114 		    default:
   9115 		      continue;
   9116 
   9117 		    case R_PPC64_TOC16:
   9118 		    case R_PPC64_TOC16_LO:
   9119 		    case R_PPC64_TOC16_HI:
   9120 		    case R_PPC64_TOC16_HA:
   9121 		    case R_PPC64_TOC16_DS:
   9122 		    case R_PPC64_TOC16_LO_DS:
   9123 		    case R_PPC64_ADDR64:
   9124 		      break;
   9125 		    }
   9126 
   9127 		  r_symndx = ELF64_R_SYM (rel->r_info);
   9128 		  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   9129 				  r_symndx, ibfd))
   9130 		    goto error_ret;
   9131 
   9132 		  if (sym_sec != toc)
   9133 		    continue;
   9134 
   9135 		  if (h != NULL)
   9136 		    val = h->root.u.def.value;
   9137 		  else
   9138 		    {
   9139 		      val = sym->st_value;
   9140 		      if (val != 0)
   9141 			local_toc_syms = TRUE;
   9142 		    }
   9143 
   9144 		  val += rel->r_addend;
   9145 
   9146 		  if (val > toc->rawsize)
   9147 		    val = toc->rawsize;
   9148 		  else if ((skip[val >> 3] & ref_from_discarded) != 0)
   9149 		    continue;
   9150 		  else if ((skip[val >> 3] & can_optimize) != 0)
   9151 		    {
   9152 		      Elf_Internal_Rela *tocrel
   9153 			= toc_relocs + (skip[val >> 3] >> 2);
   9154 		      unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
   9155 
   9156 		      switch (r_type)
   9157 			{
   9158 			case R_PPC64_TOC16_HA:
   9159 			  rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
   9160 			  break;
   9161 
   9162 			case R_PPC64_TOC16_LO_DS:
   9163 			  rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
   9164 			  break;
   9165 
   9166 			default:
   9167 			  if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
   9168 			    ppc_howto_init ();
   9169 			  info->callbacks->einfo
   9170 			    /* xgettext:c-format */
   9171 			    (_("%H: %s references "
   9172 			       "optimized away TOC entry\n"),
   9173 			     ibfd, sec, rel->r_offset,
   9174 			     ppc64_elf_howto_table[r_type]->name);
   9175 			  bfd_set_error (bfd_error_bad_value);
   9176 			  goto error_ret;
   9177 			}
   9178 		      rel->r_addend = tocrel->r_addend;
   9179 		      elf_section_data (sec)->relocs = relstart;
   9180 		      continue;
   9181 		    }
   9182 
   9183 		  if (h != NULL || sym->st_value != 0)
   9184 		    continue;
   9185 
   9186 		  rel->r_addend -= skip[val >> 3];
   9187 		  elf_section_data (sec)->relocs = relstart;
   9188 		}
   9189 
   9190 	      if (elf_section_data (sec)->relocs != relstart)
   9191 		free (relstart);
   9192 	    }
   9193 
   9194 	  /* We shouldn't have local or global symbols defined in the TOC,
   9195 	     but handle them anyway.  */
   9196 	  if (local_syms != NULL)
   9197 	    for (sym = local_syms;
   9198 		 sym < local_syms + symtab_hdr->sh_info;
   9199 		 ++sym)
   9200 	      if (sym->st_value != 0
   9201 		  && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
   9202 		{
   9203 		  unsigned long i;
   9204 
   9205 		  if (sym->st_value > toc->rawsize)
   9206 		    i = toc->rawsize >> 3;
   9207 		  else
   9208 		    i = sym->st_value >> 3;
   9209 
   9210 		  if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
   9211 		    {
   9212 		      if (local_toc_syms)
   9213 			_bfd_error_handler
   9214 			  (_("%s defined on removed toc entry"),
   9215 			   bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
   9216 		      do
   9217 			++i;
   9218 		      while ((skip[i] & (ref_from_discarded | can_optimize)));
   9219 		      sym->st_value = (bfd_vma) i << 3;
   9220 		    }
   9221 
   9222 		  sym->st_value -= skip[i];
   9223 		  symtab_hdr->contents = (unsigned char *) local_syms;
   9224 		}
   9225 
   9226 	  /* Adjust any global syms defined in this toc input section.  */
   9227 	  if (toc_inf.global_toc_syms)
   9228 	    {
   9229 	      toc_inf.toc = toc;
   9230 	      toc_inf.skip = skip;
   9231 	      toc_inf.global_toc_syms = FALSE;
   9232 	      elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
   9233 				      &toc_inf);
   9234 	    }
   9235 
   9236 	  if (toc->reloc_count != 0)
   9237 	    {
   9238 	      Elf_Internal_Shdr *rel_hdr;
   9239 	      Elf_Internal_Rela *wrel;
   9240 	      bfd_size_type sz;
   9241 
   9242 	      /* Remove unused toc relocs, and adjust those we keep.  */
   9243 	      if (toc_relocs == NULL)
   9244 		toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
   9245 							info->keep_memory);
   9246 	      if (toc_relocs == NULL)
   9247 		goto error_ret;
   9248 
   9249 	      wrel = toc_relocs;
   9250 	      for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
   9251 		if ((skip[rel->r_offset >> 3]
   9252 		     & (ref_from_discarded | can_optimize)) == 0)
   9253 		  {
   9254 		    wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
   9255 		    wrel->r_info = rel->r_info;
   9256 		    wrel->r_addend = rel->r_addend;
   9257 		    ++wrel;
   9258 		  }
   9259 		else if (!dec_dynrel_count (rel->r_info, toc, info,
   9260 					    &local_syms, NULL, NULL))
   9261 		  goto error_ret;
   9262 
   9263 	      elf_section_data (toc)->relocs = toc_relocs;
   9264 	      toc->reloc_count = wrel - toc_relocs;
   9265 	      rel_hdr = _bfd_elf_single_rel_hdr (toc);
   9266 	      sz = rel_hdr->sh_entsize;
   9267 	      rel_hdr->sh_size = toc->reloc_count * sz;
   9268 	    }
   9269 	}
   9270       else if (elf_section_data (toc)->relocs != toc_relocs)
   9271 	free (toc_relocs);
   9272 
   9273       if (local_syms != NULL
   9274 	  && symtab_hdr->contents != (unsigned char *) local_syms)
   9275 	{
   9276 	  if (!info->keep_memory)
   9277 	    free (local_syms);
   9278 	  else
   9279 	    symtab_hdr->contents = (unsigned char *) local_syms;
   9280 	}
   9281       free (skip);
   9282     }
   9283 
   9284   /* Look for cases where we can change an indirect GOT access to
   9285      a GOT relative or PC relative access, possibly reducing the
   9286      number of GOT entries.  */
   9287   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   9288     {
   9289       asection *sec;
   9290       Elf_Internal_Shdr *symtab_hdr;
   9291       Elf_Internal_Sym *local_syms;
   9292       Elf_Internal_Rela *relstart, *rel;
   9293       bfd_vma got;
   9294 
   9295       if (!is_ppc64_elf (ibfd))
   9296 	continue;
   9297 
   9298       if (!ppc64_elf_tdata (ibfd)->has_optrel)
   9299 	continue;
   9300 
   9301       sec = ppc64_elf_tdata (ibfd)->got;
   9302       got = 0;
   9303       if (sec != NULL)
   9304 	got = sec->output_section->vma + sec->output_offset + 0x8000;
   9305 
   9306       local_syms = NULL;
   9307       symtab_hdr = &elf_symtab_hdr (ibfd);
   9308 
   9309       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   9310 	{
   9311 	  if (sec->reloc_count == 0
   9312 	      || !ppc64_elf_section_data (sec)->has_optrel
   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 	    {
   9320 	    got_error_ret:
   9321 	      if (symtab_hdr->contents != (unsigned char *) local_syms)
   9322 		free (local_syms);
   9323 	      if (sec != NULL
   9324 		  && elf_section_data (sec)->relocs != relstart)
   9325 		free (relstart);
   9326 	      return FALSE;
   9327 	    }
   9328 
   9329 	  for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
   9330 	    {
   9331 	      enum elf_ppc64_reloc_type r_type;
   9332 	      unsigned long r_symndx;
   9333 	      Elf_Internal_Sym *sym;
   9334 	      asection *sym_sec;
   9335 	      struct elf_link_hash_entry *h;
   9336 	      struct got_entry *ent;
   9337 	      bfd_vma val, pc;
   9338 	      unsigned char buf[8];
   9339 	      unsigned int insn;
   9340 	      enum {no_check, check_lo, check_ha} insn_check;
   9341 
   9342 	      r_type = ELF64_R_TYPE (rel->r_info);
   9343 	      switch (r_type)
   9344 		{
   9345 		default:
   9346 		  insn_check = no_check;
   9347 		  break;
   9348 
   9349 		case R_PPC64_PLT16_HA:
   9350 		case R_PPC64_GOT_TLSLD16_HA:
   9351 		case R_PPC64_GOT_TLSGD16_HA:
   9352 		case R_PPC64_GOT_TPREL16_HA:
   9353 		case R_PPC64_GOT_DTPREL16_HA:
   9354 		case R_PPC64_GOT16_HA:
   9355 		case R_PPC64_TOC16_HA:
   9356 		  insn_check = check_ha;
   9357 		  break;
   9358 
   9359 		case R_PPC64_PLT16_LO:
   9360 		case R_PPC64_PLT16_LO_DS:
   9361 		case R_PPC64_GOT_TLSLD16_LO:
   9362 		case R_PPC64_GOT_TLSGD16_LO:
   9363 		case R_PPC64_GOT_TPREL16_LO_DS:
   9364 		case R_PPC64_GOT_DTPREL16_LO_DS:
   9365 		case R_PPC64_GOT16_LO:
   9366 		case R_PPC64_GOT16_LO_DS:
   9367 		case R_PPC64_TOC16_LO:
   9368 		case R_PPC64_TOC16_LO_DS:
   9369 		  insn_check = check_lo;
   9370 		  break;
   9371 		}
   9372 
   9373 	      if (insn_check != no_check)
   9374 		{
   9375 		  bfd_vma off = rel->r_offset & ~3;
   9376 
   9377 		  if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
   9378 		    goto got_error_ret;
   9379 
   9380 		  insn = bfd_get_32 (ibfd, buf);
   9381 		  if (insn_check == check_lo
   9382 		      ? !ok_lo_toc_insn (insn, r_type)
   9383 		      : ((insn & ((0x3fu << 26) | 0x1f << 16))
   9384 			 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
   9385 		    {
   9386 		      char str[12];
   9387 
   9388 		      ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
   9389 		      sprintf (str, "%#08x", insn);
   9390 		      info->callbacks->einfo
   9391 			/* xgettext:c-format */
   9392 			(_("%H: got/toc optimization is not supported for"
   9393 			   " %s instruction\n"),
   9394 			 ibfd, sec, rel->r_offset & ~3, str);
   9395 		      continue;
   9396 		    }
   9397 		}
   9398 
   9399 	      switch (r_type)
   9400 		{
   9401 		/* Note that we don't delete GOT entries for
   9402 		   R_PPC64_GOT16_DS since we'd need a lot more
   9403 		   analysis.  For starters, the preliminary layout is
   9404 		   before the GOT, PLT, dynamic sections and stubs are
   9405 		   laid out.  Then we'd need to allow for changes in
   9406 		   distance between sections caused by alignment.  */
   9407 		default:
   9408 		  continue;
   9409 
   9410 		case R_PPC64_GOT16_HA:
   9411 		case R_PPC64_GOT16_LO_DS:
   9412 		case R_PPC64_GOT_PCREL34:
   9413 		  break;
   9414 		}
   9415 
   9416 	      r_symndx = ELF64_R_SYM (rel->r_info);
   9417 	      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   9418 			      r_symndx, ibfd))
   9419 		goto got_error_ret;
   9420 
   9421 	      if (sym_sec == NULL
   9422 		  || sym_sec->output_section == NULL
   9423 		  || discarded_section (sym_sec))
   9424 		continue;
   9425 
   9426 	      if ((h ? h->type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC)
   9427 		continue;
   9428 
   9429 	      if (!SYMBOL_REFERENCES_LOCAL (info, h))
   9430 		continue;
   9431 
   9432 	      if (h != NULL)
   9433 		val = h->root.u.def.value;
   9434 	      else
   9435 		val = sym->st_value;
   9436 	      val += rel->r_addend;
   9437 	      val += sym_sec->output_section->vma + sym_sec->output_offset;
   9438 
   9439 /* Fudge factor to allow for the fact that the preliminary layout
   9440    isn't exact.  Reduce limits by this factor.  */
   9441 #define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
   9442 
   9443 	      switch (r_type)
   9444 		{
   9445 		default:
   9446 		  continue;
   9447 
   9448 		case R_PPC64_GOT16_HA:
   9449 		  if (val - got + LIMIT_ADJUST (0x80008000ULL)
   9450 		      >= LIMIT_ADJUST (0x100000000ULL))
   9451 		    continue;
   9452 
   9453 		  if (!bfd_get_section_contents (ibfd, sec, buf,
   9454 						 rel->r_offset & ~3, 4))
   9455 		    goto got_error_ret;
   9456 		  insn = bfd_get_32 (ibfd, buf);
   9457 		  if (((insn & ((0x3fu << 26) | 0x1f << 16))
   9458 		       != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
   9459 		    continue;
   9460 		  break;
   9461 
   9462 		case R_PPC64_GOT16_LO_DS:
   9463 		  if (val - got + LIMIT_ADJUST (0x80008000ULL)
   9464 		      >= LIMIT_ADJUST (0x100000000ULL))
   9465 		    continue;
   9466 		  if (!bfd_get_section_contents (ibfd, sec, buf,
   9467 						 rel->r_offset & ~3, 4))
   9468 		    goto got_error_ret;
   9469 		  insn = bfd_get_32 (ibfd, buf);
   9470 		  if ((insn & (0x3fu << 26 | 0x3)) != 58u << 26 /* ld */)
   9471 		    continue;
   9472 		  break;
   9473 
   9474 		case R_PPC64_GOT_PCREL34:
   9475 		  pc = rel->r_offset;
   9476 		  pc += sec->output_section->vma + sec->output_offset;
   9477 		  if (val - pc + LIMIT_ADJUST (1ULL << 33)
   9478 		      >= LIMIT_ADJUST (1ULL << 34))
   9479 		    continue;
   9480 		  if (!bfd_get_section_contents (ibfd, sec, buf,
   9481 						 rel->r_offset & ~3, 8))
   9482 		    goto got_error_ret;
   9483 		  insn = bfd_get_32 (ibfd, buf);
   9484 		  if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
   9485 		    continue;
   9486 		  insn = bfd_get_32 (ibfd, buf + 4);
   9487 		  if ((insn & (0x3fu << 26)) != 57u << 26)
   9488 		    continue;
   9489 		  break;
   9490 		}
   9491 #undef LIMIT_ADJUST
   9492 
   9493 	      if (h != NULL)
   9494 		ent = h->got.glist;
   9495 	      else
   9496 		{
   9497 		  struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
   9498 		  ent = local_got_ents[r_symndx];
   9499 		}
   9500 	      for (; ent != NULL; ent = ent->next)
   9501 		if (ent->addend == rel->r_addend
   9502 		    && ent->owner == ibfd
   9503 		    && ent->tls_type == 0)
   9504 		  break;
   9505 	      BFD_ASSERT (ent && ent->got.refcount > 0);
   9506 	      ent->got.refcount -= 1;
   9507 	    }
   9508 
   9509 	  if (elf_section_data (sec)->relocs != relstart)
   9510 	    free (relstart);
   9511 	}
   9512 
   9513       if (local_syms != NULL
   9514 	  && symtab_hdr->contents != (unsigned char *) local_syms)
   9515 	{
   9516 	  if (!info->keep_memory)
   9517 	    free (local_syms);
   9518 	  else
   9519 	    symtab_hdr->contents = (unsigned char *) local_syms;
   9520 	}
   9521     }
   9522 
   9523   return TRUE;
   9524 }
   9525 
   9526 /* Return true iff input section I references the TOC using
   9527    instructions limited to +/-32k offsets.  */
   9528 
   9529 bfd_boolean
   9530 ppc64_elf_has_small_toc_reloc (asection *i)
   9531 {
   9532   return (is_ppc64_elf (i->owner)
   9533 	  && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
   9534 }
   9535 
   9536 /* Allocate space for one GOT entry.  */
   9537 
   9538 static void
   9539 allocate_got (struct elf_link_hash_entry *h,
   9540 	      struct bfd_link_info *info,
   9541 	      struct got_entry *gent)
   9542 {
   9543   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   9544   struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
   9545   int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
   9546 		 ? 16 : 8);
   9547   int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
   9548 		  ? 2 : 1) * sizeof (Elf64_External_Rela);
   9549   asection *got = ppc64_elf_tdata (gent->owner)->got;
   9550 
   9551   gent->got.offset = got->size;
   9552   got->size += entsize;
   9553 
   9554   if (h->type == STT_GNU_IFUNC)
   9555     {
   9556       htab->elf.irelplt->size += rentsize;
   9557       htab->got_reli_size += rentsize;
   9558     }
   9559   else if (((bfd_link_pic (info)
   9560 	     && !(gent->tls_type != 0
   9561 		  && bfd_link_executable (info)
   9562 		  && SYMBOL_REFERENCES_LOCAL (info, h)))
   9563 	    || (htab->elf.dynamic_sections_created
   9564 		&& h->dynindx != -1
   9565 		&& !SYMBOL_REFERENCES_LOCAL (info, h)))
   9566 	   && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
   9567     {
   9568       asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
   9569       relgot->size += rentsize;
   9570     }
   9571 }
   9572 
   9573 /* This function merges got entries in the same toc group.  */
   9574 
   9575 static void
   9576 merge_got_entries (struct got_entry **pent)
   9577 {
   9578   struct got_entry *ent, *ent2;
   9579 
   9580   for (ent = *pent; ent != NULL; ent = ent->next)
   9581     if (!ent->is_indirect)
   9582       for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
   9583 	if (!ent2->is_indirect
   9584 	    && ent2->addend == ent->addend
   9585 	    && ent2->tls_type == ent->tls_type
   9586 	    && elf_gp (ent2->owner) == elf_gp (ent->owner))
   9587 	  {
   9588 	    ent2->is_indirect = TRUE;
   9589 	    ent2->got.ent = ent;
   9590 	  }
   9591 }
   9592 
   9593 /* If H is undefined, make it dynamic if that makes sense.  */
   9594 
   9595 static bfd_boolean
   9596 ensure_undef_dynamic (struct bfd_link_info *info,
   9597 		      struct elf_link_hash_entry *h)
   9598 {
   9599   struct elf_link_hash_table *htab = elf_hash_table (info);
   9600 
   9601   if (htab->dynamic_sections_created
   9602       && ((info->dynamic_undefined_weak != 0
   9603 	   && h->root.type == bfd_link_hash_undefweak)
   9604 	  || h->root.type == bfd_link_hash_undefined)
   9605       && h->dynindx == -1
   9606       && !h->forced_local
   9607       && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
   9608     return bfd_elf_link_record_dynamic_symbol (info, h);
   9609   return TRUE;
   9610 }
   9611 
   9612 /* Allocate space in .plt, .got and associated reloc sections for
   9613    dynamic relocs.  */
   9614 
   9615 static bfd_boolean
   9616 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
   9617 {
   9618   struct bfd_link_info *info;
   9619   struct ppc_link_hash_table *htab;
   9620   asection *s;
   9621   struct ppc_link_hash_entry *eh;
   9622   struct got_entry **pgent, *gent;
   9623 
   9624   if (h->root.type == bfd_link_hash_indirect)
   9625     return TRUE;
   9626 
   9627   info = (struct bfd_link_info *) inf;
   9628   htab = ppc_hash_table (info);
   9629   if (htab == NULL)
   9630     return FALSE;
   9631 
   9632   eh = ppc_elf_hash_entry (h);
   9633   /* Run through the TLS GD got entries first if we're changing them
   9634      to TPREL.  */
   9635   if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
   9636     for (gent = h->got.glist; gent != NULL; gent = gent->next)
   9637       if (gent->got.refcount > 0
   9638 	  && (gent->tls_type & TLS_GD) != 0)
   9639 	{
   9640 	  /* This was a GD entry that has been converted to TPREL.  If
   9641 	     there happens to be a TPREL entry we can use that one.  */
   9642 	  struct got_entry *ent;
   9643 	  for (ent = h->got.glist; ent != NULL; ent = ent->next)
   9644 	    if (ent->got.refcount > 0
   9645 		&& (ent->tls_type & TLS_TPREL) != 0
   9646 		&& ent->addend == gent->addend
   9647 		&& ent->owner == gent->owner)
   9648 	      {
   9649 		gent->got.refcount = 0;
   9650 		break;
   9651 	      }
   9652 
   9653 	  /* If not, then we'll be using our own TPREL entry.  */
   9654 	  if (gent->got.refcount != 0)
   9655 	    gent->tls_type = TLS_TLS | TLS_TPREL;
   9656 	}
   9657 
   9658   /* Remove any list entry that won't generate a word in the GOT before
   9659      we call merge_got_entries.  Otherwise we risk merging to empty
   9660      entries.  */
   9661   pgent = &h->got.glist;
   9662   while ((gent = *pgent) != NULL)
   9663     if (gent->got.refcount > 0)
   9664       {
   9665 	if ((gent->tls_type & TLS_LD) != 0
   9666 	    && SYMBOL_REFERENCES_LOCAL (info, h))
   9667 	  {
   9668 	    ppc64_tlsld_got (gent->owner)->got.refcount += 1;
   9669 	    *pgent = gent->next;
   9670 	  }
   9671 	else
   9672 	  pgent = &gent->next;
   9673       }
   9674     else
   9675       *pgent = gent->next;
   9676 
   9677   if (!htab->do_multi_toc)
   9678     merge_got_entries (&h->got.glist);
   9679 
   9680   for (gent = h->got.glist; gent != NULL; gent = gent->next)
   9681     if (!gent->is_indirect)
   9682       {
   9683 	/* Ensure we catch all the cases where this symbol should
   9684 	   be made dynamic.  */
   9685 	if (!ensure_undef_dynamic (info, h))
   9686 	  return FALSE;
   9687 
   9688 	if (!is_ppc64_elf (gent->owner))
   9689 	  abort ();
   9690 
   9691 	allocate_got (h, info, gent);
   9692       }
   9693 
   9694   /* If no dynamic sections we can't have dynamic relocs, except for
   9695      IFUNCs which are handled even in static executables.  */
   9696   if (!htab->elf.dynamic_sections_created
   9697       && h->type != STT_GNU_IFUNC)
   9698     h->dyn_relocs = NULL;
   9699 
   9700   /* Discard relocs on undefined symbols that must be local.  */
   9701   else if (h->root.type == bfd_link_hash_undefined
   9702 	   && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
   9703     h->dyn_relocs = NULL;
   9704 
   9705   /* Also discard relocs on undefined weak syms with non-default
   9706      visibility, or when dynamic_undefined_weak says so.  */
   9707   else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
   9708     h->dyn_relocs = NULL;
   9709 
   9710   if (h->dyn_relocs != NULL)
   9711     {
   9712       struct elf_dyn_relocs *p, **pp;
   9713 
   9714       /* In the shared -Bsymbolic case, discard space allocated for
   9715 	 dynamic pc-relative relocs against symbols which turn out to
   9716 	 be defined in regular objects.  For the normal shared case,
   9717 	 discard space for relocs that have become local due to symbol
   9718 	 visibility changes.  */
   9719       if (bfd_link_pic (info))
   9720 	{
   9721 	  /* Relocs that use pc_count are those that appear on a call
   9722 	     insn, or certain REL relocs (see must_be_dyn_reloc) that
   9723 	     can be generated via assembly.  We want calls to
   9724 	     protected symbols to resolve directly to the function
   9725 	     rather than going via the plt.  If people want function
   9726 	     pointer comparisons to work as expected then they should
   9727 	     avoid writing weird assembly.  */
   9728 	  if (SYMBOL_CALLS_LOCAL (info, h))
   9729 	    {
   9730 	      for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
   9731 		{
   9732 		  p->count -= p->pc_count;
   9733 		  p->pc_count = 0;
   9734 		  if (p->count == 0)
   9735 		    *pp = p->next;
   9736 		  else
   9737 		    pp = &p->next;
   9738 		}
   9739 	    }
   9740 
   9741 	  if (h->dyn_relocs != NULL)
   9742 	    {
   9743 	      /* Ensure we catch all the cases where this symbol
   9744 		 should be made dynamic.  */
   9745 	      if (!ensure_undef_dynamic (info, h))
   9746 		return FALSE;
   9747 	    }
   9748 	}
   9749 
   9750       /* For a fixed position executable, discard space for
   9751 	 relocs against symbols which are not dynamic.  */
   9752       else if (h->type != STT_GNU_IFUNC)
   9753 	{
   9754 	  if (h->dynamic_adjusted
   9755 	      && !h->def_regular
   9756 	      && !ELF_COMMON_DEF_P (h))
   9757 	    {
   9758 	      /* Ensure we catch all the cases where this symbol
   9759 		 should be made dynamic.  */
   9760 	      if (!ensure_undef_dynamic (info, h))
   9761 		return FALSE;
   9762 
   9763 	      /* But if that didn't work out, discard dynamic relocs.  */
   9764 	      if (h->dynindx == -1)
   9765 		h->dyn_relocs = NULL;
   9766 	    }
   9767 	  else
   9768 	    h->dyn_relocs = NULL;
   9769 	}
   9770 
   9771       /* Finally, allocate space.  */
   9772       for (p = h->dyn_relocs; p != NULL; p = p->next)
   9773 	{
   9774 	  asection *sreloc = elf_section_data (p->sec)->sreloc;
   9775 	  if (eh->elf.type == STT_GNU_IFUNC)
   9776 	    sreloc = htab->elf.irelplt;
   9777 	  sreloc->size += p->count * sizeof (Elf64_External_Rela);
   9778 	}
   9779     }
   9780 
   9781   /* We might need a PLT entry when the symbol
   9782      a) is dynamic, or
   9783      b) is an ifunc, or
   9784      c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
   9785      d) has plt16 relocs and we are linking statically.  */
   9786   if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
   9787       || h->type == STT_GNU_IFUNC
   9788       || (h->needs_plt && h->dynamic_adjusted)
   9789       || (h->needs_plt
   9790 	  && h->def_regular
   9791 	  && !htab->elf.dynamic_sections_created
   9792 	  && !htab->can_convert_all_inline_plt
   9793 	  && (ppc_elf_hash_entry (h)->tls_mask
   9794 	      & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
   9795     {
   9796       struct plt_entry *pent;
   9797       bfd_boolean doneone = FALSE;
   9798       for (pent = h->plt.plist; pent != NULL; pent = pent->next)
   9799 	if (pent->plt.refcount > 0)
   9800 	  {
   9801 	    if (!htab->elf.dynamic_sections_created
   9802 		|| h->dynindx == -1)
   9803 	      {
   9804 		if (h->type == STT_GNU_IFUNC)
   9805 		  {
   9806 		    s = htab->elf.iplt;
   9807 		    pent->plt.offset = s->size;
   9808 		    s->size += PLT_ENTRY_SIZE (htab);
   9809 		    s = htab->elf.irelplt;
   9810 		  }
   9811 		else
   9812 		  {
   9813 		    s = htab->pltlocal;
   9814 		    pent->plt.offset = s->size;
   9815 		    s->size += LOCAL_PLT_ENTRY_SIZE (htab);
   9816 		    s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
   9817 		  }
   9818 	      }
   9819 	    else
   9820 	      {
   9821 		/* If this is the first .plt entry, make room for the special
   9822 		   first entry.  */
   9823 		s = htab->elf.splt;
   9824 		if (s->size == 0)
   9825 		  s->size += PLT_INITIAL_ENTRY_SIZE (htab);
   9826 
   9827 		pent->plt.offset = s->size;
   9828 
   9829 		/* Make room for this entry.  */
   9830 		s->size += PLT_ENTRY_SIZE (htab);
   9831 
   9832 		/* Make room for the .glink code.  */
   9833 		s = htab->glink;
   9834 		if (s->size == 0)
   9835 		  s->size += GLINK_PLTRESOLVE_SIZE (htab);
   9836 		if (htab->opd_abi)
   9837 		  {
   9838 		    /* We need bigger stubs past index 32767.  */
   9839 		    if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
   9840 		      s->size += 4;
   9841 		    s->size += 2*4;
   9842 		  }
   9843 		else
   9844 		  s->size += 4;
   9845 
   9846 		/* We also need to make an entry in the .rela.plt section.  */
   9847 		s = htab->elf.srelplt;
   9848 	      }
   9849 	    if (s != NULL)
   9850 	      s->size += sizeof (Elf64_External_Rela);
   9851 	    doneone = TRUE;
   9852 	  }
   9853 	else
   9854 	  pent->plt.offset = (bfd_vma) -1;
   9855       if (!doneone)
   9856 	{
   9857 	  h->plt.plist = NULL;
   9858 	  h->needs_plt = 0;
   9859 	}
   9860     }
   9861   else
   9862     {
   9863       h->plt.plist = NULL;
   9864       h->needs_plt = 0;
   9865     }
   9866 
   9867   return TRUE;
   9868 }
   9869 
   9870 #define PPC_LO(v) ((v) & 0xffff)
   9871 #define PPC_HI(v) (((v) >> 16) & 0xffff)
   9872 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
   9873 #define D34(v) \
   9874   ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
   9875 #define HA34(v) ((v + (1ULL << 33)) >> 34)
   9876 
   9877 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
   9878    to set up space for global entry stubs.  These are put in glink,
   9879    after the branch table.  */
   9880 
   9881 static bfd_boolean
   9882 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
   9883 {
   9884   struct bfd_link_info *info;
   9885   struct ppc_link_hash_table *htab;
   9886   struct plt_entry *pent;
   9887   asection *s, *plt;
   9888 
   9889   if (h->root.type == bfd_link_hash_indirect)
   9890     return TRUE;
   9891 
   9892   if (!h->pointer_equality_needed)
   9893     return TRUE;
   9894 
   9895   if (h->def_regular)
   9896     return TRUE;
   9897 
   9898   info = inf;
   9899   htab = ppc_hash_table (info);
   9900   if (htab == NULL)
   9901     return FALSE;
   9902 
   9903   s = htab->global_entry;
   9904   plt = htab->elf.splt;
   9905   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
   9906     if (pent->plt.offset != (bfd_vma) -1
   9907 	&& pent->addend == 0)
   9908       {
   9909 	/* For ELFv2, if this symbol is not defined in a regular file
   9910 	   and we are not generating a shared library or pie, then we
   9911 	   need to define the symbol in the executable on a call stub.
   9912 	   This is to avoid text relocations.  */
   9913 	bfd_vma off, stub_align, stub_off, stub_size;
   9914 	unsigned int align_power;
   9915 
   9916 	stub_size = 16;
   9917 	stub_off = s->size;
   9918 	if (htab->params->plt_stub_align >= 0)
   9919 	  align_power = htab->params->plt_stub_align;
   9920 	else
   9921 	  align_power = -htab->params->plt_stub_align;
   9922 	/* Setting section alignment is delayed until we know it is
   9923 	   non-empty.  Otherwise the .text output section will be
   9924 	   aligned at least to plt_stub_align even when no global
   9925 	   entry stubs are needed.  */
   9926 	if (s->alignment_power < align_power)
   9927 	  s->alignment_power = align_power;
   9928 	stub_align = (bfd_vma) 1 << align_power;
   9929 	if (htab->params->plt_stub_align >= 0
   9930 	    || ((((stub_off + stub_size - 1) & -stub_align)
   9931 		 - (stub_off & -stub_align))
   9932 		> ((stub_size - 1) & -stub_align)))
   9933 	  stub_off = (stub_off + stub_align - 1) & -stub_align;
   9934 	off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
   9935 	off -= stub_off + s->output_offset + s->output_section->vma;
   9936 	/* Note that for --plt-stub-align negative we have a possible
   9937 	   dependency between stub offset and size.  Break that
   9938 	   dependency by assuming the max stub size when calculating
   9939 	   the stub offset.  */
   9940 	if (PPC_HA (off) == 0)
   9941 	  stub_size -= 4;
   9942 	h->root.type = bfd_link_hash_defined;
   9943 	h->root.u.def.section = s;
   9944 	h->root.u.def.value = stub_off;
   9945 	s->size = stub_off + stub_size;
   9946 	break;
   9947       }
   9948   return TRUE;
   9949 }
   9950 
   9951 /* Set the sizes of the dynamic sections.  */
   9952 
   9953 static bfd_boolean
   9954 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
   9955 				 struct bfd_link_info *info)
   9956 {
   9957   struct ppc_link_hash_table *htab;
   9958   bfd *dynobj;
   9959   asection *s;
   9960   bfd_boolean relocs;
   9961   bfd *ibfd;
   9962   struct got_entry *first_tlsld;
   9963 
   9964   htab = ppc_hash_table (info);
   9965   if (htab == NULL)
   9966     return FALSE;
   9967 
   9968   dynobj = htab->elf.dynobj;
   9969   if (dynobj == NULL)
   9970     abort ();
   9971 
   9972   if (htab->elf.dynamic_sections_created)
   9973     {
   9974       /* Set the contents of the .interp section to the interpreter.  */
   9975       if (bfd_link_executable (info) && !info->nointerp)
   9976 	{
   9977 	  s = bfd_get_linker_section (dynobj, ".interp");
   9978 	  if (s == NULL)
   9979 	    abort ();
   9980 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
   9981 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
   9982 	}
   9983     }
   9984 
   9985   /* Set up .got offsets for local syms, and space for local dynamic
   9986      relocs.  */
   9987   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   9988     {
   9989       struct got_entry **lgot_ents;
   9990       struct got_entry **end_lgot_ents;
   9991       struct plt_entry **local_plt;
   9992       struct plt_entry **end_local_plt;
   9993       unsigned char *lgot_masks;
   9994       bfd_size_type locsymcount;
   9995       Elf_Internal_Shdr *symtab_hdr;
   9996 
   9997       if (!is_ppc64_elf (ibfd))
   9998 	continue;
   9999 
   10000       for (s = ibfd->sections; s != NULL; s = s->next)
   10001 	{
   10002 	  struct ppc_dyn_relocs *p;
   10003 
   10004 	  for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
   10005 	    {
   10006 	      if (!bfd_is_abs_section (p->sec)
   10007 		  && bfd_is_abs_section (p->sec->output_section))
   10008 		{
   10009 		  /* Input section has been discarded, either because
   10010 		     it is a copy of a linkonce section or due to
   10011 		     linker script /DISCARD/, so we'll be discarding
   10012 		     the relocs too.  */
   10013 		}
   10014 	      else if (p->count != 0)
   10015 		{
   10016 		  asection *srel = elf_section_data (p->sec)->sreloc;
   10017 		  if (p->ifunc)
   10018 		    srel = htab->elf.irelplt;
   10019 		  srel->size += p->count * sizeof (Elf64_External_Rela);
   10020 		  if ((p->sec->output_section->flags & SEC_READONLY) != 0)
   10021 		    info->flags |= DF_TEXTREL;
   10022 		}
   10023 	    }
   10024 	}
   10025 
   10026       lgot_ents = elf_local_got_ents (ibfd);
   10027       if (!lgot_ents)
   10028 	continue;
   10029 
   10030       symtab_hdr = &elf_symtab_hdr (ibfd);
   10031       locsymcount = symtab_hdr->sh_info;
   10032       end_lgot_ents = lgot_ents + locsymcount;
   10033       local_plt = (struct plt_entry **) end_lgot_ents;
   10034       end_local_plt = local_plt + locsymcount;
   10035       lgot_masks = (unsigned char *) end_local_plt;
   10036       s = ppc64_elf_tdata (ibfd)->got;
   10037       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
   10038 	{
   10039 	  struct got_entry **pent, *ent;
   10040 
   10041 	  pent = lgot_ents;
   10042 	  while ((ent = *pent) != NULL)
   10043 	    if (ent->got.refcount > 0)
   10044 	      {
   10045 		if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
   10046 		  {
   10047 		    ppc64_tlsld_got (ibfd)->got.refcount += 1;
   10048 		    *pent = ent->next;
   10049 		  }
   10050 		else
   10051 		  {
   10052 		    unsigned int ent_size = 8;
   10053 		    unsigned int rel_size = sizeof (Elf64_External_Rela);
   10054 
   10055 		    ent->got.offset = s->size;
   10056 		    if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
   10057 		      {
   10058 			ent_size *= 2;
   10059 			rel_size *= 2;
   10060 		      }
   10061 		    s->size += ent_size;
   10062 		    if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
   10063 		      {
   10064 			htab->elf.irelplt->size += rel_size;
   10065 			htab->got_reli_size += rel_size;
   10066 		      }
   10067 		    else if (bfd_link_pic (info)
   10068 			     && !(ent->tls_type != 0
   10069 				  && bfd_link_executable (info)))
   10070 		      {
   10071 			asection *srel = ppc64_elf_tdata (ibfd)->relgot;
   10072 			srel->size += rel_size;
   10073 		      }
   10074 		    pent = &ent->next;
   10075 		  }
   10076 	      }
   10077 	    else
   10078 	      *pent = ent->next;
   10079 	}
   10080 
   10081       /* Allocate space for plt calls to local syms.  */
   10082       lgot_masks = (unsigned char *) end_local_plt;
   10083       for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
   10084 	{
   10085 	  struct plt_entry *ent;
   10086 
   10087 	  for (ent = *local_plt; ent != NULL; ent = ent->next)
   10088 	    if (ent->plt.refcount > 0)
   10089 	      {
   10090 		if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
   10091 		  {
   10092 		    s = htab->elf.iplt;
   10093 		    ent->plt.offset = s->size;
   10094 		    s->size += PLT_ENTRY_SIZE (htab);
   10095 		    htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
   10096 		  }
   10097 		else if (htab->can_convert_all_inline_plt
   10098 			 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
   10099 		  ent->plt.offset = (bfd_vma) -1;
   10100 		else
   10101 		  {
   10102 		    s = htab->pltlocal;
   10103 		    ent->plt.offset = s->size;
   10104 		    s->size += LOCAL_PLT_ENTRY_SIZE (htab);
   10105 		    if (bfd_link_pic (info))
   10106 		      htab->relpltlocal->size += sizeof (Elf64_External_Rela);
   10107 		  }
   10108 	      }
   10109 	    else
   10110 	      ent->plt.offset = (bfd_vma) -1;
   10111 	}
   10112     }
   10113 
   10114   /* Allocate global sym .plt and .got entries, and space for global
   10115      sym dynamic relocs.  */
   10116   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
   10117 
   10118   if (!htab->opd_abi && !bfd_link_pic (info))
   10119     elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
   10120 
   10121   first_tlsld = NULL;
   10122   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   10123     {
   10124       struct got_entry *ent;
   10125 
   10126       if (!is_ppc64_elf (ibfd))
   10127 	continue;
   10128 
   10129       ent = ppc64_tlsld_got (ibfd);
   10130       if (ent->got.refcount > 0)
   10131 	{
   10132 	  if (!htab->do_multi_toc && first_tlsld != NULL)
   10133 	    {
   10134 	      ent->is_indirect = TRUE;
   10135 	      ent->got.ent = first_tlsld;
   10136 	    }
   10137 	  else
   10138 	    {
   10139 	      if (first_tlsld == NULL)
   10140 		first_tlsld = ent;
   10141 	      s = ppc64_elf_tdata (ibfd)->got;
   10142 	      ent->got.offset = s->size;
   10143 	      ent->owner = ibfd;
   10144 	      s->size += 16;
   10145 	      if (bfd_link_dll (info))
   10146 		{
   10147 		  asection *srel = ppc64_elf_tdata (ibfd)->relgot;
   10148 		  srel->size += sizeof (Elf64_External_Rela);
   10149 		}
   10150 	    }
   10151 	}
   10152       else
   10153 	ent->got.offset = (bfd_vma) -1;
   10154     }
   10155 
   10156   /* We now have determined the sizes of the various dynamic sections.
   10157      Allocate memory for them.  */
   10158   relocs = FALSE;
   10159   for (s = dynobj->sections; s != NULL; s = s->next)
   10160     {
   10161       if ((s->flags & SEC_LINKER_CREATED) == 0)
   10162 	continue;
   10163 
   10164       if (s == htab->brlt || s == htab->relbrlt)
   10165 	/* These haven't been allocated yet;  don't strip.  */
   10166 	continue;
   10167       else if (s == htab->elf.sgot
   10168 	       || s == htab->elf.splt
   10169 	       || s == htab->elf.iplt
   10170 	       || s == htab->pltlocal
   10171 	       || s == htab->glink
   10172 	       || s == htab->global_entry
   10173 	       || s == htab->elf.sdynbss
   10174 	       || s == htab->elf.sdynrelro)
   10175 	{
   10176 	  /* Strip this section if we don't need it; see the
   10177 	     comment below.  */
   10178 	}
   10179       else if (s == htab->glink_eh_frame)
   10180 	{
   10181 	  if (!bfd_is_abs_section (s->output_section))
   10182 	    /* Not sized yet.  */
   10183 	    continue;
   10184 	}
   10185       else if (CONST_STRNEQ (s->name, ".rela"))
   10186 	{
   10187 	  if (s->size != 0)
   10188 	    {
   10189 	      if (s != htab->elf.srelplt)
   10190 		relocs = TRUE;
   10191 
   10192 	      /* We use the reloc_count field as a counter if we need
   10193 		 to copy relocs into the output file.  */
   10194 	      s->reloc_count = 0;
   10195 	    }
   10196 	}
   10197       else
   10198 	{
   10199 	  /* It's not one of our sections, so don't allocate space.  */
   10200 	  continue;
   10201 	}
   10202 
   10203       if (s->size == 0)
   10204 	{
   10205 	  /* If we don't need this section, strip it from the
   10206 	     output file.  This is mostly to handle .rela.bss and
   10207 	     .rela.plt.  We must create both sections in
   10208 	     create_dynamic_sections, because they must be created
   10209 	     before the linker maps input sections to output
   10210 	     sections.  The linker does that before
   10211 	     adjust_dynamic_symbol is called, and it is that
   10212 	     function which decides whether anything needs to go
   10213 	     into these sections.  */
   10214 	  s->flags |= SEC_EXCLUDE;
   10215 	  continue;
   10216 	}
   10217 
   10218       if (bfd_is_abs_section (s->output_section))
   10219 	_bfd_error_handler (_("warning: discarding dynamic section %s"),
   10220 			    s->name);
   10221 
   10222       if ((s->flags & SEC_HAS_CONTENTS) == 0)
   10223 	continue;
   10224 
   10225       /* Allocate memory for the section contents.  We use bfd_zalloc
   10226 	 here in case unused entries are not reclaimed before the
   10227 	 section's contents are written out.  This should not happen,
   10228 	 but this way if it does we get a R_PPC64_NONE reloc in .rela
   10229 	 sections instead of garbage.
   10230 	 We also rely on the section contents being zero when writing
   10231 	 the GOT and .dynrelro.  */
   10232       s->contents = bfd_zalloc (dynobj, s->size);
   10233       if (s->contents == NULL)
   10234 	return FALSE;
   10235     }
   10236 
   10237   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   10238     {
   10239       if (!is_ppc64_elf (ibfd))
   10240 	continue;
   10241 
   10242       s = ppc64_elf_tdata (ibfd)->got;
   10243       if (s != NULL && s != htab->elf.sgot)
   10244 	{
   10245 	  if (s->size == 0)
   10246 	    s->flags |= SEC_EXCLUDE;
   10247 	  else
   10248 	    {
   10249 	      s->contents = bfd_zalloc (ibfd, s->size);
   10250 	      if (s->contents == NULL)
   10251 		return FALSE;
   10252 	    }
   10253 	}
   10254       s = ppc64_elf_tdata (ibfd)->relgot;
   10255       if (s != NULL)
   10256 	{
   10257 	  if (s->size == 0)
   10258 	    s->flags |= SEC_EXCLUDE;
   10259 	  else
   10260 	    {
   10261 	      s->contents = bfd_zalloc (ibfd, s->size);
   10262 	      if (s->contents == NULL)
   10263 		return FALSE;
   10264 	      relocs = TRUE;
   10265 	      s->reloc_count = 0;
   10266 	    }
   10267 	}
   10268     }
   10269 
   10270   if (htab->elf.dynamic_sections_created)
   10271     {
   10272       bfd_boolean tls_opt;
   10273 
   10274       /* Add some entries to the .dynamic section.  We fill in the
   10275 	 values later, in ppc64_elf_finish_dynamic_sections, but we
   10276 	 must add the entries now so that we get the correct size for
   10277 	 the .dynamic section.  The DT_DEBUG entry is filled in by the
   10278 	 dynamic linker and used by the debugger.  */
   10279 #define add_dynamic_entry(TAG, VAL) \
   10280   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
   10281 
   10282       if (bfd_link_executable (info))
   10283 	{
   10284 	  if (!add_dynamic_entry (DT_DEBUG, 0))
   10285 	    return FALSE;
   10286 	}
   10287 
   10288       if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
   10289 	{
   10290 	  if (!add_dynamic_entry (DT_PLTGOT, 0)
   10291 	      || !add_dynamic_entry (DT_PLTRELSZ, 0)
   10292 	      || !add_dynamic_entry (DT_PLTREL, DT_RELA)
   10293 	      || !add_dynamic_entry (DT_JMPREL, 0)
   10294 	      || !add_dynamic_entry (DT_PPC64_GLINK, 0))
   10295 	    return FALSE;
   10296 	}
   10297 
   10298       if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
   10299 	{
   10300 	  if (!add_dynamic_entry (DT_PPC64_OPD, 0)
   10301 	      || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
   10302 	    return FALSE;
   10303 	}
   10304 
   10305       tls_opt = (htab->params->tls_get_addr_opt
   10306 		 && ((htab->tls_get_addr_fd != NULL
   10307 		      && htab->tls_get_addr_fd->elf.plt.plist != NULL)
   10308 		     || (htab->tga_desc_fd != NULL
   10309 			 && htab->tga_desc_fd->elf.plt.plist != NULL)));
   10310       if (tls_opt || !htab->opd_abi)
   10311 	{
   10312 	  if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
   10313 	    return FALSE;
   10314 	}
   10315 
   10316       if (relocs)
   10317 	{
   10318 	  if (!add_dynamic_entry (DT_RELA, 0)
   10319 	      || !add_dynamic_entry (DT_RELASZ, 0)
   10320 	      || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
   10321 	    return FALSE;
   10322 
   10323 	  /* If any dynamic relocs apply to a read-only section,
   10324 	     then we need a DT_TEXTREL entry.  */
   10325 	  if ((info->flags & DF_TEXTREL) == 0)
   10326 	    elf_link_hash_traverse (&htab->elf,
   10327 				    _bfd_elf_maybe_set_textrel, info);
   10328 
   10329 	  if ((info->flags & DF_TEXTREL) != 0)
   10330 	    {
   10331 	      if (!add_dynamic_entry (DT_TEXTREL, 0))
   10332 		return FALSE;
   10333 	    }
   10334 	}
   10335     }
   10336 #undef add_dynamic_entry
   10337 
   10338   return TRUE;
   10339 }
   10340 
   10341 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
   10342 
   10343 static bfd_boolean
   10344 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
   10345 {
   10346   if (h->plt.plist != NULL
   10347       && !h->def_regular
   10348       && !h->pointer_equality_needed)
   10349     return FALSE;
   10350 
   10351   return _bfd_elf_hash_symbol (h);
   10352 }
   10353 
   10354 /* Determine the type of stub needed, if any, for a call.  */
   10355 
   10356 static inline enum ppc_stub_type
   10357 ppc_type_of_stub (asection *input_sec,
   10358 		  const Elf_Internal_Rela *rel,
   10359 		  struct ppc_link_hash_entry **hash,
   10360 		  struct plt_entry **plt_ent,
   10361 		  bfd_vma destination,
   10362 		  unsigned long local_off)
   10363 {
   10364   struct ppc_link_hash_entry *h = *hash;
   10365   bfd_vma location;
   10366   bfd_vma branch_offset;
   10367   bfd_vma max_branch_offset;
   10368   enum elf_ppc64_reloc_type r_type;
   10369 
   10370   if (h != NULL)
   10371     {
   10372       struct plt_entry *ent;
   10373       struct ppc_link_hash_entry *fdh = h;
   10374       if (h->oh != NULL
   10375 	  && h->oh->is_func_descriptor)
   10376 	{
   10377 	  fdh = ppc_follow_link (h->oh);
   10378 	  *hash = fdh;
   10379 	}
   10380 
   10381       for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
   10382 	if (ent->addend == rel->r_addend
   10383 	    && ent->plt.offset != (bfd_vma) -1)
   10384 	  {
   10385 	    *plt_ent = ent;
   10386 	    return ppc_stub_plt_call;
   10387 	  }
   10388 
   10389       /* Here, we know we don't have a plt entry.  If we don't have a
   10390 	 either a defined function descriptor or a defined entry symbol
   10391 	 in a regular object file, then it is pointless trying to make
   10392 	 any other type of stub.  */
   10393       if (!is_static_defined (&fdh->elf)
   10394 	  && !is_static_defined (&h->elf))
   10395 	return ppc_stub_none;
   10396     }
   10397   else if (elf_local_got_ents (input_sec->owner) != NULL)
   10398     {
   10399       Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
   10400       struct plt_entry **local_plt = (struct plt_entry **)
   10401 	elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
   10402       unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
   10403 
   10404       if (local_plt[r_symndx] != NULL)
   10405 	{
   10406 	  struct plt_entry *ent;
   10407 
   10408 	  for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
   10409 	    if (ent->addend == rel->r_addend
   10410 		&& ent->plt.offset != (bfd_vma) -1)
   10411 	      {
   10412 		*plt_ent = ent;
   10413 		return ppc_stub_plt_call;
   10414 	      }
   10415 	}
   10416     }
   10417 
   10418   /* Determine where the call point is.  */
   10419   location = (input_sec->output_offset
   10420 	      + input_sec->output_section->vma
   10421 	      + rel->r_offset);
   10422 
   10423   branch_offset = destination - location;
   10424   r_type = ELF64_R_TYPE (rel->r_info);
   10425 
   10426   /* Determine if a long branch stub is needed.  */
   10427   max_branch_offset = 1 << 25;
   10428   if (r_type == R_PPC64_REL14
   10429       || r_type == R_PPC64_REL14_BRTAKEN
   10430       || r_type == R_PPC64_REL14_BRNTAKEN)
   10431     max_branch_offset = 1 << 15;
   10432 
   10433   if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
   10434     /* We need a stub.  Figure out whether a long_branch or plt_branch
   10435        is needed later.  */
   10436     return ppc_stub_long_branch;
   10437 
   10438   return ppc_stub_none;
   10439 }
   10440 
   10441 /* Gets the address of a label (1:) in r11 and builds an offset in r12,
   10442    then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
   10443    .	mflr	%r12
   10444    .	bcl	20,31,1f
   10445    .1:	mflr	%r11
   10446    .	mtlr	%r12
   10447    .	lis	%r12,xxx-1b@highest
   10448    .	ori	%r12,%r12,xxx-1b@higher
   10449    .	sldi	%r12,%r12,32
   10450    .	oris	%r12,%r12,xxx-1b@high
   10451    .	ori	%r12,%r12,xxx-1b@l
   10452    .	add/ldx	%r12,%r11,%r12  */
   10453 
   10454 static bfd_byte *
   10455 build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bfd_boolean load)
   10456 {
   10457   bfd_put_32 (abfd, MFLR_R12, p);
   10458   p += 4;
   10459   bfd_put_32 (abfd, BCL_20_31, p);
   10460   p += 4;
   10461   bfd_put_32 (abfd, MFLR_R11, p);
   10462   p += 4;
   10463   bfd_put_32 (abfd, MTLR_R12, p);
   10464   p += 4;
   10465   if (off + 0x8000 < 0x10000)
   10466     {
   10467       if (load)
   10468 	bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
   10469       else
   10470 	bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
   10471       p += 4;
   10472     }
   10473   else if (off + 0x80008000ULL < 0x100000000ULL)
   10474     {
   10475       bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
   10476       p += 4;
   10477       if (load)
   10478 	bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
   10479       else
   10480 	bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
   10481       p += 4;
   10482     }
   10483   else
   10484     {
   10485       if (off + 0x800000000000ULL < 0x1000000000000ULL)
   10486 	{
   10487 	  bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
   10488 	  p += 4;
   10489 	}
   10490       else
   10491 	{
   10492 	  bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
   10493 	  p += 4;
   10494 	  if (((off >> 32) & 0xffff) != 0)
   10495 	    {
   10496 	      bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
   10497 	      p += 4;
   10498 	    }
   10499 	}
   10500       if (((off >> 32) & 0xffffffffULL) != 0)
   10501 	{
   10502 	  bfd_put_32 (abfd, SLDI_R12_R12_32, p);
   10503 	  p += 4;
   10504 	}
   10505       if (PPC_HI (off) != 0)
   10506 	{
   10507 	  bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
   10508 	  p += 4;
   10509 	}
   10510       if (PPC_LO (off) != 0)
   10511 	{
   10512 	  bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
   10513 	  p += 4;
   10514 	}
   10515       if (load)
   10516 	bfd_put_32 (abfd, LDX_R12_R11_R12, p);
   10517       else
   10518 	bfd_put_32 (abfd, ADD_R12_R11_R12, p);
   10519       p += 4;
   10520     }
   10521   return p;
   10522 }
   10523 
   10524 static unsigned int
   10525 size_offset (bfd_vma off)
   10526 {
   10527   unsigned int size;
   10528   if (off + 0x8000 < 0x10000)
   10529     size = 4;
   10530   else if (off + 0x80008000ULL < 0x100000000ULL)
   10531     size = 8;
   10532   else
   10533     {
   10534       if (off + 0x800000000000ULL < 0x1000000000000ULL)
   10535 	size = 4;
   10536       else
   10537 	{
   10538 	  size = 4;
   10539 	  if (((off >> 32) & 0xffff) != 0)
   10540 	    size += 4;
   10541 	}
   10542       if (((off >> 32) & 0xffffffffULL) != 0)
   10543 	size += 4;
   10544       if (PPC_HI (off) != 0)
   10545 	size += 4;
   10546       if (PPC_LO (off) != 0)
   10547 	size += 4;
   10548       size += 4;
   10549     }
   10550   return size + 16;
   10551 }
   10552 
   10553 static unsigned int
   10554 num_relocs_for_offset (bfd_vma off)
   10555 {
   10556   unsigned int num_rel;
   10557   if (off + 0x8000 < 0x10000)
   10558     num_rel = 1;
   10559   else if (off + 0x80008000ULL < 0x100000000ULL)
   10560     num_rel = 2;
   10561   else
   10562     {
   10563       num_rel = 1;
   10564       if (off + 0x800000000000ULL >= 0x1000000000000ULL
   10565 	  && ((off >> 32) & 0xffff) != 0)
   10566 	num_rel += 1;
   10567       if (PPC_HI (off) != 0)
   10568 	num_rel += 1;
   10569       if (PPC_LO (off) != 0)
   10570 	num_rel += 1;
   10571     }
   10572   return num_rel;
   10573 }
   10574 
   10575 static Elf_Internal_Rela *
   10576 emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
   10577 			bfd_vma roff, bfd_vma targ, bfd_vma off)
   10578 {
   10579   bfd_vma relative_targ = targ - (roff - 8);
   10580   if (bfd_big_endian (info->output_bfd))
   10581     roff += 2;
   10582   r->r_offset = roff;
   10583   r->r_addend = relative_targ + roff;
   10584   if (off + 0x8000 < 0x10000)
   10585     r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
   10586   else if (off + 0x80008000ULL < 0x100000000ULL)
   10587     {
   10588       r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
   10589       ++r;
   10590       roff += 4;
   10591       r->r_offset = roff;
   10592       r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
   10593       r->r_addend = relative_targ + roff;
   10594     }
   10595   else
   10596     {
   10597       if (off + 0x800000000000ULL < 0x1000000000000ULL)
   10598 	r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
   10599       else
   10600 	{
   10601 	  r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
   10602 	  if (((off >> 32) & 0xffff) != 0)
   10603 	    {
   10604 	      ++r;
   10605 	      roff += 4;
   10606 	      r->r_offset = roff;
   10607 	      r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
   10608 	      r->r_addend = relative_targ + roff;
   10609 	    }
   10610 	}
   10611       if (((off >> 32) & 0xffffffffULL) != 0)
   10612 	roff += 4;
   10613       if (PPC_HI (off) != 0)
   10614 	{
   10615 	  ++r;
   10616 	  roff += 4;
   10617 	  r->r_offset = roff;
   10618 	  r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
   10619 	  r->r_addend = relative_targ + roff;
   10620 	}
   10621       if (PPC_LO (off) != 0)
   10622 	{
   10623 	  ++r;
   10624 	  roff += 4;
   10625 	  r->r_offset = roff;
   10626 	  r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
   10627 	  r->r_addend = relative_targ + roff;
   10628 	}
   10629     }
   10630   return r;
   10631 }
   10632 
   10633 static bfd_byte *
   10634 build_power10_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
   10635 		      bfd_boolean load)
   10636 {
   10637   uint64_t insn;
   10638   if (off - odd + (1ULL << 33) < 1ULL << 34)
   10639     {
   10640       off -= odd;
   10641       if (odd)
   10642 	{
   10643 	  bfd_put_32 (abfd, NOP, p);
   10644 	  p += 4;
   10645 	}
   10646       if (load)
   10647 	insn = PLD_R12_PC;
   10648       else
   10649 	insn = PADDI_R12_PC;
   10650       insn |= D34 (off);
   10651       bfd_put_32 (abfd, insn >> 32, p);
   10652       p += 4;
   10653       bfd_put_32 (abfd, insn, p);
   10654     }
   10655   /* The minimum value for paddi is -0x200000000.  The minimum value
   10656      for li is -0x8000, which when shifted by 34 and added gives a
   10657      minimum value of -0x2000200000000.  The maximum value is
   10658      0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1.  */
   10659   else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
   10660     {
   10661       off -= 8 - odd;
   10662       bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
   10663       p += 4;
   10664       if (!odd)
   10665 	{
   10666 	  bfd_put_32 (abfd, SLDI_R11_R11_34, p);
   10667 	  p += 4;
   10668 	}
   10669       insn = PADDI_R12_PC | D34 (off);
   10670       bfd_put_32 (abfd, insn >> 32, p);
   10671       p += 4;
   10672       bfd_put_32 (abfd, insn, p);
   10673       p += 4;
   10674       if (odd)
   10675 	{
   10676 	  bfd_put_32 (abfd, SLDI_R11_R11_34, p);
   10677 	  p += 4;
   10678 	}
   10679       if (load)
   10680 	bfd_put_32 (abfd, LDX_R12_R11_R12, p);
   10681       else
   10682 	bfd_put_32 (abfd, ADD_R12_R11_R12, p);
   10683     }
   10684   else
   10685     {
   10686       off -= odd + 8;
   10687       bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
   10688       p += 4;
   10689       bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
   10690       p += 4;
   10691       if (odd)
   10692 	{
   10693 	  bfd_put_32 (abfd, SLDI_R11_R11_34, p);
   10694 	  p += 4;
   10695 	}
   10696       insn = PADDI_R12_PC | D34 (off);
   10697       bfd_put_32 (abfd, insn >> 32, p);
   10698       p += 4;
   10699       bfd_put_32 (abfd, insn, p);
   10700       p += 4;
   10701       if (!odd)
   10702 	{
   10703 	  bfd_put_32 (abfd, SLDI_R11_R11_34, p);
   10704 	  p += 4;
   10705 	}
   10706       if (load)
   10707 	bfd_put_32 (abfd, LDX_R12_R11_R12, p);
   10708       else
   10709 	bfd_put_32 (abfd, ADD_R12_R11_R12, p);
   10710     }
   10711   p += 4;
   10712   return p;
   10713 }
   10714 
   10715 static unsigned int
   10716 size_power10_offset (bfd_vma off, int odd)
   10717 {
   10718   if (off - odd + (1ULL << 33) < 1ULL << 34)
   10719     return odd + 8;
   10720   else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
   10721     return 20;
   10722   else
   10723     return 24;
   10724 }
   10725 
   10726 static unsigned int
   10727 num_relocs_for_power10_offset (bfd_vma off, int odd)
   10728 {
   10729   if (off - odd + (1ULL << 33) < 1ULL << 34)
   10730     return 1;
   10731   else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
   10732     return 2;
   10733   else
   10734     return 3;
   10735 }
   10736 
   10737 static Elf_Internal_Rela *
   10738 emit_relocs_for_power10_offset (struct bfd_link_info *info,
   10739 				Elf_Internal_Rela *r, bfd_vma roff,
   10740 				bfd_vma targ, bfd_vma off, int odd)
   10741 {
   10742   if (off - odd + (1ULL << 33) < 1ULL << 34)
   10743     roff += odd;
   10744   else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
   10745     {
   10746       int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
   10747       r->r_offset = roff + d_offset;
   10748       r->r_addend = targ + 8 - odd - d_offset;
   10749       r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
   10750       ++r;
   10751       roff += 8 - odd;
   10752     }
   10753   else
   10754     {
   10755       int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
   10756       r->r_offset = roff + d_offset;
   10757       r->r_addend = targ + 8 + odd - d_offset;
   10758       r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
   10759       ++r;
   10760       roff += 4;
   10761       r->r_offset = roff + d_offset;
   10762       r->r_addend = targ + 4 + odd - d_offset;
   10763       r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
   10764       ++r;
   10765       roff += 4 + odd;
   10766     }
   10767   r->r_offset = roff;
   10768   r->r_addend = targ;
   10769   r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
   10770   return r;
   10771 }
   10772 
   10773 /* Emit .eh_frame opcode to advance pc by DELTA.  */
   10774 
   10775 static bfd_byte *
   10776 eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
   10777 {
   10778   delta /= 4;
   10779   if (delta < 64)
   10780     *eh++ = DW_CFA_advance_loc + delta;
   10781   else if (delta < 256)
   10782     {
   10783       *eh++ = DW_CFA_advance_loc1;
   10784       *eh++ = delta;
   10785     }
   10786   else if (delta < 65536)
   10787     {
   10788       *eh++ = DW_CFA_advance_loc2;
   10789       bfd_put_16 (abfd, delta, eh);
   10790       eh += 2;
   10791     }
   10792   else
   10793     {
   10794       *eh++ = DW_CFA_advance_loc4;
   10795       bfd_put_32 (abfd, delta, eh);
   10796       eh += 4;
   10797     }
   10798   return eh;
   10799 }
   10800 
   10801 /* Size of required .eh_frame opcode to advance pc by DELTA.  */
   10802 
   10803 static unsigned int
   10804 eh_advance_size (unsigned int delta)
   10805 {
   10806   if (delta < 64 * 4)
   10807     /* DW_CFA_advance_loc+[1..63].  */
   10808     return 1;
   10809   if (delta < 256 * 4)
   10810     /* DW_CFA_advance_loc1, byte.  */
   10811     return 2;
   10812   if (delta < 65536 * 4)
   10813     /* DW_CFA_advance_loc2, 2 bytes.  */
   10814     return 3;
   10815   /* DW_CFA_advance_loc4, 4 bytes.  */
   10816   return 5;
   10817 }
   10818 
   10819 /* With power7 weakly ordered memory model, it is possible for ld.so
   10820    to update a plt entry in one thread and have another thread see a
   10821    stale zero toc entry.  To avoid this we need some sort of acquire
   10822    barrier in the call stub.  One solution is to make the load of the
   10823    toc word seem to appear to depend on the load of the function entry
   10824    word.  Another solution is to test for r2 being zero, and branch to
   10825    the appropriate glink entry if so.
   10826 
   10827    .	fake dep barrier	compare
   10828    .	ld 12,xxx(2)		ld 12,xxx(2)
   10829    .	mtctr 12		mtctr 12
   10830    .	xor 11,12,12		ld 2,xxx+8(2)
   10831    .	add 2,2,11		cmpldi 2,0
   10832    .	ld 2,xxx+8(2)		bnectr+
   10833    .	bctr			b <glink_entry>
   10834 
   10835    The solution involving the compare turns out to be faster, so
   10836    that's what we use unless the branch won't reach.  */
   10837 
   10838 #define ALWAYS_USE_FAKE_DEP 0
   10839 #define ALWAYS_EMIT_R2SAVE 0
   10840 
   10841 static inline unsigned int
   10842 plt_stub_size (struct ppc_link_hash_table *htab,
   10843 	       struct ppc_stub_hash_entry *stub_entry,
   10844 	       bfd_vma off)
   10845 {
   10846   unsigned size;
   10847 
   10848   if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
   10849     {
   10850       if (htab->params->power10_stubs != 0)
   10851 	{
   10852 	  bfd_vma start = (stub_entry->stub_offset
   10853 			   + stub_entry->group->stub_sec->output_offset
   10854 			   + stub_entry->group->stub_sec->output_section->vma);
   10855 	  if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
   10856 	    start += 4;
   10857 	  size = 8 + size_power10_offset (off, start & 4);
   10858 	}
   10859       else
   10860 	size = 8 + size_offset (off - 8);
   10861       if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
   10862 	size += 4;
   10863       return size;
   10864     }
   10865 
   10866   size = 12;
   10867   if (ALWAYS_EMIT_R2SAVE
   10868       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   10869     size += 4;
   10870   if (PPC_HA (off) != 0)
   10871     size += 4;
   10872   if (htab->opd_abi)
   10873     {
   10874       size += 4;
   10875       if (htab->params->plt_static_chain)
   10876 	size += 4;
   10877       if (htab->params->plt_thread_safe
   10878 	  && htab->elf.dynamic_sections_created
   10879 	  && stub_entry->h != NULL
   10880 	  && stub_entry->h->elf.dynindx != -1)
   10881 	size += 8;
   10882       if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
   10883 	size += 4;
   10884     }
   10885   if (stub_entry->h != NULL
   10886       && is_tls_get_addr (&stub_entry->h->elf, htab)
   10887       && htab->params->tls_get_addr_opt)
   10888     {
   10889       if (htab->params->no_tls_get_addr_regsave)
   10890 	{
   10891 	  size += 7 * 4;
   10892 	  if (stub_entry->stub_type == ppc_stub_plt_call_r2save)
   10893 	    size += 6 * 4;
   10894 	}
   10895       else
   10896 	{
   10897 	  size += 30 * 4;
   10898 	  if (stub_entry->stub_type == ppc_stub_plt_call_r2save)
   10899 	    size += 4;
   10900 	}
   10901     }
   10902   return size;
   10903 }
   10904 
   10905 /* Depending on the sign of plt_stub_align:
   10906    If positive, return the padding to align to a 2**plt_stub_align
   10907    boundary.
   10908    If negative, if this stub would cross fewer 2**plt_stub_align
   10909    boundaries if we align, then return the padding needed to do so.  */
   10910 
   10911 static inline unsigned int
   10912 plt_stub_pad (struct ppc_link_hash_table *htab,
   10913 	      struct ppc_stub_hash_entry *stub_entry,
   10914 	      bfd_vma plt_off)
   10915 {
   10916   int stub_align;
   10917   unsigned stub_size;
   10918   bfd_vma stub_off = stub_entry->group->stub_sec->size;
   10919 
   10920   if (htab->params->plt_stub_align >= 0)
   10921     {
   10922       stub_align = 1 << htab->params->plt_stub_align;
   10923       if ((stub_off & (stub_align - 1)) != 0)
   10924 	return stub_align - (stub_off & (stub_align - 1));
   10925       return 0;
   10926     }
   10927 
   10928   stub_align = 1 << -htab->params->plt_stub_align;
   10929   stub_size = plt_stub_size (htab, stub_entry, plt_off);
   10930   if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
   10931       > ((stub_size - 1) & -stub_align))
   10932     return stub_align - (stub_off & (stub_align - 1));
   10933   return 0;
   10934 }
   10935 
   10936 /* Build a .plt call stub.  */
   10937 
   10938 static inline bfd_byte *
   10939 build_plt_stub (struct ppc_link_hash_table *htab,
   10940 		struct ppc_stub_hash_entry *stub_entry,
   10941 		bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
   10942 {
   10943   bfd *obfd = htab->params->stub_bfd;
   10944   bfd_boolean plt_load_toc = htab->opd_abi;
   10945   bfd_boolean plt_static_chain = htab->params->plt_static_chain;
   10946   bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
   10947 				 && htab->elf.dynamic_sections_created
   10948 				 && stub_entry->h != NULL
   10949 				 && stub_entry->h->elf.dynindx != -1);
   10950   bfd_boolean use_fake_dep = plt_thread_safe;
   10951   bfd_vma cmp_branch_off = 0;
   10952 
   10953   if (!ALWAYS_USE_FAKE_DEP
   10954       && plt_load_toc
   10955       && plt_thread_safe
   10956       && !(stub_entry->h != NULL
   10957 	   && is_tls_get_addr (&stub_entry->h->elf, htab)
   10958 	   && htab->params->tls_get_addr_opt))
   10959     {
   10960       bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
   10961       bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
   10962 			  / PLT_ENTRY_SIZE (htab));
   10963       bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
   10964       bfd_vma to, from;
   10965 
   10966       if (pltindex > 32768)
   10967 	glinkoff += (pltindex - 32768) * 4;
   10968       to = (glinkoff
   10969 	    + htab->glink->output_offset
   10970 	    + htab->glink->output_section->vma);
   10971       from = (p - stub_entry->group->stub_sec->contents
   10972 	      + 4 * (ALWAYS_EMIT_R2SAVE
   10973 		     || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   10974 	      + 4 * (PPC_HA (offset) != 0)
   10975 	      + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
   10976 		     != PPC_HA (offset))
   10977 	      + 4 * (plt_static_chain != 0)
   10978 	      + 20
   10979 	      + stub_entry->group->stub_sec->output_offset
   10980 	      + stub_entry->group->stub_sec->output_section->vma);
   10981       cmp_branch_off = to - from;
   10982       use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
   10983     }
   10984 
   10985   if (PPC_HA (offset) != 0)
   10986     {
   10987       if (r != NULL)
   10988 	{
   10989 	  if (ALWAYS_EMIT_R2SAVE
   10990 	      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   10991 	    r[0].r_offset += 4;
   10992 	  r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
   10993 	  r[1].r_offset = r[0].r_offset + 4;
   10994 	  r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
   10995 	  r[1].r_addend = r[0].r_addend;
   10996 	  if (plt_load_toc)
   10997 	    {
   10998 	      if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
   10999 		{
   11000 		  r[2].r_offset = r[1].r_offset + 4;
   11001 		  r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
   11002 		  r[2].r_addend = r[0].r_addend;
   11003 		}
   11004 	      else
   11005 		{
   11006 		  r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
   11007 		  r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
   11008 		  r[2].r_addend = r[0].r_addend + 8;
   11009 		  if (plt_static_chain)
   11010 		    {
   11011 		      r[3].r_offset = r[2].r_offset + 4;
   11012 		      r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
   11013 		      r[3].r_addend = r[0].r_addend + 16;
   11014 		    }
   11015 		}
   11016 	    }
   11017 	}
   11018       if (ALWAYS_EMIT_R2SAVE
   11019 	  || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   11020 	bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),	p += 4;
   11021       if (plt_load_toc)
   11022 	{
   11023 	  bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p),	p += 4;
   11024 	  bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p),	p += 4;
   11025 	}
   11026       else
   11027 	{
   11028 	  bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p),	p += 4;
   11029 	  bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p),	p += 4;
   11030 	}
   11031       if (plt_load_toc
   11032 	  && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
   11033 	{
   11034 	  bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p),	p += 4;
   11035 	  offset = 0;
   11036 	}
   11037       bfd_put_32 (obfd, MTCTR_R12, p),				p += 4;
   11038       if (plt_load_toc)
   11039 	{
   11040 	  if (use_fake_dep)
   11041 	    {
   11042 	      bfd_put_32 (obfd, XOR_R2_R12_R12, p),		p += 4;
   11043 	      bfd_put_32 (obfd, ADD_R11_R11_R2, p),		p += 4;
   11044 	    }
   11045 	  bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
   11046 	  if (plt_static_chain)
   11047 	    bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
   11048 	}
   11049     }
   11050   else
   11051     {
   11052       if (r != NULL)
   11053 	{
   11054 	  if (ALWAYS_EMIT_R2SAVE
   11055 	      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   11056 	    r[0].r_offset += 4;
   11057 	  r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
   11058 	  if (plt_load_toc)
   11059 	    {
   11060 	      if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
   11061 		{
   11062 		  r[1].r_offset = r[0].r_offset + 4;
   11063 		  r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
   11064 		  r[1].r_addend = r[0].r_addend;
   11065 		}
   11066 	      else
   11067 		{
   11068 		  r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
   11069 		  r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
   11070 		  r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
   11071 		  if (plt_static_chain)
   11072 		    {
   11073 		      r[2].r_offset = r[1].r_offset + 4;
   11074 		      r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
   11075 		      r[2].r_addend = r[0].r_addend + 8;
   11076 		    }
   11077 		}
   11078 	    }
   11079 	}
   11080       if (ALWAYS_EMIT_R2SAVE
   11081 	  || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   11082 	bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),	p += 4;
   11083       bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p),	p += 4;
   11084       if (plt_load_toc
   11085 	  && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
   11086 	{
   11087 	  bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p),	p += 4;
   11088 	  offset = 0;
   11089 	}
   11090       bfd_put_32 (obfd, MTCTR_R12, p),				p += 4;
   11091       if (plt_load_toc)
   11092 	{
   11093 	  if (use_fake_dep)
   11094 	    {
   11095 	      bfd_put_32 (obfd, XOR_R11_R12_R12, p),		p += 4;
   11096 	      bfd_put_32 (obfd, ADD_R2_R2_R11, p),		p += 4;
   11097 	    }
   11098 	  if (plt_static_chain)
   11099 	    bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
   11100 	  bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
   11101 	}
   11102     }
   11103   if (plt_load_toc && plt_thread_safe && !use_fake_dep)
   11104     {
   11105       bfd_put_32 (obfd, CMPLDI_R2_0, p),			p += 4;
   11106       bfd_put_32 (obfd, BNECTR_P4, p),				p += 4;
   11107       bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
   11108     }
   11109   else
   11110     bfd_put_32 (obfd, BCTR, p),					p += 4;
   11111   return p;
   11112 }
   11113 
   11114 /* Build a special .plt call stub for __tls_get_addr.  */
   11115 
   11116 #define LD_R0_0R3	0xe8030000
   11117 #define LD_R12_0R3	0xe9830000
   11118 #define MR_R0_R3	0x7c601b78
   11119 #define CMPDI_R0_0	0x2c200000
   11120 #define ADD_R3_R12_R13	0x7c6c6a14
   11121 #define BEQLR		0x4d820020
   11122 #define MR_R3_R0	0x7c030378
   11123 #define BCTRL		0x4e800421
   11124 
   11125 static inline bfd_byte *
   11126 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
   11127 			 struct ppc_stub_hash_entry *stub_entry,
   11128 			 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
   11129 {
   11130   bfd *obfd = htab->params->stub_bfd;
   11131   bfd_byte *loc = p;
   11132   unsigned int i;
   11133 
   11134   bfd_put_32 (obfd, LD_R0_0R3 + 0, p),		p += 4;
   11135   bfd_put_32 (obfd, LD_R12_0R3 + 8, p),		p += 4;
   11136   bfd_put_32 (obfd, CMPDI_R0_0, p),		p += 4;
   11137   bfd_put_32 (obfd, MR_R0_R3, p),		p += 4;
   11138   bfd_put_32 (obfd, ADD_R3_R12_R13, p),		p += 4;
   11139   bfd_put_32 (obfd, BEQLR, p),			p += 4;
   11140   bfd_put_32 (obfd, MR_R3_R0, p),		p += 4;
   11141   if (htab->params->no_tls_get_addr_regsave)
   11142     {
   11143       if (r != NULL)
   11144 	r[0].r_offset += 7 * 4;
   11145       if (stub_entry->stub_type != ppc_stub_plt_call_r2save)
   11146 	return build_plt_stub (htab, stub_entry, p, offset, r);
   11147 
   11148       bfd_put_32 (obfd, MFLR_R0, p);
   11149       p += 4;
   11150       bfd_put_32 (obfd, STD_R0_0R1 + STK_LINKER (htab), p);
   11151       p += 4;
   11152 
   11153       if (r != NULL)
   11154 	r[0].r_offset += 2 * 4;
   11155       p = build_plt_stub (htab, stub_entry, p, offset, r);
   11156       bfd_put_32 (obfd, BCTRL, p - 4);
   11157 
   11158       bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
   11159       p += 4;
   11160       bfd_put_32 (obfd, LD_R0_0R1 + STK_LINKER (htab), p);
   11161       p += 4;
   11162       bfd_put_32 (obfd, MTLR_R0, p);
   11163       p += 4;
   11164       bfd_put_32 (obfd, BLR, p);
   11165       p += 4;
   11166     }
   11167   else
   11168     {
   11169       p = tls_get_addr_prologue (obfd, p, htab);
   11170 
   11171       if (r != NULL)
   11172 	r[0].r_offset += 18 * 4;
   11173 
   11174       p = build_plt_stub (htab, stub_entry, p, offset, r);
   11175       bfd_put_32 (obfd, BCTRL, p - 4);
   11176 
   11177       if (stub_entry->stub_type == ppc_stub_plt_call_r2save)
   11178 	{
   11179 	  bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
   11180 	  p += 4;
   11181 	}
   11182 
   11183       p = tls_get_addr_epilogue (obfd, p, htab);
   11184     }
   11185 
   11186   if (htab->glink_eh_frame != NULL
   11187       && htab->glink_eh_frame->size != 0)
   11188     {
   11189       bfd_byte *base, *eh;
   11190 
   11191       base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
   11192       eh = base + stub_entry->group->eh_size;
   11193       if (htab->params->no_tls_get_addr_regsave)
   11194 	{
   11195 	  unsigned int lr_used, delta;
   11196 	  lr_used = stub_entry->stub_offset + (p - 20 - loc);
   11197 	  delta = lr_used - stub_entry->group->lr_restore;
   11198 	  stub_entry->group->lr_restore = lr_used + 16;
   11199 	  eh = eh_advance (htab->elf.dynobj, eh, delta);
   11200 	  *eh++ = DW_CFA_offset_extended_sf;
   11201 	  *eh++ = 65;
   11202 	  *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
   11203 	  *eh++ = DW_CFA_advance_loc + 4;
   11204 	}
   11205       else
   11206 	{
   11207 	  unsigned int cfa_updt, delta;
   11208 	  /* After the bctrl, lr has been modified so we need to emit
   11209 	     .eh_frame info saying the return address is on the stack.  In
   11210 	     fact we must put the EH info at or before the call rather
   11211 	     than after it, because the EH info for a call needs to be
   11212 	     specified by that point.
   11213 	     See libgcc/unwind-dw2.c execute_cfa_program.
   11214 	     Any stack pointer update must be described immediately after
   11215 	     the instruction making the change, and since the stdu occurs
   11216 	     after saving regs we put all the reg saves and the cfa
   11217 	     change there.  */
   11218 	  cfa_updt = stub_entry->stub_offset + 18 * 4;
   11219 	  delta = cfa_updt - stub_entry->group->lr_restore;
   11220 	  stub_entry->group->lr_restore
   11221 	    = stub_entry->stub_offset + (p - loc) - 4;
   11222 	  eh = eh_advance (htab->elf.dynobj, eh, delta);
   11223 	  *eh++ = DW_CFA_def_cfa_offset;
   11224 	  if (htab->opd_abi)
   11225 	    {
   11226 	      *eh++ = 128;
   11227 	      *eh++ = 1;
   11228 	    }
   11229 	  else
   11230 	    *eh++ = 96;
   11231 	  *eh++ = DW_CFA_offset_extended_sf;
   11232 	  *eh++ = 65;
   11233 	  *eh++ = (-16 / 8) & 0x7f;
   11234 	  for (i = 4; i < 12; i++)
   11235 	    {
   11236 	      *eh++ = DW_CFA_offset + i;
   11237 	      *eh++ = (htab->opd_abi ? 13 : 12) - i;
   11238 	    }
   11239 	  *eh++ = (DW_CFA_advance_loc
   11240 		   + (stub_entry->group->lr_restore - 8 - cfa_updt) / 4);
   11241 	  *eh++ = DW_CFA_def_cfa_offset;
   11242 	  *eh++ = 0;
   11243 	  for (i = 4; i < 12; i++)
   11244 	    *eh++ = DW_CFA_restore + i;
   11245 	  *eh++ = DW_CFA_advance_loc + 2;
   11246 	}
   11247       *eh++ = DW_CFA_restore_extended;
   11248       *eh++ = 65;
   11249       stub_entry->group->eh_size = eh - base;
   11250     }
   11251   return p;
   11252 }
   11253 
   11254 static Elf_Internal_Rela *
   11255 get_relocs (asection *sec, int count)
   11256 {
   11257   Elf_Internal_Rela *relocs;
   11258   struct bfd_elf_section_data *elfsec_data;
   11259 
   11260   elfsec_data = elf_section_data (sec);
   11261   relocs = elfsec_data->relocs;
   11262   if (relocs == NULL)
   11263     {
   11264       bfd_size_type relsize;
   11265       relsize = sec->reloc_count * sizeof (*relocs);
   11266       relocs = bfd_alloc (sec->owner, relsize);
   11267       if (relocs == NULL)
   11268 	return NULL;
   11269       elfsec_data->relocs = relocs;
   11270       elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
   11271 					  sizeof (Elf_Internal_Shdr));
   11272       if (elfsec_data->rela.hdr == NULL)
   11273 	return NULL;
   11274       elfsec_data->rela.hdr->sh_size = (sec->reloc_count
   11275 					* sizeof (Elf64_External_Rela));
   11276       elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
   11277       sec->reloc_count = 0;
   11278     }
   11279   relocs += sec->reloc_count;
   11280   sec->reloc_count += count;
   11281   return relocs;
   11282 }
   11283 
   11284 /* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
   11285    forms, to the equivalent relocs against the global symbol given by
   11286    STUB_ENTRY->H.  */
   11287 
   11288 static bfd_boolean
   11289 use_global_in_relocs (struct ppc_link_hash_table *htab,
   11290 		      struct ppc_stub_hash_entry *stub_entry,
   11291 		      Elf_Internal_Rela *r, unsigned int num_rel)
   11292 {
   11293   struct elf_link_hash_entry **hashes;
   11294   unsigned long symndx;
   11295   struct ppc_link_hash_entry *h;
   11296   bfd_vma symval;
   11297 
   11298   /* Relocs are always against symbols in their own object file.  Fake
   11299      up global sym hashes for the stub bfd (which has no symbols).  */
   11300   hashes = elf_sym_hashes (htab->params->stub_bfd);
   11301   if (hashes == NULL)
   11302     {
   11303       bfd_size_type hsize;
   11304 
   11305       /* When called the first time, stub_globals will contain the
   11306 	 total number of symbols seen during stub sizing.  After
   11307 	 allocating, stub_globals is used as an index to fill the
   11308 	 hashes array.  */
   11309       hsize = (htab->stub_globals + 1) * sizeof (*hashes);
   11310       hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
   11311       if (hashes == NULL)
   11312 	return FALSE;
   11313       elf_sym_hashes (htab->params->stub_bfd) = hashes;
   11314       htab->stub_globals = 1;
   11315     }
   11316   symndx = htab->stub_globals++;
   11317   h = stub_entry->h;
   11318   hashes[symndx] = &h->elf;
   11319   if (h->oh != NULL && h->oh->is_func)
   11320     h = ppc_follow_link (h->oh);
   11321   BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
   11322 	      || h->elf.root.type == bfd_link_hash_defweak);
   11323   symval = defined_sym_val (&h->elf);
   11324   while (num_rel-- != 0)
   11325     {
   11326       r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
   11327       if (h->elf.root.u.def.section != stub_entry->target_section)
   11328 	{
   11329 	  /* H is an opd symbol.  The addend must be zero, and the
   11330 	     branch reloc is the only one we can convert.  */
   11331 	  r->r_addend = 0;
   11332 	  break;
   11333 	}
   11334       else
   11335 	r->r_addend -= symval;
   11336       --r;
   11337     }
   11338   return TRUE;
   11339 }
   11340 
   11341 static bfd_vma
   11342 get_r2off (struct bfd_link_info *info,
   11343 	   struct ppc_stub_hash_entry *stub_entry)
   11344 {
   11345   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   11346   bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
   11347 
   11348   if (r2off == 0)
   11349     {
   11350       /* Support linking -R objects.  Get the toc pointer from the
   11351 	 opd entry.  */
   11352       char buf[8];
   11353       if (!htab->opd_abi)
   11354 	return r2off;
   11355       asection *opd = stub_entry->h->elf.root.u.def.section;
   11356       bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
   11357 
   11358       if (strcmp (opd->name, ".opd") != 0
   11359 	  || opd->reloc_count != 0)
   11360 	{
   11361 	  info->callbacks->einfo
   11362 	    (_("%P: cannot find opd entry toc for `%pT'\n"),
   11363 	     stub_entry->h->elf.root.root.string);
   11364 	  bfd_set_error (bfd_error_bad_value);
   11365 	  return (bfd_vma) -1;
   11366 	}
   11367       if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
   11368 	return (bfd_vma) -1;
   11369       r2off = bfd_get_64 (opd->owner, buf);
   11370       r2off -= elf_gp (info->output_bfd);
   11371     }
   11372   r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
   11373   return r2off;
   11374 }
   11375 
   11376 static bfd_boolean
   11377 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
   11378 {
   11379   struct ppc_stub_hash_entry *stub_entry;
   11380   struct ppc_branch_hash_entry *br_entry;
   11381   struct bfd_link_info *info;
   11382   struct ppc_link_hash_table *htab;
   11383   bfd_byte *loc;
   11384   bfd_byte *p, *relp;
   11385   bfd_vma targ, off;
   11386   Elf_Internal_Rela *r;
   11387   asection *plt;
   11388   int num_rel;
   11389   int odd;
   11390 
   11391   /* Massage our args to the form they really have.  */
   11392   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
   11393   info = in_arg;
   11394 
   11395   /* Fail if the target section could not be assigned to an output
   11396      section.  The user should fix his linker script.  */
   11397   if (stub_entry->target_section != NULL
   11398       && stub_entry->target_section->output_section == NULL
   11399       && info->non_contiguous_regions)
   11400     info->callbacks->einfo (_("%F%P: Could not assign '%pA' to an output section. "
   11401 			      "Retry without --enable-non-contiguous-regions.\n"),
   11402 			    stub_entry->target_section);
   11403 
   11404   /* Same for the group.  */
   11405   if (stub_entry->group->stub_sec != NULL
   11406       && stub_entry->group->stub_sec->output_section == NULL
   11407       && info->non_contiguous_regions)
   11408     info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
   11409 			      "output section. Retry without "
   11410 			      "--enable-non-contiguous-regions.\n"),
   11411 			    stub_entry->group->stub_sec,
   11412 			    stub_entry->target_section);
   11413 
   11414   htab = ppc_hash_table (info);
   11415   if (htab == NULL)
   11416     return FALSE;
   11417 
   11418   BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
   11419   loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
   11420 
   11421   htab->stub_count[stub_entry->stub_type - 1] += 1;
   11422   switch (stub_entry->stub_type)
   11423     {
   11424     case ppc_stub_long_branch:
   11425     case ppc_stub_long_branch_r2off:
   11426       /* Branches are relative.  This is where we are going to.  */
   11427       targ = (stub_entry->target_value
   11428 	      + stub_entry->target_section->output_offset
   11429 	      + stub_entry->target_section->output_section->vma);
   11430       targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
   11431 
   11432       /* And this is where we are coming from.  */
   11433       off = (stub_entry->stub_offset
   11434 	     + stub_entry->group->stub_sec->output_offset
   11435 	     + stub_entry->group->stub_sec->output_section->vma);
   11436       off = targ - off;
   11437 
   11438       p = loc;
   11439       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
   11440 	{
   11441 	  bfd_vma r2off = get_r2off (info, stub_entry);
   11442 
   11443 	  if (r2off == (bfd_vma) -1)
   11444 	    {
   11445 	      htab->stub_error = TRUE;
   11446 	      return FALSE;
   11447 	    }
   11448 	  bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
   11449 	  p += 4;
   11450 	  if (PPC_HA (r2off) != 0)
   11451 	    {
   11452 	      bfd_put_32 (htab->params->stub_bfd,
   11453 			  ADDIS_R2_R2 | PPC_HA (r2off), p);
   11454 	      p += 4;
   11455 	    }
   11456 	  if (PPC_LO (r2off) != 0)
   11457 	    {
   11458 	      bfd_put_32 (htab->params->stub_bfd,
   11459 			  ADDI_R2_R2 | PPC_LO (r2off), p);
   11460 	      p += 4;
   11461 	    }
   11462 	  off -= p - loc;
   11463 	}
   11464       bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
   11465       p += 4;
   11466 
   11467       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
   11468 	{
   11469 	  _bfd_error_handler
   11470 	    (_("long branch stub `%s' offset overflow"),
   11471 	     stub_entry->root.string);
   11472 	  htab->stub_error = TRUE;
   11473 	  return FALSE;
   11474 	}
   11475 
   11476       if (info->emitrelocations)
   11477 	{
   11478 	  r = get_relocs (stub_entry->group->stub_sec, 1);
   11479 	  if (r == NULL)
   11480 	    return FALSE;
   11481 	  r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
   11482 	  r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
   11483 	  r->r_addend = targ;
   11484 	  if (stub_entry->h != NULL
   11485 	      && !use_global_in_relocs (htab, stub_entry, r, 1))
   11486 	    return FALSE;
   11487 	}
   11488       break;
   11489 
   11490     case ppc_stub_plt_branch:
   11491     case ppc_stub_plt_branch_r2off:
   11492       br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
   11493 					 stub_entry->root.string + 9,
   11494 					 FALSE, FALSE);
   11495       if (br_entry == NULL)
   11496 	{
   11497 	  _bfd_error_handler (_("can't find branch stub `%s'"),
   11498 			      stub_entry->root.string);
   11499 	  htab->stub_error = TRUE;
   11500 	  return FALSE;
   11501 	}
   11502 
   11503       targ = (stub_entry->target_value
   11504 	      + stub_entry->target_section->output_offset
   11505 	      + stub_entry->target_section->output_section->vma);
   11506       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
   11507 	targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
   11508 
   11509       bfd_put_64 (htab->brlt->owner, targ,
   11510 		  htab->brlt->contents + br_entry->offset);
   11511 
   11512       if (br_entry->iter == htab->stub_iteration)
   11513 	{
   11514 	  br_entry->iter = 0;
   11515 
   11516 	  if (htab->relbrlt != NULL)
   11517 	    {
   11518 	      /* Create a reloc for the branch lookup table entry.  */
   11519 	      Elf_Internal_Rela rela;
   11520 	      bfd_byte *rl;
   11521 
   11522 	      rela.r_offset = (br_entry->offset
   11523 			       + htab->brlt->output_offset
   11524 			       + htab->brlt->output_section->vma);
   11525 	      rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   11526 	      rela.r_addend = targ;
   11527 
   11528 	      rl = htab->relbrlt->contents;
   11529 	      rl += (htab->relbrlt->reloc_count++
   11530 		     * sizeof (Elf64_External_Rela));
   11531 	      bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
   11532 	    }
   11533 	  else if (info->emitrelocations)
   11534 	    {
   11535 	      r = get_relocs (htab->brlt, 1);
   11536 	      if (r == NULL)
   11537 		return FALSE;
   11538 	      /* brlt, being SEC_LINKER_CREATED does not go through the
   11539 		 normal reloc processing.  Symbols and offsets are not
   11540 		 translated from input file to output file form, so
   11541 		 set up the offset per the output file.  */
   11542 	      r->r_offset = (br_entry->offset
   11543 			     + htab->brlt->output_offset
   11544 			     + htab->brlt->output_section->vma);
   11545 	      r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   11546 	      r->r_addend = targ;
   11547 	    }
   11548 	}
   11549 
   11550       targ = (br_entry->offset
   11551 	      + htab->brlt->output_offset
   11552 	      + htab->brlt->output_section->vma);
   11553 
   11554       off = (elf_gp (info->output_bfd)
   11555 	     + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
   11556       off = targ - off;
   11557 
   11558       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
   11559 	{
   11560 	  info->callbacks->einfo
   11561 	    (_("%P: linkage table error against `%pT'\n"),
   11562 	     stub_entry->root.string);
   11563 	  bfd_set_error (bfd_error_bad_value);
   11564 	  htab->stub_error = TRUE;
   11565 	  return FALSE;
   11566 	}
   11567 
   11568       if (info->emitrelocations)
   11569 	{
   11570 	  r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
   11571 	  if (r == NULL)
   11572 	    return FALSE;
   11573 	  r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
   11574 	  if (bfd_big_endian (info->output_bfd))
   11575 	    r[0].r_offset += 2;
   11576 	  if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
   11577 	    r[0].r_offset += 4;
   11578 	  r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
   11579 	  r[0].r_addend = targ;
   11580 	  if (PPC_HA (off) != 0)
   11581 	    {
   11582 	      r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
   11583 	      r[1].r_offset = r[0].r_offset + 4;
   11584 	      r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
   11585 	      r[1].r_addend = r[0].r_addend;
   11586 	    }
   11587 	}
   11588 
   11589       p = loc;
   11590       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
   11591 	{
   11592 	  if (PPC_HA (off) != 0)
   11593 	    {
   11594 	      bfd_put_32 (htab->params->stub_bfd,
   11595 			  ADDIS_R12_R2 | PPC_HA (off), p);
   11596 	      p += 4;
   11597 	      bfd_put_32 (htab->params->stub_bfd,
   11598 			  LD_R12_0R12 | PPC_LO (off), p);
   11599 	    }
   11600 	  else
   11601 	    bfd_put_32 (htab->params->stub_bfd,
   11602 			LD_R12_0R2 | PPC_LO (off), p);
   11603 	}
   11604       else
   11605 	{
   11606 	  bfd_vma r2off = get_r2off (info, stub_entry);
   11607 
   11608 	  if (r2off == (bfd_vma) -1)
   11609 	    {
   11610 	      htab->stub_error = TRUE;
   11611 	      return FALSE;
   11612 	    }
   11613 
   11614 	  bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
   11615 	  p += 4;
   11616 	  if (PPC_HA (off) != 0)
   11617 	    {
   11618 	      bfd_put_32 (htab->params->stub_bfd,
   11619 			  ADDIS_R12_R2 | PPC_HA (off), p);
   11620 	      p += 4;
   11621 	      bfd_put_32 (htab->params->stub_bfd,
   11622 			  LD_R12_0R12 | PPC_LO (off), p);
   11623 	    }
   11624 	  else
   11625 	    bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
   11626 
   11627 	  if (PPC_HA (r2off) != 0)
   11628 	    {
   11629 	      p += 4;
   11630 	      bfd_put_32 (htab->params->stub_bfd,
   11631 			  ADDIS_R2_R2 | PPC_HA (r2off), p);
   11632 	    }
   11633 	  if (PPC_LO (r2off) != 0)
   11634 	    {
   11635 	      p += 4;
   11636 	      bfd_put_32 (htab->params->stub_bfd,
   11637 			  ADDI_R2_R2 | PPC_LO (r2off), p);
   11638 	    }
   11639 	}
   11640       p += 4;
   11641       bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
   11642       p += 4;
   11643       bfd_put_32 (htab->params->stub_bfd, BCTR, p);
   11644       p += 4;
   11645       break;
   11646 
   11647     case ppc_stub_long_branch_notoc:
   11648     case ppc_stub_long_branch_both:
   11649     case ppc_stub_plt_branch_notoc:
   11650     case ppc_stub_plt_branch_both:
   11651     case ppc_stub_plt_call_notoc:
   11652     case ppc_stub_plt_call_both:
   11653       p = loc;
   11654       off = (stub_entry->stub_offset
   11655 	     + stub_entry->group->stub_sec->output_offset
   11656 	     + stub_entry->group->stub_sec->output_section->vma);
   11657       if (stub_entry->stub_type == ppc_stub_long_branch_both
   11658 	  || stub_entry->stub_type == ppc_stub_plt_branch_both
   11659 	  || stub_entry->stub_type == ppc_stub_plt_call_both)
   11660 	{
   11661 	  off += 4;
   11662 	  bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
   11663 	  p += 4;
   11664 	}
   11665       if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
   11666 	{
   11667 	  targ = stub_entry->plt_ent->plt.offset & ~1;
   11668 	  if (targ >= (bfd_vma) -2)
   11669 	    abort ();
   11670 
   11671 	  plt = htab->elf.splt;
   11672 	  if (!htab->elf.dynamic_sections_created
   11673 	      || stub_entry->h == NULL
   11674 	      || stub_entry->h->elf.dynindx == -1)
   11675 	    {
   11676 	      if (stub_entry->symtype == STT_GNU_IFUNC)
   11677 		plt = htab->elf.iplt;
   11678 	      else
   11679 		plt = htab->pltlocal;
   11680 	    }
   11681 	  targ += plt->output_offset + plt->output_section->vma;
   11682 	}
   11683       else
   11684 	targ = (stub_entry->target_value
   11685 		+ stub_entry->target_section->output_offset
   11686 		+ stub_entry->target_section->output_section->vma);
   11687       odd = off & 4;
   11688       off = targ - off;
   11689 
   11690       relp = p;
   11691       num_rel = 0;
   11692       if (htab->params->power10_stubs != 0)
   11693 	{
   11694 	  bfd_boolean load = stub_entry->stub_type >= ppc_stub_plt_call_notoc;
   11695 	  p = build_power10_offset (htab->params->stub_bfd, p, off, odd, load);
   11696 	}
   11697       else
   11698 	{
   11699 	  /* The notoc stubs calculate their target (either a PLT entry or
   11700 	     the global entry point of a function) relative to the PC
   11701 	     returned by the "bcl" two instructions past the start of the
   11702 	     sequence emitted by build_offset.  The offset is therefore 8
   11703 	     less than calculated from the start of the sequence.  */
   11704 	  off -= 8;
   11705 	  p = build_offset (htab->params->stub_bfd, p, off,
   11706 			    stub_entry->stub_type >= ppc_stub_plt_call_notoc);
   11707 	}
   11708 
   11709       if (stub_entry->stub_type <= ppc_stub_long_branch_both)
   11710 	{
   11711 	  bfd_vma from;
   11712 	  num_rel = 1;
   11713 	  from = (stub_entry->stub_offset
   11714 		  + stub_entry->group->stub_sec->output_offset
   11715 		  + stub_entry->group->stub_sec->output_section->vma
   11716 		  + (p - loc));
   11717 	  bfd_put_32 (htab->params->stub_bfd,
   11718 		      B_DOT | ((targ - from) & 0x3fffffc), p);
   11719 	}
   11720       else
   11721 	{
   11722 	  bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
   11723 	  p += 4;
   11724 	  bfd_put_32 (htab->params->stub_bfd, BCTR, p);
   11725 	}
   11726       p += 4;
   11727 
   11728       if (info->emitrelocations)
   11729 	{
   11730 	  bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
   11731 	  if (htab->params->power10_stubs != 0)
   11732 	    num_rel += num_relocs_for_power10_offset (off, odd);
   11733 	  else
   11734 	    {
   11735 	      num_rel += num_relocs_for_offset (off);
   11736 	      roff += 16;
   11737 	    }
   11738 	  r = get_relocs (stub_entry->group->stub_sec, num_rel);
   11739 	  if (r == NULL)
   11740 	    return FALSE;
   11741 	  if (htab->params->power10_stubs != 0)
   11742 	    r = emit_relocs_for_power10_offset (info, r, roff, targ, off, odd);
   11743 	  else
   11744 	    r = emit_relocs_for_offset (info, r, roff, targ, off);
   11745 	  if (stub_entry->stub_type == ppc_stub_long_branch_notoc
   11746 	      || stub_entry->stub_type == ppc_stub_long_branch_both)
   11747 	    {
   11748 	      ++r;
   11749 	      roff = p - 4 - stub_entry->group->stub_sec->contents;
   11750 	      r->r_offset = roff;
   11751 	      r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
   11752 	      r->r_addend = targ;
   11753 	      if (stub_entry->h != NULL
   11754 		  && !use_global_in_relocs (htab, stub_entry, r, num_rel))
   11755 		return FALSE;
   11756 	    }
   11757 	}
   11758 
   11759       if (htab->params->power10_stubs == 0
   11760 	  && htab->glink_eh_frame != NULL
   11761 	  && htab->glink_eh_frame->size != 0)
   11762 	{
   11763 	  bfd_byte *base, *eh;
   11764 	  unsigned int lr_used, delta;
   11765 
   11766 	  base = (htab->glink_eh_frame->contents
   11767 		  + stub_entry->group->eh_base + 17);
   11768 	  eh = base + stub_entry->group->eh_size;
   11769 	  lr_used = stub_entry->stub_offset + 8;
   11770 	  if (stub_entry->stub_type == ppc_stub_long_branch_both
   11771 	      || stub_entry->stub_type == ppc_stub_plt_branch_both
   11772 	      || stub_entry->stub_type == ppc_stub_plt_call_both)
   11773 	    lr_used += 4;
   11774 	  delta = lr_used - stub_entry->group->lr_restore;
   11775 	  stub_entry->group->lr_restore = lr_used + 8;
   11776 	  eh = eh_advance (htab->elf.dynobj, eh, delta);
   11777 	  *eh++ = DW_CFA_register;
   11778 	  *eh++ = 65;
   11779 	  *eh++ = 12;
   11780 	  *eh++ = DW_CFA_advance_loc + 2;
   11781 	  *eh++ = DW_CFA_restore_extended;
   11782 	  *eh++ = 65;
   11783 	  stub_entry->group->eh_size = eh - base;
   11784 	}
   11785       break;
   11786 
   11787     case ppc_stub_plt_call:
   11788     case ppc_stub_plt_call_r2save:
   11789       if (stub_entry->h != NULL
   11790 	  && stub_entry->h->is_func_descriptor
   11791 	  && stub_entry->h->oh != NULL)
   11792 	{
   11793 	  struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
   11794 
   11795 	  /* If the old-ABI "dot-symbol" is undefined make it weak so
   11796 	     we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.  */
   11797 	  if (fh->elf.root.type == bfd_link_hash_undefined
   11798 	      && (stub_entry->h->elf.root.type == bfd_link_hash_defined
   11799 		  || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
   11800 	    fh->elf.root.type = bfd_link_hash_undefweak;
   11801 	}
   11802 
   11803       /* Now build the stub.  */
   11804       targ = stub_entry->plt_ent->plt.offset & ~1;
   11805       if (targ >= (bfd_vma) -2)
   11806 	abort ();
   11807 
   11808       plt = htab->elf.splt;
   11809       if (!htab->elf.dynamic_sections_created
   11810 	  || stub_entry->h == NULL
   11811 	  || stub_entry->h->elf.dynindx == -1)
   11812 	{
   11813 	  if (stub_entry->symtype == STT_GNU_IFUNC)
   11814 	    plt = htab->elf.iplt;
   11815 	  else
   11816 	    plt = htab->pltlocal;
   11817 	}
   11818       targ += plt->output_offset + plt->output_section->vma;
   11819 
   11820       off = (elf_gp (info->output_bfd)
   11821 	     + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
   11822       off = targ - off;
   11823 
   11824       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
   11825 	{
   11826 	  info->callbacks->einfo
   11827 	    /* xgettext:c-format */
   11828 	    (_("%P: linkage table error against `%pT'\n"),
   11829 	     stub_entry->h != NULL
   11830 	     ? stub_entry->h->elf.root.root.string
   11831 	     : "<local sym>");
   11832 	  bfd_set_error (bfd_error_bad_value);
   11833 	  htab->stub_error = TRUE;
   11834 	  return FALSE;
   11835 	}
   11836 
   11837       r = NULL;
   11838       if (info->emitrelocations)
   11839 	{
   11840 	  r = get_relocs (stub_entry->group->stub_sec,
   11841 			  ((PPC_HA (off) != 0)
   11842 			   + (htab->opd_abi
   11843 			      ? 2 + (htab->params->plt_static_chain
   11844 				     && PPC_HA (off + 16) == PPC_HA (off))
   11845 			      : 1)));
   11846 	  if (r == NULL)
   11847 	    return FALSE;
   11848 	  r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
   11849 	  if (bfd_big_endian (info->output_bfd))
   11850 	    r[0].r_offset += 2;
   11851 	  r[0].r_addend = targ;
   11852 	}
   11853       if (stub_entry->h != NULL
   11854 	  && is_tls_get_addr (&stub_entry->h->elf, htab)
   11855 	  && htab->params->tls_get_addr_opt)
   11856 	p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
   11857       else
   11858 	p = build_plt_stub (htab, stub_entry, loc, off, r);
   11859       break;
   11860 
   11861     case ppc_stub_save_res:
   11862       return TRUE;
   11863 
   11864     default:
   11865       BFD_FAIL ();
   11866       return FALSE;
   11867     }
   11868 
   11869   stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
   11870 
   11871   if (htab->params->emit_stub_syms)
   11872     {
   11873       struct elf_link_hash_entry *h;
   11874       size_t len1, len2;
   11875       char *name;
   11876       const char *const stub_str[] = { "long_branch",
   11877 				       "long_branch",
   11878 				       "long_branch",
   11879 				       "long_branch",
   11880 				       "plt_branch",
   11881 				       "plt_branch",
   11882 				       "plt_branch",
   11883 				       "plt_branch",
   11884 				       "plt_call",
   11885 				       "plt_call",
   11886 				       "plt_call",
   11887 				       "plt_call" };
   11888 
   11889       len1 = strlen (stub_str[stub_entry->stub_type - 1]);
   11890       len2 = strlen (stub_entry->root.string);
   11891       name = bfd_malloc (len1 + len2 + 2);
   11892       if (name == NULL)
   11893 	return FALSE;
   11894       memcpy (name, stub_entry->root.string, 9);
   11895       memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
   11896       memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
   11897       h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
   11898       if (h == NULL)
   11899 	return FALSE;
   11900       if (h->root.type == bfd_link_hash_new)
   11901 	{
   11902 	  h->root.type = bfd_link_hash_defined;
   11903 	  h->root.u.def.section = stub_entry->group->stub_sec;
   11904 	  h->root.u.def.value = stub_entry->stub_offset;
   11905 	  h->ref_regular = 1;
   11906 	  h->def_regular = 1;
   11907 	  h->ref_regular_nonweak = 1;
   11908 	  h->forced_local = 1;
   11909 	  h->non_elf = 0;
   11910 	  h->root.linker_def = 1;
   11911 	}
   11912     }
   11913 
   11914   return TRUE;
   11915 }
   11916 
   11917 /* As above, but don't actually build the stub.  Just bump offset so
   11918    we know stub section sizes, and select plt_branch stubs where
   11919    long_branch stubs won't do.  */
   11920 
   11921 static bfd_boolean
   11922 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
   11923 {
   11924   struct ppc_stub_hash_entry *stub_entry;
   11925   struct bfd_link_info *info;
   11926   struct ppc_link_hash_table *htab;
   11927   asection *plt;
   11928   bfd_vma targ, off, r2off;
   11929   unsigned int size, extra, lr_used, delta, odd;
   11930 
   11931   /* Massage our args to the form they really have.  */
   11932   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
   11933   info = in_arg;
   11934 
   11935   htab = ppc_hash_table (info);
   11936   if (htab == NULL)
   11937     return FALSE;
   11938 
   11939   /* Fail if the target section could not be assigned to an output
   11940      section.  The user should fix his linker script.  */
   11941   if (stub_entry->target_section != NULL
   11942       && stub_entry->target_section->output_section == NULL
   11943       && info->non_contiguous_regions)
   11944     info->callbacks->einfo (_("%F%P: Could not assign %pA to an output section. "
   11945 			      "Retry without --enable-non-contiguous-regions.\n"),
   11946 			    stub_entry->target_section);
   11947 
   11948   /* Same for the group.  */
   11949   if (stub_entry->group->stub_sec != NULL
   11950       && stub_entry->group->stub_sec->output_section == NULL
   11951       && info->non_contiguous_regions)
   11952     info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
   11953 			      "output section. Retry without "
   11954 			      "--enable-non-contiguous-regions.\n"),
   11955 			    stub_entry->group->stub_sec,
   11956 			    stub_entry->target_section);
   11957 
   11958   /* Make a note of the offset within the stubs for this entry.  */
   11959   stub_entry->stub_offset = stub_entry->group->stub_sec->size;
   11960 
   11961   if (stub_entry->h != NULL
   11962       && stub_entry->h->save_res
   11963       && stub_entry->h->elf.root.type == bfd_link_hash_defined
   11964       && stub_entry->h->elf.root.u.def.section == htab->sfpr)
   11965     {
   11966       /* Don't make stubs to out-of-line register save/restore
   11967 	 functions.  Instead, emit copies of the functions.  */
   11968       stub_entry->group->needs_save_res = 1;
   11969       stub_entry->stub_type = ppc_stub_save_res;
   11970       return TRUE;
   11971     }
   11972 
   11973   switch (stub_entry->stub_type)
   11974     {
   11975     case ppc_stub_plt_branch:
   11976     case ppc_stub_plt_branch_r2off:
   11977       /* Reset the stub type from the plt branch variant in case we now
   11978 	 can reach with a shorter stub.  */
   11979       stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
   11980       /* Fall through.  */
   11981     case ppc_stub_long_branch:
   11982     case ppc_stub_long_branch_r2off:
   11983       targ = (stub_entry->target_value
   11984 	      + stub_entry->target_section->output_offset
   11985 	      + stub_entry->target_section->output_section->vma);
   11986       targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
   11987       off = (stub_entry->stub_offset
   11988 	     + stub_entry->group->stub_sec->output_offset
   11989 	     + stub_entry->group->stub_sec->output_section->vma);
   11990 
   11991       size = 4;
   11992       r2off = 0;
   11993       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
   11994 	{
   11995 	  r2off = get_r2off (info, stub_entry);
   11996 	  if (r2off == (bfd_vma) -1)
   11997 	    {
   11998 	      htab->stub_error = TRUE;
   11999 	      return FALSE;
   12000 	    }
   12001 	  size = 8;
   12002 	  if (PPC_HA (r2off) != 0)
   12003 	    size += 4;
   12004 	  if (PPC_LO (r2off) != 0)
   12005 	    size += 4;
   12006 	  off += size - 4;
   12007 	}
   12008       off = targ - off;
   12009 
   12010       /* If the branch offset is too big, use a ppc_stub_plt_branch.
   12011 	 Do the same for -R objects without function descriptors.  */
   12012       if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
   12013 	   && r2off == 0
   12014 	   && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
   12015 	  || off + (1 << 25) >= (bfd_vma) (1 << 26))
   12016 	{
   12017 	  struct ppc_branch_hash_entry *br_entry;
   12018 
   12019 	  br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
   12020 					     stub_entry->root.string + 9,
   12021 					     TRUE, FALSE);
   12022 	  if (br_entry == NULL)
   12023 	    {
   12024 	      _bfd_error_handler (_("can't build branch stub `%s'"),
   12025 				  stub_entry->root.string);
   12026 	      htab->stub_error = TRUE;
   12027 	      return FALSE;
   12028 	    }
   12029 
   12030 	  if (br_entry->iter != htab->stub_iteration)
   12031 	    {
   12032 	      br_entry->iter = htab->stub_iteration;
   12033 	      br_entry->offset = htab->brlt->size;
   12034 	      htab->brlt->size += 8;
   12035 
   12036 	      if (htab->relbrlt != NULL)
   12037 		htab->relbrlt->size += sizeof (Elf64_External_Rela);
   12038 	      else if (info->emitrelocations)
   12039 		{
   12040 		  htab->brlt->reloc_count += 1;
   12041 		  htab->brlt->flags |= SEC_RELOC;
   12042 		}
   12043 	    }
   12044 
   12045 	  targ = (br_entry->offset
   12046 		  + htab->brlt->output_offset
   12047 		  + htab->brlt->output_section->vma);
   12048 	  off = (elf_gp (info->output_bfd)
   12049 		 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
   12050 	  off = targ - off;
   12051 
   12052 	  if (info->emitrelocations)
   12053 	    {
   12054 	      stub_entry->group->stub_sec->reloc_count
   12055 		+= 1 + (PPC_HA (off) != 0);
   12056 	      stub_entry->group->stub_sec->flags |= SEC_RELOC;
   12057 	    }
   12058 
   12059 	  stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
   12060 	  if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
   12061 	    {
   12062 	      size = 12;
   12063 	      if (PPC_HA (off) != 0)
   12064 		size = 16;
   12065 	    }
   12066 	  else
   12067 	    {
   12068 	      size = 16;
   12069 	      if (PPC_HA (off) != 0)
   12070 		size += 4;
   12071 
   12072 	      if (PPC_HA (r2off) != 0)
   12073 		size += 4;
   12074 	      if (PPC_LO (r2off) != 0)
   12075 		size += 4;
   12076 	    }
   12077 	}
   12078       else if (info->emitrelocations)
   12079 	{
   12080 	  stub_entry->group->stub_sec->reloc_count += 1;
   12081 	  stub_entry->group->stub_sec->flags |= SEC_RELOC;
   12082 	}
   12083       break;
   12084 
   12085     case ppc_stub_plt_branch_notoc:
   12086     case ppc_stub_plt_branch_both:
   12087       stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
   12088       /* Fall through.  */
   12089     case ppc_stub_long_branch_notoc:
   12090     case ppc_stub_long_branch_both:
   12091       off = (stub_entry->stub_offset
   12092 	     + stub_entry->group->stub_sec->output_offset
   12093 	     + stub_entry->group->stub_sec->output_section->vma);
   12094       size = 0;
   12095       if (stub_entry->stub_type == ppc_stub_long_branch_both)
   12096 	size = 4;
   12097       off += size;
   12098       targ = (stub_entry->target_value
   12099 	      + stub_entry->target_section->output_offset
   12100 	      + stub_entry->target_section->output_section->vma);
   12101       odd = off & 4;
   12102       off = targ - off;
   12103 
   12104       if (info->emitrelocations)
   12105 	{
   12106 	  unsigned int num_rel;
   12107 	  if (htab->params->power10_stubs != 0)
   12108 	    num_rel = num_relocs_for_power10_offset (off, odd);
   12109 	  else
   12110 	    num_rel = num_relocs_for_offset (off - 8);
   12111 	  stub_entry->group->stub_sec->reloc_count += num_rel;
   12112 	  stub_entry->group->stub_sec->flags |= SEC_RELOC;
   12113 	}
   12114 
   12115       if (htab->params->power10_stubs != 0)
   12116 	extra = size_power10_offset (off, odd);
   12117       else
   12118 	extra = size_offset (off - 8);
   12119       /* Include branch insn plus those in the offset sequence.  */
   12120       size += 4 + extra;
   12121       /* The branch insn is at the end, or "extra" bytes along.  So
   12122 	 its offset will be "extra" bytes less that that already
   12123 	 calculated.  */
   12124       off -= extra;
   12125 
   12126       if (htab->params->power10_stubs == 0)
   12127 	{
   12128 	  /* After the bcl, lr has been modified so we need to emit
   12129 	     .eh_frame info saying the return address is in r12.  */
   12130 	  lr_used = stub_entry->stub_offset + 8;
   12131 	  if (stub_entry->stub_type == ppc_stub_long_branch_both)
   12132 	    lr_used += 4;
   12133 	  /* The eh_frame info will consist of a DW_CFA_advance_loc or
   12134 	     variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
   12135 	     DW_CFA_restore_extended 65.  */
   12136 	  delta = lr_used - stub_entry->group->lr_restore;
   12137 	  stub_entry->group->eh_size += eh_advance_size (delta) + 6;
   12138 	  stub_entry->group->lr_restore = lr_used + 8;
   12139 	}
   12140 
   12141       /* If the branch can't reach, use a plt_branch.  */
   12142       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
   12143 	{
   12144 	  stub_entry->stub_type += (ppc_stub_plt_branch_notoc
   12145 				    - ppc_stub_long_branch_notoc);
   12146 	  size += 4;
   12147 	}
   12148       else if (info->emitrelocations)
   12149 	stub_entry->group->stub_sec->reloc_count +=1;
   12150       break;
   12151 
   12152     case ppc_stub_plt_call_notoc:
   12153     case ppc_stub_plt_call_both:
   12154       off = (stub_entry->stub_offset
   12155 	     + stub_entry->group->stub_sec->output_offset
   12156 	     + stub_entry->group->stub_sec->output_section->vma);
   12157       if (stub_entry->stub_type == ppc_stub_plt_call_both)
   12158 	off += 4;
   12159       targ = stub_entry->plt_ent->plt.offset & ~1;
   12160       if (targ >= (bfd_vma) -2)
   12161 	abort ();
   12162 
   12163       plt = htab->elf.splt;
   12164       if (!htab->elf.dynamic_sections_created
   12165 	  || stub_entry->h == NULL
   12166 	  || stub_entry->h->elf.dynindx == -1)
   12167 	{
   12168 	  if (stub_entry->symtype == STT_GNU_IFUNC)
   12169 	    plt = htab->elf.iplt;
   12170 	  else
   12171 	    plt = htab->pltlocal;
   12172 	}
   12173       targ += plt->output_offset + plt->output_section->vma;
   12174       odd = off & 4;
   12175       off = targ - off;
   12176 
   12177       if (htab->params->plt_stub_align != 0)
   12178 	{
   12179 	  unsigned pad = plt_stub_pad (htab, stub_entry, off);
   12180 
   12181 	  stub_entry->group->stub_sec->size += pad;
   12182 	  stub_entry->stub_offset = stub_entry->group->stub_sec->size;
   12183 	  off -= pad;
   12184 	}
   12185 
   12186       if (info->emitrelocations)
   12187 	{
   12188 	  unsigned int num_rel;
   12189 	  if (htab->params->power10_stubs != 0)
   12190 	    num_rel = num_relocs_for_power10_offset (off, odd);
   12191 	  else
   12192 	    num_rel = num_relocs_for_offset (off - 8);
   12193 	  stub_entry->group->stub_sec->reloc_count += num_rel;
   12194 	  stub_entry->group->stub_sec->flags |= SEC_RELOC;
   12195 	}
   12196 
   12197       size = plt_stub_size (htab, stub_entry, off);
   12198 
   12199       if (htab->params->power10_stubs == 0)
   12200 	{
   12201 	  /* After the bcl, lr has been modified so we need to emit
   12202 	     .eh_frame info saying the return address is in r12.  */
   12203 	  lr_used = stub_entry->stub_offset + 8;
   12204 	  if (stub_entry->stub_type == ppc_stub_plt_call_both)
   12205 	    lr_used += 4;
   12206 	  /* The eh_frame info will consist of a DW_CFA_advance_loc or
   12207 	     variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
   12208 	     DW_CFA_restore_extended 65.  */
   12209 	  delta = lr_used - stub_entry->group->lr_restore;
   12210 	  stub_entry->group->eh_size += eh_advance_size (delta) + 6;
   12211 	  stub_entry->group->lr_restore = lr_used + 8;
   12212 	}
   12213       break;
   12214 
   12215     case ppc_stub_plt_call:
   12216     case ppc_stub_plt_call_r2save:
   12217       targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
   12218       if (targ >= (bfd_vma) -2)
   12219 	abort ();
   12220       plt = htab->elf.splt;
   12221       if (!htab->elf.dynamic_sections_created
   12222 	  || stub_entry->h == NULL
   12223 	  || stub_entry->h->elf.dynindx == -1)
   12224 	{
   12225 	  if (stub_entry->symtype == STT_GNU_IFUNC)
   12226 	    plt = htab->elf.iplt;
   12227 	  else
   12228 	    plt = htab->pltlocal;
   12229 	}
   12230       targ += plt->output_offset + plt->output_section->vma;
   12231 
   12232       off = (elf_gp (info->output_bfd)
   12233 	     + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
   12234       off = targ - off;
   12235 
   12236       if (htab->params->plt_stub_align != 0)
   12237 	{
   12238 	  unsigned pad = plt_stub_pad (htab, stub_entry, off);
   12239 
   12240 	  stub_entry->group->stub_sec->size += pad;
   12241 	  stub_entry->stub_offset = stub_entry->group->stub_sec->size;
   12242 	}
   12243 
   12244       if (info->emitrelocations)
   12245 	{
   12246 	  stub_entry->group->stub_sec->reloc_count
   12247 	    += ((PPC_HA (off) != 0)
   12248 		+ (htab->opd_abi
   12249 		   ? 2 + (htab->params->plt_static_chain
   12250 			  && PPC_HA (off + 16) == PPC_HA (off))
   12251 		   : 1));
   12252 	  stub_entry->group->stub_sec->flags |= SEC_RELOC;
   12253 	}
   12254 
   12255       size = plt_stub_size (htab, stub_entry, off);
   12256 
   12257       if (stub_entry->h != NULL
   12258 	  && is_tls_get_addr (&stub_entry->h->elf, htab)
   12259 	  && htab->params->tls_get_addr_opt
   12260 	  && stub_entry->stub_type == ppc_stub_plt_call_r2save)
   12261 	{
   12262 	  if (htab->params->no_tls_get_addr_regsave)
   12263 	    {
   12264 	      lr_used = stub_entry->stub_offset + size - 20;
   12265 	      /* The eh_frame info will consist of a DW_CFA_advance_loc
   12266 		 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
   12267 		 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65.  */
   12268 	      delta = lr_used - stub_entry->group->lr_restore;
   12269 	      stub_entry->group->eh_size += eh_advance_size (delta) + 6;
   12270 	    }
   12271 	  else
   12272 	    {
   12273 	      /* Adjustments to r1 need to be described.  */
   12274 	      unsigned int cfa_updt = stub_entry->stub_offset + 18 * 4;
   12275 	      delta = cfa_updt - stub_entry->group->lr_restore;
   12276 	      stub_entry->group->eh_size += eh_advance_size (delta);
   12277 	      stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
   12278 	    }
   12279 	  stub_entry->group->lr_restore = size - 4;
   12280 	}
   12281       break;
   12282 
   12283     default:
   12284       BFD_FAIL ();
   12285       return FALSE;
   12286     }
   12287 
   12288   stub_entry->group->stub_sec->size += size;
   12289   return TRUE;
   12290 }
   12291 
   12292 /* Set up various things so that we can make a list of input sections
   12293    for each output section included in the link.  Returns -1 on error,
   12294    0 when no stubs will be needed, and 1 on success.  */
   12295 
   12296 int
   12297 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
   12298 {
   12299   unsigned int id;
   12300   size_t amt;
   12301   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   12302 
   12303   if (htab == NULL)
   12304     return -1;
   12305 
   12306   htab->sec_info_arr_size = _bfd_section_id;
   12307   amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
   12308   htab->sec_info = bfd_zmalloc (amt);
   12309   if (htab->sec_info == NULL)
   12310     return -1;
   12311 
   12312   /* Set toc_off for com, und, abs and ind sections.  */
   12313   for (id = 0; id < 3; id++)
   12314     htab->sec_info[id].toc_off = TOC_BASE_OFF;
   12315 
   12316   return 1;
   12317 }
   12318 
   12319 /* Set up for first pass at multitoc partitioning.  */
   12320 
   12321 void
   12322 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
   12323 {
   12324   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   12325 
   12326   htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
   12327   htab->toc_bfd = NULL;
   12328   htab->toc_first_sec = NULL;
   12329 }
   12330 
   12331 /* The linker repeatedly calls this function for each TOC input section
   12332    and linker generated GOT section.  Group input bfds such that the toc
   12333    within a group is less than 64k in size.  */
   12334 
   12335 bfd_boolean
   12336 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
   12337 {
   12338   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   12339   bfd_vma addr, off, limit;
   12340 
   12341   if (htab == NULL)
   12342     return FALSE;
   12343 
   12344   if (!htab->second_toc_pass)
   12345     {
   12346       /* Keep track of the first .toc or .got section for this input bfd.  */
   12347       bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
   12348 
   12349       if (new_bfd)
   12350 	{
   12351 	  htab->toc_bfd = isec->owner;
   12352 	  htab->toc_first_sec = isec;
   12353 	}
   12354 
   12355       addr = isec->output_offset + isec->output_section->vma;
   12356       off = addr - htab->toc_curr;
   12357       limit = 0x80008000;
   12358       if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
   12359 	limit = 0x10000;
   12360       if (off + isec->size > limit)
   12361 	{
   12362 	  addr = (htab->toc_first_sec->output_offset
   12363 		  + htab->toc_first_sec->output_section->vma);
   12364 	  htab->toc_curr = addr;
   12365 	  htab->toc_curr &= -TOC_BASE_ALIGN;
   12366 	}
   12367 
   12368       /* toc_curr is the base address of this toc group.  Set elf_gp
   12369 	 for the input section to be the offset relative to the
   12370 	 output toc base plus 0x8000.  Making the input elf_gp an
   12371 	 offset allows us to move the toc as a whole without
   12372 	 recalculating input elf_gp.  */
   12373       off = htab->toc_curr - elf_gp (info->output_bfd);
   12374       off += TOC_BASE_OFF;
   12375 
   12376       /* Die if someone uses a linker script that doesn't keep input
   12377 	 file .toc and .got together.  */
   12378       if (new_bfd
   12379 	  && elf_gp (isec->owner) != 0
   12380 	  && elf_gp (isec->owner) != off)
   12381 	return FALSE;
   12382 
   12383       elf_gp (isec->owner) = off;
   12384       return TRUE;
   12385     }
   12386 
   12387   /* During the second pass toc_first_sec points to the start of
   12388      a toc group, and toc_curr is used to track the old elf_gp.
   12389      We use toc_bfd to ensure we only look at each bfd once.  */
   12390   if (htab->toc_bfd == isec->owner)
   12391     return TRUE;
   12392   htab->toc_bfd = isec->owner;
   12393 
   12394   if (htab->toc_first_sec == NULL
   12395       || htab->toc_curr != elf_gp (isec->owner))
   12396     {
   12397       htab->toc_curr = elf_gp (isec->owner);
   12398       htab->toc_first_sec = isec;
   12399     }
   12400   addr = (htab->toc_first_sec->output_offset
   12401 	  + htab->toc_first_sec->output_section->vma);
   12402   off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
   12403   elf_gp (isec->owner) = off;
   12404 
   12405   return TRUE;
   12406 }
   12407 
   12408 /* Called via elf_link_hash_traverse to merge GOT entries for global
   12409    symbol H.  */
   12410 
   12411 static bfd_boolean
   12412 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
   12413 {
   12414   if (h->root.type == bfd_link_hash_indirect)
   12415     return TRUE;
   12416 
   12417   merge_got_entries (&h->got.glist);
   12418 
   12419   return TRUE;
   12420 }
   12421 
   12422 /* Called via elf_link_hash_traverse to allocate GOT entries for global
   12423    symbol H.  */
   12424 
   12425 static bfd_boolean
   12426 reallocate_got (struct elf_link_hash_entry *h, void *inf)
   12427 {
   12428   struct got_entry *gent;
   12429 
   12430   if (h->root.type == bfd_link_hash_indirect)
   12431     return TRUE;
   12432 
   12433   for (gent = h->got.glist; gent != NULL; gent = gent->next)
   12434     if (!gent->is_indirect)
   12435       allocate_got (h, (struct bfd_link_info *) inf, gent);
   12436   return TRUE;
   12437 }
   12438 
   12439 /* Called on the first multitoc pass after the last call to
   12440    ppc64_elf_next_toc_section.  This function removes duplicate GOT
   12441    entries.  */
   12442 
   12443 bfd_boolean
   12444 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
   12445 {
   12446   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   12447   struct bfd *ibfd, *ibfd2;
   12448   bfd_boolean done_something;
   12449 
   12450   htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
   12451 
   12452   if (!htab->do_multi_toc)
   12453     return FALSE;
   12454 
   12455   /* Merge global sym got entries within a toc group.  */
   12456   elf_link_hash_traverse (&htab->elf, merge_global_got, info);
   12457 
   12458   /* And tlsld_got.  */
   12459   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   12460     {
   12461       struct got_entry *ent, *ent2;
   12462 
   12463       if (!is_ppc64_elf (ibfd))
   12464 	continue;
   12465 
   12466       ent = ppc64_tlsld_got (ibfd);
   12467       if (!ent->is_indirect
   12468 	  && ent->got.offset != (bfd_vma) -1)
   12469 	{
   12470 	  for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
   12471 	    {
   12472 	      if (!is_ppc64_elf (ibfd2))
   12473 		continue;
   12474 
   12475 	      ent2 = ppc64_tlsld_got (ibfd2);
   12476 	      if (!ent2->is_indirect
   12477 		  && ent2->got.offset != (bfd_vma) -1
   12478 		  && elf_gp (ibfd2) == elf_gp (ibfd))
   12479 		{
   12480 		  ent2->is_indirect = TRUE;
   12481 		  ent2->got.ent = ent;
   12482 		}
   12483 	    }
   12484 	}
   12485     }
   12486 
   12487   /* Zap sizes of got sections.  */
   12488   htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
   12489   htab->elf.irelplt->size -= htab->got_reli_size;
   12490   htab->got_reli_size = 0;
   12491 
   12492   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   12493     {
   12494       asection *got, *relgot;
   12495 
   12496       if (!is_ppc64_elf (ibfd))
   12497 	continue;
   12498 
   12499       got = ppc64_elf_tdata (ibfd)->got;
   12500       if (got != NULL)
   12501 	{
   12502 	  got->rawsize = got->size;
   12503 	  got->size = 0;
   12504 	  relgot = ppc64_elf_tdata (ibfd)->relgot;
   12505 	  relgot->rawsize = relgot->size;
   12506 	  relgot->size = 0;
   12507 	}
   12508     }
   12509 
   12510   /* Now reallocate the got, local syms first.  We don't need to
   12511      allocate section contents again since we never increase size.  */
   12512   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   12513     {
   12514       struct got_entry **lgot_ents;
   12515       struct got_entry **end_lgot_ents;
   12516       struct plt_entry **local_plt;
   12517       struct plt_entry **end_local_plt;
   12518       unsigned char *lgot_masks;
   12519       bfd_size_type locsymcount;
   12520       Elf_Internal_Shdr *symtab_hdr;
   12521       asection *s;
   12522 
   12523       if (!is_ppc64_elf (ibfd))
   12524 	continue;
   12525 
   12526       lgot_ents = elf_local_got_ents (ibfd);
   12527       if (!lgot_ents)
   12528 	continue;
   12529 
   12530       symtab_hdr = &elf_symtab_hdr (ibfd);
   12531       locsymcount = symtab_hdr->sh_info;
   12532       end_lgot_ents = lgot_ents + locsymcount;
   12533       local_plt = (struct plt_entry **) end_lgot_ents;
   12534       end_local_plt = local_plt + locsymcount;
   12535       lgot_masks = (unsigned char *) end_local_plt;
   12536       s = ppc64_elf_tdata (ibfd)->got;
   12537       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
   12538 	{
   12539 	  struct got_entry *ent;
   12540 
   12541 	  for (ent = *lgot_ents; ent != NULL; ent = ent->next)
   12542 	    {
   12543 	      unsigned int ent_size = 8;
   12544 	      unsigned int rel_size = sizeof (Elf64_External_Rela);
   12545 
   12546 	      ent->got.offset = s->size;
   12547 	      if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
   12548 		{
   12549 		  ent_size *= 2;
   12550 		  rel_size *= 2;
   12551 		}
   12552 	      s->size += ent_size;
   12553 	      if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
   12554 		{
   12555 		  htab->elf.irelplt->size += rel_size;
   12556 		  htab->got_reli_size += rel_size;
   12557 		}
   12558 	      else if (bfd_link_pic (info)
   12559 		       && !(ent->tls_type != 0
   12560 			    && bfd_link_executable (info)))
   12561 		{
   12562 		  asection *srel = ppc64_elf_tdata (ibfd)->relgot;
   12563 		  srel->size += rel_size;
   12564 		}
   12565 	    }
   12566 	}
   12567     }
   12568 
   12569   elf_link_hash_traverse (&htab->elf, reallocate_got, info);
   12570 
   12571   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   12572     {
   12573       struct got_entry *ent;
   12574 
   12575       if (!is_ppc64_elf (ibfd))
   12576 	continue;
   12577 
   12578       ent = ppc64_tlsld_got (ibfd);
   12579       if (!ent->is_indirect
   12580 	  && ent->got.offset != (bfd_vma) -1)
   12581 	{
   12582 	  asection *s = ppc64_elf_tdata (ibfd)->got;
   12583 	  ent->got.offset = s->size;
   12584 	  s->size += 16;
   12585 	  if (bfd_link_dll (info))
   12586 	    {
   12587 	      asection *srel = ppc64_elf_tdata (ibfd)->relgot;
   12588 	      srel->size += sizeof (Elf64_External_Rela);
   12589 	    }
   12590 	}
   12591     }
   12592 
   12593   done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
   12594   if (!done_something)
   12595     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   12596       {
   12597 	asection *got;
   12598 
   12599 	if (!is_ppc64_elf (ibfd))
   12600 	  continue;
   12601 
   12602 	got = ppc64_elf_tdata (ibfd)->got;
   12603 	if (got != NULL)
   12604 	  {
   12605 	    done_something = got->rawsize != got->size;
   12606 	    if (done_something)
   12607 	      break;
   12608 	  }
   12609       }
   12610 
   12611   if (done_something)
   12612     (*htab->params->layout_sections_again) ();
   12613 
   12614   /* Set up for second pass over toc sections to recalculate elf_gp
   12615      on input sections.  */
   12616   htab->toc_bfd = NULL;
   12617   htab->toc_first_sec = NULL;
   12618   htab->second_toc_pass = TRUE;
   12619   return done_something;
   12620 }
   12621 
   12622 /* Called after second pass of multitoc partitioning.  */
   12623 
   12624 void
   12625 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
   12626 {
   12627   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   12628 
   12629   /* After the second pass, toc_curr tracks the TOC offset used
   12630      for code sections below in ppc64_elf_next_input_section.  */
   12631   htab->toc_curr = TOC_BASE_OFF;
   12632 }
   12633 
   12634 /* No toc references were found in ISEC.  If the code in ISEC makes no
   12635    calls, then there's no need to use toc adjusting stubs when branching
   12636    into ISEC.  Actually, indirect calls from ISEC are OK as they will
   12637    load r2.  Returns -1 on error, 0 for no stub needed, 1 for stub
   12638    needed, and 2 if a cyclical call-graph was found but no other reason
   12639    for a stub was detected.  If called from the top level, a return of
   12640    2 means the same as a return of 0.  */
   12641 
   12642 static int
   12643 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
   12644 {
   12645   int ret;
   12646 
   12647   /* Mark this section as checked.  */
   12648   isec->call_check_done = 1;
   12649 
   12650   /* We know none of our code bearing sections will need toc stubs.  */
   12651   if ((isec->flags & SEC_LINKER_CREATED) != 0)
   12652     return 0;
   12653 
   12654   if (isec->size == 0)
   12655     return 0;
   12656 
   12657   if (isec->output_section == NULL)
   12658     return 0;
   12659 
   12660   ret = 0;
   12661   if (isec->reloc_count != 0)
   12662     {
   12663       Elf_Internal_Rela *relstart, *rel;
   12664       Elf_Internal_Sym *local_syms;
   12665       struct ppc_link_hash_table *htab;
   12666 
   12667       relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
   12668 					    info->keep_memory);
   12669       if (relstart == NULL)
   12670 	return -1;
   12671 
   12672       /* Look for branches to outside of this section.  */
   12673       local_syms = NULL;
   12674       htab = ppc_hash_table (info);
   12675       if (htab == NULL)
   12676 	return -1;
   12677 
   12678       for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
   12679 	{
   12680 	  enum elf_ppc64_reloc_type r_type;
   12681 	  unsigned long r_symndx;
   12682 	  struct elf_link_hash_entry *h;
   12683 	  struct ppc_link_hash_entry *eh;
   12684 	  Elf_Internal_Sym *sym;
   12685 	  asection *sym_sec;
   12686 	  struct _opd_sec_data *opd;
   12687 	  bfd_vma sym_value;
   12688 	  bfd_vma dest;
   12689 
   12690 	  r_type = ELF64_R_TYPE (rel->r_info);
   12691 	  if (r_type != R_PPC64_REL24
   12692 	      && r_type != R_PPC64_REL24_NOTOC
   12693 	      && r_type != R_PPC64_REL14
   12694 	      && r_type != R_PPC64_REL14_BRTAKEN
   12695 	      && r_type != R_PPC64_REL14_BRNTAKEN
   12696 	      && r_type != R_PPC64_PLTCALL
   12697 	      && r_type != R_PPC64_PLTCALL_NOTOC)
   12698 	    continue;
   12699 
   12700 	  r_symndx = ELF64_R_SYM (rel->r_info);
   12701 	  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
   12702 			  isec->owner))
   12703 	    {
   12704 	      ret = -1;
   12705 	      break;
   12706 	    }
   12707 
   12708 	  /* Calls to dynamic lib functions go through a plt call stub
   12709 	     that uses r2.  */
   12710 	  eh = ppc_elf_hash_entry (h);
   12711 	  if (eh != NULL
   12712 	      && (eh->elf.plt.plist != NULL
   12713 		  || (eh->oh != NULL
   12714 		      && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
   12715 	    {
   12716 	      ret = 1;
   12717 	      break;
   12718 	    }
   12719 
   12720 	  if (sym_sec == NULL)
   12721 	    /* Ignore other undefined symbols.  */
   12722 	    continue;
   12723 
   12724 	  /* Assume branches to other sections not included in the
   12725 	     link need stubs too, to cover -R and absolute syms.  */
   12726 	  if (sym_sec->output_section == NULL)
   12727 	    {
   12728 	      ret = 1;
   12729 	      break;
   12730 	    }
   12731 
   12732 	  if (h == NULL)
   12733 	    sym_value = sym->st_value;
   12734 	  else
   12735 	    {
   12736 	      if (h->root.type != bfd_link_hash_defined
   12737 		  && h->root.type != bfd_link_hash_defweak)
   12738 		abort ();
   12739 	      sym_value = h->root.u.def.value;
   12740 	    }
   12741 	  sym_value += rel->r_addend;
   12742 
   12743 	  /* If this branch reloc uses an opd sym, find the code section.  */
   12744 	  opd = get_opd_info (sym_sec);
   12745 	  if (opd != NULL)
   12746 	    {
   12747 	      if (h == NULL && opd->adjust != NULL)
   12748 		{
   12749 		  long adjust;
   12750 
   12751 		  adjust = opd->adjust[OPD_NDX (sym_value)];
   12752 		  if (adjust == -1)
   12753 		    /* Assume deleted functions won't ever be called.  */
   12754 		    continue;
   12755 		  sym_value += adjust;
   12756 		}
   12757 
   12758 	      dest = opd_entry_value (sym_sec, sym_value,
   12759 				      &sym_sec, NULL, FALSE);
   12760 	      if (dest == (bfd_vma) -1)
   12761 		continue;
   12762 	    }
   12763 	  else
   12764 	    dest = (sym_value
   12765 		    + sym_sec->output_offset
   12766 		    + sym_sec->output_section->vma);
   12767 
   12768 	  /* Ignore branch to self.  */
   12769 	  if (sym_sec == isec)
   12770 	    continue;
   12771 
   12772 	  /* If the called function uses the toc, we need a stub.  */
   12773 	  if (sym_sec->has_toc_reloc
   12774 	      || sym_sec->makes_toc_func_call)
   12775 	    {
   12776 	      ret = 1;
   12777 	      break;
   12778 	    }
   12779 
   12780 	  /* Assume any branch that needs a long branch stub might in fact
   12781 	     need a plt_branch stub.  A plt_branch stub uses r2.  */
   12782 	  else if (dest - (isec->output_offset
   12783 			   + isec->output_section->vma
   12784 			   + rel->r_offset) + (1 << 25)
   12785 		   >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
   12786 							     ? h->other
   12787 							     : sym->st_other))
   12788 	    {
   12789 	      ret = 1;
   12790 	      break;
   12791 	    }
   12792 
   12793 	  /* If calling back to a section in the process of being
   12794 	     tested, we can't say for sure that no toc adjusting stubs
   12795 	     are needed, so don't return zero.  */
   12796 	  else if (sym_sec->call_check_in_progress)
   12797 	    ret = 2;
   12798 
   12799 	  /* Branches to another section that itself doesn't have any TOC
   12800 	     references are OK.  Recursively call ourselves to check.  */
   12801 	  else if (!sym_sec->call_check_done)
   12802 	    {
   12803 	      int recur;
   12804 
   12805 	      /* Mark current section as indeterminate, so that other
   12806 		 sections that call back to current won't be marked as
   12807 		 known.  */
   12808 	      isec->call_check_in_progress = 1;
   12809 	      recur = toc_adjusting_stub_needed (info, sym_sec);
   12810 	      isec->call_check_in_progress = 0;
   12811 
   12812 	      if (recur != 0)
   12813 		{
   12814 		  ret = recur;
   12815 		  if (recur != 2)
   12816 		    break;
   12817 		}
   12818 	    }
   12819 	}
   12820 
   12821       if (elf_symtab_hdr (isec->owner).contents
   12822 	  != (unsigned char *) local_syms)
   12823 	free (local_syms);
   12824       if (elf_section_data (isec)->relocs != relstart)
   12825 	free (relstart);
   12826     }
   12827 
   12828   if ((ret & 1) == 0
   12829       && isec->map_head.s != NULL
   12830       && (strcmp (isec->output_section->name, ".init") == 0
   12831 	  || strcmp (isec->output_section->name, ".fini") == 0))
   12832     {
   12833       if (isec->map_head.s->has_toc_reloc
   12834 	  || isec->map_head.s->makes_toc_func_call)
   12835 	ret = 1;
   12836       else if (!isec->map_head.s->call_check_done)
   12837 	{
   12838 	  int recur;
   12839 	  isec->call_check_in_progress = 1;
   12840 	  recur = toc_adjusting_stub_needed (info, isec->map_head.s);
   12841 	  isec->call_check_in_progress = 0;
   12842 	  if (recur != 0)
   12843 	    ret = recur;
   12844 	}
   12845     }
   12846 
   12847   if (ret == 1)
   12848     isec->makes_toc_func_call = 1;
   12849 
   12850   return ret;
   12851 }
   12852 
   12853 /* The linker repeatedly calls this function for each input section,
   12854    in the order that input sections are linked into output sections.
   12855    Build lists of input sections to determine groupings between which
   12856    we may insert linker stubs.  */
   12857 
   12858 bfd_boolean
   12859 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
   12860 {
   12861   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   12862 
   12863   if (htab == NULL)
   12864     return FALSE;
   12865 
   12866   if ((isec->output_section->flags & SEC_CODE) != 0
   12867       && isec->output_section->id < htab->sec_info_arr_size)
   12868     {
   12869       /* This happens to make the list in reverse order,
   12870 	 which is what we want.  */
   12871       htab->sec_info[isec->id].u.list
   12872 	= htab->sec_info[isec->output_section->id].u.list;
   12873       htab->sec_info[isec->output_section->id].u.list = isec;
   12874     }
   12875 
   12876   if (htab->multi_toc_needed)
   12877     {
   12878       /* Analyse sections that aren't already flagged as needing a
   12879 	 valid toc pointer.  Exclude .fixup for the linux kernel.
   12880 	 .fixup contains branches, but only back to the function that
   12881 	 hit an exception.  */
   12882       if (!(isec->has_toc_reloc
   12883 	    || (isec->flags & SEC_CODE) == 0
   12884 	    || strcmp (isec->name, ".fixup") == 0
   12885 	    || isec->call_check_done))
   12886 	{
   12887 	  if (toc_adjusting_stub_needed (info, isec) < 0)
   12888 	    return FALSE;
   12889 	}
   12890       /* Make all sections use the TOC assigned for this object file.
   12891 	 This will be wrong for pasted sections;  We fix that in
   12892 	 check_pasted_section().  */
   12893       if (elf_gp (isec->owner) != 0)
   12894 	htab->toc_curr = elf_gp (isec->owner);
   12895     }
   12896 
   12897   htab->sec_info[isec->id].toc_off = htab->toc_curr;
   12898   return TRUE;
   12899 }
   12900 
   12901 /* Check that all .init and .fini sections use the same toc, if they
   12902    have toc relocs.  */
   12903 
   12904 static bfd_boolean
   12905 check_pasted_section (struct bfd_link_info *info, const char *name)
   12906 {
   12907   asection *o = bfd_get_section_by_name (info->output_bfd, name);
   12908 
   12909   if (o != NULL)
   12910     {
   12911       struct ppc_link_hash_table *htab = ppc_hash_table (info);
   12912       bfd_vma toc_off = 0;
   12913       asection *i;
   12914 
   12915       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   12916 	if (i->has_toc_reloc)
   12917 	  {
   12918 	    if (toc_off == 0)
   12919 	      toc_off = htab->sec_info[i->id].toc_off;
   12920 	    else if (toc_off != htab->sec_info[i->id].toc_off)
   12921 	      return FALSE;
   12922 	  }
   12923 
   12924       if (toc_off == 0)
   12925 	for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   12926 	  if (i->makes_toc_func_call)
   12927 	    {
   12928 	      toc_off = htab->sec_info[i->id].toc_off;
   12929 	      break;
   12930 	    }
   12931 
   12932       /* Make sure the whole pasted function uses the same toc offset.  */
   12933       if (toc_off != 0)
   12934 	for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   12935 	  htab->sec_info[i->id].toc_off = toc_off;
   12936     }
   12937   return TRUE;
   12938 }
   12939 
   12940 bfd_boolean
   12941 ppc64_elf_check_init_fini (struct bfd_link_info *info)
   12942 {
   12943   return (check_pasted_section (info, ".init")
   12944 	  & check_pasted_section (info, ".fini"));
   12945 }
   12946 
   12947 /* See whether we can group stub sections together.  Grouping stub
   12948    sections may result in fewer stubs.  More importantly, we need to
   12949    put all .init* and .fini* stubs at the beginning of the .init or
   12950    .fini output sections respectively, because glibc splits the
   12951    _init and _fini functions into multiple parts.  Putting a stub in
   12952    the middle of a function is not a good idea.  */
   12953 
   12954 static bfd_boolean
   12955 group_sections (struct bfd_link_info *info,
   12956 		bfd_size_type stub_group_size,
   12957 		bfd_boolean stubs_always_before_branch)
   12958 {
   12959   struct ppc_link_hash_table *htab;
   12960   asection *osec;
   12961   bfd_boolean suppress_size_errors;
   12962 
   12963   htab = ppc_hash_table (info);
   12964   if (htab == NULL)
   12965     return FALSE;
   12966 
   12967   suppress_size_errors = FALSE;
   12968   if (stub_group_size == 1)
   12969     {
   12970       /* Default values.  */
   12971       if (stubs_always_before_branch)
   12972 	stub_group_size = 0x1e00000;
   12973       else
   12974 	stub_group_size = 0x1c00000;
   12975       suppress_size_errors = TRUE;
   12976     }
   12977 
   12978   for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
   12979     {
   12980       asection *tail;
   12981 
   12982       if (osec->id >= htab->sec_info_arr_size)
   12983 	continue;
   12984 
   12985       tail = htab->sec_info[osec->id].u.list;
   12986       while (tail != NULL)
   12987 	{
   12988 	  asection *curr;
   12989 	  asection *prev;
   12990 	  bfd_size_type total;
   12991 	  bfd_boolean big_sec;
   12992 	  bfd_vma curr_toc;
   12993 	  struct map_stub *group;
   12994 	  bfd_size_type group_size;
   12995 
   12996 	  curr = tail;
   12997 	  total = tail->size;
   12998 	  group_size = (ppc64_elf_section_data (tail) != NULL
   12999 			&& ppc64_elf_section_data (tail)->has_14bit_branch
   13000 			? stub_group_size >> 10 : stub_group_size);
   13001 
   13002 	  big_sec = total > group_size;
   13003 	  if (big_sec && !suppress_size_errors)
   13004 	    /* xgettext:c-format */
   13005 	    _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
   13006 				tail->owner, tail);
   13007 	  curr_toc = htab->sec_info[tail->id].toc_off;
   13008 
   13009 	  while ((prev = htab->sec_info[curr->id].u.list) != NULL
   13010 		 && ((total += curr->output_offset - prev->output_offset)
   13011 		     < (ppc64_elf_section_data (prev) != NULL
   13012 			&& ppc64_elf_section_data (prev)->has_14bit_branch
   13013 			? (group_size = stub_group_size >> 10) : group_size))
   13014 		 && htab->sec_info[prev->id].toc_off == curr_toc)
   13015 	    curr = prev;
   13016 
   13017 	  /* OK, the size from the start of CURR to the end is less
   13018 	     than group_size and thus can be handled by one stub
   13019 	     section.  (or the tail section is itself larger than
   13020 	     group_size, in which case we may be toast.)  We should
   13021 	     really be keeping track of the total size of stubs added
   13022 	     here, as stubs contribute to the final output section
   13023 	     size.  That's a little tricky, and this way will only
   13024 	     break if stubs added make the total size more than 2^25,
   13025 	     ie. for the default stub_group_size, if stubs total more
   13026 	     than 2097152 bytes, or nearly 75000 plt call stubs.  */
   13027 	  group = bfd_alloc (curr->owner, sizeof (*group));
   13028 	  if (group == NULL)
   13029 	    return FALSE;
   13030 	  group->link_sec = curr;
   13031 	  group->stub_sec = NULL;
   13032 	  group->needs_save_res = 0;
   13033 	  group->lr_restore = 0;
   13034 	  group->eh_size = 0;
   13035 	  group->eh_base = 0;
   13036 	  group->next = htab->group;
   13037 	  htab->group = group;
   13038 	  do
   13039 	    {
   13040 	      prev = htab->sec_info[tail->id].u.list;
   13041 	      /* Set up this stub group.  */
   13042 	      htab->sec_info[tail->id].u.group = group;
   13043 	    }
   13044 	  while (tail != curr && (tail = prev) != NULL);
   13045 
   13046 	  /* But wait, there's more!  Input sections up to group_size
   13047 	     bytes before the stub section can be handled by it too.
   13048 	     Don't do this if we have a really large section after the
   13049 	     stubs, as adding more stubs increases the chance that
   13050 	     branches may not reach into the stub section.  */
   13051 	  if (!stubs_always_before_branch && !big_sec)
   13052 	    {
   13053 	      total = 0;
   13054 	      while (prev != NULL
   13055 		     && ((total += tail->output_offset - prev->output_offset)
   13056 			 < (ppc64_elf_section_data (prev) != NULL
   13057 			    && ppc64_elf_section_data (prev)->has_14bit_branch
   13058 			    ? (group_size = stub_group_size >> 10)
   13059 			    : group_size))
   13060 		     && htab->sec_info[prev->id].toc_off == curr_toc)
   13061 		{
   13062 		  tail = prev;
   13063 		  prev = htab->sec_info[tail->id].u.list;
   13064 		  htab->sec_info[tail->id].u.group = group;
   13065 		}
   13066 	    }
   13067 	  tail = prev;
   13068 	}
   13069     }
   13070   return TRUE;
   13071 }
   13072 
   13073 static const unsigned char glink_eh_frame_cie[] =
   13074 {
   13075   0, 0, 0, 16,				/* length.  */
   13076   0, 0, 0, 0,				/* id.  */
   13077   1,					/* CIE version.  */
   13078   'z', 'R', 0,				/* Augmentation string.  */
   13079   4,					/* Code alignment.  */
   13080   0x78,					/* Data alignment.  */
   13081   65,					/* RA reg.  */
   13082   1,					/* Augmentation size.  */
   13083   DW_EH_PE_pcrel | DW_EH_PE_sdata4,	/* FDE encoding.  */
   13084   DW_CFA_def_cfa, 1, 0			/* def_cfa: r1 offset 0.  */
   13085 };
   13086 
   13087 /* Stripping output sections is normally done before dynamic section
   13088    symbols have been allocated.  This function is called later, and
   13089    handles cases like htab->brlt which is mapped to its own output
   13090    section.  */
   13091 
   13092 static void
   13093 maybe_strip_output (struct bfd_link_info *info, asection *isec)
   13094 {
   13095   if (isec->size == 0
   13096       && isec->output_section->size == 0
   13097       && !(isec->output_section->flags & SEC_KEEP)
   13098       && !bfd_section_removed_from_list (info->output_bfd,
   13099 					 isec->output_section)
   13100       && elf_section_data (isec->output_section)->dynindx == 0)
   13101     {
   13102       isec->output_section->flags |= SEC_EXCLUDE;
   13103       bfd_section_list_remove (info->output_bfd, isec->output_section);
   13104       info->output_bfd->section_count--;
   13105     }
   13106 }
   13107 
   13108 /* Determine and set the size of the stub section for a final link.
   13109 
   13110    The basic idea here is to examine all the relocations looking for
   13111    PC-relative calls to a target that is unreachable with a "bl"
   13112    instruction.  */
   13113 
   13114 bfd_boolean
   13115 ppc64_elf_size_stubs (struct bfd_link_info *info)
   13116 {
   13117   bfd_size_type stub_group_size;
   13118   bfd_boolean stubs_always_before_branch;
   13119   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   13120 
   13121   if (htab == NULL)
   13122     return FALSE;
   13123 
   13124   if (htab->params->power10_stubs == -1 && !htab->has_power10_relocs)
   13125     htab->params->power10_stubs = 0;
   13126 
   13127   if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
   13128     htab->params->plt_thread_safe = 1;
   13129   if (!htab->opd_abi)
   13130     htab->params->plt_thread_safe = 0;
   13131   else if (htab->params->plt_thread_safe == -1)
   13132     {
   13133       static const char *const thread_starter[] =
   13134 	{
   13135 	  "pthread_create",
   13136 	  /* libstdc++ */
   13137 	  "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
   13138 	  /* librt */
   13139 	  "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
   13140 	  "mq_notify", "create_timer",
   13141 	  /* libanl */
   13142 	  "getaddrinfo_a",
   13143 	  /* libgomp */
   13144 	  "GOMP_parallel",
   13145 	  "GOMP_parallel_start",
   13146 	  "GOMP_parallel_loop_static",
   13147 	  "GOMP_parallel_loop_static_start",
   13148 	  "GOMP_parallel_loop_dynamic",
   13149 	  "GOMP_parallel_loop_dynamic_start",
   13150 	  "GOMP_parallel_loop_guided",
   13151 	  "GOMP_parallel_loop_guided_start",
   13152 	  "GOMP_parallel_loop_runtime",
   13153 	  "GOMP_parallel_loop_runtime_start",
   13154 	  "GOMP_parallel_sections",
   13155 	  "GOMP_parallel_sections_start",
   13156 	  /* libgo */
   13157 	  "__go_go",
   13158 	};
   13159       unsigned i;
   13160 
   13161       for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
   13162 	{
   13163 	  struct elf_link_hash_entry *h;
   13164 	  h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
   13165 				    FALSE, FALSE, TRUE);
   13166 	  htab->params->plt_thread_safe = h != NULL && h->ref_regular;
   13167 	  if (htab->params->plt_thread_safe)
   13168 	    break;
   13169 	}
   13170     }
   13171   stubs_always_before_branch = htab->params->group_size < 0;
   13172   if (htab->params->group_size < 0)
   13173     stub_group_size = -htab->params->group_size;
   13174   else
   13175     stub_group_size = htab->params->group_size;
   13176 
   13177   if (!group_sections (info, stub_group_size, stubs_always_before_branch))
   13178     return FALSE;
   13179 
   13180   htab->tga_group = NULL;
   13181   if (!htab->params->no_tls_get_addr_regsave
   13182       && htab->tga_desc_fd != NULL
   13183       && (htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefined
   13184 	  || htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefweak)
   13185       && htab->tls_get_addr_fd != NULL
   13186       && is_static_defined (&htab->tls_get_addr_fd->elf))
   13187     {
   13188       asection *sym_sec, *code_sec, *stub_sec;
   13189       bfd_vma sym_value;
   13190       struct _opd_sec_data *opd;
   13191 
   13192       sym_sec = htab->tls_get_addr_fd->elf.root.u.def.section;
   13193       sym_value = defined_sym_val (&htab->tls_get_addr_fd->elf);
   13194       code_sec = sym_sec;
   13195       opd = get_opd_info (sym_sec);
   13196       if (opd != NULL)
   13197 	opd_entry_value (sym_sec, sym_value, &code_sec, NULL, FALSE);
   13198       htab->tga_group = htab->sec_info[code_sec->id].u.group;
   13199       stub_sec = (*htab->params->add_stub_section) (".tga_desc.stub",
   13200 						    htab->tga_group->link_sec);
   13201       if (stub_sec == NULL)
   13202 	return FALSE;
   13203       htab->tga_group->stub_sec = stub_sec;
   13204 
   13205       htab->tga_desc_fd->elf.root.type = bfd_link_hash_defined;
   13206       htab->tga_desc_fd->elf.root.u.def.section = stub_sec;
   13207       htab->tga_desc_fd->elf.root.u.def.value = 0;
   13208       htab->tga_desc_fd->elf.type = STT_FUNC;
   13209       htab->tga_desc_fd->elf.def_regular = 1;
   13210       htab->tga_desc_fd->elf.non_elf = 0;
   13211       _bfd_elf_link_hash_hide_symbol (info, &htab->tga_desc_fd->elf, TRUE);
   13212     }
   13213 
   13214 #define STUB_SHRINK_ITER 20
   13215   /* Loop until no stubs added.  After iteration 20 of this loop we may
   13216      exit on a stub section shrinking.  This is to break out of a
   13217      pathological case where adding stubs on one iteration decreases
   13218      section gaps (perhaps due to alignment), which then requires
   13219      fewer or smaller stubs on the next iteration.  */
   13220 
   13221   while (1)
   13222     {
   13223       bfd *input_bfd;
   13224       unsigned int bfd_indx;
   13225       struct map_stub *group;
   13226 
   13227       htab->stub_iteration += 1;
   13228 
   13229       for (input_bfd = info->input_bfds, bfd_indx = 0;
   13230 	   input_bfd != NULL;
   13231 	   input_bfd = input_bfd->link.next, bfd_indx++)
   13232 	{
   13233 	  Elf_Internal_Shdr *symtab_hdr;
   13234 	  asection *section;
   13235 	  Elf_Internal_Sym *local_syms = NULL;
   13236 
   13237 	  if (!is_ppc64_elf (input_bfd))
   13238 	    continue;
   13239 
   13240 	  /* We'll need the symbol table in a second.  */
   13241 	  symtab_hdr = &elf_symtab_hdr (input_bfd);
   13242 	  if (symtab_hdr->sh_info == 0)
   13243 	    continue;
   13244 
   13245 	  /* Walk over each section attached to the input bfd.  */
   13246 	  for (section = input_bfd->sections;
   13247 	       section != NULL;
   13248 	       section = section->next)
   13249 	    {
   13250 	      Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
   13251 
   13252 	      /* If there aren't any relocs, then there's nothing more
   13253 		 to do.  */
   13254 	      if ((section->flags & SEC_RELOC) == 0
   13255 		  || (section->flags & SEC_ALLOC) == 0
   13256 		  || (section->flags & SEC_LOAD) == 0
   13257 		  || (section->flags & SEC_CODE) == 0
   13258 		  || section->reloc_count == 0)
   13259 		continue;
   13260 
   13261 	      /* If this section is a link-once section that will be
   13262 		 discarded, then don't create any stubs.  */
   13263 	      if (section->output_section == NULL
   13264 		  || section->output_section->owner != info->output_bfd)
   13265 		continue;
   13266 
   13267 	      /* Get the relocs.  */
   13268 	      internal_relocs
   13269 		= _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
   13270 					     info->keep_memory);
   13271 	      if (internal_relocs == NULL)
   13272 		goto error_ret_free_local;
   13273 
   13274 	      /* Now examine each relocation.  */
   13275 	      irela = internal_relocs;
   13276 	      irelaend = irela + section->reloc_count;
   13277 	      for (; irela < irelaend; irela++)
   13278 		{
   13279 		  enum elf_ppc64_reloc_type r_type;
   13280 		  unsigned int r_indx;
   13281 		  enum ppc_stub_type stub_type;
   13282 		  struct ppc_stub_hash_entry *stub_entry;
   13283 		  asection *sym_sec, *code_sec;
   13284 		  bfd_vma sym_value, code_value;
   13285 		  bfd_vma destination;
   13286 		  unsigned long local_off;
   13287 		  bfd_boolean ok_dest;
   13288 		  struct ppc_link_hash_entry *hash;
   13289 		  struct ppc_link_hash_entry *fdh;
   13290 		  struct elf_link_hash_entry *h;
   13291 		  Elf_Internal_Sym *sym;
   13292 		  char *stub_name;
   13293 		  const asection *id_sec;
   13294 		  struct _opd_sec_data *opd;
   13295 		  struct plt_entry *plt_ent;
   13296 
   13297 		  r_type = ELF64_R_TYPE (irela->r_info);
   13298 		  r_indx = ELF64_R_SYM (irela->r_info);
   13299 
   13300 		  if (r_type >= R_PPC64_max)
   13301 		    {
   13302 		      bfd_set_error (bfd_error_bad_value);
   13303 		      goto error_ret_free_internal;
   13304 		    }
   13305 
   13306 		  /* Only look for stubs on branch instructions.  */
   13307 		  if (r_type != R_PPC64_REL24
   13308 		      && r_type != R_PPC64_REL24_NOTOC
   13309 		      && r_type != R_PPC64_REL14
   13310 		      && r_type != R_PPC64_REL14_BRTAKEN
   13311 		      && r_type != R_PPC64_REL14_BRNTAKEN)
   13312 		    continue;
   13313 
   13314 		  /* Now determine the call target, its name, value,
   13315 		     section.  */
   13316 		  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   13317 				  r_indx, input_bfd))
   13318 		    goto error_ret_free_internal;
   13319 		  hash = ppc_elf_hash_entry (h);
   13320 
   13321 		  ok_dest = FALSE;
   13322 		  fdh = NULL;
   13323 		  sym_value = 0;
   13324 		  if (hash == NULL)
   13325 		    {
   13326 		      sym_value = sym->st_value;
   13327 		      if (sym_sec != NULL
   13328 			  && sym_sec->output_section != NULL)
   13329 			ok_dest = TRUE;
   13330 		    }
   13331 		  else if (hash->elf.root.type == bfd_link_hash_defined
   13332 			   || hash->elf.root.type == bfd_link_hash_defweak)
   13333 		    {
   13334 		      sym_value = hash->elf.root.u.def.value;
   13335 		      if (sym_sec->output_section != NULL)
   13336 			ok_dest = TRUE;
   13337 		    }
   13338 		  else if (hash->elf.root.type == bfd_link_hash_undefweak
   13339 			   || hash->elf.root.type == bfd_link_hash_undefined)
   13340 		    {
   13341 		      /* Recognise an old ABI func code entry sym, and
   13342 			 use the func descriptor sym instead if it is
   13343 			 defined.  */
   13344 		      if (hash->elf.root.root.string[0] == '.'
   13345 			  && hash->oh != NULL)
   13346 			{
   13347 			  fdh = ppc_follow_link (hash->oh);
   13348 			  if (fdh->elf.root.type == bfd_link_hash_defined
   13349 			      || fdh->elf.root.type == bfd_link_hash_defweak)
   13350 			    {
   13351 			      sym_sec = fdh->elf.root.u.def.section;
   13352 			      sym_value = fdh->elf.root.u.def.value;
   13353 			      if (sym_sec->output_section != NULL)
   13354 				ok_dest = TRUE;
   13355 			    }
   13356 			  else
   13357 			    fdh = NULL;
   13358 			}
   13359 		    }
   13360 		  else
   13361 		    {
   13362 		      bfd_set_error (bfd_error_bad_value);
   13363 		      goto error_ret_free_internal;
   13364 		    }
   13365 
   13366 		  destination = 0;
   13367 		  local_off = 0;
   13368 		  if (ok_dest)
   13369 		    {
   13370 		      sym_value += irela->r_addend;
   13371 		      destination = (sym_value
   13372 				     + sym_sec->output_offset
   13373 				     + sym_sec->output_section->vma);
   13374 		      local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
   13375 							    ? hash->elf.other
   13376 							    : sym->st_other);
   13377 		    }
   13378 
   13379 		  code_sec = sym_sec;
   13380 		  code_value = sym_value;
   13381 		  opd = get_opd_info (sym_sec);
   13382 		  if (opd != NULL)
   13383 		    {
   13384 		      bfd_vma dest;
   13385 
   13386 		      if (hash == NULL && opd->adjust != NULL)
   13387 			{
   13388 			  long adjust = opd->adjust[OPD_NDX (sym_value)];
   13389 			  if (adjust == -1)
   13390 			    continue;
   13391 			  code_value += adjust;
   13392 			  sym_value += adjust;
   13393 			}
   13394 		      dest = opd_entry_value (sym_sec, sym_value,
   13395 					      &code_sec, &code_value, FALSE);
   13396 		      if (dest != (bfd_vma) -1)
   13397 			{
   13398 			  destination = dest;
   13399 			  if (fdh != NULL)
   13400 			    {
   13401 			      /* Fixup old ABI sym to point at code
   13402 				 entry.  */
   13403 			      hash->elf.root.type = bfd_link_hash_defweak;
   13404 			      hash->elf.root.u.def.section = code_sec;
   13405 			      hash->elf.root.u.def.value = code_value;
   13406 			    }
   13407 			}
   13408 		    }
   13409 
   13410 		  /* Determine what (if any) linker stub is needed.  */
   13411 		  plt_ent = NULL;
   13412 		  stub_type = ppc_type_of_stub (section, irela, &hash,
   13413 						&plt_ent, destination,
   13414 						local_off);
   13415 
   13416 		  if (r_type == R_PPC64_REL24_NOTOC)
   13417 		    {
   13418 		      if (stub_type == ppc_stub_plt_call)
   13419 			stub_type = ppc_stub_plt_call_notoc;
   13420 		      else if (stub_type == ppc_stub_long_branch
   13421 			       || (code_sec != NULL
   13422 				   && code_sec->output_section != NULL
   13423 				   && (((hash ? hash->elf.other : sym->st_other)
   13424 					& STO_PPC64_LOCAL_MASK)
   13425 				       > 1 << STO_PPC64_LOCAL_BIT)))
   13426 			stub_type = ppc_stub_long_branch_notoc;
   13427 		    }
   13428 		  else if (stub_type != ppc_stub_plt_call)
   13429 		    {
   13430 		      /* Check whether we need a TOC adjusting stub.
   13431 			 Since the linker pastes together pieces from
   13432 			 different object files when creating the
   13433 			 _init and _fini functions, it may be that a
   13434 			 call to what looks like a local sym is in
   13435 			 fact a call needing a TOC adjustment.  */
   13436 		      if ((code_sec != NULL
   13437 			   && code_sec->output_section != NULL
   13438 			   && (code_sec->has_toc_reloc
   13439 			       || code_sec->makes_toc_func_call)
   13440 			   && (htab->sec_info[code_sec->id].toc_off
   13441 			       != htab->sec_info[section->id].toc_off))
   13442 			  || (((hash ? hash->elf.other : sym->st_other)
   13443 			       & STO_PPC64_LOCAL_MASK)
   13444 			      == 1 << STO_PPC64_LOCAL_BIT))
   13445 			stub_type = ppc_stub_long_branch_r2off;
   13446 		    }
   13447 
   13448 		  if (stub_type == ppc_stub_none)
   13449 		    continue;
   13450 
   13451 		  /* __tls_get_addr calls might be eliminated.  */
   13452 		  if (stub_type != ppc_stub_plt_call
   13453 		      && stub_type != ppc_stub_plt_call_notoc
   13454 		      && hash != NULL
   13455 		      && is_tls_get_addr (&hash->elf, htab)
   13456 		      && section->has_tls_reloc
   13457 		      && irela != internal_relocs)
   13458 		    {
   13459 		      /* Get tls info.  */
   13460 		      unsigned char *tls_mask;
   13461 
   13462 		      if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
   13463 					 irela - 1, input_bfd))
   13464 			goto error_ret_free_internal;
   13465 		      if ((*tls_mask & TLS_TLS) != 0
   13466 			  && (*tls_mask & (TLS_GD | TLS_LD)) == 0)
   13467 			continue;
   13468 		    }
   13469 
   13470 		  if (stub_type == ppc_stub_plt_call)
   13471 		    {
   13472 		      if (!htab->opd_abi
   13473 			  && htab->params->plt_localentry0 != 0
   13474 			  && is_elfv2_localentry0 (&hash->elf))
   13475 			htab->has_plt_localentry0 = 1;
   13476 		      else if (irela + 1 < irelaend
   13477 			       && irela[1].r_offset == irela->r_offset + 4
   13478 			       && (ELF64_R_TYPE (irela[1].r_info)
   13479 				   == R_PPC64_TOCSAVE))
   13480 			{
   13481 			  if (!tocsave_find (htab, INSERT,
   13482 					     &local_syms, irela + 1, input_bfd))
   13483 			    goto error_ret_free_internal;
   13484 			}
   13485 		      else
   13486 			stub_type = ppc_stub_plt_call_r2save;
   13487 		    }
   13488 
   13489 		  /* Support for grouping stub sections.  */
   13490 		  id_sec = htab->sec_info[section->id].u.group->link_sec;
   13491 
   13492 		  /* Get the name of this stub.  */
   13493 		  stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
   13494 		  if (!stub_name)
   13495 		    goto error_ret_free_internal;
   13496 
   13497 		  stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
   13498 						     stub_name, FALSE, FALSE);
   13499 		  if (stub_entry != NULL)
   13500 		    {
   13501 		      enum ppc_stub_type old_type;
   13502 
   13503 		      /* A stub has already been created, but it may
   13504 			 not be the required type.  We shouldn't be
   13505 			 transitioning from plt_call to long_branch
   13506 			 stubs or vice versa, but we might be
   13507 			 upgrading from plt_call to plt_call_r2save or
   13508 			 from long_branch to long_branch_r2off.  */
   13509 		      free (stub_name);
   13510 		      if (htab->params->power10_stubs == -1)
   13511 			{
   13512 			  /* For --power10-stubs=auto, don't merge _notoc
   13513 			     and other varieties of stubs.  (The _both
   13514 			     variety won't be created.)  */
   13515 			  bfd_boolean notoc = r_type == R_PPC64_REL24_NOTOC;
   13516 			  struct ppc_stub_hash_entry *alt_stub
   13517 			    = select_alt_stub (stub_entry, notoc);
   13518 
   13519 			  if (alt_stub == NULL)
   13520 			    {
   13521 			      alt_stub = (struct ppc_stub_hash_entry *)
   13522 				stub_hash_newfunc (NULL,
   13523 						   &htab->stub_hash_table,
   13524 						   stub_entry->root.string);
   13525 			      if (alt_stub == NULL)
   13526 				{
   13527 				  /* xgettext:c-format */
   13528 				  _bfd_error_handler
   13529 				    (_("%pB: cannot create stub entry %s"),
   13530 				     section->owner, stub_entry->root.string);
   13531 				  goto error_ret_free_internal;
   13532 				}
   13533 			      *alt_stub = *stub_entry;
   13534 			      stub_entry->root.next = &alt_stub->root;
   13535 			      if (notoc)
   13536 				/* Sort notoc stubs first, for no good
   13537 				   reason.  */
   13538 				alt_stub = stub_entry;
   13539 			      alt_stub->stub_type = stub_type;
   13540 			    }
   13541 			  stub_entry = alt_stub;
   13542 			}
   13543 		      old_type = stub_entry->stub_type;
   13544 		      switch (old_type)
   13545 			{
   13546 			default:
   13547 			  abort ();
   13548 
   13549 			case ppc_stub_save_res:
   13550 			  continue;
   13551 
   13552 			case ppc_stub_plt_call:
   13553 			case ppc_stub_plt_call_r2save:
   13554 			case ppc_stub_plt_call_notoc:
   13555 			case ppc_stub_plt_call_both:
   13556 			  if (stub_type == ppc_stub_plt_call)
   13557 			    continue;
   13558 			  else if (stub_type == ppc_stub_plt_call_r2save)
   13559 			    {
   13560 			      if (old_type == ppc_stub_plt_call_notoc)
   13561 				stub_type = ppc_stub_plt_call_both;
   13562 			    }
   13563 			  else if (stub_type == ppc_stub_plt_call_notoc)
   13564 			    {
   13565 			      if (old_type == ppc_stub_plt_call_r2save)
   13566 				stub_type = ppc_stub_plt_call_both;
   13567 			    }
   13568 			  else
   13569 			    abort ();
   13570 			  break;
   13571 
   13572 			case ppc_stub_plt_branch:
   13573 			case ppc_stub_plt_branch_r2off:
   13574 			case ppc_stub_plt_branch_notoc:
   13575 			case ppc_stub_plt_branch_both:
   13576 			  old_type += (ppc_stub_long_branch
   13577 				       - ppc_stub_plt_branch);
   13578 			  /* Fall through.  */
   13579 			case ppc_stub_long_branch:
   13580 			case ppc_stub_long_branch_r2off:
   13581 			case ppc_stub_long_branch_notoc:
   13582 			case ppc_stub_long_branch_both:
   13583 			  if (stub_type == ppc_stub_long_branch)
   13584 			    continue;
   13585 			  else if (stub_type == ppc_stub_long_branch_r2off)
   13586 			    {
   13587 			      if (old_type == ppc_stub_long_branch_notoc)
   13588 				stub_type = ppc_stub_long_branch_both;
   13589 			    }
   13590 			  else if (stub_type == ppc_stub_long_branch_notoc)
   13591 			    {
   13592 			      if (old_type == ppc_stub_long_branch_r2off)
   13593 				stub_type = ppc_stub_long_branch_both;
   13594 			    }
   13595 			  else
   13596 			    abort ();
   13597 			  break;
   13598 			}
   13599 		      if (old_type < stub_type)
   13600 			stub_entry->stub_type = stub_type;
   13601 		      continue;
   13602 		    }
   13603 
   13604 		  stub_entry = ppc_add_stub (stub_name, section, info);
   13605 		  if (stub_entry == NULL)
   13606 		    {
   13607 		      free (stub_name);
   13608 		    error_ret_free_internal:
   13609 		      if (elf_section_data (section)->relocs == NULL)
   13610 			free (internal_relocs);
   13611 		    error_ret_free_local:
   13612 		      if (symtab_hdr->contents
   13613 			  != (unsigned char *) local_syms)
   13614 			free (local_syms);
   13615 		      return FALSE;
   13616 		    }
   13617 
   13618 		  stub_entry->stub_type = stub_type;
   13619 		  if (stub_type >= ppc_stub_plt_call
   13620 		      && stub_type <= ppc_stub_plt_call_both)
   13621 		    {
   13622 		      stub_entry->target_value = sym_value;
   13623 		      stub_entry->target_section = sym_sec;
   13624 		    }
   13625 		  else
   13626 		    {
   13627 		      stub_entry->target_value = code_value;
   13628 		      stub_entry->target_section = code_sec;
   13629 		    }
   13630 		  stub_entry->h = hash;
   13631 		  stub_entry->plt_ent = plt_ent;
   13632 		  stub_entry->symtype
   13633 		    = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
   13634 		  stub_entry->other = hash ? hash->elf.other : sym->st_other;
   13635 
   13636 		  if (hash != NULL
   13637 		      && (hash->elf.root.type == bfd_link_hash_defined
   13638 			  || hash->elf.root.type == bfd_link_hash_defweak))
   13639 		    htab->stub_globals += 1;
   13640 		}
   13641 
   13642 	      /* We're done with the internal relocs, free them.  */
   13643 	      if (elf_section_data (section)->relocs != internal_relocs)
   13644 		free (internal_relocs);
   13645 	    }
   13646 
   13647 	  if (local_syms != NULL
   13648 	      && symtab_hdr->contents != (unsigned char *) local_syms)
   13649 	    {
   13650 	      if (!info->keep_memory)
   13651 		free (local_syms);
   13652 	      else
   13653 		symtab_hdr->contents = (unsigned char *) local_syms;
   13654 	    }
   13655 	}
   13656 
   13657       /* We may have added some stubs.  Find out the new size of the
   13658 	 stub sections.  */
   13659       for (group = htab->group; group != NULL; group = group->next)
   13660 	{
   13661 	  group->lr_restore = 0;
   13662 	  group->eh_size = 0;
   13663 	  if (group->stub_sec != NULL)
   13664 	    {
   13665 	      asection *stub_sec = group->stub_sec;
   13666 
   13667 	      if (htab->stub_iteration <= STUB_SHRINK_ITER
   13668 		  || stub_sec->rawsize < stub_sec->size)
   13669 		/* Past STUB_SHRINK_ITER, rawsize is the max size seen.  */
   13670 		stub_sec->rawsize = stub_sec->size;
   13671 	      stub_sec->size = 0;
   13672 	      stub_sec->reloc_count = 0;
   13673 	      stub_sec->flags &= ~SEC_RELOC;
   13674 	    }
   13675 	}
   13676       if (htab->tga_group != NULL)
   13677 	{
   13678 	  /* See emit_tga_desc and emit_tga_desc_eh_frame.  */
   13679 	  htab->tga_group->eh_size
   13680 	    = 1 + 2 + (htab->opd_abi != 0) + 3 + 8 * 2 + 3 + 8 + 3;
   13681 	  htab->tga_group->lr_restore = 23 * 4;
   13682 	  htab->tga_group->stub_sec->size = 24 * 4;
   13683 	}
   13684 
   13685       if (htab->stub_iteration <= STUB_SHRINK_ITER
   13686 	  || htab->brlt->rawsize < htab->brlt->size)
   13687 	htab->brlt->rawsize = htab->brlt->size;
   13688       htab->brlt->size = 0;
   13689       htab->brlt->reloc_count = 0;
   13690       htab->brlt->flags &= ~SEC_RELOC;
   13691       if (htab->relbrlt != NULL)
   13692 	htab->relbrlt->size = 0;
   13693 
   13694       bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
   13695 
   13696       for (group = htab->group; group != NULL; group = group->next)
   13697 	if (group->needs_save_res)
   13698 	  group->stub_sec->size += htab->sfpr->size;
   13699 
   13700       if (info->emitrelocations
   13701 	  && htab->glink != NULL && htab->glink->size != 0)
   13702 	{
   13703 	  htab->glink->reloc_count = 1;
   13704 	  htab->glink->flags |= SEC_RELOC;
   13705 	}
   13706 
   13707       if (htab->glink_eh_frame != NULL
   13708 	  && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
   13709 	  && htab->glink_eh_frame->output_section->size > 8)
   13710 	{
   13711 	  size_t size = 0, align = 4;
   13712 
   13713 	  for (group = htab->group; group != NULL; group = group->next)
   13714 	    if (group->eh_size != 0)
   13715 	      size += (group->eh_size + 17 + align - 1) & -align;
   13716 	  if (htab->glink != NULL && htab->glink->size != 0)
   13717 	    size += (24 + align - 1) & -align;
   13718 	  if (size != 0)
   13719 	    size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
   13720 	  align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
   13721 	  size = (size + align - 1) & -align;
   13722 	  htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
   13723 	  htab->glink_eh_frame->size = size;
   13724 	}
   13725 
   13726       if (htab->params->plt_stub_align != 0)
   13727 	for (group = htab->group; group != NULL; group = group->next)
   13728 	  if (group->stub_sec != NULL)
   13729 	    {
   13730 	      int align = abs (htab->params->plt_stub_align);
   13731 	      group->stub_sec->size
   13732 		= (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
   13733 	    }
   13734 
   13735       for (group = htab->group; group != NULL; group = group->next)
   13736 	if (group->stub_sec != NULL
   13737 	    && group->stub_sec->rawsize != group->stub_sec->size
   13738 	    && (htab->stub_iteration <= STUB_SHRINK_ITER
   13739 		|| group->stub_sec->rawsize < group->stub_sec->size))
   13740 	  break;
   13741 
   13742       if (group == NULL
   13743 	  && (htab->brlt->rawsize == htab->brlt->size
   13744 	      || (htab->stub_iteration > STUB_SHRINK_ITER
   13745 		  && htab->brlt->rawsize > htab->brlt->size))
   13746 	  && (htab->glink_eh_frame == NULL
   13747 	      || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size)
   13748 	  && (htab->tga_group == NULL
   13749 	      || htab->stub_iteration > 1))
   13750 	break;
   13751 
   13752       /* Ask the linker to do its stuff.  */
   13753       (*htab->params->layout_sections_again) ();
   13754     }
   13755 
   13756   if (htab->glink_eh_frame != NULL
   13757       && htab->glink_eh_frame->size != 0)
   13758     {
   13759       bfd_vma val;
   13760       bfd_byte *p, *last_fde;
   13761       size_t last_fde_len, size, align, pad;
   13762       struct map_stub *group;
   13763 
   13764       /* It is necessary to at least have a rough outline of the
   13765 	 linker generated CIEs and FDEs written before
   13766 	 bfd_elf_discard_info is run, in order for these FDEs to be
   13767 	 indexed in .eh_frame_hdr.  */
   13768       p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
   13769       if (p == NULL)
   13770 	return FALSE;
   13771       htab->glink_eh_frame->contents = p;
   13772       last_fde = p;
   13773       align = 4;
   13774 
   13775       memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
   13776       /* CIE length (rewrite in case little-endian).  */
   13777       last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
   13778       bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
   13779       p += last_fde_len + 4;
   13780 
   13781       for (group = htab->group; group != NULL; group = group->next)
   13782 	if (group->eh_size != 0)
   13783 	  {
   13784 	    group->eh_base = p - htab->glink_eh_frame->contents;
   13785 	    last_fde = p;
   13786 	    last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
   13787 	    /* FDE length.  */
   13788 	    bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
   13789 	    p += 4;
   13790 	    /* CIE pointer.  */
   13791 	    val = p - htab->glink_eh_frame->contents;
   13792 	    bfd_put_32 (htab->elf.dynobj, val, p);
   13793 	    p += 4;
   13794 	    /* Offset to stub section, written later.  */
   13795 	    p += 4;
   13796 	    /* stub section size.  */
   13797 	    bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
   13798 	    p += 4;
   13799 	    /* Augmentation.  */
   13800 	    p += 1;
   13801 	    /* Make sure we don't have all nops.  This is enough for
   13802 	       elf-eh-frame.c to detect the last non-nop opcode.  */
   13803 	    p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
   13804 	    p = last_fde + last_fde_len + 4;
   13805 	  }
   13806       if (htab->glink != NULL && htab->glink->size != 0)
   13807 	{
   13808 	  last_fde = p;
   13809 	  last_fde_len = ((24 + align - 1) & -align) - 4;
   13810 	  /* FDE length.  */
   13811 	  bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
   13812 	  p += 4;
   13813 	  /* CIE pointer.  */
   13814 	  val = p - htab->glink_eh_frame->contents;
   13815 	  bfd_put_32 (htab->elf.dynobj, val, p);
   13816 	  p += 4;
   13817 	  /* Offset to .glink, written later.  */
   13818 	  p += 4;
   13819 	  /* .glink size.  */
   13820 	  bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
   13821 	  p += 4;
   13822 	  /* Augmentation.  */
   13823 	  p += 1;
   13824 
   13825 	  *p++ = DW_CFA_advance_loc + 1;
   13826 	  *p++ = DW_CFA_register;
   13827 	  *p++ = 65;
   13828 	  *p++ = htab->opd_abi ? 12 : 0;
   13829 	  *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
   13830 	  *p++ = DW_CFA_restore_extended;
   13831 	  *p++ = 65;
   13832 	  p += ((24 + align - 1) & -align) - 24;
   13833 	}
   13834       /* Subsume any padding into the last FDE if user .eh_frame
   13835 	 sections are aligned more than glink_eh_frame.  Otherwise any
   13836 	 zero padding will be seen as a terminator.  */
   13837       align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
   13838       size = p - htab->glink_eh_frame->contents;
   13839       pad = ((size + align - 1) & -align) - size;
   13840       htab->glink_eh_frame->size = size + pad;
   13841       bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
   13842     }
   13843 
   13844   maybe_strip_output (info, htab->brlt);
   13845   if (htab->relbrlt != NULL)
   13846     maybe_strip_output (info, htab->relbrlt);
   13847   if (htab->glink_eh_frame != NULL)
   13848     maybe_strip_output (info, htab->glink_eh_frame);
   13849 
   13850   return TRUE;
   13851 }
   13852 
   13853 /* Called after we have determined section placement.  If sections
   13854    move, we'll be called again.  Provide a value for TOCstart.  */
   13855 
   13856 bfd_vma
   13857 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
   13858 {
   13859   asection *s;
   13860   bfd_vma TOCstart, adjust;
   13861 
   13862   if (info != NULL)
   13863     {
   13864       struct elf_link_hash_entry *h;
   13865       struct elf_link_hash_table *htab = elf_hash_table (info);
   13866 
   13867       if (is_elf_hash_table (htab)
   13868 	  && htab->hgot != NULL)
   13869 	h = htab->hgot;
   13870       else
   13871 	{
   13872 	  h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
   13873 	  if (is_elf_hash_table (htab))
   13874 	    htab->hgot = h;
   13875 	}
   13876       if (h != NULL
   13877 	  && h->root.type == bfd_link_hash_defined
   13878 	  && !h->root.linker_def
   13879 	  && (!is_elf_hash_table (htab)
   13880 	      || h->def_regular))
   13881 	{
   13882 	  TOCstart = defined_sym_val (h) - TOC_BASE_OFF;
   13883 	  _bfd_set_gp_value (obfd, TOCstart);
   13884 	  return TOCstart;
   13885 	}
   13886     }
   13887 
   13888   /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
   13889      order.  The TOC starts where the first of these sections starts.  */
   13890   s = bfd_get_section_by_name (obfd, ".got");
   13891   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
   13892     s = bfd_get_section_by_name (obfd, ".toc");
   13893   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
   13894     s = bfd_get_section_by_name (obfd, ".tocbss");
   13895   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
   13896     s = bfd_get_section_by_name (obfd, ".plt");
   13897   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
   13898     {
   13899       /* This may happen for
   13900 	 o  references to TOC base (SYM@toc / TOC[tc0]) without a
   13901 	 .toc directive
   13902 	 o  bad linker script
   13903 	 o --gc-sections and empty TOC sections
   13904 
   13905 	 FIXME: Warn user?  */
   13906 
   13907       /* Look for a likely section.  We probably won't even be
   13908 	 using TOCstart.  */
   13909       for (s = obfd->sections; s != NULL; s = s->next)
   13910 	if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
   13911 			 | SEC_EXCLUDE))
   13912 	    == (SEC_ALLOC | SEC_SMALL_DATA))
   13913 	  break;
   13914       if (s == NULL)
   13915 	for (s = obfd->sections; s != NULL; s = s->next)
   13916 	  if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
   13917 	      == (SEC_ALLOC | SEC_SMALL_DATA))
   13918 	    break;
   13919       if (s == NULL)
   13920 	for (s = obfd->sections; s != NULL; s = s->next)
   13921 	  if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
   13922 	      == SEC_ALLOC)
   13923 	    break;
   13924       if (s == NULL)
   13925 	for (s = obfd->sections; s != NULL; s = s->next)
   13926 	  if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
   13927 	    break;
   13928     }
   13929 
   13930   TOCstart = 0;
   13931   if (s != NULL)
   13932     TOCstart = s->output_section->vma + s->output_offset;
   13933 
   13934   /* Force alignment.  */
   13935   adjust = TOCstart & (TOC_BASE_ALIGN - 1);
   13936   TOCstart -= adjust;
   13937   _bfd_set_gp_value (obfd, TOCstart);
   13938 
   13939   if (info != NULL && s != NULL)
   13940     {
   13941       struct ppc_link_hash_table *htab = ppc_hash_table (info);
   13942 
   13943       if (htab != NULL)
   13944 	{
   13945 	  if (htab->elf.hgot != NULL)
   13946 	    {
   13947 	      htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
   13948 	      htab->elf.hgot->root.u.def.section = s;
   13949 	    }
   13950 	}
   13951       else
   13952 	{
   13953 	  struct bfd_link_hash_entry *bh = NULL;
   13954 	  _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
   13955 					    s, TOC_BASE_OFF - adjust,
   13956 					    NULL, FALSE, FALSE, &bh);
   13957 	}
   13958     }
   13959   return TOCstart;
   13960 }
   13961 
   13962 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
   13963    write out any global entry stubs, and PLT relocations.  */
   13964 
   13965 static bfd_boolean
   13966 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
   13967 {
   13968   struct bfd_link_info *info;
   13969   struct ppc_link_hash_table *htab;
   13970   struct plt_entry *ent;
   13971   asection *s;
   13972 
   13973   if (h->root.type == bfd_link_hash_indirect)
   13974     return TRUE;
   13975 
   13976   info = inf;
   13977   htab = ppc_hash_table (info);
   13978   if (htab == NULL)
   13979     return FALSE;
   13980 
   13981   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
   13982     if (ent->plt.offset != (bfd_vma) -1)
   13983       {
   13984 	/* This symbol has an entry in the procedure linkage
   13985 	   table.  Set it up.  */
   13986 	Elf_Internal_Rela rela;
   13987 	asection *plt, *relplt;
   13988 	bfd_byte *loc;
   13989 
   13990 	if (!htab->elf.dynamic_sections_created
   13991 	    || h->dynindx == -1)
   13992 	  {
   13993 	    if (!(h->def_regular
   13994 		  && (h->root.type == bfd_link_hash_defined
   13995 		      || h->root.type == bfd_link_hash_defweak)))
   13996 	      continue;
   13997 	    if (h->type == STT_GNU_IFUNC)
   13998 	      {
   13999 		plt = htab->elf.iplt;
   14000 		relplt = htab->elf.irelplt;
   14001 		htab->elf.ifunc_resolvers = TRUE;
   14002 		if (htab->opd_abi)
   14003 		  rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
   14004 		else
   14005 		  rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
   14006 	      }
   14007 	    else
   14008 	      {
   14009 		plt = htab->pltlocal;
   14010 		if (bfd_link_pic (info))
   14011 		  {
   14012 		    relplt = htab->relpltlocal;
   14013 		    if (htab->opd_abi)
   14014 		      rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
   14015 		    else
   14016 		      rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   14017 		  }
   14018 		else
   14019 		  relplt = NULL;
   14020 	      }
   14021 	    rela.r_addend = defined_sym_val (h) + ent->addend;
   14022 
   14023 	    if (relplt == NULL)
   14024 	      {
   14025 		loc = plt->contents + ent->plt.offset;
   14026 		bfd_put_64 (info->output_bfd, rela.r_addend, loc);
   14027 		if (htab->opd_abi)
   14028 		  {
   14029 		    bfd_vma toc = elf_gp (info->output_bfd);
   14030 		    toc += htab->sec_info[h->root.u.def.section->id].toc_off;
   14031 		    bfd_put_64 (info->output_bfd, toc, loc + 8);
   14032 		  }
   14033 	      }
   14034 	    else
   14035 	      {
   14036 		rela.r_offset = (plt->output_section->vma
   14037 				 + plt->output_offset
   14038 				 + ent->plt.offset);
   14039 		loc = relplt->contents + (relplt->reloc_count++
   14040 					  * sizeof (Elf64_External_Rela));
   14041 		bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
   14042 	      }
   14043 	  }
   14044 	else
   14045 	  {
   14046 	    rela.r_offset = (htab->elf.splt->output_section->vma
   14047 			     + htab->elf.splt->output_offset
   14048 			     + ent->plt.offset);
   14049 	    rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
   14050 	    rela.r_addend = ent->addend;
   14051 	    loc = (htab->elf.srelplt->contents
   14052 		   + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
   14053 		      / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
   14054 	    if (h->type == STT_GNU_IFUNC && is_static_defined (h))
   14055 	      htab->elf.ifunc_resolvers = TRUE;
   14056 	    bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
   14057 	  }
   14058       }
   14059 
   14060   if (!h->pointer_equality_needed)
   14061     return TRUE;
   14062 
   14063   if (h->def_regular)
   14064     return TRUE;
   14065 
   14066   s = htab->global_entry;
   14067   if (s == NULL || s->size == 0)
   14068     return TRUE;
   14069 
   14070   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
   14071     if (ent->plt.offset != (bfd_vma) -1
   14072 	&& ent->addend == 0)
   14073       {
   14074 	bfd_byte *p;
   14075 	asection *plt;
   14076 	bfd_vma off;
   14077 
   14078 	p = s->contents + h->root.u.def.value;
   14079 	plt = htab->elf.splt;
   14080 	if (!htab->elf.dynamic_sections_created
   14081 	    || h->dynindx == -1)
   14082 	  {
   14083 	    if (h->type == STT_GNU_IFUNC)
   14084 	      plt = htab->elf.iplt;
   14085 	    else
   14086 	      plt = htab->pltlocal;
   14087 	  }
   14088 	off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
   14089 	off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
   14090 
   14091 	if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
   14092 	  {
   14093 	    info->callbacks->einfo
   14094 	      (_("%P: linkage table error against `%pT'\n"),
   14095 	       h->root.root.string);
   14096 	    bfd_set_error (bfd_error_bad_value);
   14097 	    htab->stub_error = TRUE;
   14098 	  }
   14099 
   14100 	htab->stub_count[ppc_stub_global_entry - 1] += 1;
   14101 	if (htab->params->emit_stub_syms)
   14102 	  {
   14103 	    size_t len = strlen (h->root.root.string);
   14104 	    char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
   14105 
   14106 	    if (name == NULL)
   14107 	      return FALSE;
   14108 
   14109 	    sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
   14110 	    h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
   14111 	    if (h == NULL)
   14112 	      return FALSE;
   14113 	    if (h->root.type == bfd_link_hash_new)
   14114 	      {
   14115 		h->root.type = bfd_link_hash_defined;
   14116 		h->root.u.def.section = s;
   14117 		h->root.u.def.value = p - s->contents;
   14118 		h->ref_regular = 1;
   14119 		h->def_regular = 1;
   14120 		h->ref_regular_nonweak = 1;
   14121 		h->forced_local = 1;
   14122 		h->non_elf = 0;
   14123 		h->root.linker_def = 1;
   14124 	      }
   14125 	  }
   14126 
   14127 	if (PPC_HA (off) != 0)
   14128 	  {
   14129 	    bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
   14130 	    p += 4;
   14131 	  }
   14132 	bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
   14133 	p += 4;
   14134 	bfd_put_32 (s->owner, MTCTR_R12, p);
   14135 	p += 4;
   14136 	bfd_put_32 (s->owner, BCTR, p);
   14137 	break;
   14138       }
   14139   return TRUE;
   14140 }
   14141 
   14142 /* Write PLT relocs for locals.  */
   14143 
   14144 static bfd_boolean
   14145 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
   14146 {
   14147   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   14148   bfd *ibfd;
   14149 
   14150   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   14151     {
   14152       struct got_entry **lgot_ents, **end_lgot_ents;
   14153       struct plt_entry **local_plt, **lplt, **end_local_plt;
   14154       Elf_Internal_Shdr *symtab_hdr;
   14155       bfd_size_type locsymcount;
   14156       Elf_Internal_Sym *local_syms = NULL;
   14157       struct plt_entry *ent;
   14158 
   14159       if (!is_ppc64_elf (ibfd))
   14160 	continue;
   14161 
   14162       lgot_ents = elf_local_got_ents (ibfd);
   14163       if (!lgot_ents)
   14164 	continue;
   14165 
   14166       symtab_hdr = &elf_symtab_hdr (ibfd);
   14167       locsymcount = symtab_hdr->sh_info;
   14168       end_lgot_ents = lgot_ents + locsymcount;
   14169       local_plt = (struct plt_entry **) end_lgot_ents;
   14170       end_local_plt = local_plt + locsymcount;
   14171       for (lplt = local_plt; lplt < end_local_plt; ++lplt)
   14172 	for (ent = *lplt; ent != NULL; ent = ent->next)
   14173 	  if (ent->plt.offset != (bfd_vma) -1)
   14174 	    {
   14175 	      Elf_Internal_Sym *sym;
   14176 	      asection *sym_sec;
   14177 	      asection *plt, *relplt;
   14178 	      bfd_byte *loc;
   14179 	      bfd_vma val;
   14180 
   14181 	      if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
   14182 			      lplt - local_plt, ibfd))
   14183 		{
   14184 		  if (symtab_hdr->contents != (unsigned char *) local_syms)
   14185 		    free (local_syms);
   14186 		  return FALSE;
   14187 		}
   14188 
   14189 	      val = sym->st_value + ent->addend;
   14190 	      if (ELF_ST_TYPE (sym->st_info) != STT_GNU_IFUNC)
   14191 		val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
   14192 	      if (sym_sec != NULL && sym_sec->output_section != NULL)
   14193 		val += sym_sec->output_offset + sym_sec->output_section->vma;
   14194 
   14195 	      if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
   14196 		{
   14197 		  htab->elf.ifunc_resolvers = TRUE;
   14198 		  plt = htab->elf.iplt;
   14199 		  relplt = htab->elf.irelplt;
   14200 		}
   14201 	      else
   14202 		{
   14203 		  plt = htab->pltlocal;
   14204 		  relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
   14205 		}
   14206 
   14207 	      if (relplt == NULL)
   14208 		{
   14209 		  loc = plt->contents + ent->plt.offset;
   14210 		  bfd_put_64 (info->output_bfd, val, loc);
   14211 		  if (htab->opd_abi)
   14212 		    {
   14213 		      bfd_vma toc = elf_gp (ibfd);
   14214 		      bfd_put_64 (info->output_bfd, toc, loc + 8);
   14215 		    }
   14216 		}
   14217 	      else
   14218 		{
   14219 		  Elf_Internal_Rela rela;
   14220 		  rela.r_offset = (ent->plt.offset
   14221 				   + plt->output_offset
   14222 				   + plt->output_section->vma);
   14223 		  if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
   14224 		    {
   14225 		      if (htab->opd_abi)
   14226 			rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
   14227 		      else
   14228 			rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
   14229 		    }
   14230 		  else
   14231 		    {
   14232 		      if (htab->opd_abi)
   14233 			rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
   14234 		      else
   14235 			rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   14236 		    }
   14237 		  rela.r_addend = val;
   14238 		  loc = relplt->contents + (relplt->reloc_count++
   14239 					    * sizeof (Elf64_External_Rela));
   14240 		  bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
   14241 		}
   14242 	    }
   14243 
   14244       if (local_syms != NULL
   14245 	  && symtab_hdr->contents != (unsigned char *) local_syms)
   14246 	{
   14247 	  if (!info->keep_memory)
   14248 	    free (local_syms);
   14249 	  else
   14250 	    symtab_hdr->contents = (unsigned char *) local_syms;
   14251 	}
   14252     }
   14253   return TRUE;
   14254 }
   14255 
   14256 /* Emit the static wrapper function preserving registers around a
   14257    __tls_get_addr_opt call.  */
   14258 
   14259 static bfd_boolean
   14260 emit_tga_desc (struct ppc_link_hash_table *htab)
   14261 {
   14262   asection *stub_sec = htab->tga_group->stub_sec;
   14263   unsigned int cfa_updt = 11 * 4;
   14264   bfd_byte *p;
   14265   bfd_vma to, from, delta;
   14266 
   14267   BFD_ASSERT (htab->tga_desc_fd->elf.root.type == bfd_link_hash_defined
   14268 	      && htab->tga_desc_fd->elf.root.u.def.section == stub_sec
   14269 	      && htab->tga_desc_fd->elf.root.u.def.value == 0);
   14270   to = defined_sym_val (&htab->tls_get_addr_fd->elf);
   14271   from = defined_sym_val (&htab->tga_desc_fd->elf) + cfa_updt;
   14272   delta = to - from;
   14273   if (delta + (1 << 25) >= 1 << 26)
   14274     {
   14275       _bfd_error_handler (_("__tls_get_addr call offset overflow"));
   14276       htab->stub_error = TRUE;
   14277       return FALSE;
   14278     }
   14279 
   14280   p = stub_sec->contents;
   14281   p = tls_get_addr_prologue (htab->elf.dynobj, p, htab);
   14282   bfd_put_32 (stub_sec->owner, B_DOT | 1 | (delta & 0x3fffffc), p);
   14283   p += 4;
   14284   p = tls_get_addr_epilogue (htab->elf.dynobj, p, htab);
   14285   return stub_sec->size == (bfd_size_type) (p - stub_sec->contents);
   14286 }
   14287 
   14288 /* Emit eh_frame describing the static wrapper function.  */
   14289 
   14290 static bfd_byte *
   14291 emit_tga_desc_eh_frame (struct ppc_link_hash_table *htab, bfd_byte *p)
   14292 {
   14293   unsigned int cfa_updt = 11 * 4;
   14294   unsigned int i;
   14295 
   14296   *p++ = DW_CFA_advance_loc + cfa_updt / 4;
   14297   *p++ = DW_CFA_def_cfa_offset;
   14298   if (htab->opd_abi)
   14299     {
   14300       *p++ = 128;
   14301       *p++ = 1;
   14302     }
   14303   else
   14304     *p++ = 96;
   14305   *p++ = DW_CFA_offset_extended_sf;
   14306   *p++ = 65;
   14307   *p++ = (-16 / 8) & 0x7f;
   14308   for (i = 4; i < 12; i++)
   14309     {
   14310       *p++ = DW_CFA_offset + i;
   14311       *p++ = (htab->opd_abi ? 13 : 12) - i;
   14312     }
   14313   *p++ = DW_CFA_advance_loc + 10;
   14314   *p++ = DW_CFA_def_cfa_offset;
   14315   *p++ = 0;
   14316   for (i = 4; i < 12; i++)
   14317     *p++ = DW_CFA_restore + i;
   14318   *p++ = DW_CFA_advance_loc + 2;
   14319   *p++ = DW_CFA_restore_extended;
   14320   *p++ = 65;
   14321   return p;
   14322 }
   14323 
   14324 /* Build all the stubs associated with the current output file.
   14325    The stubs are kept in a hash table attached to the main linker
   14326    hash table.  This function is called via gldelf64ppc_finish.  */
   14327 
   14328 bfd_boolean
   14329 ppc64_elf_build_stubs (struct bfd_link_info *info,
   14330 		       char **stats)
   14331 {
   14332   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   14333   struct map_stub *group;
   14334   asection *stub_sec;
   14335   bfd_byte *p;
   14336   int stub_sec_count = 0;
   14337 
   14338   if (htab == NULL)
   14339     return FALSE;
   14340 
   14341   /* Allocate memory to hold the linker stubs.  */
   14342   for (group = htab->group; group != NULL; group = group->next)
   14343     {
   14344       group->eh_size = 0;
   14345       group->lr_restore = 0;
   14346       if ((stub_sec = group->stub_sec) != NULL
   14347 	  && stub_sec->size != 0)
   14348 	{
   14349 	  stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
   14350 					   stub_sec->size);
   14351 	  if (stub_sec->contents == NULL)
   14352 	    return FALSE;
   14353 	  stub_sec->size = 0;
   14354 	}
   14355     }
   14356 
   14357   if (htab->glink != NULL && htab->glink->size != 0)
   14358     {
   14359       unsigned int indx;
   14360       bfd_vma plt0;
   14361 
   14362       /* Build the .glink plt call stub.  */
   14363       if (htab->params->emit_stub_syms)
   14364 	{
   14365 	  struct elf_link_hash_entry *h;
   14366 	  h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
   14367 				    TRUE, FALSE, FALSE);
   14368 	  if (h == NULL)
   14369 	    return FALSE;
   14370 	  if (h->root.type == bfd_link_hash_new)
   14371 	    {
   14372 	      h->root.type = bfd_link_hash_defined;
   14373 	      h->root.u.def.section = htab->glink;
   14374 	      h->root.u.def.value = 8;
   14375 	      h->ref_regular = 1;
   14376 	      h->def_regular = 1;
   14377 	      h->ref_regular_nonweak = 1;
   14378 	      h->forced_local = 1;
   14379 	      h->non_elf = 0;
   14380 	      h->root.linker_def = 1;
   14381 	    }
   14382 	}
   14383       plt0 = (htab->elf.splt->output_section->vma
   14384 	      + htab->elf.splt->output_offset
   14385 	      - 16);
   14386       if (info->emitrelocations)
   14387 	{
   14388 	  Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
   14389 	  if (r == NULL)
   14390 	    return FALSE;
   14391 	  r->r_offset = (htab->glink->output_offset
   14392 			 + htab->glink->output_section->vma);
   14393 	  r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
   14394 	  r->r_addend = plt0;
   14395 	}
   14396       p = htab->glink->contents;
   14397       plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
   14398       bfd_put_64 (htab->glink->owner, plt0, p);
   14399       p += 8;
   14400       if (htab->opd_abi)
   14401 	{
   14402 	  bfd_put_32 (htab->glink->owner, MFLR_R12, p);
   14403 	  p += 4;
   14404 	  bfd_put_32 (htab->glink->owner, BCL_20_31, p);
   14405 	  p += 4;
   14406 	  bfd_put_32 (htab->glink->owner, MFLR_R11, p);
   14407 	  p += 4;
   14408 	  bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
   14409 	  p += 4;
   14410 	  bfd_put_32 (htab->glink->owner, MTLR_R12, p);
   14411 	  p += 4;
   14412 	  bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
   14413 	  p += 4;
   14414 	  bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
   14415 	  p += 4;
   14416 	  bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
   14417 	  p += 4;
   14418 	  bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
   14419 	  p += 4;
   14420 	  bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
   14421 	  p += 4;
   14422 	}
   14423       else
   14424 	{
   14425 	  bfd_put_32 (htab->glink->owner, MFLR_R0, p);
   14426 	  p += 4;
   14427 	  bfd_put_32 (htab->glink->owner, BCL_20_31, p);
   14428 	  p += 4;
   14429 	  bfd_put_32 (htab->glink->owner, MFLR_R11, p);
   14430 	  p += 4;
   14431 	  bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
   14432 	  p += 4;
   14433 	  bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
   14434 	  p += 4;
   14435 	  bfd_put_32 (htab->glink->owner, MTLR_R0, p);
   14436 	  p += 4;
   14437 	  bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
   14438 	  p += 4;
   14439 	  bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
   14440 	  p += 4;
   14441 	  bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
   14442 	  p += 4;
   14443 	  bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
   14444 	  p += 4;
   14445 	  bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
   14446 	  p += 4;
   14447 	  bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
   14448 	  p += 4;
   14449 	  bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
   14450 	  p += 4;
   14451 	}
   14452       bfd_put_32 (htab->glink->owner, BCTR, p);
   14453       p += 4;
   14454       BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
   14455 
   14456       /* Build the .glink lazy link call stubs.  */
   14457       indx = 0;
   14458       while (p < htab->glink->contents + htab->glink->size)
   14459 	{
   14460 	  if (htab->opd_abi)
   14461 	    {
   14462 	      if (indx < 0x8000)
   14463 		{
   14464 		  bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
   14465 		  p += 4;
   14466 		}
   14467 	      else
   14468 		{
   14469 		  bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
   14470 		  p += 4;
   14471 		  bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
   14472 			      p);
   14473 		  p += 4;
   14474 		}
   14475 	    }
   14476 	  bfd_put_32 (htab->glink->owner,
   14477 		      B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
   14478 	  indx++;
   14479 	  p += 4;
   14480 	}
   14481     }
   14482 
   14483   if (htab->tga_group != NULL)
   14484     {
   14485       htab->tga_group->lr_restore = 23 * 4;
   14486       htab->tga_group->stub_sec->size = 24 * 4;
   14487       if (!emit_tga_desc (htab))
   14488 	return FALSE;
   14489       if (htab->glink_eh_frame != NULL
   14490 	  && htab->glink_eh_frame->size != 0)
   14491 	{
   14492 	  size_t align = 4;
   14493 
   14494 	  p = htab->glink_eh_frame->contents;
   14495 	  p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
   14496 	  p += 17;
   14497 	  htab->tga_group->eh_size = emit_tga_desc_eh_frame (htab, p) - p;
   14498 	}
   14499     }
   14500 
   14501   /* Build .glink global entry stubs, and PLT relocs for globals.  */
   14502   elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
   14503 
   14504   if (!write_plt_relocs_for_local_syms (info))
   14505     return FALSE;
   14506 
   14507   if (htab->brlt != NULL && htab->brlt->size != 0)
   14508     {
   14509       htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
   14510 					 htab->brlt->size);
   14511       if (htab->brlt->contents == NULL)
   14512 	return FALSE;
   14513     }
   14514   if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
   14515     {
   14516       htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
   14517 					    htab->relbrlt->size);
   14518       if (htab->relbrlt->contents == NULL)
   14519 	return FALSE;
   14520     }
   14521 
   14522   /* Build the stubs as directed by the stub hash table.  */
   14523   bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
   14524 
   14525   for (group = htab->group; group != NULL; group = group->next)
   14526     if (group->needs_save_res)
   14527       group->stub_sec->size += htab->sfpr->size;
   14528 
   14529   if (htab->relbrlt != NULL)
   14530     htab->relbrlt->reloc_count = 0;
   14531 
   14532   if (htab->params->plt_stub_align != 0)
   14533     for (group = htab->group; group != NULL; group = group->next)
   14534       if ((stub_sec = group->stub_sec) != NULL)
   14535 	{
   14536 	  int align = abs (htab->params->plt_stub_align);
   14537 	  stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
   14538 	}
   14539 
   14540   for (group = htab->group; group != NULL; group = group->next)
   14541     if (group->needs_save_res)
   14542       {
   14543 	stub_sec = group->stub_sec;
   14544 	memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
   14545 		htab->sfpr->contents, htab->sfpr->size);
   14546 	if (htab->params->emit_stub_syms)
   14547 	  {
   14548 	    unsigned int i;
   14549 
   14550 	    for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
   14551 	      if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
   14552 		return FALSE;
   14553 	  }
   14554       }
   14555 
   14556   if (htab->glink_eh_frame != NULL
   14557       && htab->glink_eh_frame->size != 0)
   14558     {
   14559       bfd_vma val;
   14560       size_t align = 4;
   14561 
   14562       p = htab->glink_eh_frame->contents;
   14563       p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
   14564 
   14565       for (group = htab->group; group != NULL; group = group->next)
   14566 	if (group->eh_size != 0)
   14567 	  {
   14568 	    /* Offset to stub section.  */
   14569 	    val = (group->stub_sec->output_section->vma
   14570 		   + group->stub_sec->output_offset);
   14571 	    val -= (htab->glink_eh_frame->output_section->vma
   14572 		    + htab->glink_eh_frame->output_offset
   14573 		    + (p + 8 - htab->glink_eh_frame->contents));
   14574 	    if (val + 0x80000000 > 0xffffffff)
   14575 	      {
   14576 		_bfd_error_handler
   14577 		  (_("%s offset too large for .eh_frame sdata4 encoding"),
   14578 		   group->stub_sec->name);
   14579 		return FALSE;
   14580 	      }
   14581 	    bfd_put_32 (htab->elf.dynobj, val, p + 8);
   14582 	    p += (group->eh_size + 17 + 3) & -4;
   14583 	  }
   14584       if (htab->glink != NULL && htab->glink->size != 0)
   14585 	{
   14586 	  /* Offset to .glink.  */
   14587 	  val = (htab->glink->output_section->vma
   14588 		 + htab->glink->output_offset
   14589 		 + 8);
   14590 	  val -= (htab->glink_eh_frame->output_section->vma
   14591 		  + htab->glink_eh_frame->output_offset
   14592 		  + (p + 8 - htab->glink_eh_frame->contents));
   14593 	  if (val + 0x80000000 > 0xffffffff)
   14594 	    {
   14595 	      _bfd_error_handler
   14596 		(_("%s offset too large for .eh_frame sdata4 encoding"),
   14597 		 htab->glink->name);
   14598 	      return FALSE;
   14599 	    }
   14600 	  bfd_put_32 (htab->elf.dynobj, val, p + 8);
   14601 	  p += (24 + align - 1) & -align;
   14602 	}
   14603     }
   14604 
   14605   for (group = htab->group; group != NULL; group = group->next)
   14606     if ((stub_sec = group->stub_sec) != NULL)
   14607       {
   14608 	stub_sec_count += 1;
   14609 	if (stub_sec->rawsize != stub_sec->size
   14610 	    && (htab->stub_iteration <= STUB_SHRINK_ITER
   14611 		|| stub_sec->rawsize < stub_sec->size))
   14612 	  break;
   14613       }
   14614 
   14615   if (group != NULL)
   14616     {
   14617       htab->stub_error = TRUE;
   14618       _bfd_error_handler (_("stubs don't match calculated size"));
   14619     }
   14620 
   14621   if (htab->stub_error)
   14622     return FALSE;
   14623 
   14624   if (stats != NULL)
   14625     {
   14626       char *groupmsg;
   14627       if (asprintf (&groupmsg,
   14628 		    ngettext ("linker stubs in %u group\n",
   14629 			      "linker stubs in %u groups\n",
   14630 			      stub_sec_count),
   14631 		    stub_sec_count) < 0)
   14632 	*stats = NULL;
   14633       else
   14634 	{
   14635 	  if (asprintf (stats, _("%s"
   14636 				 "  branch         %lu\n"
   14637 				 "  branch toc adj %lu\n"
   14638 				 "  branch notoc   %lu\n"
   14639 				 "  branch both    %lu\n"
   14640 				 "  long branch    %lu\n"
   14641 				 "  long toc adj   %lu\n"
   14642 				 "  long notoc     %lu\n"
   14643 				 "  long both      %lu\n"
   14644 				 "  plt call       %lu\n"
   14645 				 "  plt call save  %lu\n"
   14646 				 "  plt call notoc %lu\n"
   14647 				 "  plt call both  %lu\n"
   14648 				 "  global entry   %lu"),
   14649 			groupmsg,
   14650 			htab->stub_count[ppc_stub_long_branch - 1],
   14651 			htab->stub_count[ppc_stub_long_branch_r2off - 1],
   14652 			htab->stub_count[ppc_stub_long_branch_notoc - 1],
   14653 			htab->stub_count[ppc_stub_long_branch_both - 1],
   14654 			htab->stub_count[ppc_stub_plt_branch - 1],
   14655 			htab->stub_count[ppc_stub_plt_branch_r2off - 1],
   14656 			htab->stub_count[ppc_stub_plt_branch_notoc - 1],
   14657 			htab->stub_count[ppc_stub_plt_branch_both - 1],
   14658 			htab->stub_count[ppc_stub_plt_call - 1],
   14659 			htab->stub_count[ppc_stub_plt_call_r2save - 1],
   14660 			htab->stub_count[ppc_stub_plt_call_notoc - 1],
   14661 			htab->stub_count[ppc_stub_plt_call_both - 1],
   14662 			htab->stub_count[ppc_stub_global_entry - 1]) < 0)
   14663 	    *stats = NULL;
   14664 	  free (groupmsg);
   14665 	}
   14666     }
   14667   return TRUE;
   14668 }
   14669 
   14670 /* What to do when ld finds relocations against symbols defined in
   14671    discarded sections.  */
   14672 
   14673 static unsigned int
   14674 ppc64_elf_action_discarded (asection *sec)
   14675 {
   14676   if (strcmp (".opd", sec->name) == 0)
   14677     return 0;
   14678 
   14679   if (strcmp (".toc", sec->name) == 0)
   14680     return 0;
   14681 
   14682   if (strcmp (".toc1", sec->name) == 0)
   14683     return 0;
   14684 
   14685   return _bfd_elf_default_action_discarded (sec);
   14686 }
   14687 
   14688 /* These are the dynamic relocations supported by glibc.  */
   14689 
   14690 static bfd_boolean
   14691 ppc64_glibc_dynamic_reloc (enum elf_ppc64_reloc_type r_type)
   14692 {
   14693   switch (r_type)
   14694     {
   14695     case R_PPC64_RELATIVE:
   14696     case R_PPC64_NONE:
   14697     case R_PPC64_ADDR64:
   14698     case R_PPC64_GLOB_DAT:
   14699     case R_PPC64_IRELATIVE:
   14700     case R_PPC64_JMP_IREL:
   14701     case R_PPC64_JMP_SLOT:
   14702     case R_PPC64_DTPMOD64:
   14703     case R_PPC64_DTPREL64:
   14704     case R_PPC64_TPREL64:
   14705     case R_PPC64_TPREL16_LO_DS:
   14706     case R_PPC64_TPREL16_DS:
   14707     case R_PPC64_TPREL16:
   14708     case R_PPC64_TPREL16_LO:
   14709     case R_PPC64_TPREL16_HI:
   14710     case R_PPC64_TPREL16_HIGH:
   14711     case R_PPC64_TPREL16_HA:
   14712     case R_PPC64_TPREL16_HIGHA:
   14713     case R_PPC64_TPREL16_HIGHER:
   14714     case R_PPC64_TPREL16_HIGHEST:
   14715     case R_PPC64_TPREL16_HIGHERA:
   14716     case R_PPC64_TPREL16_HIGHESTA:
   14717     case R_PPC64_ADDR16_LO_DS:
   14718     case R_PPC64_ADDR16_LO:
   14719     case R_PPC64_ADDR16_HI:
   14720     case R_PPC64_ADDR16_HIGH:
   14721     case R_PPC64_ADDR16_HA:
   14722     case R_PPC64_ADDR16_HIGHA:
   14723     case R_PPC64_REL30:
   14724     case R_PPC64_COPY:
   14725     case R_PPC64_UADDR64:
   14726     case R_PPC64_UADDR32:
   14727     case R_PPC64_ADDR32:
   14728     case R_PPC64_ADDR24:
   14729     case R_PPC64_ADDR16:
   14730     case R_PPC64_UADDR16:
   14731     case R_PPC64_ADDR16_DS:
   14732     case R_PPC64_ADDR16_HIGHER:
   14733     case R_PPC64_ADDR16_HIGHEST:
   14734     case R_PPC64_ADDR16_HIGHERA:
   14735     case R_PPC64_ADDR16_HIGHESTA:
   14736     case R_PPC64_ADDR14:
   14737     case R_PPC64_ADDR14_BRTAKEN:
   14738     case R_PPC64_ADDR14_BRNTAKEN:
   14739     case R_PPC64_REL32:
   14740     case R_PPC64_REL64:
   14741       return TRUE;
   14742 
   14743     default:
   14744       return FALSE;
   14745     }
   14746 }
   14747 
   14748 /* The RELOCATE_SECTION function is called by the ELF backend linker
   14749    to handle the relocations for a section.
   14750 
   14751    The relocs are always passed as Rela structures; if the section
   14752    actually uses Rel structures, the r_addend field will always be
   14753    zero.
   14754 
   14755    This function is responsible for adjust the section contents as
   14756    necessary, and (if using Rela relocs and generating a
   14757    relocatable output file) adjusting the reloc addend as
   14758    necessary.
   14759 
   14760    This function does not have to worry about setting the reloc
   14761    address or the reloc symbol index.
   14762 
   14763    LOCAL_SYMS is a pointer to the swapped in local symbols.
   14764 
   14765    LOCAL_SECTIONS is an array giving the section in the input file
   14766    corresponding to the st_shndx field of each local symbol.
   14767 
   14768    The global hash table entry for the global symbols can be found
   14769    via elf_sym_hashes (input_bfd).
   14770 
   14771    When generating relocatable output, this function must handle
   14772    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
   14773    going to be the section symbol corresponding to the output
   14774    section, which means that the addend must be adjusted
   14775    accordingly.  */
   14776 
   14777 static bfd_boolean
   14778 ppc64_elf_relocate_section (bfd *output_bfd,
   14779 			    struct bfd_link_info *info,
   14780 			    bfd *input_bfd,
   14781 			    asection *input_section,
   14782 			    bfd_byte *contents,
   14783 			    Elf_Internal_Rela *relocs,
   14784 			    Elf_Internal_Sym *local_syms,
   14785 			    asection **local_sections)
   14786 {
   14787   struct ppc_link_hash_table *htab;
   14788   Elf_Internal_Shdr *symtab_hdr;
   14789   struct elf_link_hash_entry **sym_hashes;
   14790   Elf_Internal_Rela *rel;
   14791   Elf_Internal_Rela *wrel;
   14792   Elf_Internal_Rela *relend;
   14793   Elf_Internal_Rela outrel;
   14794   bfd_byte *loc;
   14795   struct got_entry **local_got_ents;
   14796   bfd_vma TOCstart;
   14797   bfd_boolean ret = TRUE;
   14798   bfd_boolean is_opd;
   14799   /* Assume 'at' branch hints.  */
   14800   bfd_boolean is_isa_v2 = TRUE;
   14801   bfd_boolean warned_dynamic = FALSE;
   14802   bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
   14803 
   14804   /* Initialize howto table if needed.  */
   14805   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
   14806     ppc_howto_init ();
   14807 
   14808   htab = ppc_hash_table (info);
   14809   if (htab == NULL)
   14810     return FALSE;
   14811 
   14812   /* Don't relocate stub sections.  */
   14813   if (input_section->owner == htab->params->stub_bfd)
   14814     return TRUE;
   14815 
   14816   if (!is_ppc64_elf (input_bfd))
   14817     {
   14818       bfd_set_error (bfd_error_wrong_format);
   14819       return FALSE;
   14820     }
   14821 
   14822   local_got_ents = elf_local_got_ents (input_bfd);
   14823   TOCstart = elf_gp (output_bfd);
   14824   symtab_hdr = &elf_symtab_hdr (input_bfd);
   14825   sym_hashes = elf_sym_hashes (input_bfd);
   14826   is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
   14827 
   14828   rel = wrel = relocs;
   14829   relend = relocs + input_section->reloc_count;
   14830   for (; rel < relend; wrel++, rel++)
   14831     {
   14832       enum elf_ppc64_reloc_type r_type;
   14833       bfd_vma addend;
   14834       bfd_reloc_status_type r;
   14835       Elf_Internal_Sym *sym;
   14836       asection *sec;
   14837       struct elf_link_hash_entry *h_elf;
   14838       struct ppc_link_hash_entry *h;
   14839       struct ppc_link_hash_entry *fdh;
   14840       const char *sym_name;
   14841       unsigned long r_symndx, toc_symndx;
   14842       bfd_vma toc_addend;
   14843       unsigned char tls_mask, tls_gd, tls_type;
   14844       unsigned char sym_type;
   14845       bfd_vma relocation;
   14846       bfd_boolean unresolved_reloc, save_unresolved_reloc;
   14847       bfd_boolean warned;
   14848       enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
   14849       unsigned int insn;
   14850       unsigned int mask;
   14851       struct ppc_stub_hash_entry *stub_entry;
   14852       bfd_vma max_br_offset;
   14853       bfd_vma from;
   14854       Elf_Internal_Rela orig_rel;
   14855       reloc_howto_type *howto;
   14856       struct reloc_howto_struct alt_howto;
   14857       uint64_t pinsn;
   14858       bfd_vma offset;
   14859 
   14860     again:
   14861       orig_rel = *rel;
   14862 
   14863       r_type = ELF64_R_TYPE (rel->r_info);
   14864       r_symndx = ELF64_R_SYM (rel->r_info);
   14865 
   14866       /* For old style R_PPC64_TOC relocs with a zero symbol, use the
   14867 	 symbol of the previous ADDR64 reloc.  The symbol gives us the
   14868 	 proper TOC base to use.  */
   14869       if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
   14870 	  && wrel != relocs
   14871 	  && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
   14872 	  && is_opd)
   14873 	r_symndx = ELF64_R_SYM (wrel[-1].r_info);
   14874 
   14875       sym = NULL;
   14876       sec = NULL;
   14877       h_elf = NULL;
   14878       sym_name = NULL;
   14879       unresolved_reloc = FALSE;
   14880       warned = FALSE;
   14881 
   14882       if (r_symndx < symtab_hdr->sh_info)
   14883 	{
   14884 	  /* It's a local symbol.  */
   14885 	  struct _opd_sec_data *opd;
   14886 
   14887 	  sym = local_syms + r_symndx;
   14888 	  sec = local_sections[r_symndx];
   14889 	  sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
   14890 	  sym_type = ELF64_ST_TYPE (sym->st_info);
   14891 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
   14892 	  opd = get_opd_info (sec);
   14893 	  if (opd != NULL && opd->adjust != NULL)
   14894 	    {
   14895 	      long adjust = opd->adjust[OPD_NDX (sym->st_value
   14896 						 + rel->r_addend)];
   14897 	      if (adjust == -1)
   14898 		relocation = 0;
   14899 	      else
   14900 		{
   14901 		  /* If this is a relocation against the opd section sym
   14902 		     and we have edited .opd, adjust the reloc addend so
   14903 		     that ld -r and ld --emit-relocs output is correct.
   14904 		     If it is a reloc against some other .opd symbol,
   14905 		     then the symbol value will be adjusted later.  */
   14906 		  if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
   14907 		    rel->r_addend += adjust;
   14908 		  else
   14909 		    relocation += adjust;
   14910 		}
   14911 	    }
   14912 	}
   14913       else
   14914 	{
   14915 	  bfd_boolean ignored;
   14916 
   14917 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
   14918 				   r_symndx, symtab_hdr, sym_hashes,
   14919 				   h_elf, sec, relocation,
   14920 				   unresolved_reloc, warned, ignored);
   14921 	  sym_name = h_elf->root.root.string;
   14922 	  sym_type = h_elf->type;
   14923 	  if (sec != NULL
   14924 	      && sec->owner == output_bfd
   14925 	      && strcmp (sec->name, ".opd") == 0)
   14926 	    {
   14927 	      /* This is a symbol defined in a linker script.  All
   14928 		 such are defined in output sections, even those
   14929 		 defined by simple assignment from a symbol defined in
   14930 		 an input section.  Transfer the symbol to an
   14931 		 appropriate input .opd section, so that a branch to
   14932 		 this symbol will be mapped to the location specified
   14933 		 by the opd entry.  */
   14934 	      struct bfd_link_order *lo;
   14935 	      for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
   14936 		if (lo->type == bfd_indirect_link_order)
   14937 		  {
   14938 		    asection *isec = lo->u.indirect.section;
   14939 		    if (h_elf->root.u.def.value >= isec->output_offset
   14940 			&& h_elf->root.u.def.value < (isec->output_offset
   14941 						      + isec->size))
   14942 		      {
   14943 			h_elf->root.u.def.value -= isec->output_offset;
   14944 			h_elf->root.u.def.section = isec;
   14945 			sec = isec;
   14946 			break;
   14947 		      }
   14948 		  }
   14949 	    }
   14950 	}
   14951       h = ppc_elf_hash_entry (h_elf);
   14952 
   14953       if (sec != NULL && discarded_section (sec))
   14954 	{
   14955 	  _bfd_clear_contents (ppc64_elf_howto_table[r_type],
   14956 			       input_bfd, input_section,
   14957 			       contents, rel->r_offset);
   14958 	  wrel->r_offset = rel->r_offset;
   14959 	  wrel->r_info = 0;
   14960 	  wrel->r_addend = 0;
   14961 
   14962 	  /* For ld -r, remove relocations in debug sections against
   14963 	     symbols defined in discarded sections.  Not done for
   14964 	     non-debug to preserve relocs in .eh_frame which the
   14965 	     eh_frame editing code expects to be present.  */
   14966 	  if (bfd_link_relocatable (info)
   14967 	      && (input_section->flags & SEC_DEBUGGING))
   14968 	    wrel--;
   14969 
   14970 	  continue;
   14971 	}
   14972 
   14973       if (bfd_link_relocatable (info))
   14974 	goto copy_reloc;
   14975 
   14976       if (h != NULL && &h->elf == htab->elf.hgot)
   14977 	{
   14978 	  relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
   14979 	  sec = bfd_abs_section_ptr;
   14980 	  unresolved_reloc = FALSE;
   14981 	}
   14982 
   14983       /* TLS optimizations.  Replace instruction sequences and relocs
   14984 	 based on information we collected in tls_optimize.  We edit
   14985 	 RELOCS so that --emit-relocs will output something sensible
   14986 	 for the final instruction stream.  */
   14987       tls_mask = 0;
   14988       tls_gd = 0;
   14989       toc_symndx = 0;
   14990       if (h != NULL)
   14991 	tls_mask = h->tls_mask;
   14992       else if (local_got_ents != NULL)
   14993 	{
   14994 	  struct plt_entry **local_plt = (struct plt_entry **)
   14995 	    (local_got_ents + symtab_hdr->sh_info);
   14996 	  unsigned char *lgot_masks = (unsigned char *)
   14997 	    (local_plt + symtab_hdr->sh_info);
   14998 	  tls_mask = lgot_masks[r_symndx];
   14999 	}
   15000       if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
   15001 	  && (r_type == R_PPC64_TLS
   15002 	      || r_type == R_PPC64_TLSGD
   15003 	      || r_type == R_PPC64_TLSLD))
   15004 	{
   15005 	  /* Check for toc tls entries.  */
   15006 	  unsigned char *toc_tls;
   15007 
   15008 	  if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
   15009 			     &local_syms, rel, input_bfd))
   15010 	    return FALSE;
   15011 
   15012 	  if (toc_tls)
   15013 	    tls_mask = *toc_tls;
   15014 	}
   15015 
   15016       /* Check that tls relocs are used with tls syms, and non-tls
   15017 	 relocs are used with non-tls syms.  */
   15018       if (r_symndx != STN_UNDEF
   15019 	  && r_type != R_PPC64_NONE
   15020 	  && (h == NULL
   15021 	      || h->elf.root.type == bfd_link_hash_defined
   15022 	      || h->elf.root.type == bfd_link_hash_defweak)
   15023 	  && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
   15024 	{
   15025 	  if ((tls_mask & TLS_TLS) != 0
   15026 	      && (r_type == R_PPC64_TLS
   15027 		  || r_type == R_PPC64_TLSGD
   15028 		  || r_type == R_PPC64_TLSLD))
   15029 	    /* R_PPC64_TLS is OK against a symbol in the TOC.  */
   15030 	    ;
   15031 	  else
   15032 	    info->callbacks->einfo
   15033 	      (!IS_PPC64_TLS_RELOC (r_type)
   15034 	       /* xgettext:c-format */
   15035 	       ? _("%H: %s used with TLS symbol `%pT'\n")
   15036 	       /* xgettext:c-format */
   15037 	       : _("%H: %s used with non-TLS symbol `%pT'\n"),
   15038 	       input_bfd, input_section, rel->r_offset,
   15039 	       ppc64_elf_howto_table[r_type]->name,
   15040 	       sym_name);
   15041 	}
   15042 
   15043       /* Ensure reloc mapping code below stays sane.  */
   15044       if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
   15045 	  || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
   15046 	  || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TLSGD16 & 3)
   15047 	  || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
   15048 	  || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
   15049 	  || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
   15050 	  || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TPREL16_DS & 3)
   15051 	  || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
   15052 	  || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
   15053 	  || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
   15054 	abort ();
   15055 
   15056       switch (r_type)
   15057 	{
   15058 	default:
   15059 	  break;
   15060 
   15061 	case R_PPC64_LO_DS_OPT:
   15062 	  insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
   15063 	  if ((insn & (0x3fu << 26)) != 58u << 26)
   15064 	    abort ();
   15065 	  insn += (14u << 26) - (58u << 26);
   15066 	  bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
   15067 	  r_type = R_PPC64_TOC16_LO;
   15068 	  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   15069 	  break;
   15070 
   15071 	case R_PPC64_TOC16:
   15072 	case R_PPC64_TOC16_LO:
   15073 	case R_PPC64_TOC16_DS:
   15074 	case R_PPC64_TOC16_LO_DS:
   15075 	  {
   15076 	    /* Check for toc tls entries.  */
   15077 	    unsigned char *toc_tls;
   15078 	    int retval;
   15079 
   15080 	    retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
   15081 				   &local_syms, rel, input_bfd);
   15082 	    if (retval == 0)
   15083 	      return FALSE;
   15084 
   15085 	    if (toc_tls)
   15086 	      {
   15087 		tls_mask = *toc_tls;
   15088 		if (r_type == R_PPC64_TOC16_DS
   15089 		    || r_type == R_PPC64_TOC16_LO_DS)
   15090 		  {
   15091 		    if ((tls_mask & TLS_TLS) != 0
   15092 			&& (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
   15093 		      goto toctprel;
   15094 		  }
   15095 		else
   15096 		  {
   15097 		    /* If we found a GD reloc pair, then we might be
   15098 		       doing a GD->IE transition.  */
   15099 		    if (retval == 2)
   15100 		      {
   15101 			tls_gd = TLS_GDIE;
   15102 			if ((tls_mask & TLS_TLS) != 0
   15103 			    && (tls_mask & TLS_GD) == 0)
   15104 			  goto tls_ldgd_opt;
   15105 		      }
   15106 		    else if (retval == 3)
   15107 		      {
   15108 			if ((tls_mask & TLS_TLS) != 0
   15109 			    && (tls_mask & TLS_LD) == 0)
   15110 			  goto tls_ldgd_opt;
   15111 		      }
   15112 		  }
   15113 	      }
   15114 	  }
   15115 	  break;
   15116 
   15117 	case R_PPC64_GOT_TPREL16_HI:
   15118 	case R_PPC64_GOT_TPREL16_HA:
   15119 	  if ((tls_mask & TLS_TLS) != 0
   15120 	      && (tls_mask & TLS_TPREL) == 0)
   15121 	    {
   15122 	      rel->r_offset -= d_offset;
   15123 	      bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
   15124 	      r_type = R_PPC64_NONE;
   15125 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   15126 	    }
   15127 	  break;
   15128 
   15129 	case R_PPC64_GOT_TPREL16_DS:
   15130 	case R_PPC64_GOT_TPREL16_LO_DS:
   15131 	  if ((tls_mask & TLS_TLS) != 0
   15132 	      && (tls_mask & TLS_TPREL) == 0)
   15133 	    {
   15134 	    toctprel:
   15135 	      insn = bfd_get_32 (input_bfd,
   15136 				 contents + rel->r_offset - d_offset);
   15137 	      insn &= 31 << 21;
   15138 	      insn |= 0x3c0d0000;	/* addis 0,13,0 */
   15139 	      bfd_put_32 (input_bfd, insn,
   15140 			  contents + rel->r_offset - d_offset);
   15141 	      r_type = R_PPC64_TPREL16_HA;
   15142 	      if (toc_symndx != 0)
   15143 		{
   15144 		  rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
   15145 		  rel->r_addend = toc_addend;
   15146 		  /* We changed the symbol.  Start over in order to
   15147 		     get h, sym, sec etc. right.  */
   15148 		  goto again;
   15149 		}
   15150 	      else
   15151 		rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   15152 	    }
   15153 	  break;
   15154 
   15155 	case R_PPC64_GOT_TPREL_PCREL34:
   15156 	  if ((tls_mask & TLS_TLS) != 0
   15157 	      && (tls_mask & TLS_TPREL) == 0)
   15158 	    {
   15159 	      /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel  */
   15160 	      pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   15161 	      pinsn <<= 32;
   15162 	      pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
   15163 	      pinsn += ((2ULL << 56) + (-1ULL << 52)
   15164 			+ (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
   15165 	      bfd_put_32 (input_bfd, pinsn >> 32,
   15166 			  contents + rel->r_offset);
   15167 	      bfd_put_32 (input_bfd, pinsn & 0xffffffff,
   15168 			  contents + rel->r_offset + 4);
   15169 	      r_type = R_PPC64_TPREL34;
   15170 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   15171 	    }
   15172 	  break;
   15173 
   15174 	case R_PPC64_TLS:
   15175 	  if ((tls_mask & TLS_TLS) != 0
   15176 	      && (tls_mask & TLS_TPREL) == 0)
   15177 	    {
   15178 	      insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
   15179 	      insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
   15180 	      if (insn == 0)
   15181 		break;
   15182 	      if ((rel->r_offset & 3) == 0)
   15183 		{
   15184 		  bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   15185 		  /* Was PPC64_TLS which sits on insn boundary, now
   15186 		     PPC64_TPREL16_LO which is at low-order half-word.  */
   15187 		  rel->r_offset += d_offset;
   15188 		  r_type = R_PPC64_TPREL16_LO;
   15189 		  if (toc_symndx != 0)
   15190 		    {
   15191 		      rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
   15192 		      rel->r_addend = toc_addend;
   15193 		      /* We changed the symbol.  Start over in order to
   15194 			 get h, sym, sec etc. right.  */
   15195 		      goto again;
   15196 		    }
   15197 		  else
   15198 		    rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   15199 		}
   15200 	      else if ((rel->r_offset & 3) == 1)
   15201 		{
   15202 		  /* For pcrel IE to LE we already have the full
   15203 		     offset and thus don't need an addi here.  A nop
   15204 		     or mr will do.  */
   15205 		  if ((insn & (0x3fu << 26)) == 14 << 26)
   15206 		    {
   15207 		      /* Extract regs from addi rt,ra,si.  */
   15208 		      unsigned int rt = (insn >> 21) & 0x1f;
   15209 		      unsigned int ra = (insn >> 16) & 0x1f;
   15210 		      if (rt == ra)
   15211 			insn = NOP;
   15212 		      else
   15213 			{
   15214 			  /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs.  */
   15215 			  insn = (rt << 16) | (ra << 21) | (ra << 11);
   15216 			  insn |= (31u << 26) | (444u << 1);
   15217 			}
   15218 		    }
   15219 		  bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
   15220 		}
   15221 	    }
   15222 	  break;
   15223 
   15224 	case R_PPC64_GOT_TLSGD16_HI:
   15225 	case R_PPC64_GOT_TLSGD16_HA:
   15226 	  tls_gd = TLS_GDIE;
   15227 	  if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
   15228 	    goto tls_gdld_hi;
   15229 	  break;
   15230 
   15231 	case R_PPC64_GOT_TLSLD16_HI:
   15232 	case R_PPC64_GOT_TLSLD16_HA:
   15233 	  if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
   15234 	    {
   15235 	    tls_gdld_hi:
   15236 	      if ((tls_mask & tls_gd) != 0)
   15237 		r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
   15238 			  + R_PPC64_GOT_TPREL16_DS);
   15239 	      else
   15240 		{
   15241 		  rel->r_offset -= d_offset;
   15242 		  bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
   15243 		  r_type = R_PPC64_NONE;
   15244 		}
   15245 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   15246 	    }
   15247 	  break;
   15248 
   15249 	case R_PPC64_GOT_TLSGD16:
   15250 	case R_PPC64_GOT_TLSGD16_LO:
   15251 	  tls_gd = TLS_GDIE;
   15252 	  if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
   15253 	    goto tls_ldgd_opt;
   15254 	  break;
   15255 
   15256 	case R_PPC64_GOT_TLSLD16:
   15257 	case R_PPC64_GOT_TLSLD16_LO:
   15258 	  if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
   15259 	    {
   15260 	      unsigned int insn1, insn2;
   15261 
   15262 	    tls_ldgd_opt:
   15263 	      offset = (bfd_vma) -1;
   15264 	      /* If not using the newer R_PPC64_TLSGD/LD to mark
   15265 		 __tls_get_addr calls, we must trust that the call
   15266 		 stays with its arg setup insns, ie. that the next
   15267 		 reloc is the __tls_get_addr call associated with
   15268 		 the current reloc.  Edit both insns.  */
   15269 	      if (input_section->nomark_tls_get_addr
   15270 		  && rel + 1 < relend
   15271 		  && branch_reloc_hash_match (input_bfd, rel + 1,
   15272 					      htab->tls_get_addr_fd,
   15273 					      htab->tga_desc_fd,
   15274 					      htab->tls_get_addr,
   15275 					      htab->tga_desc))
   15276 		offset = rel[1].r_offset;
   15277 	      /* We read the low GOT_TLS (or TOC16) insn because we
   15278 		 need to keep the destination reg.  It may be
   15279 		 something other than the usual r3, and moved to r3
   15280 		 before the call by intervening code.  */
   15281 	      insn1 = bfd_get_32 (input_bfd,
   15282 				  contents + rel->r_offset - d_offset);
   15283 	      if ((tls_mask & tls_gd) != 0)
   15284 		{
   15285 		  /* IE */
   15286 		  insn1 &= (0x1f << 21) | (0x1f << 16);
   15287 		  insn1 |= 58u << 26;	/* ld */
   15288 		  insn2 = 0x7c636a14;	/* add 3,3,13 */
   15289 		  if (offset != (bfd_vma) -1)
   15290 		    rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
   15291 		  if (r_type == R_PPC64_TOC16
   15292 		      || r_type == R_PPC64_TOC16_LO)
   15293 		    r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
   15294 		  else
   15295 		    r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
   15296 			      + R_PPC64_GOT_TPREL16_DS);
   15297 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   15298 		}
   15299 	      else
   15300 		{
   15301 		  /* LE */
   15302 		  insn1 &= 0x1f << 21;
   15303 		  insn1 |= 0x3c0d0000;	/* addis r,13,0 */
   15304 		  insn2 = 0x38630000;	/* addi 3,3,0 */
   15305 		  if (tls_gd == 0)
   15306 		    {
   15307 		      /* Was an LD reloc.  */
   15308 		      r_symndx = STN_UNDEF;
   15309 		      rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
   15310 		    }
   15311 		  else if (toc_symndx != 0)
   15312 		    {
   15313 		      r_symndx = toc_symndx;
   15314 		      rel->r_addend = toc_addend;
   15315 		    }
   15316 		  r_type = R_PPC64_TPREL16_HA;
   15317 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   15318 		  if (offset != (bfd_vma) -1)
   15319 		    {
   15320 		      rel[1].r_info = ELF64_R_INFO (r_symndx,
   15321 						    R_PPC64_TPREL16_LO);
   15322 		      rel[1].r_offset = offset + d_offset;
   15323 		      rel[1].r_addend = rel->r_addend;
   15324 		    }
   15325 		}
   15326 	      bfd_put_32 (input_bfd, insn1,
   15327 			  contents + rel->r_offset - d_offset);
   15328 	      if (offset != (bfd_vma) -1)
   15329 		{
   15330 		  bfd_put_32 (input_bfd, insn2, contents + offset);
   15331 		  if (offset + 8 <= input_section->size)
   15332 		    {
   15333 		      insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
   15334 		      if (insn2 == LD_R2_0R1 + STK_TOC (htab))
   15335 			bfd_put_32 (input_bfd, NOP, contents + offset + 4);
   15336 		    }
   15337 		}
   15338 	      if ((tls_mask & tls_gd) == 0
   15339 		  && (tls_gd == 0 || toc_symndx != 0))
   15340 		{
   15341 		  /* We changed the symbol.  Start over in order
   15342 		     to get h, sym, sec etc. right.  */
   15343 		  goto again;
   15344 		}
   15345 	    }
   15346 	  break;
   15347 
   15348 	case R_PPC64_GOT_TLSGD_PCREL34:
   15349 	  if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
   15350 	    {
   15351 	      pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   15352 	      pinsn <<= 32;
   15353 	      pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
   15354 	      if ((tls_mask & TLS_GDIE) != 0)
   15355 		{
   15356 		  /* IE, pla -> pld  */
   15357 		  pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
   15358 		  r_type = R_PPC64_GOT_TPREL_PCREL34;
   15359 		}
   15360 	      else
   15361 		{
   15362 		  /* LE, pla pcrel -> paddi r13  */
   15363 		  pinsn += (-1ULL << 52) + (13ULL << 16);
   15364 		  r_type = R_PPC64_TPREL34;
   15365 		}
   15366 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   15367 	      bfd_put_32 (input_bfd, pinsn >> 32,
   15368 			  contents + rel->r_offset);
   15369 	      bfd_put_32 (input_bfd, pinsn & 0xffffffff,
   15370 			  contents + rel->r_offset + 4);
   15371 	    }
   15372 	  break;
   15373 
   15374 	case R_PPC64_GOT_TLSLD_PCREL34:
   15375 	  if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
   15376 	    {
   15377 	      pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   15378 	      pinsn <<= 32;
   15379 	      pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
   15380 	      pinsn += (-1ULL << 52) + (13ULL << 16);
   15381 	      bfd_put_32 (input_bfd, pinsn >> 32,
   15382 			  contents + rel->r_offset);
   15383 	      bfd_put_32 (input_bfd, pinsn & 0xffffffff,
   15384 			  contents + rel->r_offset + 4);
   15385 	      rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
   15386 	      r_symndx = STN_UNDEF;
   15387 	      r_type = R_PPC64_TPREL34;
   15388 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   15389 	      goto again;
   15390 	    }
   15391 	  break;
   15392 
   15393 	case R_PPC64_TLSGD:
   15394 	  if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
   15395 	      && rel + 1 < relend)
   15396 	    {
   15397 	      unsigned int insn2;
   15398 	      enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
   15399 
   15400 	      offset = rel->r_offset;
   15401 	      if (is_plt_seq_reloc (r_type1))
   15402 		{
   15403 		  bfd_put_32 (output_bfd, NOP, contents + offset);
   15404 		  if (r_type1 == R_PPC64_PLT_PCREL34
   15405 		      || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
   15406 		    bfd_put_32 (output_bfd, NOP, contents + offset + 4);
   15407 		  rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
   15408 		  break;
   15409 		}
   15410 
   15411 	      if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
   15412 		bfd_put_32 (output_bfd, NOP, contents + offset + 4);
   15413 
   15414 	      if ((tls_mask & TLS_GDIE) != 0)
   15415 		{
   15416 		  /* IE */
   15417 		  r_type = R_PPC64_NONE;
   15418 		  insn2 = 0x7c636a14;	/* add 3,3,13 */
   15419 		}
   15420 	      else
   15421 		{
   15422 		  /* LE */
   15423 		  if (toc_symndx != 0)
   15424 		    {
   15425 		      r_symndx = toc_symndx;
   15426 		      rel->r_addend = toc_addend;
   15427 		    }
   15428 		  if (r_type1 == R_PPC64_REL24_NOTOC
   15429 		      || r_type1 == R_PPC64_PLTCALL_NOTOC)
   15430 		    {
   15431 		      r_type = R_PPC64_NONE;
   15432 		      insn2 = NOP;
   15433 		    }
   15434 		  else
   15435 		    {
   15436 		      rel->r_offset = offset + d_offset;
   15437 		      r_type = R_PPC64_TPREL16_LO;
   15438 		      insn2 = 0x38630000;	/* addi 3,3,0 */
   15439 		    }
   15440 		}
   15441 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   15442 	      /* Zap the reloc on the _tls_get_addr call too.  */
   15443 	      BFD_ASSERT (offset == rel[1].r_offset);
   15444 	      rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
   15445 	      bfd_put_32 (input_bfd, insn2, contents + offset);
   15446 	      if ((tls_mask & TLS_GDIE) == 0
   15447 		  && toc_symndx != 0
   15448 		  && r_type != R_PPC64_NONE)
   15449 		goto again;
   15450 	    }
   15451 	  break;
   15452 
   15453 	case R_PPC64_TLSLD:
   15454 	  if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
   15455 	      && rel + 1 < relend)
   15456 	    {
   15457 	      unsigned int insn2;
   15458 	      enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
   15459 
   15460 	      offset = rel->r_offset;
   15461 	      if (is_plt_seq_reloc (r_type1))
   15462 		{
   15463 		  bfd_put_32 (output_bfd, NOP, contents + offset);
   15464 		  if (r_type1 == R_PPC64_PLT_PCREL34
   15465 		      || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
   15466 		    bfd_put_32 (output_bfd, NOP, contents + offset + 4);
   15467 		  rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
   15468 		  break;
   15469 		}
   15470 
   15471 	      if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
   15472 		bfd_put_32 (output_bfd, NOP, contents + offset + 4);
   15473 
   15474 	      if (r_type1 == R_PPC64_REL24_NOTOC
   15475 		  || r_type1 == R_PPC64_PLTCALL_NOTOC)
   15476 		{
   15477 		  r_type = R_PPC64_NONE;
   15478 		  insn2 = NOP;
   15479 		}
   15480 	      else
   15481 		{
   15482 		  rel->r_offset = offset + d_offset;
   15483 		  r_symndx = STN_UNDEF;
   15484 		  r_type = R_PPC64_TPREL16_LO;
   15485 		  rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
   15486 		  insn2 = 0x38630000;	/* addi 3,3,0 */
   15487 		}
   15488 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   15489 	      /* Zap the reloc on the _tls_get_addr call too.  */
   15490 	      BFD_ASSERT (offset == rel[1].r_offset);
   15491 	      rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
   15492 	      bfd_put_32 (input_bfd, insn2, contents + offset);
   15493 	      if (r_type != R_PPC64_NONE)
   15494 		goto again;
   15495 	    }
   15496 	  break;
   15497 
   15498 	case R_PPC64_DTPMOD64:
   15499 	  if (rel + 1 < relend
   15500 	      && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
   15501 	      && rel[1].r_offset == rel->r_offset + 8)
   15502 	    {
   15503 	      if ((tls_mask & TLS_GD) == 0)
   15504 		{
   15505 		  rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
   15506 		  if ((tls_mask & TLS_GDIE) != 0)
   15507 		    r_type = R_PPC64_TPREL64;
   15508 		  else
   15509 		    {
   15510 		      bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
   15511 		      r_type = R_PPC64_NONE;
   15512 		    }
   15513 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   15514 		}
   15515 	    }
   15516 	  else
   15517 	    {
   15518 	      if ((tls_mask & TLS_LD) == 0)
   15519 		{
   15520 		  bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
   15521 		  r_type = R_PPC64_NONE;
   15522 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   15523 		}
   15524 	    }
   15525 	  break;
   15526 
   15527 	case R_PPC64_TPREL64:
   15528 	  if ((tls_mask & TLS_TPREL) == 0)
   15529 	    {
   15530 	      r_type = R_PPC64_NONE;
   15531 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   15532 	    }
   15533 	  break;
   15534 
   15535 	case R_PPC64_ENTRY:
   15536 	  relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
   15537 	  if (!bfd_link_pic (info)
   15538 	      && !info->traditional_format
   15539 	      && relocation + 0x80008000 <= 0xffffffff)
   15540 	    {
   15541 	      unsigned int insn1, insn2;
   15542 
   15543 	      insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
   15544 	      insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
   15545 	      if ((insn1 & ~0xfffc) == LD_R2_0R12
   15546 		  && insn2 == ADD_R2_R2_R12)
   15547 		{
   15548 		  bfd_put_32 (input_bfd,
   15549 			      LIS_R2 + PPC_HA (relocation),
   15550 			      contents + rel->r_offset);
   15551 		  bfd_put_32 (input_bfd,
   15552 			      ADDI_R2_R2 + PPC_LO (relocation),
   15553 			      contents + rel->r_offset + 4);
   15554 		}
   15555 	    }
   15556 	  else
   15557 	    {
   15558 	      relocation -= (rel->r_offset
   15559 			     + input_section->output_offset
   15560 			     + input_section->output_section->vma);
   15561 	      if (relocation + 0x80008000 <= 0xffffffff)
   15562 		{
   15563 		  unsigned int insn1, insn2;
   15564 
   15565 		  insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
   15566 		  insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
   15567 		  if ((insn1 & ~0xfffc) == LD_R2_0R12
   15568 		      && insn2 == ADD_R2_R2_R12)
   15569 		    {
   15570 		      bfd_put_32 (input_bfd,
   15571 				  ADDIS_R2_R12 + PPC_HA (relocation),
   15572 				  contents + rel->r_offset);
   15573 		      bfd_put_32 (input_bfd,
   15574 				  ADDI_R2_R2 + PPC_LO (relocation),
   15575 				  contents + rel->r_offset + 4);
   15576 		    }
   15577 		}
   15578 	    }
   15579 	  break;
   15580 
   15581 	case R_PPC64_REL16_HA:
   15582 	  /* If we are generating a non-PIC executable, edit
   15583 	     .	0:	addis 2,12,.TOC.-0b@ha
   15584 	     .		addi 2,2,.TOC.-0b@l
   15585 	     used by ELFv2 global entry points to set up r2, to
   15586 	     .		lis 2,.TOC.@ha
   15587 	     .		addi 2,2,.TOC.@l
   15588 	     if .TOC. is in range.  */
   15589 	  if (!bfd_link_pic (info)
   15590 	      && !info->traditional_format
   15591 	      && !htab->opd_abi
   15592 	      && rel->r_addend == d_offset
   15593 	      && h != NULL && &h->elf == htab->elf.hgot
   15594 	      && rel + 1 < relend
   15595 	      && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
   15596 	      && rel[1].r_offset == rel->r_offset + 4
   15597 	      && rel[1].r_addend == rel->r_addend + 4
   15598 	      && relocation + 0x80008000 <= 0xffffffff)
   15599 	    {
   15600 	      unsigned int insn1, insn2;
   15601 	      offset = rel->r_offset - d_offset;
   15602 	      insn1 = bfd_get_32 (input_bfd, contents + offset);
   15603 	      insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
   15604 	      if ((insn1 & 0xffff0000) == ADDIS_R2_R12
   15605 		  && (insn2 & 0xffff0000) == ADDI_R2_R2)
   15606 		{
   15607 		  r_type = R_PPC64_ADDR16_HA;
   15608 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   15609 		  rel->r_addend -= d_offset;
   15610 		  rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
   15611 		  rel[1].r_addend -= d_offset + 4;
   15612 		  bfd_put_32 (input_bfd, LIS_R2, contents + offset);
   15613 		}
   15614 	    }
   15615 	  break;
   15616 	}
   15617 
   15618       /* Handle other relocations that tweak non-addend part of insn.  */
   15619       insn = 0;
   15620       max_br_offset = 1 << 25;
   15621       addend = rel->r_addend;
   15622       reloc_dest = DEST_NORMAL;
   15623       switch (r_type)
   15624 	{
   15625 	default:
   15626 	  break;
   15627 
   15628 	case R_PPC64_TOCSAVE:
   15629 	  if (relocation + addend == (rel->r_offset
   15630 				      + input_section->output_offset
   15631 				      + input_section->output_section->vma)
   15632 	      && tocsave_find (htab, NO_INSERT,
   15633 			       &local_syms, rel, input_bfd))
   15634 	    {
   15635 	      insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   15636 	      if (insn == NOP
   15637 		  || insn == CROR_151515 || insn == CROR_313131)
   15638 		bfd_put_32 (input_bfd,
   15639 			    STD_R2_0R1 + STK_TOC (htab),
   15640 			    contents + rel->r_offset);
   15641 	    }
   15642 	  break;
   15643 
   15644 	  /* Branch taken prediction relocations.  */
   15645 	case R_PPC64_ADDR14_BRTAKEN:
   15646 	case R_PPC64_REL14_BRTAKEN:
   15647 	  insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
   15648 	  /* Fall through.  */
   15649 
   15650 	  /* Branch not taken prediction relocations.  */
   15651 	case R_PPC64_ADDR14_BRNTAKEN:
   15652 	case R_PPC64_REL14_BRNTAKEN:
   15653 	  insn |= bfd_get_32 (input_bfd,
   15654 			      contents + rel->r_offset) & ~(0x01 << 21);
   15655 	  /* Fall through.  */
   15656 
   15657 	case R_PPC64_REL14:
   15658 	  max_br_offset = 1 << 15;
   15659 	  /* Fall through.  */
   15660 
   15661 	case R_PPC64_REL24:
   15662 	case R_PPC64_REL24_NOTOC:
   15663 	case R_PPC64_PLTCALL:
   15664 	case R_PPC64_PLTCALL_NOTOC:
   15665 	  /* Calls to functions with a different TOC, such as calls to
   15666 	     shared objects, need to alter the TOC pointer.  This is
   15667 	     done using a linkage stub.  A REL24 branching to these
   15668 	     linkage stubs needs to be followed by a nop, as the nop
   15669 	     will be replaced with an instruction to restore the TOC
   15670 	     base pointer.  */
   15671 	  fdh = h;
   15672 	  if (h != NULL
   15673 	      && h->oh != NULL
   15674 	      && h->oh->is_func_descriptor)
   15675 	    fdh = ppc_follow_link (h->oh);
   15676 	  stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
   15677 					   htab);
   15678 	  if ((r_type == R_PPC64_PLTCALL
   15679 	       || r_type == R_PPC64_PLTCALL_NOTOC)
   15680 	      && stub_entry != NULL
   15681 	      && stub_entry->stub_type >= ppc_stub_plt_call
   15682 	      && stub_entry->stub_type <= ppc_stub_plt_call_both)
   15683 	    stub_entry = NULL;
   15684 
   15685 	  if (stub_entry != NULL
   15686 	      && ((stub_entry->stub_type >= ppc_stub_plt_call
   15687 		   && stub_entry->stub_type <= ppc_stub_plt_call_both)
   15688 		  || stub_entry->stub_type == ppc_stub_plt_branch_r2off
   15689 		  || stub_entry->stub_type == ppc_stub_plt_branch_both
   15690 		  || stub_entry->stub_type == ppc_stub_long_branch_r2off
   15691 		  || stub_entry->stub_type == ppc_stub_long_branch_both))
   15692 	    {
   15693 	      bfd_boolean can_plt_call = FALSE;
   15694 
   15695 	      if (stub_entry->stub_type == ppc_stub_plt_call
   15696 		  && !htab->opd_abi
   15697 		  && htab->params->plt_localentry0 != 0
   15698 		  && h != NULL
   15699 		  && is_elfv2_localentry0 (&h->elf))
   15700 		{
   15701 		  /* The function doesn't use or change r2.  */
   15702 		  can_plt_call = TRUE;
   15703 		}
   15704 	      else if (r_type == R_PPC64_REL24_NOTOC)
   15705 		{
   15706 		  /* NOTOC calls don't need to restore r2.  */
   15707 		  can_plt_call = TRUE;
   15708 		}
   15709 
   15710 	      /* All of these stubs may modify r2, so there must be a
   15711 		 branch and link followed by a nop.  The nop is
   15712 		 replaced by an insn to restore r2.  */
   15713 	      else if (rel->r_offset + 8 <= input_section->size)
   15714 		{
   15715 		  unsigned long br;
   15716 
   15717 		  br = bfd_get_32 (input_bfd,
   15718 				   contents + rel->r_offset);
   15719 		  if ((br & 1) != 0)
   15720 		    {
   15721 		      unsigned long nop;
   15722 
   15723 		      nop = bfd_get_32 (input_bfd,
   15724 					contents + rel->r_offset + 4);
   15725 		      if (nop == LD_R2_0R1 + STK_TOC (htab))
   15726 			can_plt_call = TRUE;
   15727 		      else if (nop == NOP
   15728 			       || nop == CROR_151515
   15729 			       || nop == CROR_313131)
   15730 			{
   15731 			  if (h != NULL
   15732 			      && is_tls_get_addr (&h->elf, htab)
   15733 			      && htab->params->tls_get_addr_opt)
   15734 			    {
   15735 			      /* Special stub used, leave nop alone.  */
   15736 			    }
   15737 			  else
   15738 			    bfd_put_32 (input_bfd,
   15739 					LD_R2_0R1 + STK_TOC (htab),
   15740 					contents + rel->r_offset + 4);
   15741 			  can_plt_call = TRUE;
   15742 			}
   15743 		    }
   15744 		}
   15745 
   15746 	      if (!can_plt_call && h != NULL)
   15747 		{
   15748 		  const char *name = h->elf.root.root.string;
   15749 
   15750 		  if (*name == '.')
   15751 		    ++name;
   15752 
   15753 		  if (strncmp (name, "__libc_start_main", 17) == 0
   15754 		      && (name[17] == 0 || name[17] == '@'))
   15755 		    {
   15756 		      /* Allow crt1 branch to go via a toc adjusting
   15757 			 stub.  Other calls that never return could do
   15758 			 the same, if we could detect such.  */
   15759 		      can_plt_call = TRUE;
   15760 		    }
   15761 		}
   15762 
   15763 	      if (!can_plt_call)
   15764 		{
   15765 		  /* g++ as of 20130507 emits self-calls without a
   15766 		     following nop.  This is arguably wrong since we
   15767 		     have conflicting information.  On the one hand a
   15768 		     global symbol and on the other a local call
   15769 		     sequence, but don't error for this special case.
   15770 		     It isn't possible to cheaply verify we have
   15771 		     exactly such a call.  Allow all calls to the same
   15772 		     section.  */
   15773 		  asection *code_sec = sec;
   15774 
   15775 		  if (get_opd_info (sec) != NULL)
   15776 		    {
   15777 		      bfd_vma off = (relocation + addend
   15778 				     - sec->output_section->vma
   15779 				     - sec->output_offset);
   15780 
   15781 		      opd_entry_value (sec, off, &code_sec, NULL, FALSE);
   15782 		    }
   15783 		  if (code_sec == input_section)
   15784 		    can_plt_call = TRUE;
   15785 		}
   15786 
   15787 	      if (!can_plt_call)
   15788 		{
   15789 		  if (stub_entry->stub_type >= ppc_stub_plt_call
   15790 		      && stub_entry->stub_type <= ppc_stub_plt_call_both)
   15791 		    info->callbacks->einfo
   15792 		      /* xgettext:c-format */
   15793 		      (_("%H: call to `%pT' lacks nop, can't restore toc; "
   15794 			 "(plt call stub)\n"),
   15795 		       input_bfd, input_section, rel->r_offset, sym_name);
   15796 		  else
   15797 		    info->callbacks->einfo
   15798 		      /* xgettext:c-format */
   15799 		      (_("%H: call to `%pT' lacks nop, can't restore toc; "
   15800 			 "(toc save/adjust stub)\n"),
   15801 		       input_bfd, input_section, rel->r_offset, sym_name);
   15802 
   15803 		  bfd_set_error (bfd_error_bad_value);
   15804 		  ret = FALSE;
   15805 		}
   15806 
   15807 	      if (can_plt_call
   15808 		  && stub_entry->stub_type >= ppc_stub_plt_call
   15809 		  && stub_entry->stub_type <= ppc_stub_plt_call_both)
   15810 		unresolved_reloc = FALSE;
   15811 	    }
   15812 
   15813 	  if ((stub_entry == NULL
   15814 	       || stub_entry->stub_type == ppc_stub_long_branch
   15815 	       || stub_entry->stub_type == ppc_stub_plt_branch)
   15816 	      && get_opd_info (sec) != NULL)
   15817 	    {
   15818 	      /* The branch destination is the value of the opd entry. */
   15819 	      bfd_vma off = (relocation + addend
   15820 			     - sec->output_section->vma
   15821 			     - sec->output_offset);
   15822 	      bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
   15823 	      if (dest != (bfd_vma) -1)
   15824 		{
   15825 		  relocation = dest;
   15826 		  addend = 0;
   15827 		  reloc_dest = DEST_OPD;
   15828 		}
   15829 	    }
   15830 
   15831 	  /* If the branch is out of reach we ought to have a long
   15832 	     branch stub.  */
   15833 	  from = (rel->r_offset
   15834 		  + input_section->output_offset
   15835 		  + input_section->output_section->vma);
   15836 
   15837 	  relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
   15838 						  ? fdh->elf.other
   15839 						  : sym->st_other);
   15840 
   15841 	  if (stub_entry != NULL
   15842 	      && (stub_entry->stub_type == ppc_stub_long_branch
   15843 		  || stub_entry->stub_type == ppc_stub_plt_branch)
   15844 	      && (r_type == R_PPC64_ADDR14_BRTAKEN
   15845 		  || r_type == R_PPC64_ADDR14_BRNTAKEN
   15846 		  || (relocation + addend - from + max_br_offset
   15847 		      < 2 * max_br_offset)))
   15848 	    /* Don't use the stub if this branch is in range.  */
   15849 	    stub_entry = NULL;
   15850 
   15851 	  if (stub_entry != NULL
   15852 	      && (stub_entry->stub_type == ppc_stub_long_branch_notoc
   15853 		  || stub_entry->stub_type == ppc_stub_long_branch_both
   15854 		  || stub_entry->stub_type == ppc_stub_plt_branch_notoc
   15855 		  || stub_entry->stub_type == ppc_stub_plt_branch_both)
   15856 	      && (r_type != R_PPC64_REL24_NOTOC
   15857 		  || ((fdh ? fdh->elf.other : sym->st_other)
   15858 		      & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
   15859 	      && (relocation + addend - from + max_br_offset
   15860 		  < 2 * max_br_offset))
   15861 	    stub_entry = NULL;
   15862 
   15863 	  if (stub_entry != NULL
   15864 	      && (stub_entry->stub_type == ppc_stub_long_branch_r2off
   15865 		  || stub_entry->stub_type == ppc_stub_long_branch_both
   15866 		  || stub_entry->stub_type == ppc_stub_plt_branch_r2off
   15867 		  || stub_entry->stub_type == ppc_stub_plt_branch_both)
   15868 	      && r_type == R_PPC64_REL24_NOTOC
   15869 	      && (relocation + addend - from + max_br_offset
   15870 		  < 2 * max_br_offset))
   15871 	    stub_entry = NULL;
   15872 
   15873 	  if (stub_entry != NULL)
   15874 	    {
   15875 	      /* Munge up the value and addend so that we call the stub
   15876 		 rather than the procedure directly.  */
   15877 	      asection *stub_sec = stub_entry->group->stub_sec;
   15878 
   15879 	      if (stub_entry->stub_type == ppc_stub_save_res)
   15880 		relocation += (stub_sec->output_offset
   15881 			       + stub_sec->output_section->vma
   15882 			       + stub_sec->size - htab->sfpr->size
   15883 			       - htab->sfpr->output_offset
   15884 			       - htab->sfpr->output_section->vma);
   15885 	      else
   15886 		relocation = (stub_entry->stub_offset
   15887 			      + stub_sec->output_offset
   15888 			      + stub_sec->output_section->vma);
   15889 	      addend = 0;
   15890 	      reloc_dest = DEST_STUB;
   15891 
   15892 	      if (((stub_entry->stub_type == ppc_stub_plt_call
   15893 		    && ALWAYS_EMIT_R2SAVE)
   15894 		   || stub_entry->stub_type == ppc_stub_plt_call_r2save
   15895 		   || stub_entry->stub_type == ppc_stub_plt_call_both)
   15896 		  && !(h != NULL
   15897 		       && is_tls_get_addr (&h->elf, htab)
   15898 		       && htab->params->tls_get_addr_opt)
   15899 		  && rel + 1 < relend
   15900 		  && rel[1].r_offset == rel->r_offset + 4
   15901 		  && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
   15902 		relocation += 4;
   15903 	      else if ((stub_entry->stub_type == ppc_stub_long_branch_both
   15904 			|| stub_entry->stub_type == ppc_stub_plt_branch_both
   15905 			|| stub_entry->stub_type == ppc_stub_plt_call_both)
   15906 		       && r_type == R_PPC64_REL24_NOTOC)
   15907 		relocation += 4;
   15908 
   15909 	      if (r_type == R_PPC64_REL24_NOTOC
   15910 		  && (stub_entry->stub_type == ppc_stub_plt_call_notoc
   15911 		      || stub_entry->stub_type == ppc_stub_plt_call_both))
   15912 		htab->notoc_plt = 1;
   15913 	    }
   15914 
   15915 	  if (insn != 0)
   15916 	    {
   15917 	      if (is_isa_v2)
   15918 		{
   15919 		  /* Set 'a' bit.  This is 0b00010 in BO field for branch
   15920 		     on CR(BI) insns (BO == 001at or 011at), and 0b01000
   15921 		     for branch on CTR insns (BO == 1a00t or 1a01t).  */
   15922 		  if ((insn & (0x14 << 21)) == (0x04 << 21))
   15923 		    insn |= 0x02 << 21;
   15924 		  else if ((insn & (0x14 << 21)) == (0x10 << 21))
   15925 		    insn |= 0x08 << 21;
   15926 		  else
   15927 		    break;
   15928 		}
   15929 	      else
   15930 		{
   15931 		  /* Invert 'y' bit if not the default.  */
   15932 		  if ((bfd_signed_vma) (relocation + addend - from) < 0)
   15933 		    insn ^= 0x01 << 21;
   15934 		}
   15935 
   15936 	      bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   15937 	    }
   15938 
   15939 	  /* NOP out calls to undefined weak functions.
   15940 	     We can thus call a weak function without first
   15941 	     checking whether the function is defined.  */
   15942 	  else if (h != NULL
   15943 		   && h->elf.root.type == bfd_link_hash_undefweak
   15944 		   && h->elf.dynindx == -1
   15945 		   && (r_type == R_PPC64_REL24
   15946 		       || r_type == R_PPC64_REL24_NOTOC)
   15947 		   && relocation == 0
   15948 		   && addend == 0)
   15949 	    {
   15950 	      bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
   15951 	      goto copy_reloc;
   15952 	    }
   15953 	  break;
   15954 
   15955 	case R_PPC64_GOT16_DS:
   15956 	  if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
   15957 	      || !htab->do_toc_opt)
   15958 	    break;
   15959 	  from = TOCstart + htab->sec_info[input_section->id].toc_off;
   15960 	  if (relocation + addend - from + 0x8000 < 0x10000
   15961 	      && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
   15962 	    {
   15963 	      insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
   15964 	      if ((insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
   15965 		{
   15966 		  insn += (14u << 26) - (58u << 26);
   15967 		  bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
   15968 		  r_type = R_PPC64_TOC16;
   15969 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   15970 		}
   15971 	    }
   15972 	  break;
   15973 
   15974 	case R_PPC64_GOT16_LO_DS:
   15975 	case R_PPC64_GOT16_HA:
   15976 	  if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
   15977 	      || !htab->do_toc_opt)
   15978 	    break;
   15979 	  from = TOCstart + htab->sec_info[input_section->id].toc_off;
   15980 	  if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
   15981 	      && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
   15982 	    {
   15983 	      insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
   15984 	      if ((insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
   15985 		{
   15986 		  insn += (14u << 26) - (58u << 26);
   15987 		  bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
   15988 		  r_type = R_PPC64_TOC16_LO;
   15989 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   15990 		}
   15991 	      else if ((insn & (0x3fu << 26)) == 15u << 26 /* addis */)
   15992 		{
   15993 		  r_type = R_PPC64_TOC16_HA;
   15994 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   15995 		}
   15996 	    }
   15997 	  break;
   15998 
   15999 	case R_PPC64_GOT_PCREL34:
   16000 	  if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
   16001 	      || !htab->do_toc_opt)
   16002 	    break;
   16003 	  from = (rel->r_offset
   16004 		  + input_section->output_section->vma
   16005 		  + input_section->output_offset);
   16006 	  if (!(relocation - from + (1ULL << 33) < 1ULL << 34
   16007 		&& (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
   16008 	    break;
   16009 
   16010 	  offset = rel->r_offset;
   16011 	  pinsn = bfd_get_32 (input_bfd, contents + offset);
   16012 	  pinsn <<= 32;
   16013 	  pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
   16014 	  if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
   16015 	      != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
   16016 	    break;
   16017 
   16018 	  /* Replace with paddi.  */
   16019 	  pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
   16020 	  r_type = R_PPC64_PCREL34;
   16021 	  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   16022 	  bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
   16023 	  bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
   16024 	  /* Fall through.  */
   16025 
   16026 	case R_PPC64_PCREL34:
   16027 	  if (!htab->params->no_pcrel_opt
   16028 	      && rel + 1 < relend
   16029 	      && rel[1].r_offset == rel->r_offset
   16030 	      && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT)
   16031 	      && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
   16032 	    {
   16033 	      offset = rel->r_offset;
   16034 	      pinsn = bfd_get_32 (input_bfd, contents + offset);
   16035 	      pinsn <<= 32;
   16036 	      pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
   16037 	      if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
   16038 		   == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
   16039 		       | (14ULL << 26) /* paddi */))
   16040 		{
   16041 		  bfd_vma off2 = rel[1].r_addend;
   16042 		  if (off2 == 0)
   16043 		    /* zero means next insn.  */
   16044 		    off2 = 8;
   16045 		  off2 += offset;
   16046 		  if (off2 + 4 <= input_section->size)
   16047 		    {
   16048 		      uint64_t pinsn2;
   16049 		      bfd_signed_vma addend_off;
   16050 		      pinsn2 = bfd_get_32 (input_bfd, contents + off2);
   16051 		      pinsn2 <<= 32;
   16052 		      if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
   16053 			{
   16054 			  if (off2 + 8 > input_section->size)
   16055 			    break;
   16056 			  pinsn2 |= bfd_get_32 (input_bfd,
   16057 						contents + off2 + 4);
   16058 			}
   16059 		      if (xlate_pcrel_opt (&pinsn, &pinsn2, &addend_off))
   16060 			{
   16061 			  addend += addend_off;
   16062 			  rel->r_addend = addend;
   16063 			  bfd_put_32 (input_bfd, pinsn >> 32,
   16064 				      contents + offset);
   16065 			  bfd_put_32 (input_bfd, pinsn,
   16066 				      contents + offset + 4);
   16067 			  bfd_put_32 (input_bfd, pinsn2 >> 32,
   16068 				      contents + off2);
   16069 			  if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
   16070 			    bfd_put_32 (input_bfd, pinsn2,
   16071 					contents + off2 + 4);
   16072 			}
   16073 		    }
   16074 		}
   16075 	    }
   16076 	  break;
   16077 	}
   16078 
   16079       tls_type = 0;
   16080       save_unresolved_reloc = unresolved_reloc;
   16081       switch (r_type)
   16082 	{
   16083 	default:
   16084 	  /* xgettext:c-format */
   16085 	  _bfd_error_handler (_("%pB: %s unsupported"),
   16086 			      input_bfd, ppc64_elf_howto_table[r_type]->name);
   16087 
   16088 	  bfd_set_error (bfd_error_bad_value);
   16089 	  ret = FALSE;
   16090 	  goto copy_reloc;
   16091 
   16092 	case R_PPC64_NONE:
   16093 	case R_PPC64_TLS:
   16094 	case R_PPC64_TLSGD:
   16095 	case R_PPC64_TLSLD:
   16096 	case R_PPC64_TOCSAVE:
   16097 	case R_PPC64_GNU_VTINHERIT:
   16098 	case R_PPC64_GNU_VTENTRY:
   16099 	case R_PPC64_ENTRY:
   16100 	case R_PPC64_PCREL_OPT:
   16101 	  goto copy_reloc;
   16102 
   16103 	  /* GOT16 relocations.  Like an ADDR16 using the symbol's
   16104 	     address in the GOT as relocation value instead of the
   16105 	     symbol's value itself.  Also, create a GOT entry for the
   16106 	     symbol and put the symbol value there.  */
   16107 	case R_PPC64_GOT_TLSGD16:
   16108 	case R_PPC64_GOT_TLSGD16_LO:
   16109 	case R_PPC64_GOT_TLSGD16_HI:
   16110 	case R_PPC64_GOT_TLSGD16_HA:
   16111 	case R_PPC64_GOT_TLSGD_PCREL34:
   16112 	  tls_type = TLS_TLS | TLS_GD;
   16113 	  goto dogot;
   16114 
   16115 	case R_PPC64_GOT_TLSLD16:
   16116 	case R_PPC64_GOT_TLSLD16_LO:
   16117 	case R_PPC64_GOT_TLSLD16_HI:
   16118 	case R_PPC64_GOT_TLSLD16_HA:
   16119 	case R_PPC64_GOT_TLSLD_PCREL34:
   16120 	  tls_type = TLS_TLS | TLS_LD;
   16121 	  goto dogot;
   16122 
   16123 	case R_PPC64_GOT_TPREL16_DS:
   16124 	case R_PPC64_GOT_TPREL16_LO_DS:
   16125 	case R_PPC64_GOT_TPREL16_HI:
   16126 	case R_PPC64_GOT_TPREL16_HA:
   16127 	case R_PPC64_GOT_TPREL_PCREL34:
   16128 	  tls_type = TLS_TLS | TLS_TPREL;
   16129 	  goto dogot;
   16130 
   16131 	case R_PPC64_GOT_DTPREL16_DS:
   16132 	case R_PPC64_GOT_DTPREL16_LO_DS:
   16133 	case R_PPC64_GOT_DTPREL16_HI:
   16134 	case R_PPC64_GOT_DTPREL16_HA:
   16135 	case R_PPC64_GOT_DTPREL_PCREL34:
   16136 	  tls_type = TLS_TLS | TLS_DTPREL;
   16137 	  goto dogot;
   16138 
   16139 	case R_PPC64_GOT16:
   16140 	case R_PPC64_GOT16_LO:
   16141 	case R_PPC64_GOT16_HI:
   16142 	case R_PPC64_GOT16_HA:
   16143 	case R_PPC64_GOT16_DS:
   16144 	case R_PPC64_GOT16_LO_DS:
   16145 	case R_PPC64_GOT_PCREL34:
   16146 	dogot:
   16147 	  {
   16148 	    /* Relocation is to the entry for this symbol in the global
   16149 	       offset table.  */
   16150 	    asection *got;
   16151 	    bfd_vma *offp;
   16152 	    bfd_vma off;
   16153 	    unsigned long indx = 0;
   16154 	    struct got_entry *ent;
   16155 
   16156 	    if (tls_type == (TLS_TLS | TLS_LD)
   16157 		&& (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
   16158 	      ent = ppc64_tlsld_got (input_bfd);
   16159 	    else
   16160 	      {
   16161 		if (h != NULL)
   16162 		  {
   16163 		    if (!htab->elf.dynamic_sections_created
   16164 			|| h->elf.dynindx == -1
   16165 			|| SYMBOL_REFERENCES_LOCAL (info, &h->elf)
   16166 			|| UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
   16167 		      /* This is actually a static link, or it is a
   16168 			 -Bsymbolic link and the symbol is defined
   16169 			 locally, or the symbol was forced to be local
   16170 			 because of a version file.  */
   16171 		      ;
   16172 		    else
   16173 		      {
   16174 			indx = h->elf.dynindx;
   16175 			unresolved_reloc = FALSE;
   16176 		      }
   16177 		    ent = h->elf.got.glist;
   16178 		  }
   16179 		else
   16180 		  {
   16181 		    if (local_got_ents == NULL)
   16182 		      abort ();
   16183 		    ent = local_got_ents[r_symndx];
   16184 		  }
   16185 
   16186 		for (; ent != NULL; ent = ent->next)
   16187 		  if (ent->addend == orig_rel.r_addend
   16188 		      && ent->owner == input_bfd
   16189 		      && ent->tls_type == tls_type)
   16190 		    break;
   16191 	      }
   16192 
   16193 	    if (ent == NULL)
   16194 	      abort ();
   16195 	    if (ent->is_indirect)
   16196 	      ent = ent->got.ent;
   16197 	    offp = &ent->got.offset;
   16198 	    got = ppc64_elf_tdata (ent->owner)->got;
   16199 	    if (got == NULL)
   16200 	      abort ();
   16201 
   16202 	    /* The offset must always be a multiple of 8.  We use the
   16203 	       least significant bit to record whether we have already
   16204 	       processed this entry.  */
   16205 	    off = *offp;
   16206 	    if ((off & 1) != 0)
   16207 	      off &= ~1;
   16208 	    else
   16209 	      {
   16210 		/* Generate relocs for the dynamic linker, except in
   16211 		   the case of TLSLD where we'll use one entry per
   16212 		   module.  */
   16213 		asection *relgot;
   16214 		bfd_boolean ifunc;
   16215 
   16216 		*offp = off | 1;
   16217 		relgot = NULL;
   16218 		ifunc = (h != NULL
   16219 			 ? h->elf.type == STT_GNU_IFUNC
   16220 			 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
   16221 		if (ifunc)
   16222 		  {
   16223 		    relgot = htab->elf.irelplt;
   16224 		    if (indx == 0 || is_static_defined (&h->elf))
   16225 		      htab->elf.ifunc_resolvers = TRUE;
   16226 		  }
   16227 		else if (indx != 0
   16228 			 || (bfd_link_pic (info)
   16229 			     && (h == NULL
   16230 				 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
   16231 			     && !(tls_type != 0
   16232 				  && bfd_link_executable (info)
   16233 				  && (h == NULL
   16234 				      || SYMBOL_REFERENCES_LOCAL (info,
   16235 								  &h->elf)))))
   16236 		  relgot = ppc64_elf_tdata (ent->owner)->relgot;
   16237 		if (relgot != NULL)
   16238 		  {
   16239 		    outrel.r_offset = (got->output_section->vma
   16240 				       + got->output_offset
   16241 				       + off);
   16242 		    outrel.r_addend = orig_rel.r_addend;
   16243 		    if (tls_type & (TLS_LD | TLS_GD))
   16244 		      {
   16245 			outrel.r_addend = 0;
   16246 			outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
   16247 			if (tls_type == (TLS_TLS | TLS_GD))
   16248 			  {
   16249 			    loc = relgot->contents;
   16250 			    loc += (relgot->reloc_count++
   16251 				    * sizeof (Elf64_External_Rela));
   16252 			    bfd_elf64_swap_reloca_out (output_bfd,
   16253 						       &outrel, loc);
   16254 			    outrel.r_offset += 8;
   16255 			    outrel.r_addend = orig_rel.r_addend;
   16256 			    outrel.r_info
   16257 			      = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
   16258 			  }
   16259 		      }
   16260 		    else if (tls_type == (TLS_TLS | TLS_DTPREL))
   16261 		      outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
   16262 		    else if (tls_type == (TLS_TLS | TLS_TPREL))
   16263 		      outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
   16264 		    else if (indx != 0)
   16265 		      outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
   16266 		    else
   16267 		      {
   16268 			if (ifunc)
   16269 			  outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
   16270 			else
   16271 			  outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   16272 
   16273 			/* Write the .got section contents for the sake
   16274 			   of prelink.  */
   16275 			loc = got->contents + off;
   16276 			bfd_put_64 (output_bfd, outrel.r_addend + relocation,
   16277 				    loc);
   16278 		      }
   16279 
   16280 		    if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
   16281 		      {
   16282 			outrel.r_addend += relocation;
   16283 			if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
   16284 			  {
   16285 			    if (htab->elf.tls_sec == NULL)
   16286 			      outrel.r_addend = 0;
   16287 			    else
   16288 			      outrel.r_addend -= htab->elf.tls_sec->vma;
   16289 			  }
   16290 		      }
   16291 		    loc = relgot->contents;
   16292 		    loc += (relgot->reloc_count++
   16293 			    * sizeof (Elf64_External_Rela));
   16294 		    bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
   16295 		  }
   16296 
   16297 		/* Init the .got section contents here if we're not
   16298 		   emitting a reloc.  */
   16299 		else
   16300 		  {
   16301 		    relocation += orig_rel.r_addend;
   16302 		    if (tls_type != 0)
   16303 		      {
   16304 			if (htab->elf.tls_sec == NULL)
   16305 			  relocation = 0;
   16306 			else
   16307 			  {
   16308 			    if (tls_type & TLS_LD)
   16309 			      relocation = 0;
   16310 			    else
   16311 			      relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
   16312 			    if (tls_type & TLS_TPREL)
   16313 			      relocation += DTP_OFFSET - TP_OFFSET;
   16314 			  }
   16315 
   16316 			if (tls_type & (TLS_GD | TLS_LD))
   16317 			  {
   16318 			    bfd_put_64 (output_bfd, relocation,
   16319 					got->contents + off + 8);
   16320 			    relocation = 1;
   16321 			  }
   16322 		      }
   16323 		    bfd_put_64 (output_bfd, relocation,
   16324 				got->contents + off);
   16325 		  }
   16326 	      }
   16327 
   16328 	    if (off >= (bfd_vma) -2)
   16329 	      abort ();
   16330 
   16331 	    relocation = got->output_section->vma + got->output_offset + off;
   16332 	    addend = 0;
   16333 	    if (!(r_type == R_PPC64_GOT_PCREL34
   16334 		  || r_type == R_PPC64_GOT_TLSGD_PCREL34
   16335 		  || r_type == R_PPC64_GOT_TLSLD_PCREL34
   16336 		  || r_type == R_PPC64_GOT_TPREL_PCREL34
   16337 		  || r_type == R_PPC64_GOT_DTPREL_PCREL34))
   16338 	      addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
   16339 	  }
   16340 	  break;
   16341 
   16342 	case R_PPC64_PLT16_HA:
   16343 	case R_PPC64_PLT16_HI:
   16344 	case R_PPC64_PLT16_LO:
   16345 	case R_PPC64_PLT16_LO_DS:
   16346 	case R_PPC64_PLT_PCREL34:
   16347 	case R_PPC64_PLT_PCREL34_NOTOC:
   16348 	case R_PPC64_PLT32:
   16349 	case R_PPC64_PLT64:
   16350 	case R_PPC64_PLTSEQ:
   16351 	case R_PPC64_PLTSEQ_NOTOC:
   16352 	case R_PPC64_PLTCALL:
   16353 	case R_PPC64_PLTCALL_NOTOC:
   16354 	  /* Relocation is to the entry for this symbol in the
   16355 	     procedure linkage table.  */
   16356 	  unresolved_reloc = TRUE;
   16357 	  {
   16358 	    struct plt_entry **plt_list = NULL;
   16359 	    if (h != NULL)
   16360 	      plt_list = &h->elf.plt.plist;
   16361 	    else if (local_got_ents != NULL)
   16362 	      {
   16363 		struct plt_entry **local_plt = (struct plt_entry **)
   16364 		  (local_got_ents + symtab_hdr->sh_info);
   16365 		plt_list = local_plt + r_symndx;
   16366 	      }
   16367 	    if (plt_list)
   16368 	      {
   16369 		struct plt_entry *ent;
   16370 
   16371 		for (ent = *plt_list; ent != NULL; ent = ent->next)
   16372 		  if (ent->plt.offset != (bfd_vma) -1
   16373 		      && ent->addend == orig_rel.r_addend)
   16374 		    {
   16375 		      asection *plt;
   16376 		      bfd_vma got;
   16377 
   16378 		      plt = htab->elf.splt;
   16379 		      if (!htab->elf.dynamic_sections_created
   16380 			  || h == NULL
   16381 			  || h->elf.dynindx == -1)
   16382 			{
   16383 			  if (h != NULL
   16384 			      ? h->elf.type == STT_GNU_IFUNC
   16385 			      : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
   16386 			    plt = htab->elf.iplt;
   16387 			  else
   16388 			    plt = htab->pltlocal;
   16389 			}
   16390 		      relocation = (plt->output_section->vma
   16391 				    + plt->output_offset
   16392 				    + ent->plt.offset);
   16393 		      if (r_type == R_PPC64_PLT16_HA
   16394 			  || r_type == R_PPC64_PLT16_HI
   16395 			  || r_type == R_PPC64_PLT16_LO
   16396 			  || r_type == R_PPC64_PLT16_LO_DS)
   16397 			{
   16398 			  got = (elf_gp (output_bfd)
   16399 				 + htab->sec_info[input_section->id].toc_off);
   16400 			  relocation -= got;
   16401 			}
   16402 		      addend = 0;
   16403 		      unresolved_reloc = FALSE;
   16404 		      break;
   16405 		    }
   16406 	      }
   16407 	  }
   16408 	  break;
   16409 
   16410 	case R_PPC64_TOC:
   16411 	  /* Relocation value is TOC base.  */
   16412 	  relocation = TOCstart;
   16413 	  if (r_symndx == STN_UNDEF)
   16414 	    relocation += htab->sec_info[input_section->id].toc_off;
   16415 	  else if (unresolved_reloc)
   16416 	    ;
   16417 	  else if (sec != NULL && sec->id < htab->sec_info_arr_size)
   16418 	    relocation += htab->sec_info[sec->id].toc_off;
   16419 	  else
   16420 	    unresolved_reloc = TRUE;
   16421 	  goto dodyn;
   16422 
   16423 	  /* TOC16 relocs.  We want the offset relative to the TOC base,
   16424 	     which is the address of the start of the TOC plus 0x8000.
   16425 	     The TOC consists of sections .got, .toc, .tocbss, and .plt,
   16426 	     in this order.  */
   16427 	case R_PPC64_TOC16:
   16428 	case R_PPC64_TOC16_LO:
   16429 	case R_PPC64_TOC16_HI:
   16430 	case R_PPC64_TOC16_DS:
   16431 	case R_PPC64_TOC16_LO_DS:
   16432 	case R_PPC64_TOC16_HA:
   16433 	  addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
   16434 	  if (h != NULL)
   16435 	    goto dodyn;
   16436 	  break;
   16437 
   16438 	  /* Relocate against the beginning of the section.  */
   16439 	case R_PPC64_SECTOFF:
   16440 	case R_PPC64_SECTOFF_LO:
   16441 	case R_PPC64_SECTOFF_HI:
   16442 	case R_PPC64_SECTOFF_DS:
   16443 	case R_PPC64_SECTOFF_LO_DS:
   16444 	case R_PPC64_SECTOFF_HA:
   16445 	  if (sec != NULL)
   16446 	    addend -= sec->output_section->vma;
   16447 	  break;
   16448 
   16449 	case R_PPC64_REL16:
   16450 	case R_PPC64_REL16_LO:
   16451 	case R_PPC64_REL16_HI:
   16452 	case R_PPC64_REL16_HA:
   16453 	case R_PPC64_REL16_HIGH:
   16454 	case R_PPC64_REL16_HIGHA:
   16455 	case R_PPC64_REL16_HIGHER:
   16456 	case R_PPC64_REL16_HIGHERA:
   16457 	case R_PPC64_REL16_HIGHEST:
   16458 	case R_PPC64_REL16_HIGHESTA:
   16459 	case R_PPC64_REL16_HIGHER34:
   16460 	case R_PPC64_REL16_HIGHERA34:
   16461 	case R_PPC64_REL16_HIGHEST34:
   16462 	case R_PPC64_REL16_HIGHESTA34:
   16463 	case R_PPC64_REL16DX_HA:
   16464 	case R_PPC64_REL14:
   16465 	case R_PPC64_REL14_BRNTAKEN:
   16466 	case R_PPC64_REL14_BRTAKEN:
   16467 	case R_PPC64_REL24:
   16468 	case R_PPC64_REL24_NOTOC:
   16469 	case R_PPC64_PCREL34:
   16470 	case R_PPC64_PCREL28:
   16471 	  break;
   16472 
   16473 	case R_PPC64_TPREL16:
   16474 	case R_PPC64_TPREL16_LO:
   16475 	case R_PPC64_TPREL16_HI:
   16476 	case R_PPC64_TPREL16_HA:
   16477 	case R_PPC64_TPREL16_DS:
   16478 	case R_PPC64_TPREL16_LO_DS:
   16479 	case R_PPC64_TPREL16_HIGH:
   16480 	case R_PPC64_TPREL16_HIGHA:
   16481 	case R_PPC64_TPREL16_HIGHER:
   16482 	case R_PPC64_TPREL16_HIGHERA:
   16483 	case R_PPC64_TPREL16_HIGHEST:
   16484 	case R_PPC64_TPREL16_HIGHESTA:
   16485 	case R_PPC64_TPREL34:
   16486 	  if (h != NULL
   16487 	      && h->elf.root.type == bfd_link_hash_undefweak
   16488 	      && h->elf.dynindx == -1)
   16489 	    {
   16490 	      /* Make this relocation against an undefined weak symbol
   16491 		 resolve to zero.  This is really just a tweak, since
   16492 		 code using weak externs ought to check that they are
   16493 		 defined before using them.  */
   16494 	      bfd_byte *p = contents + rel->r_offset - d_offset;
   16495 
   16496 	      insn = bfd_get_32 (input_bfd, p);
   16497 	      insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
   16498 	      if (insn != 0)
   16499 		bfd_put_32 (input_bfd, insn, p);
   16500 	      break;
   16501 	    }
   16502 	  if (htab->elf.tls_sec != NULL)
   16503 	    addend -= htab->elf.tls_sec->vma + TP_OFFSET;
   16504 	  /* The TPREL16 relocs shouldn't really be used in shared
   16505 	     libs or with non-local symbols as that will result in
   16506 	     DT_TEXTREL being set, but support them anyway.  */
   16507 	  goto dodyn;
   16508 
   16509 	case R_PPC64_DTPREL16:
   16510 	case R_PPC64_DTPREL16_LO:
   16511 	case R_PPC64_DTPREL16_HI:
   16512 	case R_PPC64_DTPREL16_HA:
   16513 	case R_PPC64_DTPREL16_DS:
   16514 	case R_PPC64_DTPREL16_LO_DS:
   16515 	case R_PPC64_DTPREL16_HIGH:
   16516 	case R_PPC64_DTPREL16_HIGHA:
   16517 	case R_PPC64_DTPREL16_HIGHER:
   16518 	case R_PPC64_DTPREL16_HIGHERA:
   16519 	case R_PPC64_DTPREL16_HIGHEST:
   16520 	case R_PPC64_DTPREL16_HIGHESTA:
   16521 	case R_PPC64_DTPREL34:
   16522 	  if (htab->elf.tls_sec != NULL)
   16523 	    addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
   16524 	  break;
   16525 
   16526 	case R_PPC64_ADDR64_LOCAL:
   16527 	  addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
   16528 					      ? h->elf.other
   16529 					      : sym->st_other);
   16530 	  break;
   16531 
   16532 	case R_PPC64_DTPMOD64:
   16533 	  relocation = 1;
   16534 	  addend = 0;
   16535 	  goto dodyn;
   16536 
   16537 	case R_PPC64_TPREL64:
   16538 	  if (htab->elf.tls_sec != NULL)
   16539 	    addend -= htab->elf.tls_sec->vma + TP_OFFSET;
   16540 	  goto dodyn;
   16541 
   16542 	case R_PPC64_DTPREL64:
   16543 	  if (htab->elf.tls_sec != NULL)
   16544 	    addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
   16545 	  /* Fall through.  */
   16546 
   16547 	  /* Relocations that may need to be propagated if this is a
   16548 	     dynamic object.  */
   16549 	case R_PPC64_REL30:
   16550 	case R_PPC64_REL32:
   16551 	case R_PPC64_REL64:
   16552 	case R_PPC64_ADDR14:
   16553 	case R_PPC64_ADDR14_BRNTAKEN:
   16554 	case R_PPC64_ADDR14_BRTAKEN:
   16555 	case R_PPC64_ADDR16:
   16556 	case R_PPC64_ADDR16_DS:
   16557 	case R_PPC64_ADDR16_HA:
   16558 	case R_PPC64_ADDR16_HI:
   16559 	case R_PPC64_ADDR16_HIGH:
   16560 	case R_PPC64_ADDR16_HIGHA:
   16561 	case R_PPC64_ADDR16_HIGHER:
   16562 	case R_PPC64_ADDR16_HIGHERA:
   16563 	case R_PPC64_ADDR16_HIGHEST:
   16564 	case R_PPC64_ADDR16_HIGHESTA:
   16565 	case R_PPC64_ADDR16_LO:
   16566 	case R_PPC64_ADDR16_LO_DS:
   16567 	case R_PPC64_ADDR16_HIGHER34:
   16568 	case R_PPC64_ADDR16_HIGHERA34:
   16569 	case R_PPC64_ADDR16_HIGHEST34:
   16570 	case R_PPC64_ADDR16_HIGHESTA34:
   16571 	case R_PPC64_ADDR24:
   16572 	case R_PPC64_ADDR32:
   16573 	case R_PPC64_ADDR64:
   16574 	case R_PPC64_UADDR16:
   16575 	case R_PPC64_UADDR32:
   16576 	case R_PPC64_UADDR64:
   16577 	case R_PPC64_D34:
   16578 	case R_PPC64_D34_LO:
   16579 	case R_PPC64_D34_HI30:
   16580 	case R_PPC64_D34_HA30:
   16581 	case R_PPC64_D28:
   16582 	dodyn:
   16583 	  if ((input_section->flags & SEC_ALLOC) == 0)
   16584 	    break;
   16585 
   16586 	  if (NO_OPD_RELOCS && is_opd)
   16587 	    break;
   16588 
   16589 	  if (bfd_link_pic (info)
   16590 	      ? ((h == NULL
   16591 		  || h->elf.dyn_relocs != NULL)
   16592 		 && ((h != NULL && pc_dynrelocs (h))
   16593 		     || must_be_dyn_reloc (info, r_type)))
   16594 	      : (h != NULL
   16595 		 ? h->elf.dyn_relocs != NULL
   16596 		 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
   16597 	    {
   16598 	      bfd_boolean skip, relocate;
   16599 	      asection *sreloc;
   16600 	      bfd_vma out_off;
   16601 	      long indx = 0;
   16602 
   16603 	      /* When generating a dynamic object, these relocations
   16604 		 are copied into the output file to be resolved at run
   16605 		 time.  */
   16606 
   16607 	      skip = FALSE;
   16608 	      relocate = FALSE;
   16609 
   16610 	      out_off = _bfd_elf_section_offset (output_bfd, info,
   16611 						 input_section, rel->r_offset);
   16612 	      if (out_off == (bfd_vma) -1)
   16613 		skip = TRUE;
   16614 	      else if (out_off == (bfd_vma) -2)
   16615 		skip = TRUE, relocate = TRUE;
   16616 	      out_off += (input_section->output_section->vma
   16617 			  + input_section->output_offset);
   16618 	      outrel.r_offset = out_off;
   16619 	      outrel.r_addend = rel->r_addend;
   16620 
   16621 	      /* Optimize unaligned reloc use.  */
   16622 	      if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
   16623 		  || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
   16624 		r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
   16625 	      else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
   16626 		       || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
   16627 		r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
   16628 	      else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
   16629 		       || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
   16630 		r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
   16631 
   16632 	      if (skip)
   16633 		memset (&outrel, 0, sizeof outrel);
   16634 	      else if (h != NULL
   16635 		       && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)
   16636 		       && !is_opd
   16637 		       && r_type != R_PPC64_TOC)
   16638 		{
   16639 		  indx = h->elf.dynindx;
   16640 		  BFD_ASSERT (indx != -1);
   16641 		  outrel.r_info = ELF64_R_INFO (indx, r_type);
   16642 		}
   16643 	      else
   16644 		{
   16645 		  /* This symbol is local, or marked to become local,
   16646 		     or this is an opd section reloc which must point
   16647 		     at a local function.  */
   16648 		  outrel.r_addend += relocation;
   16649 		  if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
   16650 		    {
   16651 		      if (is_opd && h != NULL)
   16652 			{
   16653 			  /* Lie about opd entries.  This case occurs
   16654 			     when building shared libraries and we
   16655 			     reference a function in another shared
   16656 			     lib.  The same thing happens for a weak
   16657 			     definition in an application that's
   16658 			     overridden by a strong definition in a
   16659 			     shared lib.  (I believe this is a generic
   16660 			     bug in binutils handling of weak syms.)
   16661 			     In these cases we won't use the opd
   16662 			     entry in this lib.  */
   16663 			  unresolved_reloc = FALSE;
   16664 			}
   16665 		      if (!is_opd
   16666 			  && r_type == R_PPC64_ADDR64
   16667 			  && (h != NULL
   16668 			      ? h->elf.type == STT_GNU_IFUNC
   16669 			      : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
   16670 			outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
   16671 		      else
   16672 			{
   16673 			  outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   16674 
   16675 			  /* We need to relocate .opd contents for ld.so.
   16676 			     Prelink also wants simple and consistent rules
   16677 			     for relocs.  This make all RELATIVE relocs have
   16678 			     *r_offset equal to r_addend.  */
   16679 			  relocate = TRUE;
   16680 			}
   16681 		    }
   16682 		  else
   16683 		    {
   16684 		      if (h != NULL
   16685 			  ? h->elf.type == STT_GNU_IFUNC
   16686 			  : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
   16687 			{
   16688 			  info->callbacks->einfo
   16689 			    /* xgettext:c-format */
   16690 			    (_("%H: %s for indirect "
   16691 			       "function `%pT' unsupported\n"),
   16692 			     input_bfd, input_section, rel->r_offset,
   16693 			     ppc64_elf_howto_table[r_type]->name,
   16694 			     sym_name);
   16695 			  ret = FALSE;
   16696 			}
   16697 		      else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
   16698 			;
   16699 		      else if (sec == NULL || sec->owner == NULL)
   16700 			{
   16701 			  bfd_set_error (bfd_error_bad_value);
   16702 			  return FALSE;
   16703 			}
   16704 		      else
   16705 			{
   16706 			  asection *osec = sec->output_section;
   16707 
   16708 			  if ((osec->flags & SEC_THREAD_LOCAL) != 0)
   16709 			    {
   16710 			      /* TLS symbol values are relative to the
   16711 				 TLS segment.  Dynamic relocations for
   16712 				 local TLS symbols therefore can't be
   16713 				 reduced to a relocation against their
   16714 				 section symbol because it holds the
   16715 				 address of the section, not a value
   16716 				 relative to the TLS segment.  We could
   16717 				 change the .tdata dynamic section symbol
   16718 				 to be zero value but STN_UNDEF works
   16719 				 and is used elsewhere, eg. for TPREL64
   16720 				 GOT relocs against local TLS symbols.  */
   16721 			      osec = htab->elf.tls_sec;
   16722 			      indx = 0;
   16723 			    }
   16724 			  else
   16725 			    {
   16726 			      indx = elf_section_data (osec)->dynindx;
   16727 			      if (indx == 0)
   16728 				{
   16729 				  if ((osec->flags & SEC_READONLY) == 0
   16730 				      && htab->elf.data_index_section != NULL)
   16731 				    osec = htab->elf.data_index_section;
   16732 				  else
   16733 				    osec = htab->elf.text_index_section;
   16734 				  indx = elf_section_data (osec)->dynindx;
   16735 				}
   16736 			      BFD_ASSERT (indx != 0);
   16737 			    }
   16738 
   16739 			  /* We are turning this relocation into one
   16740 			     against a section symbol, so subtract out
   16741 			     the output section's address but not the
   16742 			     offset of the input section in the output
   16743 			     section.  */
   16744 			  outrel.r_addend -= osec->vma;
   16745 			}
   16746 
   16747 		      outrel.r_info = ELF64_R_INFO (indx, r_type);
   16748 		    }
   16749 		}
   16750 
   16751 	      sreloc = elf_section_data (input_section)->sreloc;
   16752 	      if (h != NULL
   16753 		  ? h->elf.type == STT_GNU_IFUNC
   16754 		  : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
   16755 		{
   16756 		  sreloc = htab->elf.irelplt;
   16757 		  if (indx == 0 || is_static_defined (&h->elf))
   16758 		    htab->elf.ifunc_resolvers = TRUE;
   16759 		}
   16760 	      if (sreloc == NULL)
   16761 		abort ();
   16762 
   16763 	      if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
   16764 		  >= sreloc->size)
   16765 		abort ();
   16766 	      loc = sreloc->contents;
   16767 	      loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
   16768 	      bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
   16769 
   16770 	      if (!warned_dynamic
   16771 		  && !ppc64_glibc_dynamic_reloc (ELF64_R_TYPE (outrel.r_info)))
   16772 		{
   16773 		  info->callbacks->einfo
   16774 		    /* xgettext:c-format */
   16775 		    (_("%X%P: %pB: %s against %pT "
   16776 		       "is not supported by glibc as a dynamic relocation\n"),
   16777 		     input_bfd,
   16778 		     ppc64_elf_howto_table[ELF64_R_TYPE (outrel.r_info)]->name,
   16779 		     sym_name);
   16780 		  warned_dynamic = TRUE;
   16781 		}
   16782 
   16783 	      /* If this reloc is against an external symbol, it will
   16784 		 be computed at runtime, so there's no need to do
   16785 		 anything now.  However, for the sake of prelink ensure
   16786 		 that the section contents are a known value.  */
   16787 	      if (!relocate)
   16788 		{
   16789 		  unresolved_reloc = FALSE;
   16790 		  /* The value chosen here is quite arbitrary as ld.so
   16791 		     ignores section contents except for the special
   16792 		     case of .opd where the contents might be accessed
   16793 		     before relocation.  Choose zero, as that won't
   16794 		     cause reloc overflow.  */
   16795 		  relocation = 0;
   16796 		  addend = 0;
   16797 		  /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
   16798 		     to improve backward compatibility with older
   16799 		     versions of ld.  */
   16800 		  if (r_type == R_PPC64_ADDR64)
   16801 		    addend = outrel.r_addend;
   16802 		  /* Adjust pc_relative relocs to have zero in *r_offset.  */
   16803 		  else if (ppc64_elf_howto_table[r_type]->pc_relative)
   16804 		    addend = outrel.r_offset;
   16805 		}
   16806 	    }
   16807 	  break;
   16808 
   16809 	case R_PPC64_COPY:
   16810 	case R_PPC64_GLOB_DAT:
   16811 	case R_PPC64_JMP_SLOT:
   16812 	case R_PPC64_JMP_IREL:
   16813 	case R_PPC64_RELATIVE:
   16814 	  /* We shouldn't ever see these dynamic relocs in relocatable
   16815 	     files.  */
   16816 	  /* Fall through.  */
   16817 
   16818 	case R_PPC64_PLTGOT16:
   16819 	case R_PPC64_PLTGOT16_DS:
   16820 	case R_PPC64_PLTGOT16_HA:
   16821 	case R_PPC64_PLTGOT16_HI:
   16822 	case R_PPC64_PLTGOT16_LO:
   16823 	case R_PPC64_PLTGOT16_LO_DS:
   16824 	case R_PPC64_PLTREL32:
   16825 	case R_PPC64_PLTREL64:
   16826 	  /* These ones haven't been implemented yet.  */
   16827 
   16828 	  info->callbacks->einfo
   16829 	    /* xgettext:c-format */
   16830 	    (_("%P: %pB: %s is not supported for `%pT'\n"),
   16831 	     input_bfd,
   16832 	     ppc64_elf_howto_table[r_type]->name, sym_name);
   16833 
   16834 	  bfd_set_error (bfd_error_invalid_operation);
   16835 	  ret = FALSE;
   16836 	  goto copy_reloc;
   16837 	}
   16838 
   16839       /* Multi-instruction sequences that access the TOC can be
   16840 	 optimized, eg. addis ra,r2,0; addi rb,ra,x;
   16841 	 to		nop;	       addi rb,r2,x;  */
   16842       switch (r_type)
   16843 	{
   16844 	default:
   16845 	  break;
   16846 
   16847 	case R_PPC64_GOT_TLSLD16_HI:
   16848 	case R_PPC64_GOT_TLSGD16_HI:
   16849 	case R_PPC64_GOT_TPREL16_HI:
   16850 	case R_PPC64_GOT_DTPREL16_HI:
   16851 	case R_PPC64_GOT16_HI:
   16852 	case R_PPC64_TOC16_HI:
   16853 	  /* These relocs would only be useful if building up an
   16854 	     offset to later add to r2, perhaps in an indexed
   16855 	     addressing mode instruction.  Don't try to optimize.
   16856 	     Unfortunately, the possibility of someone building up an
   16857 	     offset like this or even with the HA relocs, means that
   16858 	     we need to check the high insn when optimizing the low
   16859 	     insn.  */
   16860 	  break;
   16861 
   16862 	case R_PPC64_PLTCALL_NOTOC:
   16863 	  if (!unresolved_reloc)
   16864 	    htab->notoc_plt = 1;
   16865 	  /* Fall through.  */
   16866 	case R_PPC64_PLTCALL:
   16867 	  if (unresolved_reloc)
   16868 	    {
   16869 	      /* No plt entry.  Make this into a direct call.  */
   16870 	      bfd_byte *p = contents + rel->r_offset;
   16871 	      insn = bfd_get_32 (input_bfd, p);
   16872 	      insn &= 1;
   16873 	      bfd_put_32 (input_bfd, B_DOT | insn, p);
   16874 	      if (r_type == R_PPC64_PLTCALL)
   16875 		bfd_put_32 (input_bfd, NOP, p + 4);
   16876 	      unresolved_reloc = save_unresolved_reloc;
   16877 	      r_type = R_PPC64_REL24;
   16878 	    }
   16879 	  break;
   16880 
   16881 	case R_PPC64_PLTSEQ_NOTOC:
   16882 	case R_PPC64_PLTSEQ:
   16883 	  if (unresolved_reloc)
   16884 	    {
   16885 	      unresolved_reloc = FALSE;
   16886 	      goto nop_it;
   16887 	    }
   16888 	  break;
   16889 
   16890 	case R_PPC64_PLT_PCREL34_NOTOC:
   16891 	  if (!unresolved_reloc)
   16892 	    htab->notoc_plt = 1;
   16893 	  /* Fall through.  */
   16894 	case R_PPC64_PLT_PCREL34:
   16895 	  if (unresolved_reloc)
   16896 	    {
   16897 	      bfd_byte *p = contents + rel->r_offset;
   16898 	      bfd_put_32 (input_bfd, PNOP >> 32, p);
   16899 	      bfd_put_32 (input_bfd, PNOP, p + 4);
   16900 	      unresolved_reloc = FALSE;
   16901 	      goto copy_reloc;
   16902 	    }
   16903 	  break;
   16904 
   16905 	case R_PPC64_PLT16_HA:
   16906 	  if (unresolved_reloc)
   16907 	    {
   16908 	      unresolved_reloc = FALSE;
   16909 	      goto nop_it;
   16910 	    }
   16911 	  /* Fall through.  */
   16912 	case R_PPC64_GOT_TLSLD16_HA:
   16913 	case R_PPC64_GOT_TLSGD16_HA:
   16914 	case R_PPC64_GOT_TPREL16_HA:
   16915 	case R_PPC64_GOT_DTPREL16_HA:
   16916 	case R_PPC64_GOT16_HA:
   16917 	case R_PPC64_TOC16_HA:
   16918 	  if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
   16919 	      && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
   16920 	    {
   16921 	      bfd_byte *p;
   16922 	    nop_it:
   16923 	      p = contents + (rel->r_offset & ~3);
   16924 	      bfd_put_32 (input_bfd, NOP, p);
   16925 	      goto copy_reloc;
   16926 	    }
   16927 	  break;
   16928 
   16929 	case R_PPC64_PLT16_LO:
   16930 	case R_PPC64_PLT16_LO_DS:
   16931 	  if (unresolved_reloc)
   16932 	    {
   16933 	      unresolved_reloc = FALSE;
   16934 	      goto nop_it;
   16935 	    }
   16936 	  /* Fall through.  */
   16937 	case R_PPC64_GOT_TLSLD16_LO:
   16938 	case R_PPC64_GOT_TLSGD16_LO:
   16939 	case R_PPC64_GOT_TPREL16_LO_DS:
   16940 	case R_PPC64_GOT_DTPREL16_LO_DS:
   16941 	case R_PPC64_GOT16_LO:
   16942 	case R_PPC64_GOT16_LO_DS:
   16943 	case R_PPC64_TOC16_LO:
   16944 	case R_PPC64_TOC16_LO_DS:
   16945 	  if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
   16946 	      && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
   16947 	    {
   16948 	      bfd_byte *p = contents + (rel->r_offset & ~3);
   16949 	      insn = bfd_get_32 (input_bfd, p);
   16950 	      if ((insn & (0x3fu << 26)) == 12u << 26 /* addic */)
   16951 		{
   16952 		  /* Transform addic to addi when we change reg.  */
   16953 		  insn &= ~((0x3fu << 26) | (0x1f << 16));
   16954 		  insn |= (14u << 26) | (2 << 16);
   16955 		}
   16956 	      else
   16957 		{
   16958 		  insn &= ~(0x1f << 16);
   16959 		  insn |= 2 << 16;
   16960 		}
   16961 	      bfd_put_32 (input_bfd, insn, p);
   16962 	    }
   16963 	  break;
   16964 
   16965 	case R_PPC64_TPREL16_HA:
   16966 	  if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
   16967 	    {
   16968 	      bfd_byte *p = contents + (rel->r_offset & ~3);
   16969 	      bfd_put_32 (input_bfd, NOP, p);
   16970 	      goto copy_reloc;
   16971 	    }
   16972 	  break;
   16973 
   16974 	case R_PPC64_TPREL16_LO:
   16975 	case R_PPC64_TPREL16_LO_DS:
   16976 	  if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
   16977 	    {
   16978 	      bfd_byte *p = contents + (rel->r_offset & ~3);
   16979 	      insn = bfd_get_32 (input_bfd, p);
   16980 	      insn &= ~(0x1f << 16);
   16981 	      insn |= 13 << 16;
   16982 	      bfd_put_32 (input_bfd, insn, p);
   16983 	    }
   16984 	  break;
   16985 	}
   16986 
   16987       /* Do any further special processing.  */
   16988       switch (r_type)
   16989 	{
   16990 	default:
   16991 	  break;
   16992 
   16993 	case R_PPC64_REL16_HA:
   16994 	case R_PPC64_REL16_HIGHA:
   16995 	case R_PPC64_REL16_HIGHERA:
   16996 	case R_PPC64_REL16_HIGHESTA:
   16997 	case R_PPC64_REL16DX_HA:
   16998 	case R_PPC64_ADDR16_HA:
   16999 	case R_PPC64_ADDR16_HIGHA:
   17000 	case R_PPC64_ADDR16_HIGHERA:
   17001 	case R_PPC64_ADDR16_HIGHESTA:
   17002 	case R_PPC64_TOC16_HA:
   17003 	case R_PPC64_SECTOFF_HA:
   17004 	case R_PPC64_TPREL16_HA:
   17005 	case R_PPC64_TPREL16_HIGHA:
   17006 	case R_PPC64_TPREL16_HIGHERA:
   17007 	case R_PPC64_TPREL16_HIGHESTA:
   17008 	case R_PPC64_DTPREL16_HA:
   17009 	case R_PPC64_DTPREL16_HIGHA:
   17010 	case R_PPC64_DTPREL16_HIGHERA:
   17011 	case R_PPC64_DTPREL16_HIGHESTA:
   17012 	  /* It's just possible that this symbol is a weak symbol
   17013 	     that's not actually defined anywhere. In that case,
   17014 	     'sec' would be NULL, and we should leave the symbol
   17015 	     alone (it will be set to zero elsewhere in the link).  */
   17016 	  if (sec == NULL)
   17017 	    break;
   17018 	  /* Fall through.  */
   17019 
   17020 	case R_PPC64_GOT16_HA:
   17021 	case R_PPC64_PLTGOT16_HA:
   17022 	case R_PPC64_PLT16_HA:
   17023 	case R_PPC64_GOT_TLSGD16_HA:
   17024 	case R_PPC64_GOT_TLSLD16_HA:
   17025 	case R_PPC64_GOT_TPREL16_HA:
   17026 	case R_PPC64_GOT_DTPREL16_HA:
   17027 	  /* Add 0x10000 if sign bit in 0:15 is set.
   17028 	     Bits 0:15 are not used.  */
   17029 	  addend += 0x8000;
   17030 	  break;
   17031 
   17032 	case R_PPC64_D34_HA30:
   17033 	case R_PPC64_ADDR16_HIGHERA34:
   17034 	case R_PPC64_ADDR16_HIGHESTA34:
   17035 	case R_PPC64_REL16_HIGHERA34:
   17036 	case R_PPC64_REL16_HIGHESTA34:
   17037 	  if (sec != NULL)
   17038 	    addend += 1ULL << 33;
   17039 	  break;
   17040 
   17041 	case R_PPC64_ADDR16_DS:
   17042 	case R_PPC64_ADDR16_LO_DS:
   17043 	case R_PPC64_GOT16_DS:
   17044 	case R_PPC64_GOT16_LO_DS:
   17045 	case R_PPC64_PLT16_LO_DS:
   17046 	case R_PPC64_SECTOFF_DS:
   17047 	case R_PPC64_SECTOFF_LO_DS:
   17048 	case R_PPC64_TOC16_DS:
   17049 	case R_PPC64_TOC16_LO_DS:
   17050 	case R_PPC64_PLTGOT16_DS:
   17051 	case R_PPC64_PLTGOT16_LO_DS:
   17052 	case R_PPC64_GOT_TPREL16_DS:
   17053 	case R_PPC64_GOT_TPREL16_LO_DS:
   17054 	case R_PPC64_GOT_DTPREL16_DS:
   17055 	case R_PPC64_GOT_DTPREL16_LO_DS:
   17056 	case R_PPC64_TPREL16_DS:
   17057 	case R_PPC64_TPREL16_LO_DS:
   17058 	case R_PPC64_DTPREL16_DS:
   17059 	case R_PPC64_DTPREL16_LO_DS:
   17060 	  insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
   17061 	  mask = 3;
   17062 	  /* If this reloc is against an lq, lxv, or stxv insn, then
   17063 	     the value must be a multiple of 16.  This is somewhat of
   17064 	     a hack, but the "correct" way to do this by defining _DQ
   17065 	     forms of all the _DS relocs bloats all reloc switches in
   17066 	     this file.  It doesn't make much sense to use these
   17067 	     relocs in data, so testing the insn should be safe.  */
   17068 	  if ((insn & (0x3fu << 26)) == (56u << 26)
   17069 	      || ((insn & (0x3fu << 26)) == (61u << 26) && (insn & 3) == 1))
   17070 	    mask = 15;
   17071 	  relocation += addend;
   17072 	  addend = insn & (mask ^ 3);
   17073 	  if ((relocation & mask) != 0)
   17074 	    {
   17075 	      relocation ^= relocation & mask;
   17076 	      info->callbacks->einfo
   17077 		/* xgettext:c-format */
   17078 		(_("%H: error: %s not a multiple of %u\n"),
   17079 		 input_bfd, input_section, rel->r_offset,
   17080 		 ppc64_elf_howto_table[r_type]->name,
   17081 		 mask + 1);
   17082 	      bfd_set_error (bfd_error_bad_value);
   17083 	      ret = FALSE;
   17084 	      goto copy_reloc;
   17085 	    }
   17086 	  break;
   17087 	}
   17088 
   17089       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
   17090 	 because such sections are not SEC_ALLOC and thus ld.so will
   17091 	 not process them.  */
   17092       howto = ppc64_elf_howto_table[(int) r_type];
   17093       if (unresolved_reloc
   17094 	  && !((input_section->flags & SEC_DEBUGGING) != 0
   17095 	       && h->elf.def_dynamic)
   17096 	  && _bfd_elf_section_offset (output_bfd, info, input_section,
   17097 				      rel->r_offset) != (bfd_vma) -1)
   17098 	{
   17099 	  info->callbacks->einfo
   17100 	    /* xgettext:c-format */
   17101 	    (_("%H: unresolvable %s against `%pT'\n"),
   17102 	     input_bfd, input_section, rel->r_offset,
   17103 	     howto->name,
   17104 	     h->elf.root.root.string);
   17105 	  ret = FALSE;
   17106 	}
   17107 
   17108       /* 16-bit fields in insns mostly have signed values, but a
   17109 	 few insns have 16-bit unsigned values.  Really, we should
   17110 	 have different reloc types.  */
   17111       if (howto->complain_on_overflow != complain_overflow_dont
   17112 	  && howto->dst_mask == 0xffff
   17113 	  && (input_section->flags & SEC_CODE) != 0)
   17114 	{
   17115 	  enum complain_overflow complain = complain_overflow_signed;
   17116 
   17117 	  insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
   17118 	  if ((insn & (0x3fu << 26)) == 10u << 26 /* cmpli */)
   17119 	    complain = complain_overflow_bitfield;
   17120 	  else if (howto->rightshift == 0
   17121 		   ? ((insn & (0x3fu << 26)) == 28u << 26 /* andi */
   17122 		      || (insn & (0x3fu << 26)) == 24u << 26 /* ori */
   17123 		      || (insn & (0x3fu << 26)) == 26u << 26 /* xori */)
   17124 		   : ((insn & (0x3fu << 26)) == 29u << 26 /* andis */
   17125 		      || (insn & (0x3fu << 26)) == 25u << 26 /* oris */
   17126 		      || (insn & (0x3fu << 26)) == 27u << 26 /* xoris */))
   17127 	    complain = complain_overflow_unsigned;
   17128 	  if (howto->complain_on_overflow != complain)
   17129 	    {
   17130 	      alt_howto = *howto;
   17131 	      alt_howto.complain_on_overflow = complain;
   17132 	      howto = &alt_howto;
   17133 	    }
   17134 	}
   17135 
   17136       switch (r_type)
   17137 	{
   17138 	  /* Split field relocs aren't handled by _bfd_final_link_relocate.  */
   17139 	case R_PPC64_D34:
   17140 	case R_PPC64_D34_LO:
   17141 	case R_PPC64_D34_HI30:
   17142 	case R_PPC64_D34_HA30:
   17143 	case R_PPC64_PCREL34:
   17144 	case R_PPC64_GOT_PCREL34:
   17145 	case R_PPC64_TPREL34:
   17146 	case R_PPC64_DTPREL34:
   17147 	case R_PPC64_GOT_TLSGD_PCREL34:
   17148 	case R_PPC64_GOT_TLSLD_PCREL34:
   17149 	case R_PPC64_GOT_TPREL_PCREL34:
   17150 	case R_PPC64_GOT_DTPREL_PCREL34:
   17151 	case R_PPC64_PLT_PCREL34:
   17152 	case R_PPC64_PLT_PCREL34_NOTOC:
   17153 	case R_PPC64_D28:
   17154 	case R_PPC64_PCREL28:
   17155 	  if (rel->r_offset + 8 > input_section->size)
   17156 	    r = bfd_reloc_outofrange;
   17157 	  else
   17158 	    {
   17159 	      relocation += addend;
   17160 	      if (howto->pc_relative)
   17161 		relocation -= (rel->r_offset
   17162 			       + input_section->output_offset
   17163 			       + input_section->output_section->vma);
   17164 	      relocation >>= howto->rightshift;
   17165 
   17166 	      pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   17167 	      pinsn <<= 32;
   17168 	      pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
   17169 
   17170 	      pinsn &= ~howto->dst_mask;
   17171 	      pinsn |= (((relocation << 16) | (relocation & 0xffff))
   17172 			& howto->dst_mask);
   17173 	      bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
   17174 	      bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
   17175 	      r = bfd_reloc_ok;
   17176 	      if (howto->complain_on_overflow == complain_overflow_signed
   17177 		  && (relocation + (1ULL << (howto->bitsize - 1))
   17178 		      >= 1ULL << howto->bitsize))
   17179 		r = bfd_reloc_overflow;
   17180 	    }
   17181 	  break;
   17182 
   17183 	case R_PPC64_REL16DX_HA:
   17184 	  if (rel->r_offset + 4 > input_section->size)
   17185 	    r = bfd_reloc_outofrange;
   17186 	  else
   17187 	    {
   17188 	      relocation += addend;
   17189 	      relocation -= (rel->r_offset
   17190 			     + input_section->output_offset
   17191 			     + input_section->output_section->vma);
   17192 	      relocation = (bfd_signed_vma) relocation >> 16;
   17193 	      insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   17194 	      insn &= ~0x1fffc1;
   17195 	      insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
   17196 	      bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   17197 	      r = bfd_reloc_ok;
   17198 	      if (relocation + 0x8000 > 0xffff)
   17199 		r = bfd_reloc_overflow;
   17200 	    }
   17201 	  break;
   17202 
   17203 	default:
   17204 	  r = _bfd_final_link_relocate (howto, input_bfd, input_section,
   17205 					contents, rel->r_offset,
   17206 					relocation, addend);
   17207 	}
   17208 
   17209       if (r != bfd_reloc_ok)
   17210 	{
   17211 	  char *more_info = NULL;
   17212 	  const char *reloc_name = howto->name;
   17213 
   17214 	  if (reloc_dest != DEST_NORMAL)
   17215 	    {
   17216 	      more_info = bfd_malloc (strlen (reloc_name) + 8);
   17217 	      if (more_info != NULL)
   17218 		{
   17219 		  strcpy (more_info, reloc_name);
   17220 		  strcat (more_info, (reloc_dest == DEST_OPD
   17221 				      ? " (OPD)" : " (stub)"));
   17222 		  reloc_name = more_info;
   17223 		}
   17224 	    }
   17225 
   17226 	  if (r == bfd_reloc_overflow)
   17227 	    {
   17228 	      /* On code like "if (foo) foo();" don't report overflow
   17229 		 on a branch to zero when foo is undefined.  */
   17230 	      if (!warned
   17231 		  && (reloc_dest == DEST_STUB
   17232 		      || !(h != NULL
   17233 			   && (h->elf.root.type == bfd_link_hash_undefweak
   17234 			       || h->elf.root.type == bfd_link_hash_undefined)
   17235 			   && is_branch_reloc (r_type))))
   17236 		info->callbacks->reloc_overflow
   17237 		  (info, (struct bfd_link_hash_entry *) h, sym_name,
   17238 		   reloc_name, orig_rel.r_addend, input_bfd, input_section,
   17239 		   rel->r_offset);
   17240 	    }
   17241 	  else
   17242 	    {
   17243 	      info->callbacks->einfo
   17244 		/* xgettext:c-format */
   17245 		(_("%H: %s against `%pT': error %d\n"),
   17246 		 input_bfd, input_section, rel->r_offset,
   17247 		 reloc_name, sym_name, (int) r);
   17248 	      ret = FALSE;
   17249 	    }
   17250 	  free (more_info);
   17251 	}
   17252     copy_reloc:
   17253       if (wrel != rel)
   17254 	*wrel = *rel;
   17255     }
   17256 
   17257   if (wrel != rel)
   17258     {
   17259       Elf_Internal_Shdr *rel_hdr;
   17260       size_t deleted = rel - wrel;
   17261 
   17262       rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
   17263       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
   17264       if (rel_hdr->sh_size == 0)
   17265 	{
   17266 	  /* It is too late to remove an empty reloc section.  Leave
   17267 	     one NONE reloc.
   17268 	     ??? What is wrong with an empty section???  */
   17269 	  rel_hdr->sh_size = rel_hdr->sh_entsize;
   17270 	  deleted -= 1;
   17271 	}
   17272       rel_hdr = _bfd_elf_single_rel_hdr (input_section);
   17273       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
   17274       input_section->reloc_count -= deleted;
   17275     }
   17276 
   17277   /* If we're emitting relocations, then shortly after this function
   17278      returns, reloc offsets and addends for this section will be
   17279      adjusted.  Worse, reloc symbol indices will be for the output
   17280      file rather than the input.  Save a copy of the relocs for
   17281      opd_entry_value.  */
   17282   if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
   17283     {
   17284       bfd_size_type amt;
   17285       amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
   17286       rel = bfd_alloc (input_bfd, amt);
   17287       BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
   17288       ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
   17289       if (rel == NULL)
   17290 	return FALSE;
   17291       memcpy (rel, relocs, amt);
   17292     }
   17293   return ret;
   17294 }
   17295 
   17296 /* Adjust the value of any local symbols in opd sections.  */
   17297 
   17298 static int
   17299 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
   17300 			      const char *name ATTRIBUTE_UNUSED,
   17301 			      Elf_Internal_Sym *elfsym,
   17302 			      asection *input_sec,
   17303 			      struct elf_link_hash_entry *h)
   17304 {
   17305   struct _opd_sec_data *opd;
   17306   long adjust;
   17307   bfd_vma value;
   17308 
   17309   if (h != NULL)
   17310     return 1;
   17311 
   17312   opd = get_opd_info (input_sec);
   17313   if (opd == NULL || opd->adjust == NULL)
   17314     return 1;
   17315 
   17316   value = elfsym->st_value - input_sec->output_offset;
   17317   if (!bfd_link_relocatable (info))
   17318     value -= input_sec->output_section->vma;
   17319 
   17320   adjust = opd->adjust[OPD_NDX (value)];
   17321   if (adjust == -1)
   17322     return 2;
   17323 
   17324   elfsym->st_value += adjust;
   17325   return 1;
   17326 }
   17327 
   17328 /* Finish up dynamic symbol handling.  We set the contents of various
   17329    dynamic sections here.  */
   17330 
   17331 static bfd_boolean
   17332 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
   17333 				 struct bfd_link_info *info,
   17334 				 struct elf_link_hash_entry *h,
   17335 				 Elf_Internal_Sym *sym)
   17336 {
   17337   struct ppc_link_hash_table *htab;
   17338   struct plt_entry *ent;
   17339 
   17340   htab = ppc_hash_table (info);
   17341   if (htab == NULL)
   17342     return FALSE;
   17343 
   17344   if (!htab->opd_abi && !h->def_regular)
   17345     for (ent = h->plt.plist; ent != NULL; ent = ent->next)
   17346       if (ent->plt.offset != (bfd_vma) -1)
   17347 	{
   17348 	  /* Mark the symbol as undefined, rather than as
   17349 	     defined in glink.  Leave the value if there were
   17350 	     any relocations where pointer equality matters
   17351 	     (this is a clue for the dynamic linker, to make
   17352 	     function pointer comparisons work between an
   17353 	     application and shared library), otherwise set it
   17354 	     to zero.  */
   17355 	  sym->st_shndx = SHN_UNDEF;
   17356 	  if (!h->pointer_equality_needed)
   17357 	    sym->st_value = 0;
   17358 	  else if (!h->ref_regular_nonweak)
   17359 	    {
   17360 	      /* This breaks function pointer comparisons, but
   17361 		 that is better than breaking tests for a NULL
   17362 		 function pointer.  */
   17363 	      sym->st_value = 0;
   17364 	    }
   17365 	  break;
   17366 	}
   17367 
   17368   if (h->needs_copy
   17369       && (h->root.type == bfd_link_hash_defined
   17370 	  || h->root.type == bfd_link_hash_defweak)
   17371       && (h->root.u.def.section == htab->elf.sdynbss
   17372 	  || h->root.u.def.section == htab->elf.sdynrelro))
   17373     {
   17374       /* This symbol needs a copy reloc.  Set it up.  */
   17375       Elf_Internal_Rela rela;
   17376       asection *srel;
   17377       bfd_byte *loc;
   17378 
   17379       if (h->dynindx == -1)
   17380 	abort ();
   17381 
   17382       rela.r_offset = defined_sym_val (h);
   17383       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
   17384       rela.r_addend = 0;
   17385       if (h->root.u.def.section == htab->elf.sdynrelro)
   17386 	srel = htab->elf.sreldynrelro;
   17387       else
   17388 	srel = htab->elf.srelbss;
   17389       loc = srel->contents;
   17390       loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
   17391       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
   17392     }
   17393 
   17394   return TRUE;
   17395 }
   17396 
   17397 /* Used to decide how to sort relocs in an optimal manner for the
   17398    dynamic linker, before writing them out.  */
   17399 
   17400 static enum elf_reloc_type_class
   17401 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
   17402 			    const asection *rel_sec,
   17403 			    const Elf_Internal_Rela *rela)
   17404 {
   17405   enum elf_ppc64_reloc_type r_type;
   17406   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   17407 
   17408   if (rel_sec == htab->elf.irelplt)
   17409     return reloc_class_ifunc;
   17410 
   17411   r_type = ELF64_R_TYPE (rela->r_info);
   17412   switch (r_type)
   17413     {
   17414     case R_PPC64_RELATIVE:
   17415       return reloc_class_relative;
   17416     case R_PPC64_JMP_SLOT:
   17417       return reloc_class_plt;
   17418     case R_PPC64_COPY:
   17419       return reloc_class_copy;
   17420     default:
   17421       return reloc_class_normal;
   17422     }
   17423 }
   17424 
   17425 /* Finish up the dynamic sections.  */
   17426 
   17427 static bfd_boolean
   17428 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
   17429 				   struct bfd_link_info *info)
   17430 {
   17431   struct ppc_link_hash_table *htab;
   17432   bfd *dynobj;
   17433   asection *sdyn;
   17434 
   17435   htab = ppc_hash_table (info);
   17436   if (htab == NULL)
   17437     return FALSE;
   17438 
   17439   dynobj = htab->elf.dynobj;
   17440   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
   17441 
   17442   if (htab->elf.dynamic_sections_created)
   17443     {
   17444       Elf64_External_Dyn *dyncon, *dynconend;
   17445 
   17446       if (sdyn == NULL || htab->elf.sgot == NULL)
   17447 	abort ();
   17448 
   17449       dyncon = (Elf64_External_Dyn *) sdyn->contents;
   17450       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
   17451       for (; dyncon < dynconend; dyncon++)
   17452 	{
   17453 	  Elf_Internal_Dyn dyn;
   17454 	  asection *s;
   17455 
   17456 	  bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
   17457 
   17458 	  switch (dyn.d_tag)
   17459 	    {
   17460 	    default:
   17461 	      continue;
   17462 
   17463 	    case DT_PPC64_GLINK:
   17464 	      s = htab->glink;
   17465 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   17466 	      /* We stupidly defined DT_PPC64_GLINK to be the start
   17467 		 of glink rather than the first entry point, which is
   17468 		 what ld.so needs, and now have a bigger stub to
   17469 		 support automatic multiple TOCs.  */
   17470 	      dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
   17471 	      break;
   17472 
   17473 	    case DT_PPC64_OPD:
   17474 	      s = bfd_get_section_by_name (output_bfd, ".opd");
   17475 	      if (s == NULL)
   17476 		continue;
   17477 	      dyn.d_un.d_ptr = s->vma;
   17478 	      break;
   17479 
   17480 	    case DT_PPC64_OPT:
   17481 	      if ((htab->do_multi_toc && htab->multi_toc_needed)
   17482 		  || htab->notoc_plt)
   17483 		dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
   17484 	      if (htab->has_plt_localentry0)
   17485 		dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
   17486 	      break;
   17487 
   17488 	    case DT_PPC64_OPDSZ:
   17489 	      s = bfd_get_section_by_name (output_bfd, ".opd");
   17490 	      if (s == NULL)
   17491 		continue;
   17492 	      dyn.d_un.d_val = s->size;
   17493 	      break;
   17494 
   17495 	    case DT_PLTGOT:
   17496 	      s = htab->elf.splt;
   17497 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   17498 	      break;
   17499 
   17500 	    case DT_JMPREL:
   17501 	      s = htab->elf.srelplt;
   17502 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   17503 	      break;
   17504 
   17505 	    case DT_PLTRELSZ:
   17506 	      dyn.d_un.d_val = htab->elf.srelplt->size;
   17507 	      break;
   17508 
   17509 	    case DT_TEXTREL:
   17510 	      if (htab->elf.ifunc_resolvers)
   17511 		info->callbacks->einfo
   17512 		  (_("%P: warning: text relocations and GNU indirect "
   17513 		     "functions may result in a segfault at runtime\n"));
   17514 	      continue;
   17515 	    }
   17516 
   17517 	  bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
   17518 	}
   17519     }
   17520 
   17521   if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
   17522       && htab->elf.sgot->output_section != bfd_abs_section_ptr)
   17523     {
   17524       /* Fill in the first entry in the global offset table.
   17525 	 We use it to hold the link-time TOCbase.  */
   17526       bfd_put_64 (output_bfd,
   17527 		  elf_gp (output_bfd) + TOC_BASE_OFF,
   17528 		  htab->elf.sgot->contents);
   17529 
   17530       /* Set .got entry size.  */
   17531       elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
   17532 	= 8;
   17533     }
   17534 
   17535   if (htab->elf.splt != NULL && htab->elf.splt->size != 0
   17536       && htab->elf.splt->output_section != bfd_abs_section_ptr)
   17537     {
   17538       /* Set .plt entry size.  */
   17539       elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
   17540 	= PLT_ENTRY_SIZE (htab);
   17541     }
   17542 
   17543   /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
   17544      brlt ourselves if emitrelocations.  */
   17545   if (htab->brlt != NULL
   17546       && htab->brlt->reloc_count != 0
   17547       && !_bfd_elf_link_output_relocs (output_bfd,
   17548 				       htab->brlt,
   17549 				       elf_section_data (htab->brlt)->rela.hdr,
   17550 				       elf_section_data (htab->brlt)->relocs,
   17551 				       NULL))
   17552     return FALSE;
   17553 
   17554   if (htab->glink != NULL
   17555       && htab->glink->reloc_count != 0
   17556       && !_bfd_elf_link_output_relocs (output_bfd,
   17557 				       htab->glink,
   17558 				       elf_section_data (htab->glink)->rela.hdr,
   17559 				       elf_section_data (htab->glink)->relocs,
   17560 				       NULL))
   17561     return FALSE;
   17562 
   17563 
   17564   if (htab->glink_eh_frame != NULL
   17565       && htab->glink_eh_frame->size != 0
   17566       && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
   17567       && !_bfd_elf_write_section_eh_frame (output_bfd, info,
   17568 					   htab->glink_eh_frame,
   17569 					   htab->glink_eh_frame->contents))
   17570     return FALSE;
   17571 
   17572   /* We need to handle writing out multiple GOT sections ourselves,
   17573      since we didn't add them to DYNOBJ.  We know dynobj is the first
   17574      bfd.  */
   17575   while ((dynobj = dynobj->link.next) != NULL)
   17576     {
   17577       asection *s;
   17578 
   17579       if (!is_ppc64_elf (dynobj))
   17580 	continue;
   17581 
   17582       s = ppc64_elf_tdata (dynobj)->got;
   17583       if (s != NULL
   17584 	  && s->size != 0
   17585 	  && s->output_section != bfd_abs_section_ptr
   17586 	  && !bfd_set_section_contents (output_bfd, s->output_section,
   17587 					s->contents, s->output_offset,
   17588 					s->size))
   17589 	return FALSE;
   17590       s = ppc64_elf_tdata (dynobj)->relgot;
   17591       if (s != NULL
   17592 	  && s->size != 0
   17593 	  && s->output_section != bfd_abs_section_ptr
   17594 	  && !bfd_set_section_contents (output_bfd, s->output_section,
   17595 					s->contents, s->output_offset,
   17596 					s->size))
   17597 	return FALSE;
   17598     }
   17599 
   17600   return TRUE;
   17601 }
   17602 
   17603 #include "elf64-target.h"
   17604 
   17605 /* FreeBSD support */
   17606 
   17607 #undef  TARGET_LITTLE_SYM
   17608 #undef  TARGET_LITTLE_NAME
   17609 
   17610 #undef  TARGET_BIG_SYM
   17611 #define TARGET_BIG_SYM	powerpc_elf64_fbsd_vec
   17612 #undef  TARGET_BIG_NAME
   17613 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
   17614 
   17615 #undef  ELF_OSABI
   17616 #define	ELF_OSABI       ELFOSABI_FREEBSD
   17617 
   17618 #undef  elf64_bed
   17619 #define elf64_bed	elf64_powerpc_fbsd_bed
   17620 
   17621 #include "elf64-target.h"
   17622