Home | History | Annotate | Line # | Download | only in bfd
elf64-ppc.c revision 1.1.1.10
      1 /* PowerPC64-specific support for 64-bit ELF.
      2    Copyright (C) 1999-2022 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 /* The assembler should generate a full set of section symbols even
     29    when they appear unused.  The linux kernel build tool recordmcount
     30    needs them.  */
     31 #define TARGET_KEEP_UNUSED_SECTION_SYMBOLS true
     32 
     33 #include "sysdep.h"
     34 #include <stdarg.h>
     35 #include "bfd.h"
     36 #include "bfdlink.h"
     37 #include "libbfd.h"
     38 #include "elf-bfd.h"
     39 #include "elf/ppc64.h"
     40 #include "elf64-ppc.h"
     41 #include "dwarf2.h"
     42 
     43 /* All users of this file have bfd_octets_per_byte (abfd, sec) == 1.  */
     44 #define OCTETS_PER_BYTE(ABFD, SEC) 1
     45 
     46 static bfd_reloc_status_type ppc64_elf_ha_reloc
     47   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     48 static bfd_reloc_status_type ppc64_elf_branch_reloc
     49   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     50 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
     51   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     52 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
     53   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     54 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
     55   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     56 static bfd_reloc_status_type ppc64_elf_toc_reloc
     57   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     58 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
     59   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     60 static bfd_reloc_status_type ppc64_elf_toc64_reloc
     61   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     62 static bfd_reloc_status_type ppc64_elf_prefix_reloc
     63   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     64 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
     65   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     66 static bfd_vma opd_entry_value
     67   (asection *, bfd_vma, asection **, bfd_vma *, bool);
     68 
     69 #define TARGET_LITTLE_SYM	powerpc_elf64_le_vec
     70 #define TARGET_LITTLE_NAME	"elf64-powerpcle"
     71 #define TARGET_BIG_SYM		powerpc_elf64_vec
     72 #define TARGET_BIG_NAME		"elf64-powerpc"
     73 #define ELF_ARCH		bfd_arch_powerpc
     74 #define ELF_TARGET_ID		PPC64_ELF_DATA
     75 #define ELF_MACHINE_CODE	EM_PPC64
     76 #define ELF_MAXPAGESIZE		0x10000
     77 #define ELF_COMMONPAGESIZE	0x1000
     78 #define elf_info_to_howto	ppc64_elf_info_to_howto
     79 
     80 #define elf_backend_want_got_sym 0
     81 #define elf_backend_want_plt_sym 0
     82 #define elf_backend_plt_alignment 3
     83 #define elf_backend_plt_not_loaded 1
     84 #define elf_backend_got_header_size 8
     85 #define elf_backend_want_dynrelro 1
     86 #define elf_backend_can_gc_sections 1
     87 #define elf_backend_can_refcount 1
     88 #define elf_backend_rela_normal 1
     89 #define elf_backend_dtrel_excludes_plt 1
     90 #define elf_backend_default_execstack 0
     91 
     92 #define bfd_elf64_mkobject		      ppc64_elf_mkobject
     93 #define bfd_elf64_bfd_reloc_type_lookup	      ppc64_elf_reloc_type_lookup
     94 #define bfd_elf64_bfd_reloc_name_lookup	      ppc64_elf_reloc_name_lookup
     95 #define bfd_elf64_bfd_merge_private_bfd_data  ppc64_elf_merge_private_bfd_data
     96 #define bfd_elf64_bfd_print_private_bfd_data  ppc64_elf_print_private_bfd_data
     97 #define bfd_elf64_new_section_hook	      ppc64_elf_new_section_hook
     98 #define bfd_elf64_bfd_link_hash_table_create  ppc64_elf_link_hash_table_create
     99 #define bfd_elf64_get_synthetic_symtab	      ppc64_elf_get_synthetic_symtab
    100 #define bfd_elf64_bfd_link_just_syms	      ppc64_elf_link_just_syms
    101 #define bfd_elf64_bfd_gc_sections	      ppc64_elf_gc_sections
    102 
    103 #define elf_backend_object_p		      ppc64_elf_object_p
    104 #define elf_backend_grok_prstatus	      ppc64_elf_grok_prstatus
    105 #define elf_backend_grok_psinfo		      ppc64_elf_grok_psinfo
    106 #define elf_backend_write_core_note	      ppc64_elf_write_core_note
    107 #define elf_backend_create_dynamic_sections   _bfd_elf_create_dynamic_sections
    108 #define elf_backend_copy_indirect_symbol      ppc64_elf_copy_indirect_symbol
    109 #define elf_backend_add_symbol_hook	      ppc64_elf_add_symbol_hook
    110 #define elf_backend_check_directives	      ppc64_elf_before_check_relocs
    111 #define elf_backend_notice_as_needed	      ppc64_elf_notice_as_needed
    112 #define elf_backend_archive_symbol_lookup     ppc64_elf_archive_symbol_lookup
    113 #define elf_backend_check_relocs	      ppc64_elf_check_relocs
    114 #define elf_backend_relocs_compatible	      _bfd_elf_relocs_compatible
    115 #define elf_backend_gc_keep		      ppc64_elf_gc_keep
    116 #define elf_backend_gc_mark_dynamic_ref       ppc64_elf_gc_mark_dynamic_ref
    117 #define elf_backend_gc_mark_hook	      ppc64_elf_gc_mark_hook
    118 #define elf_backend_adjust_dynamic_symbol     ppc64_elf_adjust_dynamic_symbol
    119 #define elf_backend_hide_symbol		      ppc64_elf_hide_symbol
    120 #define elf_backend_maybe_function_sym	      ppc64_elf_maybe_function_sym
    121 #define elf_backend_always_size_sections      ppc64_elf_edit
    122 #define elf_backend_size_dynamic_sections     ppc64_elf_size_dynamic_sections
    123 #define elf_backend_hash_symbol		      ppc64_elf_hash_symbol
    124 #define elf_backend_init_index_section	      _bfd_elf_init_2_index_sections
    125 #define elf_backend_action_discarded	      ppc64_elf_action_discarded
    126 #define elf_backend_relocate_section	      ppc64_elf_relocate_section
    127 #define elf_backend_finish_dynamic_symbol     ppc64_elf_finish_dynamic_symbol
    128 #define elf_backend_reloc_type_class	      ppc64_elf_reloc_type_class
    129 #define elf_backend_finish_dynamic_sections   ppc64_elf_finish_dynamic_sections
    130 #define elf_backend_link_output_symbol_hook   ppc64_elf_output_symbol_hook
    131 #define elf_backend_special_sections	      ppc64_elf_special_sections
    132 #define elf_backend_section_flags	      ppc64_elf_section_flags
    133 #define elf_backend_merge_symbol_attribute    ppc64_elf_merge_symbol_attribute
    134 #define elf_backend_merge_symbol	      ppc64_elf_merge_symbol
    135 #define elf_backend_get_reloc_section	      bfd_get_section_by_name
    136 
    137 /* The name of the dynamic interpreter.  This is put in the .interp
    138    section.  */
    139 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
    140 
    141 /* The size in bytes of an entry in the procedure linkage table.  */
    142 #define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
    143 #define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
    144 
    145 /* The initial size of the plt reserved for the dynamic linker.  */
    146 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
    147 
    148 /* Offsets to some stack save slots.  */
    149 #define STK_LR 16
    150 #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
    151 /* This one is dodgy.  ELFv2 does not have a linker word, so use the
    152    CR save slot.  Used only by optimised __tls_get_addr call stub,
    153    relying on __tls_get_addr_opt not saving CR..  */
    154 #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
    155 
    156 /* TOC base pointers offset from start of TOC.  */
    157 #define TOC_BASE_OFF	0x8000
    158 /* TOC base alignment.  */
    159 #define TOC_BASE_ALIGN	256
    160 
    161 /* Offset of tp and dtp pointers from start of TLS block.  */
    162 #define TP_OFFSET	0x7000
    163 #define DTP_OFFSET	0x8000
    164 
    165 /* .plt call stub instructions.  The normal stub is like this, but
    166    sometimes the .plt entry crosses a 64k boundary and we need to
    167    insert an addi to adjust r11.  */
    168 #define STD_R2_0R1	0xf8410000	/* std	 %r2,0+40(%r1)	     */
    169 #define ADDIS_R11_R2	0x3d620000	/* addis %r11,%r2,xxx@ha     */
    170 #define LD_R12_0R11	0xe98b0000	/* ld	 %r12,xxx+0@l(%r11)  */
    171 #define MTCTR_R12	0x7d8903a6	/* mtctr %r12		     */
    172 #define LD_R2_0R11	0xe84b0000	/* ld	 %r2,xxx+8@l(%r11)   */
    173 #define LD_R11_0R11	0xe96b0000	/* ld	 %r11,xxx+16@l(%r11) */
    174 #define BCTR		0x4e800420	/* bctr			     */
    175 
    176 #define ADDI_R11_R11	0x396b0000	/* addi %r11,%r11,off@l	 */
    177 #define ADDI_R12_R11	0x398b0000	/* addi %r12,%r11,off@l	 */
    178 #define ADDI_R12_R12	0x398c0000	/* addi %r12,%r12,off@l	 */
    179 #define ADDIS_R2_R2	0x3c420000	/* addis %r2,%r2,off@ha	 */
    180 #define ADDI_R2_R2	0x38420000	/* addi	 %r2,%r2,off@l	 */
    181 
    182 #define XOR_R2_R12_R12	0x7d826278	/* xor	 %r2,%r12,%r12	 */
    183 #define ADD_R11_R11_R2	0x7d6b1214	/* add	 %r11,%r11,%r2	 */
    184 #define XOR_R11_R12_R12	0x7d8b6278	/* xor	 %r11,%r12,%r12	 */
    185 #define ADD_R2_R2_R11	0x7c425a14	/* add	 %r2,%r2,%r11	 */
    186 #define CMPLDI_R2_0	0x28220000	/* cmpldi %r2,0		 */
    187 #define BNECTR		0x4ca20420	/* bnectr+		 */
    188 #define BNECTR_P4	0x4ce20420	/* bnectr+		 */
    189 
    190 #define LD_R12_0R2	0xe9820000	/* ld	 %r12,xxx+0(%r2) */
    191 #define LD_R11_0R2	0xe9620000	/* ld	 %r11,xxx+0(%r2) */
    192 #define LD_R2_0R2	0xe8420000	/* ld	 %r2,xxx+0(%r2)	 */
    193 
    194 #define LD_R2_0R1	0xe8410000	/* ld	 %r2,0(%r1)	 */
    195 #define LD_R2_0R12	0xe84c0000	/* ld	 %r2,0(%r12)	 */
    196 #define ADD_R2_R2_R12	0x7c426214	/* add	 %r2,%r2,%r12	 */
    197 
    198 #define LI_R11_0	0x39600000	/* li    %r11,0		*/
    199 #define LIS_R2		0x3c400000	/* lis %r2,xxx@ha	  */
    200 #define LIS_R11		0x3d600000	/* lis %r11,xxx@ha	  */
    201 #define LIS_R12		0x3d800000	/* lis %r12,xxx@ha	  */
    202 #define ADDIS_R2_R12	0x3c4c0000	/* addis %r2,%r12,xxx@ha  */
    203 #define ADDIS_R12_R2	0x3d820000	/* addis %r12,%r2,xxx@ha  */
    204 #define ADDIS_R12_R11	0x3d8b0000	/* addis %r12,%r11,xxx@ha */
    205 #define ADDIS_R12_R12	0x3d8c0000	/* addis %r12,%r12,xxx@ha */
    206 #define ORIS_R12_R12_0	0x658c0000	/* oris  %r12,%r12,xxx@hi */
    207 #define ORI_R11_R11_0	0x616b0000	/* ori   %r11,%r11,xxx@l  */
    208 #define ORI_R12_R12_0	0x618c0000	/* ori   %r12,%r12,xxx@l  */
    209 #define LD_R12_0R12	0xe98c0000	/* ld	 %r12,xxx@l(%r12) */
    210 #define SLDI_R11_R11_34	0x796b1746	/* sldi  %r11,%r11,34     */
    211 #define SLDI_R12_R12_32	0x799c07c6	/* sldi  %r12,%r12,32     */
    212 #define LDX_R12_R11_R12 0x7d8b602a	/* ldx   %r12,%r11,%r12   */
    213 #define ADD_R12_R11_R12 0x7d8b6214	/* add   %r12,%r11,%r12   */
    214 #define PADDI_R12_PC	0x0610000039800000ULL
    215 #define PLD_R12_PC	0x04100000e5800000ULL
    216 #define PNOP		0x0700000000000000ULL
    217 
    218 /* __glink_PLTresolve stub instructions.  We enter with the index in
    219    R0 for ELFv1, and the address of a glink branch in R12 for ELFv2.  */
    220 #define GLINK_PLTRESOLVE_SIZE(htab)			\
    221   (8u + (htab->opd_abi ? 11 * 4 : htab->has_plt_localentry0 ? 14 * 4 : 13 * 4))
    222 					/* 0:				*/
    223 					/*  .quad plt0-1f		*/
    224 					/* __glink:			*/
    225 #define MFLR_R12	0x7d8802a6	/*  mflr %12			*/
    226 #define BCL_20_31	0x429f0005	/*  bcl 20,31,1f		*/
    227 					/* 1:				*/
    228 #define MFLR_R11	0x7d6802a6	/*  mflr %11			*/
    229 					/*  ld %2,(0b-1b)(%11)		*/
    230 #define MTLR_R12	0x7d8803a6	/*  mtlr %12			*/
    231 #define ADD_R11_R2_R11	0x7d625a14	/*  add %11,%2,%11		*/
    232 					/*  ld %12,0(%11)		*/
    233 					/*  ld %2,8(%11)		*/
    234 					/*  mtctr %12			*/
    235 					/*  ld %11,16(%11)		*/
    236 					/*  bctr			*/
    237 
    238 #define MFLR_R0		0x7c0802a6	/* mflr %r0			*/
    239 #define MTLR_R0		0x7c0803a6	/* mtlr %r0			*/
    240 #define SUB_R12_R12_R11	0x7d8b6050	/* subf %r12,%r11,%r12		*/
    241 #define ADDI_R0_R12	0x380c0000	/* addi %r0,%r12,0		*/
    242 #define SRDI_R0_R0_2	0x7800f082	/* rldicl %r0,%r0,62,2		*/
    243 #define LD_R0_0R11	0xe80b0000	/* ld %r0,0(%r11)		*/
    244 #define ADD_R11_R0_R11	0x7d605a14	/* add %r11,%r0,%r11		*/
    245 
    246 /* Pad with this.  */
    247 #define NOP		0x60000000
    248 
    249 /* Some other nops.  */
    250 #define CROR_151515	0x4def7b82
    251 #define CROR_313131	0x4ffffb82
    252 
    253 /* .glink entries for the first 32k functions are two instructions.  */
    254 #define LI_R0_0		0x38000000	/* li    %r0,0		*/
    255 #define B_DOT		0x48000000	/* b     .		*/
    256 
    257 /* After that, we need two instructions to load the index, followed by
    258    a branch.  */
    259 #define LIS_R0_0	0x3c000000	/* lis   %r0,0		*/
    260 #define ORI_R0_R0_0	0x60000000	/* ori	 %r0,%r0,0	*/
    261 
    262 /* Instructions used by the save and restore reg functions.  */
    263 #define STD_R0_0R1	0xf8010000	/* std   %r0,0(%r1)	*/
    264 #define STD_R0_0R12	0xf80c0000	/* std   %r0,0(%r12)	*/
    265 #define LD_R0_0R1	0xe8010000	/* ld    %r0,0(%r1)	*/
    266 #define LD_R0_0R12	0xe80c0000	/* ld    %r0,0(%r12)	*/
    267 #define STFD_FR0_0R1	0xd8010000	/* stfd  %fr0,0(%r1)	*/
    268 #define LFD_FR0_0R1	0xc8010000	/* lfd   %fr0,0(%r1)	*/
    269 #define LI_R12_0	0x39800000	/* li    %r12,0		*/
    270 #define STVX_VR0_R12_R0	0x7c0c01ce	/* stvx  %v0,%r12,%r0	*/
    271 #define LVX_VR0_R12_R0	0x7c0c00ce	/* lvx   %v0,%r12,%r0	*/
    272 #define MTLR_R0		0x7c0803a6	/* mtlr  %r0		*/
    273 #define BLR		0x4e800020	/* blr			*/
    274 
    275 /* Since .opd is an array of descriptors and each entry will end up
    276    with identical R_PPC64_RELATIVE relocs, there is really no need to
    277    propagate .opd relocs;  The dynamic linker should be taught to
    278    relocate .opd without reloc entries.  */
    279 #ifndef NO_OPD_RELOCS
    280 #define NO_OPD_RELOCS 0
    281 #endif
    282 
    283 #ifndef ARRAY_SIZE
    284 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
    285 #endif
    286 
    287 static inline int
    288 abiversion (bfd *abfd)
    289 {
    290   return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
    291 }
    292 
    293 static inline void
    294 set_abiversion (bfd *abfd, int ver)
    295 {
    296   elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
    297   elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
    298 }
    299 
    300 /* Relocation HOWTO's.  */
    302 /* Like other ELF RELA targets that don't apply multiple
    303    field-altering relocations to the same localation, src_mask is
    304    always zero and pcrel_offset is the same as pc_relative.
    305    PowerPC can always use a zero bitpos, even when the field is not at
    306    the LSB.  For example, a REL24 could use rightshift=2, bisize=24
    307    and bitpos=2 which matches the ABI description, or as we do here,
    308    rightshift=0, bitsize=26 and bitpos=0.  */
    309 #define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
    310 	    complain, special_func)				\
    311   HOWTO (type, rightshift, size, bitsize, pc_relative, 0,	\
    312 	 complain_overflow_ ## complain, special_func,		\
    313 	 #type, false, 0, mask, pc_relative)
    314 
    315 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
    316 
    317 static reloc_howto_type ppc64_elf_howto_raw[] =
    318 {
    319   /* This reloc does nothing.  */
    320   HOW (R_PPC64_NONE, 0, 0, 0, 0, false, dont,
    321        bfd_elf_generic_reloc),
    322 
    323   /* A standard 32 bit relocation.  */
    324   HOW (R_PPC64_ADDR32, 4, 32, 0xffffffff, 0, false, bitfield,
    325        bfd_elf_generic_reloc),
    326 
    327   /* An absolute 26 bit branch; the lower two bits must be zero.
    328      FIXME: we don't check that, we just clear them.  */
    329   HOW (R_PPC64_ADDR24, 4, 26, 0x03fffffc, 0, false, bitfield,
    330        bfd_elf_generic_reloc),
    331 
    332   /* A standard 16 bit relocation.  */
    333   HOW (R_PPC64_ADDR16, 2, 16, 0xffff, 0, false, bitfield,
    334        bfd_elf_generic_reloc),
    335 
    336   /* A 16 bit relocation without overflow.  */
    337   HOW (R_PPC64_ADDR16_LO, 2, 16, 0xffff, 0, false, dont,
    338        bfd_elf_generic_reloc),
    339 
    340   /* Bits 16-31 of an address.  */
    341   HOW (R_PPC64_ADDR16_HI, 2, 16, 0xffff, 16, false, signed,
    342        bfd_elf_generic_reloc),
    343 
    344   /* Bits 16-31 of an address, plus 1 if the contents of the low 16
    345      bits, treated as a signed number, is negative.  */
    346   HOW (R_PPC64_ADDR16_HA, 2, 16, 0xffff, 16, false, signed,
    347        ppc64_elf_ha_reloc),
    348 
    349   /* An absolute 16 bit branch; the lower two bits must be zero.
    350      FIXME: we don't check that, we just clear them.  */
    351   HOW (R_PPC64_ADDR14, 4, 16, 0x0000fffc, 0, false, signed,
    352        ppc64_elf_branch_reloc),
    353 
    354   /* An absolute 16 bit branch, for which bit 10 should be set to
    355      indicate that the branch is expected to be taken.  The lower two
    356      bits must be zero.  */
    357   HOW (R_PPC64_ADDR14_BRTAKEN, 4, 16, 0x0000fffc, 0, false, signed,
    358        ppc64_elf_brtaken_reloc),
    359 
    360   /* An absolute 16 bit branch, for which bit 10 should be set to
    361      indicate that the branch is not expected to be taken.  The lower
    362      two bits must be zero.  */
    363   HOW (R_PPC64_ADDR14_BRNTAKEN, 4, 16, 0x0000fffc, 0, false, signed,
    364        ppc64_elf_brtaken_reloc),
    365 
    366   /* A relative 26 bit branch; the lower two bits must be zero.  */
    367   HOW (R_PPC64_REL24, 4, 26, 0x03fffffc, 0, true, signed,
    368        ppc64_elf_branch_reloc),
    369 
    370   /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer.  */
    371   HOW (R_PPC64_REL24_NOTOC, 4, 26, 0x03fffffc, 0, true, signed,
    372        ppc64_elf_branch_reloc),
    373 
    374   /* Another variant, when p10 insns can't be used on stubs.  */
    375   HOW (R_PPC64_REL24_P9NOTOC, 4, 26, 0x03fffffc, 0, true, signed,
    376        ppc64_elf_branch_reloc),
    377 
    378   /* A relative 16 bit branch; the lower two bits must be zero.  */
    379   HOW (R_PPC64_REL14, 4, 16, 0x0000fffc, 0, true, signed,
    380        ppc64_elf_branch_reloc),
    381 
    382   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
    383      the branch is expected to be taken.  The lower two bits must be
    384      zero.  */
    385   HOW (R_PPC64_REL14_BRTAKEN, 4, 16, 0x0000fffc, 0, true, signed,
    386        ppc64_elf_brtaken_reloc),
    387 
    388   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
    389      the branch is not expected to be taken.  The lower two bits must
    390      be zero.  */
    391   HOW (R_PPC64_REL14_BRNTAKEN, 4, 16, 0x0000fffc, 0, true, signed,
    392        ppc64_elf_brtaken_reloc),
    393 
    394   /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
    395      symbol.  */
    396   HOW (R_PPC64_GOT16, 2, 16, 0xffff, 0, false, signed,
    397        ppc64_elf_unhandled_reloc),
    398 
    399   /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
    400      the symbol.  */
    401   HOW (R_PPC64_GOT16_LO, 2, 16, 0xffff, 0, false, dont,
    402        ppc64_elf_unhandled_reloc),
    403 
    404   /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
    405      the symbol.  */
    406   HOW (R_PPC64_GOT16_HI, 2, 16, 0xffff, 16, false, signed,
    407        ppc64_elf_unhandled_reloc),
    408 
    409   /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
    410      the symbol.  */
    411   HOW (R_PPC64_GOT16_HA, 2, 16, 0xffff, 16, false, signed,
    412        ppc64_elf_unhandled_reloc),
    413 
    414   /* This is used only by the dynamic linker.  The symbol should exist
    415      both in the object being run and in some shared library.  The
    416      dynamic linker copies the data addressed by the symbol from the
    417      shared library into the object, because the object being
    418      run has to have the data at some particular address.  */
    419   HOW (R_PPC64_COPY, 0, 0, 0, 0, false, dont,
    420        ppc64_elf_unhandled_reloc),
    421 
    422   /* Like R_PPC64_ADDR64, but used when setting global offset table
    423      entries.  */
    424   HOW (R_PPC64_GLOB_DAT, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
    425        ppc64_elf_unhandled_reloc),
    426 
    427   /* Created by the link editor.  Marks a procedure linkage table
    428      entry for a symbol.  */
    429   HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, false, dont,
    430        ppc64_elf_unhandled_reloc),
    431 
    432   /* Used only by the dynamic linker.  When the object is run, this
    433      doubleword64 is set to the load address of the object, plus the
    434      addend.  */
    435   HOW (R_PPC64_RELATIVE, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
    436        bfd_elf_generic_reloc),
    437 
    438   /* Like R_PPC64_ADDR32, but may be unaligned.  */
    439   HOW (R_PPC64_UADDR32, 4, 32, 0xffffffff, 0, false, bitfield,
    440        bfd_elf_generic_reloc),
    441 
    442   /* Like R_PPC64_ADDR16, but may be unaligned.  */
    443   HOW (R_PPC64_UADDR16, 2, 16, 0xffff, 0, false, bitfield,
    444        bfd_elf_generic_reloc),
    445 
    446   /* 32-bit PC relative.  */
    447   HOW (R_PPC64_REL32, 4, 32, 0xffffffff, 0, true, signed,
    448        bfd_elf_generic_reloc),
    449 
    450   /* 32-bit relocation to the symbol's procedure linkage table.  */
    451   HOW (R_PPC64_PLT32, 4, 32, 0xffffffff, 0, false, bitfield,
    452        ppc64_elf_unhandled_reloc),
    453 
    454   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
    455      FIXME: R_PPC64_PLTREL32 not supported.  */
    456   HOW (R_PPC64_PLTREL32, 4, 32, 0xffffffff, 0, true, signed,
    457        ppc64_elf_unhandled_reloc),
    458 
    459   /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
    460      the symbol.  */
    461   HOW (R_PPC64_PLT16_LO, 2, 16, 0xffff, 0, false, dont,
    462        ppc64_elf_unhandled_reloc),
    463 
    464   /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
    465      the symbol.  */
    466   HOW (R_PPC64_PLT16_HI, 2, 16, 0xffff, 16, false, signed,
    467        ppc64_elf_unhandled_reloc),
    468 
    469   /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
    470      the symbol.  */
    471   HOW (R_PPC64_PLT16_HA, 2, 16, 0xffff, 16, false, signed,
    472        ppc64_elf_unhandled_reloc),
    473 
    474   /* 16-bit section relative relocation.  */
    475   HOW (R_PPC64_SECTOFF, 2, 16, 0xffff, 0, false, signed,
    476        ppc64_elf_sectoff_reloc),
    477 
    478   /* Like R_PPC64_SECTOFF, but no overflow warning.  */
    479   HOW (R_PPC64_SECTOFF_LO, 2, 16, 0xffff, 0, false, dont,
    480        ppc64_elf_sectoff_reloc),
    481 
    482   /* 16-bit upper half section relative relocation.  */
    483   HOW (R_PPC64_SECTOFF_HI, 2, 16, 0xffff, 16, false, signed,
    484        ppc64_elf_sectoff_reloc),
    485 
    486   /* 16-bit upper half adjusted section relative relocation.  */
    487   HOW (R_PPC64_SECTOFF_HA, 2, 16, 0xffff, 16, false, signed,
    488        ppc64_elf_sectoff_ha_reloc),
    489 
    490   /* Like R_PPC64_REL24 without touching the two least significant bits.  */
    491   HOW (R_PPC64_REL30, 4, 30, 0xfffffffc, 2, true, dont,
    492        bfd_elf_generic_reloc),
    493 
    494   /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
    495 
    496   /* A standard 64-bit relocation.  */
    497   HOW (R_PPC64_ADDR64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
    498        bfd_elf_generic_reloc),
    499 
    500   /* The bits 32-47 of an address.  */
    501   HOW (R_PPC64_ADDR16_HIGHER, 2, 16, 0xffff, 32, false, dont,
    502        bfd_elf_generic_reloc),
    503 
    504   /* The bits 32-47 of an address, plus 1 if the contents of the low
    505      16 bits, treated as a signed number, is negative.  */
    506   HOW (R_PPC64_ADDR16_HIGHERA, 2, 16, 0xffff, 32, false, dont,
    507        ppc64_elf_ha_reloc),
    508 
    509   /* The bits 48-63 of an address.  */
    510   HOW (R_PPC64_ADDR16_HIGHEST, 2, 16, 0xffff, 48, false, dont,
    511        bfd_elf_generic_reloc),
    512 
    513   /* The bits 48-63 of an address, plus 1 if the contents of the low
    514      16 bits, treated as a signed number, is negative.  */
    515   HOW (R_PPC64_ADDR16_HIGHESTA, 2, 16, 0xffff, 48, false, dont,
    516        ppc64_elf_ha_reloc),
    517 
    518   /* Like ADDR64, but may be unaligned.  */
    519   HOW (R_PPC64_UADDR64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
    520        bfd_elf_generic_reloc),
    521 
    522   /* 64-bit relative relocation.  */
    523   HOW (R_PPC64_REL64, 8, 64, 0xffffffffffffffffULL, 0, true, dont,
    524        bfd_elf_generic_reloc),
    525 
    526   /* 64-bit relocation to the symbol's procedure linkage table.  */
    527   HOW (R_PPC64_PLT64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
    528        ppc64_elf_unhandled_reloc),
    529 
    530   /* 64-bit PC relative relocation to the symbol's procedure linkage
    531      table.  */
    532   /* FIXME: R_PPC64_PLTREL64 not supported.  */
    533   HOW (R_PPC64_PLTREL64, 8, 64, 0xffffffffffffffffULL, 0, true, dont,
    534        ppc64_elf_unhandled_reloc),
    535 
    536   /* 16 bit TOC-relative relocation.  */
    537   /* R_PPC64_TOC16	  47	   half16*	S + A - .TOC.  */
    538   HOW (R_PPC64_TOC16, 2, 16, 0xffff, 0, false, signed,
    539        ppc64_elf_toc_reloc),
    540 
    541   /* 16 bit TOC-relative relocation without overflow.  */
    542   /* R_PPC64_TOC16_LO	  48	   half16	 #lo (S + A - .TOC.)  */
    543   HOW (R_PPC64_TOC16_LO, 2, 16, 0xffff, 0, false, dont,
    544        ppc64_elf_toc_reloc),
    545 
    546   /* 16 bit TOC-relative relocation, high 16 bits.  */
    547   /* R_PPC64_TOC16_HI	  49	   half16	 #hi (S + A - .TOC.)  */
    548   HOW (R_PPC64_TOC16_HI, 2, 16, 0xffff, 16, false, signed,
    549        ppc64_elf_toc_reloc),
    550 
    551   /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
    552      contents of the low 16 bits, treated as a signed number, is
    553      negative.  */
    554   /* R_PPC64_TOC16_HA	  50	   half16	 #ha (S + A - .TOC.)  */
    555   HOW (R_PPC64_TOC16_HA, 2, 16, 0xffff, 16, false, signed,
    556        ppc64_elf_toc_ha_reloc),
    557 
    558   /* 64-bit relocation; insert value of TOC base (.TOC.).  */
    559   /* R_PPC64_TOC		  51	   doubleword64	 .TOC.  */
    560   HOW (R_PPC64_TOC, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
    561        ppc64_elf_toc64_reloc),
    562 
    563   /* Like R_PPC64_GOT16, but also informs the link editor that the
    564      value to relocate may (!) refer to a PLT entry which the link
    565      editor (a) may replace with the symbol value.  If the link editor
    566      is unable to fully resolve the symbol, it may (b) create a PLT
    567      entry and store the address to the new PLT entry in the GOT.
    568      This permits lazy resolution of function symbols at run time.
    569      The link editor may also skip all of this and just (c) emit a
    570      R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
    571   /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
    572     HOW (R_PPC64_PLTGOT16, 2, 16, 0xffff, 0, false,signed,
    573 	  ppc64_elf_unhandled_reloc),
    574 
    575   /* Like R_PPC64_PLTGOT16, but without overflow.  */
    576   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
    577   HOW (R_PPC64_PLTGOT16_LO, 2, 16, 0xffff, 0, false, dont,
    578        ppc64_elf_unhandled_reloc),
    579 
    580   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
    581   /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
    582   HOW (R_PPC64_PLTGOT16_HI, 2, 16, 0xffff, 16, false, signed,
    583        ppc64_elf_unhandled_reloc),
    584 
    585   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
    586      1 if the contents of the low 16 bits, treated as a signed number,
    587      is negative.  */
    588   /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
    589   HOW (R_PPC64_PLTGOT16_HA, 2, 16, 0xffff, 16, false, signed,
    590        ppc64_elf_unhandled_reloc),
    591 
    592   /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
    593   HOW (R_PPC64_ADDR16_DS, 2, 16, 0xfffc, 0, false, signed,
    594        bfd_elf_generic_reloc),
    595 
    596   /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
    597   HOW (R_PPC64_ADDR16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
    598        bfd_elf_generic_reloc),
    599 
    600   /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
    601   HOW (R_PPC64_GOT16_DS, 2, 16, 0xfffc, 0, false, signed,
    602        ppc64_elf_unhandled_reloc),
    603 
    604   /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
    605   HOW (R_PPC64_GOT16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
    606        ppc64_elf_unhandled_reloc),
    607 
    608   /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
    609   HOW (R_PPC64_PLT16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
    610        ppc64_elf_unhandled_reloc),
    611 
    612   /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
    613   HOW (R_PPC64_SECTOFF_DS, 2, 16, 0xfffc, 0, false, signed,
    614        ppc64_elf_sectoff_reloc),
    615 
    616   /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
    617   HOW (R_PPC64_SECTOFF_LO_DS, 2, 16, 0xfffc, 0, false, dont,
    618        ppc64_elf_sectoff_reloc),
    619 
    620   /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
    621   HOW (R_PPC64_TOC16_DS, 2, 16, 0xfffc, 0, false, signed,
    622        ppc64_elf_toc_reloc),
    623 
    624   /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
    625   HOW (R_PPC64_TOC16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
    626        ppc64_elf_toc_reloc),
    627 
    628   /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
    629   /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
    630   HOW (R_PPC64_PLTGOT16_DS, 2, 16, 0xfffc, 0, false, signed,
    631        ppc64_elf_unhandled_reloc),
    632 
    633   /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
    634   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
    635   HOW (R_PPC64_PLTGOT16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
    636        ppc64_elf_unhandled_reloc),
    637 
    638   /* Marker relocs for TLS.  */
    639   HOW (R_PPC64_TLS, 4, 32, 0, 0, false, dont,
    640        bfd_elf_generic_reloc),
    641 
    642   HOW (R_PPC64_TLSGD, 4, 32, 0, 0, false, dont,
    643        bfd_elf_generic_reloc),
    644 
    645   HOW (R_PPC64_TLSLD, 4, 32, 0, 0, false, dont,
    646        bfd_elf_generic_reloc),
    647 
    648   /* Marker reloc for optimizing r2 save in prologue rather than on
    649      each plt call stub.  */
    650   HOW (R_PPC64_TOCSAVE, 4, 32, 0, 0, false, dont,
    651        bfd_elf_generic_reloc),
    652 
    653   /* Marker relocs on inline plt call instructions.  */
    654   HOW (R_PPC64_PLTSEQ, 4, 32, 0, 0, false, dont,
    655        bfd_elf_generic_reloc),
    656 
    657   HOW (R_PPC64_PLTCALL, 4, 32, 0, 0, false, dont,
    658        bfd_elf_generic_reloc),
    659 
    660   /* Computes the load module index of the load module that contains the
    661      definition of its TLS sym.  */
    662   HOW (R_PPC64_DTPMOD64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
    663        ppc64_elf_unhandled_reloc),
    664 
    665   /* Computes a dtv-relative displacement, the difference between the value
    666      of sym+add and the base address of the thread-local storage block that
    667      contains the definition of sym, minus 0x8000.  */
    668   HOW (R_PPC64_DTPREL64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
    669        ppc64_elf_unhandled_reloc),
    670 
    671   /* A 16 bit dtprel reloc.  */
    672   HOW (R_PPC64_DTPREL16, 2, 16, 0xffff, 0, false, signed,
    673        ppc64_elf_unhandled_reloc),
    674 
    675   /* Like DTPREL16, but no overflow.  */
    676   HOW (R_PPC64_DTPREL16_LO, 2, 16, 0xffff, 0, false, dont,
    677        ppc64_elf_unhandled_reloc),
    678 
    679   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
    680   HOW (R_PPC64_DTPREL16_HI, 2, 16, 0xffff, 16, false, signed,
    681        ppc64_elf_unhandled_reloc),
    682 
    683   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
    684   HOW (R_PPC64_DTPREL16_HA, 2, 16, 0xffff, 16, false, signed,
    685        ppc64_elf_unhandled_reloc),
    686 
    687   /* Like DTPREL16_HI, but next higher group of 16 bits.  */
    688   HOW (R_PPC64_DTPREL16_HIGHER, 2, 16, 0xffff, 32, false, dont,
    689        ppc64_elf_unhandled_reloc),
    690 
    691   /* Like DTPREL16_HIGHER, but adjust for low 16 bits.  */
    692   HOW (R_PPC64_DTPREL16_HIGHERA, 2, 16, 0xffff, 32, false, dont,
    693        ppc64_elf_unhandled_reloc),
    694 
    695   /* Like DTPREL16_HIGHER, but next higher group of 16 bits.  */
    696   HOW (R_PPC64_DTPREL16_HIGHEST, 2, 16, 0xffff, 48, false, dont,
    697        ppc64_elf_unhandled_reloc),
    698 
    699   /* Like DTPREL16_HIGHEST, but adjust for low 16 bits.  */
    700   HOW (R_PPC64_DTPREL16_HIGHESTA, 2, 16, 0xffff, 48, false, dont,
    701        ppc64_elf_unhandled_reloc),
    702 
    703   /* Like DTPREL16, but for insns with a DS field.  */
    704   HOW (R_PPC64_DTPREL16_DS, 2, 16, 0xfffc, 0, false, signed,
    705        ppc64_elf_unhandled_reloc),
    706 
    707   /* Like DTPREL16_DS, but no overflow.  */
    708   HOW (R_PPC64_DTPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
    709        ppc64_elf_unhandled_reloc),
    710 
    711   /* Computes a tp-relative displacement, the difference between the value of
    712      sym+add and the value of the thread pointer (r13).  */
    713   HOW (R_PPC64_TPREL64, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
    714        ppc64_elf_unhandled_reloc),
    715 
    716   /* A 16 bit tprel reloc.  */
    717   HOW (R_PPC64_TPREL16, 2, 16, 0xffff, 0, false, signed,
    718        ppc64_elf_unhandled_reloc),
    719 
    720   /* Like TPREL16, but no overflow.  */
    721   HOW (R_PPC64_TPREL16_LO, 2, 16, 0xffff, 0, false, dont,
    722        ppc64_elf_unhandled_reloc),
    723 
    724   /* Like TPREL16_LO, but next higher group of 16 bits.  */
    725   HOW (R_PPC64_TPREL16_HI, 2, 16, 0xffff, 16, false, signed,
    726        ppc64_elf_unhandled_reloc),
    727 
    728   /* Like TPREL16_HI, but adjust for low 16 bits.  */
    729   HOW (R_PPC64_TPREL16_HA, 2, 16, 0xffff, 16, false, signed,
    730        ppc64_elf_unhandled_reloc),
    731 
    732   /* Like TPREL16_HI, but next higher group of 16 bits.  */
    733   HOW (R_PPC64_TPREL16_HIGHER, 2, 16, 0xffff, 32, false, dont,
    734        ppc64_elf_unhandled_reloc),
    735 
    736   /* Like TPREL16_HIGHER, but adjust for low 16 bits.  */
    737   HOW (R_PPC64_TPREL16_HIGHERA, 2, 16, 0xffff, 32, false, dont,
    738        ppc64_elf_unhandled_reloc),
    739 
    740   /* Like TPREL16_HIGHER, but next higher group of 16 bits.  */
    741   HOW (R_PPC64_TPREL16_HIGHEST, 2, 16, 0xffff, 48, false, dont,
    742        ppc64_elf_unhandled_reloc),
    743 
    744   /* Like TPREL16_HIGHEST, but adjust for low 16 bits.  */
    745   HOW (R_PPC64_TPREL16_HIGHESTA, 2, 16, 0xffff, 48, false, dont,
    746        ppc64_elf_unhandled_reloc),
    747 
    748   /* Like TPREL16, but for insns with a DS field.  */
    749   HOW (R_PPC64_TPREL16_DS, 2, 16, 0xfffc, 0, false, signed,
    750        ppc64_elf_unhandled_reloc),
    751 
    752   /* Like TPREL16_DS, but no overflow.  */
    753   HOW (R_PPC64_TPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
    754        ppc64_elf_unhandled_reloc),
    755 
    756   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
    757      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
    758      to the first entry relative to the TOC base (r2).  */
    759   HOW (R_PPC64_GOT_TLSGD16, 2, 16, 0xffff, 0, false, signed,
    760        ppc64_elf_unhandled_reloc),
    761 
    762   /* Like GOT_TLSGD16, but no overflow.  */
    763   HOW (R_PPC64_GOT_TLSGD16_LO, 2, 16, 0xffff, 0, false, dont,
    764        ppc64_elf_unhandled_reloc),
    765 
    766   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
    767   HOW (R_PPC64_GOT_TLSGD16_HI, 2, 16, 0xffff, 16, false, signed,
    768        ppc64_elf_unhandled_reloc),
    769 
    770   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
    771   HOW (R_PPC64_GOT_TLSGD16_HA, 2, 16, 0xffff, 16, false, signed,
    772        ppc64_elf_unhandled_reloc),
    773 
    774   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
    775      with values (sym+add)@dtpmod and zero, and computes the offset to the
    776      first entry relative to the TOC base (r2).  */
    777   HOW (R_PPC64_GOT_TLSLD16, 2, 16, 0xffff, 0, false, signed,
    778        ppc64_elf_unhandled_reloc),
    779 
    780   /* Like GOT_TLSLD16, but no overflow.  */
    781   HOW (R_PPC64_GOT_TLSLD16_LO, 2, 16, 0xffff, 0, false, dont,
    782        ppc64_elf_unhandled_reloc),
    783 
    784   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
    785   HOW (R_PPC64_GOT_TLSLD16_HI, 2, 16, 0xffff, 16, false, signed,
    786        ppc64_elf_unhandled_reloc),
    787 
    788   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
    789   HOW (R_PPC64_GOT_TLSLD16_HA, 2, 16, 0xffff, 16, false, signed,
    790        ppc64_elf_unhandled_reloc),
    791 
    792   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
    793      the offset to the entry relative to the TOC base (r2).  */
    794   HOW (R_PPC64_GOT_DTPREL16_DS, 2, 16, 0xfffc, 0, false, signed,
    795        ppc64_elf_unhandled_reloc),
    796 
    797   /* Like GOT_DTPREL16_DS, but no overflow.  */
    798   HOW (R_PPC64_GOT_DTPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
    799        ppc64_elf_unhandled_reloc),
    800 
    801   /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits.  */
    802   HOW (R_PPC64_GOT_DTPREL16_HI, 2, 16, 0xffff, 16, false, signed,
    803        ppc64_elf_unhandled_reloc),
    804 
    805   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
    806   HOW (R_PPC64_GOT_DTPREL16_HA, 2, 16, 0xffff, 16, false, signed,
    807        ppc64_elf_unhandled_reloc),
    808 
    809   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
    810      offset to the entry relative to the TOC base (r2).  */
    811   HOW (R_PPC64_GOT_TPREL16_DS, 2, 16, 0xfffc, 0, false, signed,
    812        ppc64_elf_unhandled_reloc),
    813 
    814   /* Like GOT_TPREL16_DS, but no overflow.  */
    815   HOW (R_PPC64_GOT_TPREL16_LO_DS, 2, 16, 0xfffc, 0, false, dont,
    816        ppc64_elf_unhandled_reloc),
    817 
    818   /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits.  */
    819   HOW (R_PPC64_GOT_TPREL16_HI, 2, 16, 0xffff, 16, false, signed,
    820        ppc64_elf_unhandled_reloc),
    821 
    822   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
    823   HOW (R_PPC64_GOT_TPREL16_HA, 2, 16, 0xffff, 16, false, signed,
    824        ppc64_elf_unhandled_reloc),
    825 
    826   HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, false, dont,
    827        ppc64_elf_unhandled_reloc),
    828 
    829   HOW (R_PPC64_IRELATIVE, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
    830        bfd_elf_generic_reloc),
    831 
    832   /* A 16 bit relative relocation.  */
    833   HOW (R_PPC64_REL16, 2, 16, 0xffff, 0, true, signed,
    834        bfd_elf_generic_reloc),
    835 
    836   /* A 16 bit relative relocation without overflow.  */
    837   HOW (R_PPC64_REL16_LO, 2, 16, 0xffff, 0, true, dont,
    838        bfd_elf_generic_reloc),
    839 
    840   /* The high order 16 bits of a relative address.  */
    841   HOW (R_PPC64_REL16_HI, 2, 16, 0xffff, 16, true, signed,
    842        bfd_elf_generic_reloc),
    843 
    844   /* The high order 16 bits of a relative address, plus 1 if the contents of
    845      the low 16 bits, treated as a signed number, is negative.  */
    846   HOW (R_PPC64_REL16_HA, 2, 16, 0xffff, 16, true, signed,
    847        ppc64_elf_ha_reloc),
    848 
    849   HOW (R_PPC64_REL16_HIGH, 2, 16, 0xffff, 16, true, dont,
    850        bfd_elf_generic_reloc),
    851 
    852   HOW (R_PPC64_REL16_HIGHA, 2, 16, 0xffff, 16, true, dont,
    853        ppc64_elf_ha_reloc),
    854 
    855   HOW (R_PPC64_REL16_HIGHER, 2, 16, 0xffff, 32, true, dont,
    856        bfd_elf_generic_reloc),
    857 
    858   HOW (R_PPC64_REL16_HIGHERA, 2, 16, 0xffff, 32, true, dont,
    859        ppc64_elf_ha_reloc),
    860 
    861   HOW (R_PPC64_REL16_HIGHEST, 2, 16, 0xffff, 48, true, dont,
    862        bfd_elf_generic_reloc),
    863 
    864   HOW (R_PPC64_REL16_HIGHESTA, 2, 16, 0xffff, 48, true, dont,
    865        ppc64_elf_ha_reloc),
    866 
    867   /* Like R_PPC64_REL16_HA but for split field in addpcis.  */
    868   HOW (R_PPC64_REL16DX_HA, 4, 16, 0x1fffc1, 16, true, signed,
    869        ppc64_elf_ha_reloc),
    870 
    871   /* A split-field reloc for addpcis, non-relative (gas internal use only).  */
    872   HOW (R_PPC64_16DX_HA, 4, 16, 0x1fffc1, 16, false, signed,
    873        ppc64_elf_ha_reloc),
    874 
    875   /* Like R_PPC64_ADDR16_HI, but no overflow.  */
    876   HOW (R_PPC64_ADDR16_HIGH, 2, 16, 0xffff, 16, false, dont,
    877        bfd_elf_generic_reloc),
    878 
    879   /* Like R_PPC64_ADDR16_HA, but no overflow.  */
    880   HOW (R_PPC64_ADDR16_HIGHA, 2, 16, 0xffff, 16, false, dont,
    881        ppc64_elf_ha_reloc),
    882 
    883   /* Like R_PPC64_DTPREL16_HI, but no overflow.  */
    884   HOW (R_PPC64_DTPREL16_HIGH, 2, 16, 0xffff, 16, false, dont,
    885        ppc64_elf_unhandled_reloc),
    886 
    887   /* Like R_PPC64_DTPREL16_HA, but no overflow.  */
    888   HOW (R_PPC64_DTPREL16_HIGHA, 2, 16, 0xffff, 16, false, dont,
    889        ppc64_elf_unhandled_reloc),
    890 
    891   /* Like R_PPC64_TPREL16_HI, but no overflow.  */
    892   HOW (R_PPC64_TPREL16_HIGH, 2, 16, 0xffff, 16, false, dont,
    893        ppc64_elf_unhandled_reloc),
    894 
    895   /* Like R_PPC64_TPREL16_HA, but no overflow.  */
    896   HOW (R_PPC64_TPREL16_HIGHA, 2, 16, 0xffff, 16, false, dont,
    897        ppc64_elf_unhandled_reloc),
    898 
    899   /* Marker reloc on ELFv2 large-model function entry.  */
    900   HOW (R_PPC64_ENTRY, 4, 32, 0, 0, false, dont,
    901        bfd_elf_generic_reloc),
    902 
    903   /* Like ADDR64, but use local entry point of function.  */
    904   HOW (R_PPC64_ADDR64_LOCAL, 8, 64, 0xffffffffffffffffULL, 0, false, dont,
    905        bfd_elf_generic_reloc),
    906 
    907   HOW (R_PPC64_PLTSEQ_NOTOC, 4, 32, 0, 0, false, dont,
    908        bfd_elf_generic_reloc),
    909 
    910   HOW (R_PPC64_PLTCALL_NOTOC, 4, 32, 0, 0, false, dont,
    911        bfd_elf_generic_reloc),
    912 
    913   HOW (R_PPC64_PCREL_OPT, 4, 32, 0, 0, false, dont,
    914        bfd_elf_generic_reloc),
    915 
    916   HOW (R_PPC64_D34, 8, 34, 0x3ffff0000ffffULL, 0, false, signed,
    917        ppc64_elf_prefix_reloc),
    918 
    919   HOW (R_PPC64_D34_LO, 8, 34, 0x3ffff0000ffffULL, 0, false, dont,
    920        ppc64_elf_prefix_reloc),
    921 
    922   HOW (R_PPC64_D34_HI30, 8, 34, 0x3ffff0000ffffULL, 34, false, dont,
    923        ppc64_elf_prefix_reloc),
    924 
    925   HOW (R_PPC64_D34_HA30, 8, 34, 0x3ffff0000ffffULL, 34, false, dont,
    926        ppc64_elf_prefix_reloc),
    927 
    928   HOW (R_PPC64_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
    929        ppc64_elf_prefix_reloc),
    930 
    931   HOW (R_PPC64_GOT_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
    932        ppc64_elf_unhandled_reloc),
    933 
    934   HOW (R_PPC64_PLT_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
    935        ppc64_elf_unhandled_reloc),
    936 
    937   HOW (R_PPC64_PLT_PCREL34_NOTOC, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
    938        ppc64_elf_unhandled_reloc),
    939 
    940   HOW (R_PPC64_TPREL34, 8, 34, 0x3ffff0000ffffULL, 0, false, signed,
    941        ppc64_elf_unhandled_reloc),
    942 
    943   HOW (R_PPC64_DTPREL34, 8, 34, 0x3ffff0000ffffULL, 0, false, signed,
    944        ppc64_elf_unhandled_reloc),
    945 
    946   HOW (R_PPC64_GOT_TLSGD_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
    947        ppc64_elf_unhandled_reloc),
    948 
    949   HOW (R_PPC64_GOT_TLSLD_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
    950        ppc64_elf_unhandled_reloc),
    951 
    952   HOW (R_PPC64_GOT_TPREL_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
    953        ppc64_elf_unhandled_reloc),
    954 
    955   HOW (R_PPC64_GOT_DTPREL_PCREL34, 8, 34, 0x3ffff0000ffffULL, 0, true, signed,
    956        ppc64_elf_unhandled_reloc),
    957 
    958   HOW (R_PPC64_ADDR16_HIGHER34, 2, 16, 0xffff, 34, false, dont,
    959        bfd_elf_generic_reloc),
    960 
    961   HOW (R_PPC64_ADDR16_HIGHERA34, 2, 16, 0xffff, 34, false, dont,
    962        ppc64_elf_ha_reloc),
    963 
    964   HOW (R_PPC64_ADDR16_HIGHEST34, 2, 16, 0xffff, 50, false, dont,
    965        bfd_elf_generic_reloc),
    966 
    967   HOW (R_PPC64_ADDR16_HIGHESTA34, 2, 16, 0xffff, 50, false, dont,
    968        ppc64_elf_ha_reloc),
    969 
    970   HOW (R_PPC64_REL16_HIGHER34, 2, 16, 0xffff, 34, true, dont,
    971        bfd_elf_generic_reloc),
    972 
    973   HOW (R_PPC64_REL16_HIGHERA34, 2, 16, 0xffff, 34, true, dont,
    974        ppc64_elf_ha_reloc),
    975 
    976   HOW (R_PPC64_REL16_HIGHEST34, 2, 16, 0xffff, 50, true, dont,
    977        bfd_elf_generic_reloc),
    978 
    979   HOW (R_PPC64_REL16_HIGHESTA34, 2, 16, 0xffff, 50, true, dont,
    980        ppc64_elf_ha_reloc),
    981 
    982   HOW (R_PPC64_D28, 8, 28, 0xfff0000ffffULL, 0, false, signed,
    983        ppc64_elf_prefix_reloc),
    984 
    985   HOW (R_PPC64_PCREL28, 8, 28, 0xfff0000ffffULL, 0, true, signed,
    986        ppc64_elf_prefix_reloc),
    987 
    988   /* GNU extension to record C++ vtable hierarchy.  */
    989   HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, false, dont,
    990        NULL),
    991 
    992   /* GNU extension to record C++ vtable member usage.  */
    993   HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, false, dont,
    994        NULL),
    995 };
    996 
    997 
    998 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
   1000    be done.  */
   1001 
   1002 static void
   1003 ppc_howto_init (void)
   1004 {
   1005   unsigned int i, type;
   1006 
   1007   for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
   1008     {
   1009       type = ppc64_elf_howto_raw[i].type;
   1010       BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
   1011       ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
   1012     }
   1013 }
   1014 
   1015 static reloc_howto_type *
   1016 ppc64_elf_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
   1017 {
   1018   enum elf_ppc64_reloc_type r = R_PPC64_NONE;
   1019 
   1020   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
   1021     /* Initialize howto table if needed.  */
   1022     ppc_howto_init ();
   1023 
   1024   switch (code)
   1025     {
   1026     default:
   1027       /* xgettext:c-format */
   1028       _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
   1029 			  (int) code);
   1030       bfd_set_error (bfd_error_bad_value);
   1031       return NULL;
   1032 
   1033     case BFD_RELOC_NONE:			r = R_PPC64_NONE;
   1034       break;
   1035     case BFD_RELOC_32:				r = R_PPC64_ADDR32;
   1036       break;
   1037     case BFD_RELOC_PPC_BA26:			r = R_PPC64_ADDR24;
   1038       break;
   1039     case BFD_RELOC_16:				r = R_PPC64_ADDR16;
   1040       break;
   1041     case BFD_RELOC_LO16:			r = R_PPC64_ADDR16_LO;
   1042       break;
   1043     case BFD_RELOC_HI16:			r = R_PPC64_ADDR16_HI;
   1044       break;
   1045     case BFD_RELOC_PPC64_ADDR16_HIGH:		r = R_PPC64_ADDR16_HIGH;
   1046       break;
   1047     case BFD_RELOC_HI16_S:			r = R_PPC64_ADDR16_HA;
   1048       break;
   1049     case BFD_RELOC_PPC64_ADDR16_HIGHA:		r = R_PPC64_ADDR16_HIGHA;
   1050       break;
   1051     case BFD_RELOC_PPC_BA16:			r = R_PPC64_ADDR14;
   1052       break;
   1053     case BFD_RELOC_PPC_BA16_BRTAKEN:		r = R_PPC64_ADDR14_BRTAKEN;
   1054       break;
   1055     case BFD_RELOC_PPC_BA16_BRNTAKEN:		r = R_PPC64_ADDR14_BRNTAKEN;
   1056       break;
   1057     case BFD_RELOC_PPC_B26:			r = R_PPC64_REL24;
   1058       break;
   1059     case BFD_RELOC_PPC64_REL24_NOTOC:		r = R_PPC64_REL24_NOTOC;
   1060       break;
   1061     case BFD_RELOC_PPC64_REL24_P9NOTOC:		r = R_PPC64_REL24_P9NOTOC;
   1062       break;
   1063     case BFD_RELOC_PPC_B16:			r = R_PPC64_REL14;
   1064       break;
   1065     case BFD_RELOC_PPC_B16_BRTAKEN:		r = R_PPC64_REL14_BRTAKEN;
   1066       break;
   1067     case BFD_RELOC_PPC_B16_BRNTAKEN:		r = R_PPC64_REL14_BRNTAKEN;
   1068       break;
   1069     case BFD_RELOC_16_GOTOFF:			r = R_PPC64_GOT16;
   1070       break;
   1071     case BFD_RELOC_LO16_GOTOFF:			r = R_PPC64_GOT16_LO;
   1072       break;
   1073     case BFD_RELOC_HI16_GOTOFF:			r = R_PPC64_GOT16_HI;
   1074       break;
   1075     case BFD_RELOC_HI16_S_GOTOFF:		r = R_PPC64_GOT16_HA;
   1076       break;
   1077     case BFD_RELOC_PPC_COPY:			r = R_PPC64_COPY;
   1078       break;
   1079     case BFD_RELOC_PPC_GLOB_DAT:		r = R_PPC64_GLOB_DAT;
   1080       break;
   1081     case BFD_RELOC_32_PCREL:			r = R_PPC64_REL32;
   1082       break;
   1083     case BFD_RELOC_32_PLTOFF:			r = R_PPC64_PLT32;
   1084       break;
   1085     case BFD_RELOC_32_PLT_PCREL:		r = R_PPC64_PLTREL32;
   1086       break;
   1087     case BFD_RELOC_LO16_PLTOFF:			r = R_PPC64_PLT16_LO;
   1088       break;
   1089     case BFD_RELOC_HI16_PLTOFF:			r = R_PPC64_PLT16_HI;
   1090       break;
   1091     case BFD_RELOC_HI16_S_PLTOFF:		r = R_PPC64_PLT16_HA;
   1092       break;
   1093     case BFD_RELOC_16_BASEREL:			r = R_PPC64_SECTOFF;
   1094       break;
   1095     case BFD_RELOC_LO16_BASEREL:		r = R_PPC64_SECTOFF_LO;
   1096       break;
   1097     case BFD_RELOC_HI16_BASEREL:		r = R_PPC64_SECTOFF_HI;
   1098       break;
   1099     case BFD_RELOC_HI16_S_BASEREL:		r = R_PPC64_SECTOFF_HA;
   1100       break;
   1101     case BFD_RELOC_CTOR:			r = R_PPC64_ADDR64;
   1102       break;
   1103     case BFD_RELOC_64:				r = R_PPC64_ADDR64;
   1104       break;
   1105     case BFD_RELOC_PPC64_HIGHER:		r = R_PPC64_ADDR16_HIGHER;
   1106       break;
   1107     case BFD_RELOC_PPC64_HIGHER_S:		r = R_PPC64_ADDR16_HIGHERA;
   1108       break;
   1109     case BFD_RELOC_PPC64_HIGHEST:		r = R_PPC64_ADDR16_HIGHEST;
   1110       break;
   1111     case BFD_RELOC_PPC64_HIGHEST_S:		r = R_PPC64_ADDR16_HIGHESTA;
   1112       break;
   1113     case BFD_RELOC_64_PCREL:			r = R_PPC64_REL64;
   1114       break;
   1115     case BFD_RELOC_64_PLTOFF:			r = R_PPC64_PLT64;
   1116       break;
   1117     case BFD_RELOC_64_PLT_PCREL:		r = R_PPC64_PLTREL64;
   1118       break;
   1119     case BFD_RELOC_PPC_TOC16:			r = R_PPC64_TOC16;
   1120       break;
   1121     case BFD_RELOC_PPC64_TOC16_LO:		r = R_PPC64_TOC16_LO;
   1122       break;
   1123     case BFD_RELOC_PPC64_TOC16_HI:		r = R_PPC64_TOC16_HI;
   1124       break;
   1125     case BFD_RELOC_PPC64_TOC16_HA:		r = R_PPC64_TOC16_HA;
   1126       break;
   1127     case BFD_RELOC_PPC64_TOC:			r = R_PPC64_TOC;
   1128       break;
   1129     case BFD_RELOC_PPC64_PLTGOT16:		r = R_PPC64_PLTGOT16;
   1130       break;
   1131     case BFD_RELOC_PPC64_PLTGOT16_LO:		r = R_PPC64_PLTGOT16_LO;
   1132       break;
   1133     case BFD_RELOC_PPC64_PLTGOT16_HI:		r = R_PPC64_PLTGOT16_HI;
   1134       break;
   1135     case BFD_RELOC_PPC64_PLTGOT16_HA:		r = R_PPC64_PLTGOT16_HA;
   1136       break;
   1137     case BFD_RELOC_PPC64_ADDR16_DS:		r = R_PPC64_ADDR16_DS;
   1138       break;
   1139     case BFD_RELOC_PPC64_ADDR16_LO_DS:		r = R_PPC64_ADDR16_LO_DS;
   1140       break;
   1141     case BFD_RELOC_PPC64_GOT16_DS:		r = R_PPC64_GOT16_DS;
   1142       break;
   1143     case BFD_RELOC_PPC64_GOT16_LO_DS:		r = R_PPC64_GOT16_LO_DS;
   1144       break;
   1145     case BFD_RELOC_PPC64_PLT16_LO_DS:		r = R_PPC64_PLT16_LO_DS;
   1146       break;
   1147     case BFD_RELOC_PPC64_SECTOFF_DS:		r = R_PPC64_SECTOFF_DS;
   1148       break;
   1149     case BFD_RELOC_PPC64_SECTOFF_LO_DS:		r = R_PPC64_SECTOFF_LO_DS;
   1150       break;
   1151     case BFD_RELOC_PPC64_TOC16_DS:		r = R_PPC64_TOC16_DS;
   1152       break;
   1153     case BFD_RELOC_PPC64_TOC16_LO_DS:		r = R_PPC64_TOC16_LO_DS;
   1154       break;
   1155     case BFD_RELOC_PPC64_PLTGOT16_DS:		r = R_PPC64_PLTGOT16_DS;
   1156       break;
   1157     case BFD_RELOC_PPC64_PLTGOT16_LO_DS:	r = R_PPC64_PLTGOT16_LO_DS;
   1158       break;
   1159     case BFD_RELOC_PPC64_TLS_PCREL:
   1160     case BFD_RELOC_PPC_TLS:			r = R_PPC64_TLS;
   1161       break;
   1162     case BFD_RELOC_PPC_TLSGD:			r = R_PPC64_TLSGD;
   1163       break;
   1164     case BFD_RELOC_PPC_TLSLD:			r = R_PPC64_TLSLD;
   1165       break;
   1166     case BFD_RELOC_PPC_DTPMOD:			r = R_PPC64_DTPMOD64;
   1167       break;
   1168     case BFD_RELOC_PPC_TPREL16:			r = R_PPC64_TPREL16;
   1169       break;
   1170     case BFD_RELOC_PPC_TPREL16_LO:		r = R_PPC64_TPREL16_LO;
   1171       break;
   1172     case BFD_RELOC_PPC_TPREL16_HI:		r = R_PPC64_TPREL16_HI;
   1173       break;
   1174     case BFD_RELOC_PPC64_TPREL16_HIGH:		r = R_PPC64_TPREL16_HIGH;
   1175       break;
   1176     case BFD_RELOC_PPC_TPREL16_HA:		r = R_PPC64_TPREL16_HA;
   1177       break;
   1178     case BFD_RELOC_PPC64_TPREL16_HIGHA:		r = R_PPC64_TPREL16_HIGHA;
   1179       break;
   1180     case BFD_RELOC_PPC_TPREL:			r = R_PPC64_TPREL64;
   1181       break;
   1182     case BFD_RELOC_PPC_DTPREL16:		r = R_PPC64_DTPREL16;
   1183       break;
   1184     case BFD_RELOC_PPC_DTPREL16_LO:		r = R_PPC64_DTPREL16_LO;
   1185       break;
   1186     case BFD_RELOC_PPC_DTPREL16_HI:		r = R_PPC64_DTPREL16_HI;
   1187       break;
   1188     case BFD_RELOC_PPC64_DTPREL16_HIGH:		r = R_PPC64_DTPREL16_HIGH;
   1189       break;
   1190     case BFD_RELOC_PPC_DTPREL16_HA:		r = R_PPC64_DTPREL16_HA;
   1191       break;
   1192     case BFD_RELOC_PPC64_DTPREL16_HIGHA:	r = R_PPC64_DTPREL16_HIGHA;
   1193       break;
   1194     case BFD_RELOC_PPC_DTPREL:			r = R_PPC64_DTPREL64;
   1195       break;
   1196     case BFD_RELOC_PPC_GOT_TLSGD16:		r = R_PPC64_GOT_TLSGD16;
   1197       break;
   1198     case BFD_RELOC_PPC_GOT_TLSGD16_LO:		r = R_PPC64_GOT_TLSGD16_LO;
   1199       break;
   1200     case BFD_RELOC_PPC_GOT_TLSGD16_HI:		r = R_PPC64_GOT_TLSGD16_HI;
   1201       break;
   1202     case BFD_RELOC_PPC_GOT_TLSGD16_HA:		r = R_PPC64_GOT_TLSGD16_HA;
   1203       break;
   1204     case BFD_RELOC_PPC_GOT_TLSLD16:		r = R_PPC64_GOT_TLSLD16;
   1205       break;
   1206     case BFD_RELOC_PPC_GOT_TLSLD16_LO:		r = R_PPC64_GOT_TLSLD16_LO;
   1207       break;
   1208     case BFD_RELOC_PPC_GOT_TLSLD16_HI:		r = R_PPC64_GOT_TLSLD16_HI;
   1209       break;
   1210     case BFD_RELOC_PPC_GOT_TLSLD16_HA:		r = R_PPC64_GOT_TLSLD16_HA;
   1211       break;
   1212     case BFD_RELOC_PPC_GOT_TPREL16:		r = R_PPC64_GOT_TPREL16_DS;
   1213       break;
   1214     case BFD_RELOC_PPC_GOT_TPREL16_LO:		r = R_PPC64_GOT_TPREL16_LO_DS;
   1215       break;
   1216     case BFD_RELOC_PPC_GOT_TPREL16_HI:		r = R_PPC64_GOT_TPREL16_HI;
   1217       break;
   1218     case BFD_RELOC_PPC_GOT_TPREL16_HA:		r = R_PPC64_GOT_TPREL16_HA;
   1219       break;
   1220     case BFD_RELOC_PPC_GOT_DTPREL16:		r = R_PPC64_GOT_DTPREL16_DS;
   1221       break;
   1222     case BFD_RELOC_PPC_GOT_DTPREL16_LO:		r = R_PPC64_GOT_DTPREL16_LO_DS;
   1223       break;
   1224     case BFD_RELOC_PPC_GOT_DTPREL16_HI:		r = R_PPC64_GOT_DTPREL16_HI;
   1225       break;
   1226     case BFD_RELOC_PPC_GOT_DTPREL16_HA:		r = R_PPC64_GOT_DTPREL16_HA;
   1227       break;
   1228     case BFD_RELOC_PPC64_TPREL16_DS:		r = R_PPC64_TPREL16_DS;
   1229       break;
   1230     case BFD_RELOC_PPC64_TPREL16_LO_DS:		r = R_PPC64_TPREL16_LO_DS;
   1231       break;
   1232     case BFD_RELOC_PPC64_TPREL16_HIGHER:	r = R_PPC64_TPREL16_HIGHER;
   1233       break;
   1234     case BFD_RELOC_PPC64_TPREL16_HIGHERA:	r = R_PPC64_TPREL16_HIGHERA;
   1235       break;
   1236     case BFD_RELOC_PPC64_TPREL16_HIGHEST:	r = R_PPC64_TPREL16_HIGHEST;
   1237       break;
   1238     case BFD_RELOC_PPC64_TPREL16_HIGHESTA:	r = R_PPC64_TPREL16_HIGHESTA;
   1239       break;
   1240     case BFD_RELOC_PPC64_DTPREL16_DS:		r = R_PPC64_DTPREL16_DS;
   1241       break;
   1242     case BFD_RELOC_PPC64_DTPREL16_LO_DS:	r = R_PPC64_DTPREL16_LO_DS;
   1243       break;
   1244     case BFD_RELOC_PPC64_DTPREL16_HIGHER:	r = R_PPC64_DTPREL16_HIGHER;
   1245       break;
   1246     case BFD_RELOC_PPC64_DTPREL16_HIGHERA:	r = R_PPC64_DTPREL16_HIGHERA;
   1247       break;
   1248     case BFD_RELOC_PPC64_DTPREL16_HIGHEST:	r = R_PPC64_DTPREL16_HIGHEST;
   1249       break;
   1250     case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:	r = R_PPC64_DTPREL16_HIGHESTA;
   1251       break;
   1252     case BFD_RELOC_16_PCREL:			r = R_PPC64_REL16;
   1253       break;
   1254     case BFD_RELOC_LO16_PCREL:			r = R_PPC64_REL16_LO;
   1255       break;
   1256     case BFD_RELOC_HI16_PCREL:			r = R_PPC64_REL16_HI;
   1257       break;
   1258     case BFD_RELOC_HI16_S_PCREL:		r = R_PPC64_REL16_HA;
   1259       break;
   1260     case BFD_RELOC_PPC64_REL16_HIGH:		r = R_PPC64_REL16_HIGH;
   1261       break;
   1262     case BFD_RELOC_PPC64_REL16_HIGHA:		r = R_PPC64_REL16_HIGHA;
   1263       break;
   1264     case BFD_RELOC_PPC64_REL16_HIGHER:		r = R_PPC64_REL16_HIGHER;
   1265       break;
   1266     case BFD_RELOC_PPC64_REL16_HIGHERA:		r = R_PPC64_REL16_HIGHERA;
   1267       break;
   1268     case BFD_RELOC_PPC64_REL16_HIGHEST:		r = R_PPC64_REL16_HIGHEST;
   1269       break;
   1270     case BFD_RELOC_PPC64_REL16_HIGHESTA:	r = R_PPC64_REL16_HIGHESTA;
   1271       break;
   1272     case BFD_RELOC_PPC_16DX_HA:			r = R_PPC64_16DX_HA;
   1273       break;
   1274     case BFD_RELOC_PPC_REL16DX_HA:		r = R_PPC64_REL16DX_HA;
   1275       break;
   1276     case BFD_RELOC_PPC64_ENTRY:			r = R_PPC64_ENTRY;
   1277       break;
   1278     case BFD_RELOC_PPC64_ADDR64_LOCAL:		r = R_PPC64_ADDR64_LOCAL;
   1279       break;
   1280     case BFD_RELOC_PPC64_D34:			r = R_PPC64_D34;
   1281       break;
   1282     case BFD_RELOC_PPC64_D34_LO:		r = R_PPC64_D34_LO;
   1283       break;
   1284     case BFD_RELOC_PPC64_D34_HI30:		r = R_PPC64_D34_HI30;
   1285       break;
   1286     case BFD_RELOC_PPC64_D34_HA30:		r = R_PPC64_D34_HA30;
   1287       break;
   1288     case BFD_RELOC_PPC64_PCREL34:		r = R_PPC64_PCREL34;
   1289       break;
   1290     case BFD_RELOC_PPC64_GOT_PCREL34:		r = R_PPC64_GOT_PCREL34;
   1291       break;
   1292     case BFD_RELOC_PPC64_PLT_PCREL34:		r = R_PPC64_PLT_PCREL34;
   1293       break;
   1294     case BFD_RELOC_PPC64_TPREL34:		r = R_PPC64_TPREL34;
   1295       break;
   1296     case BFD_RELOC_PPC64_DTPREL34:		r = R_PPC64_DTPREL34;
   1297       break;
   1298     case BFD_RELOC_PPC64_GOT_TLSGD_PCREL34:	r = R_PPC64_GOT_TLSGD_PCREL34;
   1299       break;
   1300     case BFD_RELOC_PPC64_GOT_TLSLD_PCREL34:	r = R_PPC64_GOT_TLSLD_PCREL34;
   1301       break;
   1302     case BFD_RELOC_PPC64_GOT_TPREL_PCREL34:	r = R_PPC64_GOT_TPREL_PCREL34;
   1303       break;
   1304     case BFD_RELOC_PPC64_GOT_DTPREL_PCREL34:	r = R_PPC64_GOT_DTPREL_PCREL34;
   1305       break;
   1306     case BFD_RELOC_PPC64_ADDR16_HIGHER34:	r = R_PPC64_ADDR16_HIGHER34;
   1307       break;
   1308     case BFD_RELOC_PPC64_ADDR16_HIGHERA34:	r = R_PPC64_ADDR16_HIGHERA34;
   1309       break;
   1310     case BFD_RELOC_PPC64_ADDR16_HIGHEST34:	r = R_PPC64_ADDR16_HIGHEST34;
   1311       break;
   1312     case BFD_RELOC_PPC64_ADDR16_HIGHESTA34:	r = R_PPC64_ADDR16_HIGHESTA34;
   1313       break;
   1314     case BFD_RELOC_PPC64_REL16_HIGHER34:	r = R_PPC64_REL16_HIGHER34;
   1315       break;
   1316     case BFD_RELOC_PPC64_REL16_HIGHERA34:	r = R_PPC64_REL16_HIGHERA34;
   1317       break;
   1318     case BFD_RELOC_PPC64_REL16_HIGHEST34:	r = R_PPC64_REL16_HIGHEST34;
   1319       break;
   1320     case BFD_RELOC_PPC64_REL16_HIGHESTA34:	r = R_PPC64_REL16_HIGHESTA34;
   1321       break;
   1322     case BFD_RELOC_PPC64_D28:			r = R_PPC64_D28;
   1323       break;
   1324     case BFD_RELOC_PPC64_PCREL28:		r = R_PPC64_PCREL28;
   1325       break;
   1326     case BFD_RELOC_VTABLE_INHERIT:		r = R_PPC64_GNU_VTINHERIT;
   1327       break;
   1328     case BFD_RELOC_VTABLE_ENTRY:		r = R_PPC64_GNU_VTENTRY;
   1329       break;
   1330     }
   1331 
   1332   return ppc64_elf_howto_table[r];
   1333 };
   1334 
   1335 static reloc_howto_type *
   1336 ppc64_elf_reloc_name_lookup (bfd *abfd, const char *r_name)
   1337 {
   1338   unsigned int i;
   1339   static char *compat_map[][2] = {
   1340     { "R_PPC64_GOT_TLSGD34", "R_PPC64_GOT_TLSGD_PCREL34" },
   1341     { "R_PPC64_GOT_TLSLD34", "R_PPC64_GOT_TLSLD_PCREL34" },
   1342     { "R_PPC64_GOT_TPREL34", "R_PPC64_GOT_TPREL_PCREL34" },
   1343     { "R_PPC64_GOT_DTPREL34", "R_PPC64_GOT_DTPREL_PCREL34" }
   1344   };
   1345 
   1346   for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
   1347     if (ppc64_elf_howto_raw[i].name != NULL
   1348 	&& strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
   1349       return &ppc64_elf_howto_raw[i];
   1350 
   1351   /* Handle old names of relocations in case they were used by
   1352      .reloc directives.
   1353      FIXME: Remove this soon.  Mapping the reloc names is very likely
   1354      completely unnecessary.  */
   1355   for (i = 0; i < ARRAY_SIZE (compat_map); i++)
   1356     if (strcasecmp (compat_map[i][0], r_name) == 0)
   1357       {
   1358 	_bfd_error_handler (_("warning: %s should be used rather than %s"),
   1359 			    compat_map[i][1], compat_map[i][0]);
   1360 	return ppc64_elf_reloc_name_lookup (abfd, compat_map[i][1]);
   1361       }
   1362 
   1363   return NULL;
   1364 }
   1365 
   1366 /* Set the howto pointer for a PowerPC ELF reloc.  */
   1367 
   1368 static bool
   1369 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
   1370 			 Elf_Internal_Rela *dst)
   1371 {
   1372   unsigned int type;
   1373 
   1374   /* Initialize howto table if needed.  */
   1375   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
   1376     ppc_howto_init ();
   1377 
   1378   type = ELF64_R_TYPE (dst->r_info);
   1379   if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
   1380     {
   1381       /* xgettext:c-format */
   1382       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
   1383 			  abfd, type);
   1384       bfd_set_error (bfd_error_bad_value);
   1385       return false;
   1386     }
   1387   cache_ptr->howto = ppc64_elf_howto_table[type];
   1388   if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
   1389     {
   1390       /* xgettext:c-format */
   1391       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
   1392 			  abfd, type);
   1393       bfd_set_error (bfd_error_bad_value);
   1394       return false;
   1395     }
   1396 
   1397   return true;
   1398 }
   1399 
   1400 /* Handle the R_PPC64_ADDR16_HA and similar relocs.  */
   1401 
   1402 static bfd_reloc_status_type
   1403 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   1404 		    void *data, asection *input_section,
   1405 		    bfd *output_bfd, char **error_message)
   1406 {
   1407   enum elf_ppc64_reloc_type r_type;
   1408   long insn;
   1409   bfd_size_type octets;
   1410   bfd_vma value;
   1411 
   1412   /* If this is a relocatable link (output_bfd test tells us), just
   1413      call the generic function.  Any adjustment will be done at final
   1414      link time.  */
   1415   if (output_bfd != NULL)
   1416     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   1417 				  input_section, output_bfd, error_message);
   1418 
   1419   /* Adjust the addend for sign extension of the low 16 (or 34) bits.
   1420      We won't actually be using the low bits, so trashing them
   1421      doesn't matter.  */
   1422   r_type = reloc_entry->howto->type;
   1423   if (r_type == R_PPC64_ADDR16_HIGHERA34
   1424       || r_type == R_PPC64_ADDR16_HIGHESTA34
   1425       || r_type == R_PPC64_REL16_HIGHERA34
   1426       || r_type == R_PPC64_REL16_HIGHESTA34)
   1427     reloc_entry->addend += 1ULL << 33;
   1428   else
   1429     reloc_entry->addend += 1U << 15;
   1430   if (r_type != R_PPC64_REL16DX_HA)
   1431     return bfd_reloc_continue;
   1432 
   1433   value = 0;
   1434   if (!bfd_is_com_section (symbol->section))
   1435     value = symbol->value;
   1436   value += (reloc_entry->addend
   1437 	    + symbol->section->output_offset
   1438 	    + symbol->section->output_section->vma);
   1439   value -= (reloc_entry->address
   1440 	    + input_section->output_offset
   1441 	    + input_section->output_section->vma);
   1442   value = (bfd_signed_vma) value >> 16;
   1443 
   1444   octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
   1445   if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
   1446 				  input_section, octets))
   1447     return bfd_reloc_outofrange;
   1448 
   1449   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
   1450   insn &= ~0x1fffc1;
   1451   insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
   1452   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
   1453   if (value + 0x8000 > 0xffff)
   1454     return bfd_reloc_overflow;
   1455   return bfd_reloc_ok;
   1456 }
   1457 
   1458 static bfd_reloc_status_type
   1459 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   1460 			void *data, asection *input_section,
   1461 			bfd *output_bfd, char **error_message)
   1462 {
   1463   if (output_bfd != NULL)
   1464     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   1465 				  input_section, output_bfd, error_message);
   1466 
   1467   if (strcmp (symbol->section->name, ".opd") == 0
   1468       && (symbol->section->owner->flags & DYNAMIC) == 0)
   1469     {
   1470       bfd_vma dest = opd_entry_value (symbol->section,
   1471 				      symbol->value + reloc_entry->addend,
   1472 				      NULL, NULL, false);
   1473       if (dest != (bfd_vma) -1)
   1474 	reloc_entry->addend = dest - (symbol->value
   1475 				      + symbol->section->output_section->vma
   1476 				      + symbol->section->output_offset);
   1477     }
   1478   else
   1479     {
   1480       elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
   1481 
   1482       if (symbol->section->owner != abfd
   1483 	  && symbol->section->owner != NULL
   1484 	  && abiversion (symbol->section->owner) >= 2)
   1485 	{
   1486 	  unsigned int i;
   1487 
   1488 	  for (i = 0; i < symbol->section->owner->symcount; ++i)
   1489 	    {
   1490 	      asymbol *symdef = symbol->section->owner->outsymbols[i];
   1491 
   1492 	      if (strcmp (symdef->name, symbol->name) == 0)
   1493 		{
   1494 		  elfsym = (elf_symbol_type *) symdef;
   1495 		  break;
   1496 		}
   1497 	    }
   1498 	}
   1499       reloc_entry->addend
   1500 	+= PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
   1501     }
   1502   return bfd_reloc_continue;
   1503 }
   1504 
   1505 static bfd_reloc_status_type
   1506 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   1507 			 void *data, asection *input_section,
   1508 			 bfd *output_bfd, char **error_message)
   1509 {
   1510   long insn;
   1511   enum elf_ppc64_reloc_type r_type;
   1512   bfd_size_type octets;
   1513   /* Assume 'at' branch hints.  */
   1514   bool is_isa_v2 = true;
   1515 
   1516   /* If this is a relocatable link (output_bfd test tells us), just
   1517      call the generic function.  Any adjustment will be done at final
   1518      link time.  */
   1519   if (output_bfd != NULL)
   1520     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   1521 				  input_section, output_bfd, error_message);
   1522 
   1523   octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
   1524   if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
   1525 				  input_section, octets))
   1526     return bfd_reloc_outofrange;
   1527 
   1528   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
   1529   insn &= ~(0x01 << 21);
   1530   r_type = reloc_entry->howto->type;
   1531   if (r_type == R_PPC64_ADDR14_BRTAKEN
   1532       || r_type == R_PPC64_REL14_BRTAKEN)
   1533     insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
   1534 
   1535   if (is_isa_v2)
   1536     {
   1537       /* Set 'a' bit.  This is 0b00010 in BO field for branch
   1538 	 on CR(BI) insns (BO == 001at or 011at), and 0b01000
   1539 	 for branch on CTR insns (BO == 1a00t or 1a01t).  */
   1540       if ((insn & (0x14 << 21)) == (0x04 << 21))
   1541 	insn |= 0x02 << 21;
   1542       else if ((insn & (0x14 << 21)) == (0x10 << 21))
   1543 	insn |= 0x08 << 21;
   1544       else
   1545 	goto out;
   1546     }
   1547   else
   1548     {
   1549       bfd_vma target = 0;
   1550       bfd_vma from;
   1551 
   1552       if (!bfd_is_com_section (symbol->section))
   1553 	target = symbol->value;
   1554       target += symbol->section->output_section->vma;
   1555       target += symbol->section->output_offset;
   1556       target += reloc_entry->addend;
   1557 
   1558       from = (reloc_entry->address
   1559 	      + input_section->output_offset
   1560 	      + input_section->output_section->vma);
   1561 
   1562       /* Invert 'y' bit if not the default.  */
   1563       if ((bfd_signed_vma) (target - from) < 0)
   1564 	insn ^= 0x01 << 21;
   1565     }
   1566   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
   1567  out:
   1568   return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
   1569 				 input_section, output_bfd, error_message);
   1570 }
   1571 
   1572 static bfd_reloc_status_type
   1573 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   1574 			 void *data, asection *input_section,
   1575 			 bfd *output_bfd, char **error_message)
   1576 {
   1577   /* If this is a relocatable link (output_bfd test tells us), just
   1578      call the generic function.  Any adjustment will be done at final
   1579      link time.  */
   1580   if (output_bfd != NULL)
   1581     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   1582 				  input_section, output_bfd, error_message);
   1583 
   1584   /* Subtract the symbol section base address.  */
   1585   reloc_entry->addend -= symbol->section->output_section->vma;
   1586   return bfd_reloc_continue;
   1587 }
   1588 
   1589 static bfd_reloc_status_type
   1590 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   1591 			    void *data, asection *input_section,
   1592 			    bfd *output_bfd, char **error_message)
   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   /* Subtract the symbol section base address.  */
   1602   reloc_entry->addend -= symbol->section->output_section->vma;
   1603 
   1604   /* Adjust the addend for sign extension of the low 16 bits.  */
   1605   reloc_entry->addend += 0x8000;
   1606   return bfd_reloc_continue;
   1607 }
   1608 
   1609 static bfd_reloc_status_type
   1610 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   1611 		     void *data, asection *input_section,
   1612 		     bfd *output_bfd, char **error_message)
   1613 {
   1614   bfd_vma TOCstart;
   1615 
   1616   /* If this is a relocatable link (output_bfd test tells us), just
   1617      call the generic function.  Any adjustment will be done at final
   1618      link time.  */
   1619   if (output_bfd != NULL)
   1620     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   1621 				  input_section, output_bfd, error_message);
   1622 
   1623   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
   1624   if (TOCstart == 0)
   1625     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
   1626 
   1627   /* Subtract the TOC base address.  */
   1628   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
   1629   return bfd_reloc_continue;
   1630 }
   1631 
   1632 static bfd_reloc_status_type
   1633 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   1634 			void *data, asection *input_section,
   1635 			bfd *output_bfd, char **error_message)
   1636 {
   1637   bfd_vma TOCstart;
   1638 
   1639   /* If this is a relocatable link (output_bfd test tells us), just
   1640      call the generic function.  Any adjustment will be done at final
   1641      link time.  */
   1642   if (output_bfd != NULL)
   1643     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   1644 				  input_section, output_bfd, error_message);
   1645 
   1646   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
   1647   if (TOCstart == 0)
   1648     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
   1649 
   1650   /* Subtract the TOC base address.  */
   1651   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
   1652 
   1653   /* Adjust the addend for sign extension of the low 16 bits.  */
   1654   reloc_entry->addend += 0x8000;
   1655   return bfd_reloc_continue;
   1656 }
   1657 
   1658 static bfd_reloc_status_type
   1659 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   1660 		       void *data, asection *input_section,
   1661 		       bfd *output_bfd, char **error_message)
   1662 {
   1663   bfd_vma TOCstart;
   1664   bfd_size_type octets;
   1665 
   1666   /* If this is a relocatable link (output_bfd test tells us), just
   1667      call the generic function.  Any adjustment will be done at final
   1668      link time.  */
   1669   if (output_bfd != NULL)
   1670     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   1671 				  input_section, output_bfd, error_message);
   1672 
   1673   octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
   1674   if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
   1675 				  input_section, octets))
   1676     return bfd_reloc_outofrange;
   1677 
   1678   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
   1679   if (TOCstart == 0)
   1680     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
   1681 
   1682   bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
   1683   return bfd_reloc_ok;
   1684 }
   1685 
   1686 static bfd_reloc_status_type
   1687 ppc64_elf_prefix_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   1688 			void *data, asection *input_section,
   1689 			bfd *output_bfd, char **error_message)
   1690 {
   1691   uint64_t insn;
   1692   bfd_vma targ;
   1693   bfd_size_type octets;
   1694 
   1695   if (output_bfd != NULL)
   1696     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   1697 				  input_section, output_bfd, error_message);
   1698 
   1699   octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
   1700   if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
   1701 				  input_section, octets))
   1702     return bfd_reloc_outofrange;
   1703 
   1704   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
   1705   insn <<= 32;
   1706   insn |= bfd_get_32 (abfd, (bfd_byte *) data + octets + 4);
   1707 
   1708   targ = (symbol->section->output_section->vma
   1709 	  + symbol->section->output_offset
   1710 	  + reloc_entry->addend);
   1711   if (!bfd_is_com_section (symbol->section))
   1712     targ += symbol->value;
   1713   if (reloc_entry->howto->type == R_PPC64_D34_HA30)
   1714     targ += 1ULL << 33;
   1715   if (reloc_entry->howto->pc_relative)
   1716     {
   1717       bfd_vma from = (reloc_entry->address
   1718 		      + input_section->output_offset
   1719 		      + input_section->output_section->vma);
   1720       targ -=from;
   1721     }
   1722   targ >>= reloc_entry->howto->rightshift;
   1723   insn &= ~reloc_entry->howto->dst_mask;
   1724   insn |= ((targ << 16) | (targ & 0xffff)) & reloc_entry->howto->dst_mask;
   1725   bfd_put_32 (abfd, insn >> 32, (bfd_byte *) data + octets);
   1726   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets + 4);
   1727   if (reloc_entry->howto->complain_on_overflow == complain_overflow_signed
   1728       && (targ + (1ULL << (reloc_entry->howto->bitsize - 1))
   1729 	  >= 1ULL << reloc_entry->howto->bitsize))
   1730     return bfd_reloc_overflow;
   1731   return bfd_reloc_ok;
   1732 }
   1733 
   1734 static bfd_reloc_status_type
   1735 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   1736 			   void *data, asection *input_section,
   1737 			   bfd *output_bfd, char **error_message)
   1738 {
   1739   /* If this is a relocatable link (output_bfd test tells us), just
   1740      call the generic function.  Any adjustment will be done at final
   1741      link time.  */
   1742   if (output_bfd != NULL)
   1743     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   1744 				  input_section, output_bfd, error_message);
   1745 
   1746   if (error_message != NULL)
   1747     {
   1748       static char *message;
   1749       free (message);
   1750       if (asprintf (&message, _("generic linker can't handle %s"),
   1751 		    reloc_entry->howto->name) < 0)
   1752 	message = NULL;
   1753       *error_message = message;
   1754     }
   1755   return bfd_reloc_dangerous;
   1756 }
   1757 
   1758 /* Track GOT entries needed for a given symbol.  We might need more
   1759    than one got entry per symbol.  */
   1760 struct got_entry
   1761 {
   1762   struct got_entry *next;
   1763 
   1764   /* The symbol addend that we'll be placing in the GOT.  */
   1765   bfd_vma addend;
   1766 
   1767   /* Unlike other ELF targets, we use separate GOT entries for the same
   1768      symbol referenced from different input files.  This is to support
   1769      automatic multiple TOC/GOT sections, where the TOC base can vary
   1770      from one input file to another.  After partitioning into TOC groups
   1771      we merge entries within the group.
   1772 
   1773      Point to the BFD owning this GOT entry.  */
   1774   bfd *owner;
   1775 
   1776   /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
   1777      TLS_TPREL or TLS_DTPREL for tls entries.  */
   1778   unsigned char tls_type;
   1779 
   1780   /* Non-zero if got.ent points to real entry.  */
   1781   unsigned char is_indirect;
   1782 
   1783   /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
   1784   union
   1785   {
   1786     bfd_signed_vma refcount;
   1787     bfd_vma offset;
   1788     struct got_entry *ent;
   1789   } got;
   1790 };
   1791 
   1792 /* The same for PLT.  */
   1793 struct plt_entry
   1794 {
   1795   struct plt_entry *next;
   1796 
   1797   bfd_vma addend;
   1798 
   1799   union
   1800   {
   1801     bfd_signed_vma refcount;
   1802     bfd_vma offset;
   1803   } plt;
   1804 };
   1805 
   1806 struct ppc64_elf_obj_tdata
   1807 {
   1808   struct elf_obj_tdata elf;
   1809 
   1810   /* Shortcuts to dynamic linker sections.  */
   1811   asection *got;
   1812   asection *relgot;
   1813 
   1814   /* Used during garbage collection.  We attach global symbols defined
   1815      on removed .opd entries to this section so that the sym is removed.  */
   1816   asection *deleted_section;
   1817 
   1818   /* TLS local dynamic got entry handling.  Support for multiple GOT
   1819      sections means we potentially need one of these for each input bfd.  */
   1820   struct got_entry tlsld_got;
   1821 
   1822   union
   1823   {
   1824     /* A copy of relocs before they are modified for --emit-relocs.  */
   1825     Elf_Internal_Rela *relocs;
   1826 
   1827     /* Section contents.  */
   1828     bfd_byte *contents;
   1829   } opd;
   1830 
   1831   /* Nonzero if this bfd has small toc/got relocs, ie. that expect
   1832      the reloc to be in the range -32768 to 32767.  */
   1833   unsigned int has_small_toc_reloc : 1;
   1834 
   1835   /* Set if toc/got ha relocs detected not using r2, or lo reloc
   1836      instruction not one we handle.  */
   1837   unsigned int unexpected_toc_insn : 1;
   1838 
   1839   /* Set if PLT/GOT/TOC relocs that can be optimised are present in
   1840      this file.  */
   1841   unsigned int has_optrel : 1;
   1842 };
   1843 
   1844 #define ppc64_elf_tdata(bfd) \
   1845   ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
   1846 
   1847 #define ppc64_tlsld_got(bfd) \
   1848   (&ppc64_elf_tdata (bfd)->tlsld_got)
   1849 
   1850 #define is_ppc64_elf(bfd) \
   1851   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
   1852    && elf_object_id (bfd) == PPC64_ELF_DATA)
   1853 
   1854 /* Override the generic function because we store some extras.  */
   1855 
   1856 static bool
   1857 ppc64_elf_mkobject (bfd *abfd)
   1858 {
   1859   return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
   1860 				  PPC64_ELF_DATA);
   1861 }
   1862 
   1863 /* Fix bad default arch selected for a 64 bit input bfd when the
   1864    default is 32 bit.  Also select arch based on apuinfo.  */
   1865 
   1866 static bool
   1867 ppc64_elf_object_p (bfd *abfd)
   1868 {
   1869   if (!abfd->arch_info->the_default)
   1870     return true;
   1871 
   1872   if (abfd->arch_info->bits_per_word == 32)
   1873     {
   1874       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
   1875 
   1876       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
   1877 	{
   1878 	  /* Relies on arch after 32 bit default being 64 bit default.  */
   1879 	  abfd->arch_info = abfd->arch_info->next;
   1880 	  BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
   1881 	}
   1882     }
   1883   return _bfd_elf_ppc_set_arch (abfd);
   1884 }
   1885 
   1886 /* Support for core dump NOTE sections.  */
   1887 
   1888 static bool
   1889 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
   1890 {
   1891   size_t offset, size;
   1892 
   1893   if (note->descsz != 504)
   1894     return false;
   1895 
   1896   /* pr_cursig */
   1897   elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
   1898 
   1899   /* pr_pid */
   1900   elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
   1901 
   1902   /* pr_reg */
   1903   offset = 112;
   1904   size = 384;
   1905 
   1906   /* Make a ".reg/999" section.  */
   1907   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
   1908 					  size, note->descpos + offset);
   1909 }
   1910 
   1911 static bool
   1912 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
   1913 {
   1914   if (note->descsz != 136)
   1915     return false;
   1916 
   1917   elf_tdata (abfd)->core->pid
   1918     = bfd_get_32 (abfd, note->descdata + 24);
   1919   elf_tdata (abfd)->core->program
   1920     = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
   1921   elf_tdata (abfd)->core->command
   1922     = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
   1923 
   1924   return true;
   1925 }
   1926 
   1927 static char *
   1928 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
   1929 			   ...)
   1930 {
   1931   switch (note_type)
   1932     {
   1933     default:
   1934       return NULL;
   1935 
   1936     case NT_PRPSINFO:
   1937       {
   1938 	char data[136] ATTRIBUTE_NONSTRING;
   1939 	va_list ap;
   1940 
   1941 	va_start (ap, note_type);
   1942 	memset (data, 0, sizeof (data));
   1943 	strncpy (data + 40, va_arg (ap, const char *), 16);
   1944 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
   1945 	DIAGNOSTIC_PUSH;
   1946 	/* GCC 8.0 and 8.1 warn about 80 equals destination size with
   1947 	   -Wstringop-truncation:
   1948 	   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
   1949 	 */
   1950 	DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
   1951 #endif
   1952 	strncpy (data + 56, va_arg (ap, const char *), 80);
   1953 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
   1954 	DIAGNOSTIC_POP;
   1955 #endif
   1956 	va_end (ap);
   1957 	return elfcore_write_note (abfd, buf, bufsiz,
   1958 				   "CORE", note_type, data, sizeof (data));
   1959       }
   1960 
   1961     case NT_PRSTATUS:
   1962       {
   1963 	char data[504];
   1964 	va_list ap;
   1965 	long pid;
   1966 	int cursig;
   1967 	const void *greg;
   1968 
   1969 	va_start (ap, note_type);
   1970 	memset (data, 0, 112);
   1971 	pid = va_arg (ap, long);
   1972 	bfd_put_32 (abfd, pid, data + 32);
   1973 	cursig = va_arg (ap, int);
   1974 	bfd_put_16 (abfd, cursig, data + 12);
   1975 	greg = va_arg (ap, const void *);
   1976 	memcpy (data + 112, greg, 384);
   1977 	memset (data + 496, 0, 8);
   1978 	va_end (ap);
   1979 	return elfcore_write_note (abfd, buf, bufsiz,
   1980 				   "CORE", note_type, data, sizeof (data));
   1981       }
   1982     }
   1983 }
   1984 
   1985 /* Add extra PPC sections.  */
   1986 
   1987 static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
   1988 {
   1989   { STRING_COMMA_LEN (".plt"),	  0, SHT_NOBITS,   0 },
   1990   { STRING_COMMA_LEN (".sbss"),	 -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
   1991   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   1992   { STRING_COMMA_LEN (".toc"),	  0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   1993   { STRING_COMMA_LEN (".toc1"),	  0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   1994   { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
   1995   { NULL,		      0,  0, 0,		   0 }
   1996 };
   1997 
   1998 enum _ppc64_sec_type {
   1999   sec_normal = 0,
   2000   sec_opd = 1,
   2001   sec_toc = 2,
   2002   sec_stub = 3
   2003 };
   2004 
   2005 struct _ppc64_elf_section_data
   2006 {
   2007   struct bfd_elf_section_data elf;
   2008 
   2009   union
   2010   {
   2011     /* An array with one entry for each opd function descriptor,
   2012        and some spares since opd entries may be either 16 or 24 bytes.  */
   2013 #define OPD_NDX(OFF) ((OFF) >> 4)
   2014     struct _opd_sec_data
   2015     {
   2016       /* Points to the function code section for local opd entries.  */
   2017       asection **func_sec;
   2018 
   2019       /* After editing .opd, adjust references to opd local syms.  */
   2020       long *adjust;
   2021     } opd;
   2022 
   2023     /* An array for toc sections, indexed by offset/8.  */
   2024     struct _toc_sec_data
   2025     {
   2026       /* Specifies the relocation symbol index used at a given toc offset.  */
   2027       unsigned *symndx;
   2028 
   2029       /* And the relocation addend.  */
   2030       bfd_vma *add;
   2031     } toc;
   2032 
   2033     /* Stub debugging.  */
   2034     struct ppc_stub_hash_entry *last_ent;
   2035   } u;
   2036 
   2037   enum _ppc64_sec_type sec_type:2;
   2038 
   2039   /* Flag set when small branches are detected.  Used to
   2040      select suitable defaults for the stub group size.  */
   2041   unsigned int has_14bit_branch:1;
   2042 
   2043   /* Flag set when PLTCALL relocs are detected.  */
   2044   unsigned int has_pltcall:1;
   2045 
   2046   /* Flag set when section has PLT/GOT/TOC relocations that can be
   2047      optimised.  */
   2048   unsigned int has_optrel:1;
   2049 };
   2050 
   2051 #define ppc64_elf_section_data(sec) \
   2052   ((struct _ppc64_elf_section_data *) elf_section_data (sec))
   2053 
   2054 static bool
   2055 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
   2056 {
   2057   if (!sec->used_by_bfd)
   2058     {
   2059       struct _ppc64_elf_section_data *sdata;
   2060       size_t amt = sizeof (*sdata);
   2061 
   2062       sdata = bfd_zalloc (abfd, amt);
   2063       if (sdata == NULL)
   2064 	return false;
   2065       sec->used_by_bfd = sdata;
   2066     }
   2067 
   2068   return _bfd_elf_new_section_hook (abfd, sec);
   2069 }
   2070 
   2071 static bool
   2072 ppc64_elf_section_flags (const Elf_Internal_Shdr *hdr)
   2073 {
   2074   const char *name = hdr->bfd_section->name;
   2075 
   2076   if (startswith (name, ".sbss")
   2077       || startswith (name, ".sdata"))
   2078     hdr->bfd_section->flags |= SEC_SMALL_DATA;
   2079 
   2080   return true;
   2081 }
   2082 
   2083 static struct _opd_sec_data *
   2084 get_opd_info (asection * sec)
   2085 {
   2086   if (sec != NULL
   2087       && ppc64_elf_section_data (sec) != NULL
   2088       && ppc64_elf_section_data (sec)->sec_type == sec_opd)
   2089     return &ppc64_elf_section_data (sec)->u.opd;
   2090   return NULL;
   2091 }
   2092 
   2093 /* Parameters for the qsort hook.  */
   2095 static bool synthetic_relocatable;
   2096 static const asection *synthetic_opd;
   2097 
   2098 /* qsort comparison function for ppc64_elf_get_synthetic_symtab.  */
   2099 
   2100 static int
   2101 compare_symbols (const void *ap, const void *bp)
   2102 {
   2103   const asymbol *a = *(const asymbol **) ap;
   2104   const asymbol *b = *(const asymbol **) bp;
   2105 
   2106   /* Section symbols first.  */
   2107   if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
   2108     return -1;
   2109   if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
   2110     return 1;
   2111 
   2112   /* then .opd symbols.  */
   2113   if (synthetic_opd != NULL)
   2114     {
   2115       if (strcmp (a->section->name, ".opd") == 0
   2116 	  && strcmp (b->section->name, ".opd") != 0)
   2117 	return -1;
   2118       if (strcmp (a->section->name, ".opd") != 0
   2119 	  && strcmp (b->section->name, ".opd") == 0)
   2120 	return 1;
   2121     }
   2122 
   2123   /* then other code symbols.  */
   2124   if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   2125        == (SEC_CODE | SEC_ALLOC))
   2126       && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   2127 	  != (SEC_CODE | SEC_ALLOC)))
   2128     return -1;
   2129 
   2130   if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   2131        != (SEC_CODE | SEC_ALLOC))
   2132       && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   2133 	  == (SEC_CODE | SEC_ALLOC)))
   2134     return 1;
   2135 
   2136   if (synthetic_relocatable)
   2137     {
   2138       if (a->section->id < b->section->id)
   2139 	return -1;
   2140 
   2141       if (a->section->id > b->section->id)
   2142 	return 1;
   2143     }
   2144 
   2145   if (a->value + a->section->vma < b->value + b->section->vma)
   2146     return -1;
   2147 
   2148   if (a->value + a->section->vma > b->value + b->section->vma)
   2149     return 1;
   2150 
   2151   /* For syms with the same value, prefer strong dynamic global function
   2152      syms over other syms.  */
   2153   if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
   2154     return -1;
   2155 
   2156   if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
   2157     return 1;
   2158 
   2159   if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
   2160     return -1;
   2161 
   2162   if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
   2163     return 1;
   2164 
   2165   if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
   2166     return -1;
   2167 
   2168   if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
   2169     return 1;
   2170 
   2171   if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
   2172     return -1;
   2173 
   2174   if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
   2175     return 1;
   2176 
   2177   /* Finally, sort on where the symbol is in memory.  The symbols will
   2178      be in at most two malloc'd blocks, one for static syms, one for
   2179      dynamic syms, and we distinguish the two blocks above by testing
   2180      BSF_DYNAMIC.  Since we are sorting the symbol pointers which were
   2181      originally in the same order as the symbols (and we're not
   2182      sorting the symbols themselves), this ensures a stable sort.  */
   2183   if (a < b)
   2184     return -1;
   2185   if (a > b)
   2186     return 1;
   2187   return 0;
   2188 }
   2189 
   2190 /* Search SYMS for a symbol of the given VALUE.  */
   2191 
   2192 static asymbol *
   2193 sym_exists_at (asymbol **syms, size_t lo, size_t hi, unsigned int id,
   2194 	       bfd_vma value)
   2195 {
   2196   size_t mid;
   2197 
   2198   if (id == (unsigned) -1)
   2199     {
   2200       while (lo < hi)
   2201 	{
   2202 	  mid = (lo + hi) >> 1;
   2203 	  if (syms[mid]->value + syms[mid]->section->vma < value)
   2204 	    lo = mid + 1;
   2205 	  else if (syms[mid]->value + syms[mid]->section->vma > value)
   2206 	    hi = mid;
   2207 	  else
   2208 	    return syms[mid];
   2209 	}
   2210     }
   2211   else
   2212     {
   2213       while (lo < hi)
   2214 	{
   2215 	  mid = (lo + hi) >> 1;
   2216 	  if (syms[mid]->section->id < id)
   2217 	    lo = mid + 1;
   2218 	  else if (syms[mid]->section->id > id)
   2219 	    hi = mid;
   2220 	  else if (syms[mid]->value < value)
   2221 	    lo = mid + 1;
   2222 	  else if (syms[mid]->value > value)
   2223 	    hi = mid;
   2224 	  else
   2225 	    return syms[mid];
   2226 	}
   2227     }
   2228   return NULL;
   2229 }
   2230 
   2231 static bool
   2232 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
   2233 {
   2234   bfd_vma vma = *(bfd_vma *) ptr;
   2235   return ((section->flags & SEC_ALLOC) != 0
   2236 	  && section->vma <= vma
   2237 	  && vma < section->vma + section->size);
   2238 }
   2239 
   2240 /* Create synthetic symbols, effectively restoring "dot-symbol" function
   2241    entry syms.  Also generate @plt symbols for the glink branch table.
   2242    Returns count of synthetic symbols in RET or -1 on error.  */
   2243 
   2244 static long
   2245 ppc64_elf_get_synthetic_symtab (bfd *abfd,
   2246 				long static_count, asymbol **static_syms,
   2247 				long dyn_count, asymbol **dyn_syms,
   2248 				asymbol **ret)
   2249 {
   2250   asymbol *s;
   2251   size_t i, j, count;
   2252   char *names;
   2253   size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
   2254   asection *opd = NULL;
   2255   bool relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
   2256   asymbol **syms;
   2257   int abi = abiversion (abfd);
   2258 
   2259   *ret = NULL;
   2260 
   2261   if (abi < 2)
   2262     {
   2263       opd = bfd_get_section_by_name (abfd, ".opd");
   2264       if (opd == NULL && abi == 1)
   2265 	return 0;
   2266     }
   2267 
   2268   syms = NULL;
   2269   codesecsym = 0;
   2270   codesecsymend = 0;
   2271   secsymend = 0;
   2272   opdsymend = 0;
   2273   symcount = 0;
   2274   if (opd != NULL)
   2275     {
   2276       symcount = static_count;
   2277       if (!relocatable)
   2278 	symcount += dyn_count;
   2279       if (symcount == 0)
   2280 	return 0;
   2281 
   2282       syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
   2283       if (syms == NULL)
   2284 	return -1;
   2285 
   2286       if (!relocatable && static_count != 0 && dyn_count != 0)
   2287 	{
   2288 	  /* Use both symbol tables.  */
   2289 	  memcpy (syms, static_syms, static_count * sizeof (*syms));
   2290 	  memcpy (syms + static_count, dyn_syms,
   2291 		  (dyn_count + 1) * sizeof (*syms));
   2292 	}
   2293       else if (!relocatable && static_count == 0)
   2294 	memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
   2295       else
   2296 	memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
   2297 
   2298       /* Trim uninteresting symbols.  Interesting symbols are section,
   2299 	 function, and notype symbols.  */
   2300       for (i = 0, j = 0; i < symcount; ++i)
   2301 	if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
   2302 			       | BSF_RELC | BSF_SRELC)) == 0)
   2303 	  syms[j++] = syms[i];
   2304       symcount = j;
   2305 
   2306       synthetic_relocatable = relocatable;
   2307       synthetic_opd = opd;
   2308       qsort (syms, symcount, sizeof (*syms), compare_symbols);
   2309 
   2310       if (!relocatable && symcount > 1)
   2311 	{
   2312 	  /* Trim duplicate syms, since we may have merged the normal
   2313 	     and dynamic symbols.  Actually, we only care about syms
   2314 	     that have different values, so trim any with the same
   2315 	     value.  Don't consider ifunc and ifunc resolver symbols
   2316 	     duplicates however, because GDB wants to know whether a
   2317 	     text symbol is an ifunc resolver.  */
   2318 	  for (i = 1, j = 1; i < symcount; ++i)
   2319 	    {
   2320 	      const asymbol *s0 = syms[i - 1];
   2321 	      const asymbol *s1 = syms[i];
   2322 
   2323 	      if ((s0->value + s0->section->vma
   2324 		   != s1->value + s1->section->vma)
   2325 		  || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
   2326 		      != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
   2327 		syms[j++] = syms[i];
   2328 	    }
   2329 	  symcount = j;
   2330 	}
   2331 
   2332       i = 0;
   2333       /* Note that here and in compare_symbols we can't compare opd and
   2334 	 sym->section directly.  With separate debug info files, the
   2335 	 symbols will be extracted from the debug file while abfd passed
   2336 	 to this function is the real binary.  */
   2337       if ((syms[i]->flags & BSF_SECTION_SYM) != 0
   2338 	  && strcmp (syms[i]->section->name, ".opd") == 0)
   2339 	++i;
   2340       codesecsym = i;
   2341 
   2342       for (; i < symcount; ++i)
   2343 	if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
   2344 					 | SEC_THREAD_LOCAL))
   2345 	     != (SEC_CODE | SEC_ALLOC))
   2346 	    || (syms[i]->flags & BSF_SECTION_SYM) == 0)
   2347 	  break;
   2348       codesecsymend = i;
   2349 
   2350       for (; i < symcount; ++i)
   2351 	if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
   2352 	  break;
   2353       secsymend = i;
   2354 
   2355       for (; i < symcount; ++i)
   2356 	if (strcmp (syms[i]->section->name, ".opd") != 0)
   2357 	  break;
   2358       opdsymend = i;
   2359 
   2360       for (; i < symcount; ++i)
   2361 	if (((syms[i]->section->flags
   2362 	      & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
   2363 	    != (SEC_CODE | SEC_ALLOC))
   2364 	  break;
   2365       symcount = i;
   2366     }
   2367   count = 0;
   2368 
   2369   if (relocatable)
   2370     {
   2371       bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
   2372       arelent *r;
   2373       size_t size;
   2374       size_t relcount;
   2375 
   2376       if (opdsymend == secsymend)
   2377 	goto done;
   2378 
   2379       slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   2380       relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
   2381       if (relcount == 0)
   2382 	goto done;
   2383 
   2384       if (!(*slurp_relocs) (abfd, opd, static_syms, false))
   2385 	{
   2386 	  count = -1;
   2387 	  goto done;
   2388 	}
   2389 
   2390       size = 0;
   2391       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
   2392 	{
   2393 	  asymbol *sym;
   2394 
   2395 	  while (r < opd->relocation + relcount
   2396 		 && r->address < syms[i]->value + opd->vma)
   2397 	    ++r;
   2398 
   2399 	  if (r == opd->relocation + relcount)
   2400 	    break;
   2401 
   2402 	  if (r->address != syms[i]->value + opd->vma)
   2403 	    continue;
   2404 
   2405 	  if (r->howto->type != R_PPC64_ADDR64)
   2406 	    continue;
   2407 
   2408 	  sym = *r->sym_ptr_ptr;
   2409 	  if (!sym_exists_at (syms, opdsymend, symcount,
   2410 			      sym->section->id, sym->value + r->addend))
   2411 	    {
   2412 	      ++count;
   2413 	      size += sizeof (asymbol);
   2414 	      size += strlen (syms[i]->name) + 2;
   2415 	    }
   2416 	}
   2417 
   2418       if (size == 0)
   2419 	goto done;
   2420       s = *ret = bfd_malloc (size);
   2421       if (s == NULL)
   2422 	{
   2423 	  count = -1;
   2424 	  goto done;
   2425 	}
   2426 
   2427       names = (char *) (s + count);
   2428 
   2429       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
   2430 	{
   2431 	  asymbol *sym;
   2432 
   2433 	  while (r < opd->relocation + relcount
   2434 		 && r->address < syms[i]->value + opd->vma)
   2435 	    ++r;
   2436 
   2437 	  if (r == opd->relocation + relcount)
   2438 	    break;
   2439 
   2440 	  if (r->address != syms[i]->value + opd->vma)
   2441 	    continue;
   2442 
   2443 	  if (r->howto->type != R_PPC64_ADDR64)
   2444 	    continue;
   2445 
   2446 	  sym = *r->sym_ptr_ptr;
   2447 	  if (!sym_exists_at (syms, opdsymend, symcount,
   2448 			      sym->section->id, sym->value + r->addend))
   2449 	    {
   2450 	      size_t len;
   2451 
   2452 	      *s = *syms[i];
   2453 	      s->flags |= BSF_SYNTHETIC;
   2454 	      s->section = sym->section;
   2455 	      s->value = sym->value + r->addend;
   2456 	      s->name = names;
   2457 	      *names++ = '.';
   2458 	      len = strlen (syms[i]->name);
   2459 	      memcpy (names, syms[i]->name, len + 1);
   2460 	      names += len + 1;
   2461 	      /* Have udata.p point back to the original symbol this
   2462 		 synthetic symbol was derived from.  */
   2463 	      s->udata.p = syms[i];
   2464 	      s++;
   2465 	    }
   2466 	}
   2467     }
   2468   else
   2469     {
   2470       bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
   2471       bfd_byte *contents = NULL;
   2472       size_t size;
   2473       size_t plt_count = 0;
   2474       bfd_vma glink_vma = 0, resolv_vma = 0;
   2475       asection *dynamic, *glink = NULL, *relplt = NULL;
   2476       arelent *p;
   2477 
   2478       if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
   2479 	{
   2480 	free_contents_and_exit_err:
   2481 	  count = -1;
   2482 	free_contents_and_exit:
   2483 	  free (contents);
   2484 	  goto done;
   2485 	}
   2486 
   2487       size = 0;
   2488       for (i = secsymend; i < opdsymend; ++i)
   2489 	{
   2490 	  bfd_vma ent;
   2491 
   2492 	  /* Ignore bogus symbols.  */
   2493 	  if (syms[i]->value > opd->size - 8)
   2494 	    continue;
   2495 
   2496 	  ent = bfd_get_64 (abfd, contents + syms[i]->value);
   2497 	  if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
   2498 	    {
   2499 	      ++count;
   2500 	      size += sizeof (asymbol);
   2501 	      size += strlen (syms[i]->name) + 2;
   2502 	    }
   2503 	}
   2504 
   2505       /* Get start of .glink stubs from DT_PPC64_GLINK.  */
   2506       if (dyn_count != 0
   2507 	  && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
   2508 	{
   2509 	  bfd_byte *dynbuf, *extdyn, *extdynend;
   2510 	  size_t extdynsize;
   2511 	  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
   2512 
   2513 	  if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
   2514 	    goto free_contents_and_exit_err;
   2515 
   2516 	  extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
   2517 	  swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
   2518 
   2519 	  for (extdyn = dynbuf, extdynend = dynbuf + dynamic->size;
   2520 	       (size_t) (extdynend - extdyn) >= extdynsize;
   2521 	       extdyn += extdynsize)
   2522 	    {
   2523 	      Elf_Internal_Dyn dyn;
   2524 	      (*swap_dyn_in) (abfd, extdyn, &dyn);
   2525 
   2526 	      if (dyn.d_tag == DT_NULL)
   2527 		break;
   2528 
   2529 	      if (dyn.d_tag == DT_PPC64_GLINK)
   2530 		{
   2531 		  /* The first glink stub starts at DT_PPC64_GLINK plus 32.
   2532 		     See comment in ppc64_elf_finish_dynamic_sections. */
   2533 		  glink_vma = dyn.d_un.d_val + 8 * 4;
   2534 		  /* The .glink section usually does not survive the final
   2535 		     link; search for the section (usually .text) where the
   2536 		     glink stubs now reside.  */
   2537 		  glink = bfd_sections_find_if (abfd, section_covers_vma,
   2538 						&glink_vma);
   2539 		  break;
   2540 		}
   2541 	    }
   2542 
   2543 	  free (dynbuf);
   2544 	}
   2545 
   2546       if (glink != NULL)
   2547 	{
   2548 	  /* Determine __glink trampoline by reading the relative branch
   2549 	     from the first glink stub.  */
   2550 	  bfd_byte buf[4];
   2551 	  unsigned int off = 0;
   2552 
   2553 	  while (bfd_get_section_contents (abfd, glink, buf,
   2554 					   glink_vma + off - glink->vma, 4))
   2555 	    {
   2556 	      unsigned int insn = bfd_get_32 (abfd, buf);
   2557 	      insn ^= B_DOT;
   2558 	      if ((insn & ~0x3fffffc) == 0)
   2559 		{
   2560 		  resolv_vma
   2561 		    = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
   2562 		  break;
   2563 		}
   2564 	      off += 4;
   2565 	      if (off > 4)
   2566 		break;
   2567 	    }
   2568 
   2569 	  if (resolv_vma)
   2570 	    size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
   2571 
   2572 	  relplt = bfd_get_section_by_name (abfd, ".rela.plt");
   2573 	  if (relplt != NULL)
   2574 	    {
   2575 	      slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   2576 	      if (!(*slurp_relocs) (abfd, relplt, dyn_syms, true))
   2577 		goto free_contents_and_exit_err;
   2578 
   2579 	      plt_count = relplt->size / sizeof (Elf64_External_Rela);
   2580 	      size += plt_count * sizeof (asymbol);
   2581 
   2582 	      p = relplt->relocation;
   2583 	      for (i = 0; i < plt_count; i++, p++)
   2584 		{
   2585 		  size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
   2586 		  if (p->addend != 0)
   2587 		    size += sizeof ("+0x") - 1 + 16;
   2588 		}
   2589 	    }
   2590 	}
   2591 
   2592       if (size == 0)
   2593 	goto free_contents_and_exit;
   2594       s = *ret = bfd_malloc (size);
   2595       if (s == NULL)
   2596 	goto free_contents_and_exit_err;
   2597 
   2598       names = (char *) (s + count + plt_count + (resolv_vma != 0));
   2599 
   2600       for (i = secsymend; i < opdsymend; ++i)
   2601 	{
   2602 	  bfd_vma ent;
   2603 
   2604 	  if (syms[i]->value > opd->size - 8)
   2605 	    continue;
   2606 
   2607 	  ent = bfd_get_64 (abfd, contents + syms[i]->value);
   2608 	  if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
   2609 	    {
   2610 	      size_t lo, hi;
   2611 	      size_t len;
   2612 	      asection *sec = abfd->sections;
   2613 
   2614 	      *s = *syms[i];
   2615 	      lo = codesecsym;
   2616 	      hi = codesecsymend;
   2617 	      while (lo < hi)
   2618 		{
   2619 		  size_t mid = (lo + hi) >> 1;
   2620 		  if (syms[mid]->section->vma < ent)
   2621 		    lo = mid + 1;
   2622 		  else if (syms[mid]->section->vma > ent)
   2623 		    hi = mid;
   2624 		  else
   2625 		    {
   2626 		      sec = syms[mid]->section;
   2627 		      break;
   2628 		    }
   2629 		}
   2630 
   2631 	      if (lo >= hi && lo > codesecsym)
   2632 		sec = syms[lo - 1]->section;
   2633 
   2634 	      for (; sec != NULL; sec = sec->next)
   2635 		{
   2636 		  if (sec->vma > ent)
   2637 		    break;
   2638 		  /* SEC_LOAD may not be set if SEC is from a separate debug
   2639 		     info file.  */
   2640 		  if ((sec->flags & SEC_ALLOC) == 0)
   2641 		    break;
   2642 		  if ((sec->flags & SEC_CODE) != 0)
   2643 		    s->section = sec;
   2644 		}
   2645 	      s->flags |= BSF_SYNTHETIC;
   2646 	      s->value = ent - s->section->vma;
   2647 	      s->name = names;
   2648 	      *names++ = '.';
   2649 	      len = strlen (syms[i]->name);
   2650 	      memcpy (names, syms[i]->name, len + 1);
   2651 	      names += len + 1;
   2652 	      /* Have udata.p point back to the original symbol this
   2653 		 synthetic symbol was derived from.  */
   2654 	      s->udata.p = syms[i];
   2655 	      s++;
   2656 	    }
   2657 	}
   2658       free (contents);
   2659 
   2660       if (glink != NULL && relplt != NULL)
   2661 	{
   2662 	  if (resolv_vma)
   2663 	    {
   2664 	      /* Add a symbol for the main glink trampoline.  */
   2665 	      memset (s, 0, sizeof *s);
   2666 	      s->the_bfd = abfd;
   2667 	      s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
   2668 	      s->section = glink;
   2669 	      s->value = resolv_vma - glink->vma;
   2670 	      s->name = names;
   2671 	      memcpy (names, "__glink_PLTresolve",
   2672 		      sizeof ("__glink_PLTresolve"));
   2673 	      names += sizeof ("__glink_PLTresolve");
   2674 	      s++;
   2675 	      count++;
   2676 	    }
   2677 
   2678 	  /* FIXME: It would be very much nicer to put sym@plt on the
   2679 	     stub rather than on the glink branch table entry.  The
   2680 	     objdump disassembler would then use a sensible symbol
   2681 	     name on plt calls.  The difficulty in doing so is
   2682 	     a) finding the stubs, and,
   2683 	     b) matching stubs against plt entries, and,
   2684 	     c) there can be multiple stubs for a given plt entry.
   2685 
   2686 	     Solving (a) could be done by code scanning, but older
   2687 	     ppc64 binaries used different stubs to current code.
   2688 	     (b) is the tricky one since you need to known the toc
   2689 	     pointer for at least one function that uses a pic stub to
   2690 	     be able to calculate the plt address referenced.
   2691 	     (c) means gdb would need to set multiple breakpoints (or
   2692 	     find the glink branch itself) when setting breakpoints
   2693 	     for pending shared library loads.  */
   2694 	  p = relplt->relocation;
   2695 	  for (i = 0; i < plt_count; i++, p++)
   2696 	    {
   2697 	      size_t len;
   2698 
   2699 	      *s = **p->sym_ptr_ptr;
   2700 	      /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
   2701 		 we are defining a symbol, ensure one of them is set.  */
   2702 	      if ((s->flags & BSF_LOCAL) == 0)
   2703 		s->flags |= BSF_GLOBAL;
   2704 	      s->flags |= BSF_SYNTHETIC;
   2705 	      s->section = glink;
   2706 	      s->value = glink_vma - glink->vma;
   2707 	      s->name = names;
   2708 	      s->udata.p = NULL;
   2709 	      len = strlen ((*p->sym_ptr_ptr)->name);
   2710 	      memcpy (names, (*p->sym_ptr_ptr)->name, len);
   2711 	      names += len;
   2712 	      if (p->addend != 0)
   2713 		{
   2714 		  memcpy (names, "+0x", sizeof ("+0x") - 1);
   2715 		  names += sizeof ("+0x") - 1;
   2716 		  bfd_sprintf_vma (abfd, names, p->addend);
   2717 		  names += strlen (names);
   2718 		}
   2719 	      memcpy (names, "@plt", sizeof ("@plt"));
   2720 	      names += sizeof ("@plt");
   2721 	      s++;
   2722 	      if (abi < 2)
   2723 		{
   2724 		  glink_vma += 8;
   2725 		  if (i >= 0x8000)
   2726 		    glink_vma += 4;
   2727 		}
   2728 	      else
   2729 		glink_vma += 4;
   2730 	    }
   2731 	  count += plt_count;
   2732 	}
   2733     }
   2734 
   2735  done:
   2736   free (syms);
   2737   return count;
   2738 }
   2739 
   2740 /* The following functions are specific to the ELF linker, while
   2742    functions above are used generally.  Those named ppc64_elf_* are
   2743    called by the main ELF linker code.  They appear in this file more
   2744    or less in the order in which they are called.  eg.
   2745    ppc64_elf_check_relocs is called early in the link process,
   2746    ppc64_elf_finish_dynamic_sections is one of the last functions
   2747    called.
   2748 
   2749    PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
   2750    functions have both a function code symbol and a function descriptor
   2751    symbol.  A call to foo in a relocatable object file looks like:
   2752 
   2753    .		.text
   2754    .	x:
   2755    .		bl	.foo
   2756    .		nop
   2757 
   2758    The function definition in another object file might be:
   2759 
   2760    .		.section .opd
   2761    .	foo:	.quad	.foo
   2762    .		.quad	.TOC.@tocbase
   2763    .		.quad	0
   2764    .
   2765    .		.text
   2766    .	.foo:	blr
   2767 
   2768    When the linker resolves the call during a static link, the branch
   2769    unsurprisingly just goes to .foo and the .opd information is unused.
   2770    If the function definition is in a shared library, things are a little
   2771    different:  The call goes via a plt call stub, the opd information gets
   2772    copied to the plt, and the linker patches the nop.
   2773 
   2774    .	x:
   2775    .		bl	.foo_stub
   2776    .		ld	2,40(1)
   2777    .
   2778    .
   2779    .	.foo_stub:
   2780    .		std	2,40(1)			# in practice, the call stub
   2781    .		addis	11,2,Lfoo@toc@ha	# is slightly optimized, but
   2782    .		addi	11,11,Lfoo@toc@l	# this is the general idea
   2783    .		ld	12,0(11)
   2784    .		ld	2,8(11)
   2785    .		mtctr	12
   2786    .		ld	11,16(11)
   2787    .		bctr
   2788    .
   2789    .		.section .plt
   2790    .	Lfoo:	reloc (R_PPC64_JMP_SLOT, foo)
   2791 
   2792    The "reloc ()" notation is supposed to indicate that the linker emits
   2793    an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
   2794    copying.
   2795 
   2796    What are the difficulties here?  Well, firstly, the relocations
   2797    examined by the linker in check_relocs are against the function code
   2798    sym .foo, while the dynamic relocation in the plt is emitted against
   2799    the function descriptor symbol, foo.  Somewhere along the line, we need
   2800    to carefully copy dynamic link information from one symbol to the other.
   2801    Secondly, the generic part of the elf linker will make .foo a dynamic
   2802    symbol as is normal for most other backends.  We need foo dynamic
   2803    instead, at least for an application final link.  However, when
   2804    creating a shared library containing foo, we need to have both symbols
   2805    dynamic so that references to .foo are satisfied during the early
   2806    stages of linking.  Otherwise the linker might decide to pull in a
   2807    definition from some other object, eg. a static library.
   2808 
   2809    Update: As of August 2004, we support a new convention.  Function
   2810    calls may use the function descriptor symbol, ie. "bl foo".  This
   2811    behaves exactly as "bl .foo".  */
   2812 
   2813 /* Of those relocs that might be copied as dynamic relocs, this
   2814    function selects those that must be copied when linking a shared
   2815    library or PIE, even when the symbol is local.  */
   2816 
   2817 static int
   2818 must_be_dyn_reloc (struct bfd_link_info *info,
   2819 		   enum elf_ppc64_reloc_type r_type)
   2820 {
   2821   switch (r_type)
   2822     {
   2823     default:
   2824       /* Only relative relocs can be resolved when the object load
   2825 	 address isn't fixed.  DTPREL64 is excluded because the
   2826 	 dynamic linker needs to differentiate global dynamic from
   2827 	 local dynamic __tls_index pairs when PPC64_OPT_TLS is set.  */
   2828       return 1;
   2829 
   2830     case R_PPC64_REL32:
   2831     case R_PPC64_REL64:
   2832     case R_PPC64_REL30:
   2833     case R_PPC64_TOC16:
   2834     case R_PPC64_TOC16_DS:
   2835     case R_PPC64_TOC16_LO:
   2836     case R_PPC64_TOC16_HI:
   2837     case R_PPC64_TOC16_HA:
   2838     case R_PPC64_TOC16_LO_DS:
   2839       return 0;
   2840 
   2841     case R_PPC64_TPREL16:
   2842     case R_PPC64_TPREL16_LO:
   2843     case R_PPC64_TPREL16_HI:
   2844     case R_PPC64_TPREL16_HA:
   2845     case R_PPC64_TPREL16_DS:
   2846     case R_PPC64_TPREL16_LO_DS:
   2847     case R_PPC64_TPREL16_HIGH:
   2848     case R_PPC64_TPREL16_HIGHA:
   2849     case R_PPC64_TPREL16_HIGHER:
   2850     case R_PPC64_TPREL16_HIGHERA:
   2851     case R_PPC64_TPREL16_HIGHEST:
   2852     case R_PPC64_TPREL16_HIGHESTA:
   2853     case R_PPC64_TPREL64:
   2854     case R_PPC64_TPREL34:
   2855       /* These relocations are relative but in a shared library the
   2856 	 linker doesn't know the thread pointer base.  */
   2857       return bfd_link_dll (info);
   2858     }
   2859 }
   2860 
   2861 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
   2862    copying dynamic variables from a shared lib into an app's .dynbss
   2863    section, and instead use a dynamic relocation to point into the
   2864    shared lib.  With code that gcc generates it is vital that this be
   2865    enabled;  In the PowerPC64 ELFv1 ABI the address of a function is
   2866    actually the address of a function descriptor which resides in the
   2867    .opd section.  gcc uses the descriptor directly rather than going
   2868    via the GOT as some other ABIs do, which means that initialized
   2869    function pointers reference the descriptor.  Thus, a function
   2870    pointer initialized to the address of a function in a shared
   2871    library will either require a .dynbss copy and a copy reloc, or a
   2872    dynamic reloc.  Using a .dynbss copy redefines the function
   2873    descriptor symbol to point to the copy.  This presents a problem as
   2874    a PLT entry for that function is also initialized from the function
   2875    descriptor symbol and the copy may not be initialized first.  */
   2876 #define ELIMINATE_COPY_RELOCS 1
   2877 
   2878 /* Section name for stubs is the associated section name plus this
   2879    string.  */
   2880 #define STUB_SUFFIX ".stub"
   2881 
   2882 /* Linker stubs.
   2883    ppc_stub_long_branch:
   2884    Used when a 14 bit branch (or even a 24 bit branch) can't reach its
   2885    destination, but a 24 bit branch in a stub section will reach.
   2886    .	b	dest
   2887 
   2888    ppc_stub_plt_branch:
   2889    Similar to the above, but a 24 bit branch in the stub section won't
   2890    reach its destination.
   2891    .	addis	%r12,%r2,xxx@toc@ha
   2892    .	ld	%r12,xxx@toc@l(%r12)
   2893    .	mtctr	%r12
   2894    .	bctr
   2895 
   2896    ppc_stub_plt_call:
   2897    Used to call a function in a shared library.  If it so happens that
   2898    the plt entry referenced crosses a 64k boundary, then an extra
   2899    "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
   2900    An r2save variant starts with "std %r2,40(%r1)".
   2901    .	addis	%r11,%r2,xxx@toc@ha
   2902    .	ld	%r12,xxx+0@toc@l(%r11)
   2903    .	mtctr	%r12
   2904    .	ld	%r2,xxx+8@toc@l(%r11)
   2905    .	ld	%r11,xxx+16@toc@l(%r11)
   2906    .	bctr
   2907 
   2908    ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
   2909    code to adjust the value and save r2 to support multiple toc sections.
   2910    A ppc_stub_long_branch with an r2 offset looks like:
   2911    .	std	%r2,40(%r1)
   2912    .	addis	%r2,%r2,off@ha
   2913    .	addi	%r2,%r2,off@l
   2914    .	b	dest
   2915 
   2916    A ppc_stub_plt_branch with an r2 offset looks like:
   2917    .	std	%r2,40(%r1)
   2918    .	addis	%r12,%r2,xxx@toc@ha
   2919    .	ld	%r12,xxx@toc@l(%r12)
   2920    .	addis	%r2,%r2,off@ha
   2921    .	addi	%r2,%r2,off@l
   2922    .	mtctr	%r12
   2923    .	bctr
   2924 
   2925    All of the above stubs are shown as their ELFv1 variants.  ELFv2
   2926    variants exist too, simpler for plt calls since a new toc pointer
   2927    and static chain are not loaded by the stub.  In addition, ELFv2
   2928    has some more complex stubs to handle calls marked with NOTOC
   2929    relocs from functions where r2 is not a valid toc pointer.
   2930    ppc_stub_long_branch_p9notoc:
   2931    .	mflr	%r12
   2932    .	bcl	20,31,1f
   2933    .  1:
   2934    .	mflr	%r11
   2935    .	mtlr	%r12
   2936    .	addis	%r12,%r11,dest-1b@ha
   2937    .	addi	%r12,%r12,dest-1b@l
   2938    .	b	dest
   2939 
   2940    ppc_stub_plt_branch_p9notoc:
   2941    .	mflr	%r12
   2942    .	bcl	20,31,1f
   2943    .  1:
   2944    .	mflr	%r11
   2945    .	mtlr	%r12
   2946    .	lis	%r12,xxx-1b@highest
   2947    .	ori	%r12,%r12,xxx-1b@higher
   2948    .	sldi	%r12,%r12,32
   2949    .	oris	%r12,%r12,xxx-1b@high
   2950    .	ori	%r12,%r12,xxx-1b@l
   2951    .	add	%r12,%r11,%r12
   2952    .	mtctr	%r12
   2953    .	bctr
   2954 
   2955    ppc_stub_plt_call_p9notoc:
   2956    .	mflr	%r12
   2957    .	bcl	20,31,1f
   2958    .  1:
   2959    .	mflr	%r11
   2960    .	mtlr	%r12
   2961    .	lis	%r12,xxx-1b@highest
   2962    .	ori	%r12,%r12,xxx-1b@higher
   2963    .	sldi	%r12,%r12,32
   2964    .	oris	%r12,%r12,xxx-1b@high
   2965    .	ori	%r12,%r12,xxx-1b@l
   2966    .	ldx	%r12,%r11,%r12
   2967    .	mtctr	%r12
   2968    .	bctr
   2969 
   2970    There are also ELFv1 power10 variants of these stubs.
   2971    ppc_stub_long_branch_notoc:
   2972    .	pla	%r12,dest@pcrel
   2973    .	b	dest
   2974    ppc_stub_plt_branch_notoc:
   2975    .	lis	%r11,(dest-1f)@highesta34
   2976    .	ori	%r11,%r11,(dest-1f)@highera34
   2977    .	sldi	%r11,%r11,34
   2978    . 1: pla	%r12,dest@pcrel
   2979    .	add	%r12,%r11,%r12
   2980    .	mtctr	%r12
   2981    .	bctr
   2982    ppc_stub_plt_call_notoc:
   2983    .	lis	%r11,(xxx-1f)@highesta34
   2984    .	ori	%r11,%r11,(xxx-1f)@highera34
   2985    .	sldi	%r11,%r11,34
   2986    . 1: pla	%r12,xxx@pcrel
   2987    .	ldx	%r12,%r11,%r12
   2988    .	mtctr	%r12
   2989    .	bctr
   2990 
   2991    In cases where the high instructions would add zero, they are
   2992    omitted and following instructions modified in some cases.
   2993    For example, a power10 ppc_stub_plt_call_notoc might simplify down
   2994    to
   2995    .	pld	%r12,xxx@pcrel
   2996    .	mtctr	%r12
   2997    .	bctr
   2998 
   2999    Stub variants may be merged.  For example, if printf is called from
   3000    code with the tocsave optimization (ie. r2 saved in function
   3001    prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
   3002    and from other code without the tocsave optimization requiring a
   3003    ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
   3004    type will be created.  Calls with the tocsave optimization will
   3005    enter this stub after the instruction saving r2.  A similar
   3006    situation exists when calls are marked with R_PPC64_REL24_NOTOC
   3007    relocations.  These require a ppc_stub_plt_call_notoc linkage stub
   3008    to call an external function like printf.  If other calls to printf
   3009    require a ppc_stub_plt_call linkage stub then a single
   3010    ppc_stub_plt_call_notoc linkage stub may be used for both types of
   3011    call.  */
   3012 
   3013 enum ppc_stub_main_type
   3014 {
   3015   ppc_stub_none,
   3016   ppc_stub_long_branch,
   3017   ppc_stub_plt_branch,
   3018   ppc_stub_plt_call,
   3019   ppc_stub_global_entry,
   3020   ppc_stub_save_res
   3021 };
   3022 
   3023 /* ppc_stub_long_branch, ppc_stub_plt_branch and ppc_stub_plt_call have
   3024    these variations.  */
   3025 
   3026 enum ppc_stub_sub_type
   3027 {
   3028   ppc_stub_toc,
   3029   ppc_stub_notoc,
   3030   ppc_stub_p9notoc
   3031 };
   3032 
   3033 struct ppc_stub_type
   3034 {
   3035   ENUM_BITFIELD (ppc_stub_main_type) main : 3;
   3036   ENUM_BITFIELD (ppc_stub_sub_type) sub : 2;
   3037   unsigned int r2save : 1;
   3038 };
   3039 
   3040 /* Information on stub grouping.  */
   3041 struct map_stub
   3042 {
   3043   /* The stub section.  */
   3044   asection *stub_sec;
   3045   /* This is the section to which stubs in the group will be attached.  */
   3046   asection *link_sec;
   3047   /* Next group.  */
   3048   struct map_stub *next;
   3049   /* Whether to emit a copy of register save/restore functions in this
   3050      group.  */
   3051   int needs_save_res;
   3052   /* Current offset within stubs after the insn restoring lr in a
   3053      _notoc or _both stub using bcl for pc-relative addressing, or
   3054      after the insn restoring lr in a __tls_get_addr_opt plt stub.  */
   3055   unsigned int lr_restore;
   3056   /* Accumulated size of EH info emitted to describe return address
   3057      if stubs modify lr.  Does not include 17 byte FDE header.  */
   3058   unsigned int eh_size;
   3059   /* Offset in glink_eh_frame to the start of EH info for this group.  */
   3060   unsigned int eh_base;
   3061 };
   3062 
   3063 struct ppc_stub_hash_entry
   3064 {
   3065   /* Base hash table entry structure.  */
   3066   struct bfd_hash_entry root;
   3067 
   3068   struct ppc_stub_type type;
   3069 
   3070   /* Group information.  */
   3071   struct map_stub *group;
   3072 
   3073   /* Offset within stub_sec of the beginning of this stub.  */
   3074   bfd_vma stub_offset;
   3075 
   3076   /* Given the symbol's value and its section we can determine its final
   3077      value when building the stubs (so the stub knows where to jump.  */
   3078   bfd_vma target_value;
   3079   asection *target_section;
   3080 
   3081   /* The symbol table entry, if any, that this was derived from.  */
   3082   struct ppc_link_hash_entry *h;
   3083   struct plt_entry *plt_ent;
   3084 
   3085   /* Symbol type.  */
   3086   unsigned char symtype;
   3087 
   3088   /* Symbol st_other.  */
   3089   unsigned char other;
   3090 
   3091   /* Debug: Track hash table traversal.  */
   3092   unsigned int id;
   3093 };
   3094 
   3095 struct ppc_branch_hash_entry
   3096 {
   3097   /* Base hash table entry structure.  */
   3098   struct bfd_hash_entry root;
   3099 
   3100   /* Offset within branch lookup table.  */
   3101   unsigned int offset;
   3102 
   3103   /* Generation marker.  */
   3104   unsigned int iter;
   3105 };
   3106 
   3107 /* Used to track dynamic relocations.  */
   3108 struct ppc_dyn_relocs
   3109 {
   3110   struct ppc_dyn_relocs *next;
   3111 
   3112   /* The input section of the reloc.  */
   3113   asection *sec;
   3114 
   3115   /* Total number of relocs copied for the input section.  */
   3116   unsigned int count;
   3117 
   3118   /* Number of pc-relative relocs copied for the input section.  */
   3119   unsigned int pc_count;
   3120 
   3121   /* Number of relocs that might become R_PPC64_RELATIVE.  */
   3122   unsigned int rel_count;
   3123 };
   3124 
   3125 struct ppc_local_dyn_relocs
   3126 {
   3127   struct ppc_local_dyn_relocs *next;
   3128 
   3129   /* The input section of the reloc.  */
   3130   asection *sec;
   3131 
   3132   /* Total number of relocs copied for the input section.  */
   3133   unsigned int count;
   3134 
   3135   /* Number of relocs that might become R_PPC64_RELATIVE.  */
   3136   unsigned int rel_count : 31;
   3137 
   3138   /* Whether this entry is for STT_GNU_IFUNC symbols.  */
   3139   unsigned int ifunc : 1;
   3140 };
   3141 
   3142 struct ppc_link_hash_entry
   3143 {
   3144   struct elf_link_hash_entry elf;
   3145 
   3146   union
   3147   {
   3148     /* A pointer to the most recently used stub hash entry against this
   3149        symbol.  */
   3150     struct ppc_stub_hash_entry *stub_cache;
   3151 
   3152     /* A pointer to the next symbol starting with a '.'  */
   3153     struct ppc_link_hash_entry *next_dot_sym;
   3154   } u;
   3155 
   3156   /* Link between function code and descriptor symbols.  */
   3157   struct ppc_link_hash_entry *oh;
   3158 
   3159   /* Flag function code and descriptor symbols.  */
   3160   unsigned int is_func:1;
   3161   unsigned int is_func_descriptor:1;
   3162   unsigned int fake:1;
   3163 
   3164   /* Whether global opd/toc sym has been adjusted or not.
   3165      After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
   3166      should be set for all globals defined in any opd/toc section.  */
   3167   unsigned int adjust_done:1;
   3168 
   3169   /* Set if this is an out-of-line register save/restore function,
   3170      with non-standard calling convention.  */
   3171   unsigned int save_res:1;
   3172 
   3173   /* Set if a duplicate symbol with non-zero localentry is detected,
   3174      even when the duplicate symbol does not provide a definition.  */
   3175   unsigned int non_zero_localentry:1;
   3176 
   3177   /* Contexts in which symbol is used in the GOT (or TOC).
   3178      Bits are or'd into the mask as the corresponding relocs are
   3179      encountered during check_relocs, with TLS_TLS being set when any
   3180      of the other TLS bits are set.  tls_optimize clears bits when
   3181      optimizing to indicate the corresponding GOT entry type is not
   3182      needed.  If set, TLS_TLS is never cleared.  tls_optimize may also
   3183      set TLS_GDIE when a GD reloc turns into an IE one.
   3184      These flags are also kept for local symbols.  */
   3185 #define TLS_TLS		 1	/* Any TLS reloc.  */
   3186 #define TLS_GD		 2	/* GD reloc. */
   3187 #define TLS_LD		 4	/* LD reloc. */
   3188 #define TLS_TPREL	 8	/* TPREL reloc, => IE. */
   3189 #define TLS_DTPREL	16	/* DTPREL reloc, => LD. */
   3190 #define TLS_MARK	32	/* __tls_get_addr call marked. */
   3191 #define TLS_GDIE	64	/* GOT TPREL reloc resulting from GD->IE. */
   3192 #define TLS_EXPLICIT   256	/* TOC section TLS reloc, not stored. */
   3193   unsigned char tls_mask;
   3194 
   3195   /* The above field is also used to mark function symbols.  In which
   3196      case TLS_TLS will be 0.  */
   3197 #define PLT_IFUNC	 2	/* STT_GNU_IFUNC.  */
   3198 #define PLT_KEEP	 4	/* inline plt call requires plt entry.  */
   3199 #define NON_GOT        256	/* local symbol plt, not stored.  */
   3200 };
   3201 
   3202 static inline struct ppc_link_hash_entry *
   3203 ppc_elf_hash_entry (struct elf_link_hash_entry *ent)
   3204 {
   3205   return (struct ppc_link_hash_entry *) ent;
   3206 }
   3207 
   3208 static inline struct elf_link_hash_entry *
   3209 elf_hash_entry (struct ppc_link_hash_entry *ent)
   3210 {
   3211   return (struct elf_link_hash_entry *) ent;
   3212 }
   3213 
   3214 /* ppc64 ELF linker hash table.  */
   3215 
   3216 struct ppc_link_hash_table
   3217 {
   3218   struct elf_link_hash_table elf;
   3219 
   3220   /* The stub hash table.  */
   3221   struct bfd_hash_table stub_hash_table;
   3222 
   3223   /* Another hash table for plt_branch stubs.  */
   3224   struct bfd_hash_table branch_hash_table;
   3225 
   3226   /* Hash table for function prologue tocsave.  */
   3227   htab_t tocsave_htab;
   3228 
   3229   /* Various options and other info passed from the linker.  */
   3230   struct ppc64_elf_params *params;
   3231 
   3232   /* The size of sec_info below.  */
   3233   unsigned int sec_info_arr_size;
   3234 
   3235   /* Per-section array of extra section info.  Done this way rather
   3236      than as part of ppc64_elf_section_data so we have the info for
   3237      non-ppc64 sections.  */
   3238   struct
   3239   {
   3240     /* Along with elf_gp, specifies the TOC pointer used by this section.  */
   3241     bfd_vma toc_off;
   3242 
   3243     union
   3244     {
   3245       /* The section group that this section belongs to.  */
   3246       struct map_stub *group;
   3247       /* A temp section list pointer.  */
   3248       asection *list;
   3249     } u;
   3250   } *sec_info;
   3251 
   3252   /* Linked list of groups.  */
   3253   struct map_stub *group;
   3254 
   3255   /* Temp used when calculating TOC pointers.  */
   3256   bfd_vma toc_curr;
   3257   bfd *toc_bfd;
   3258   asection *toc_first_sec;
   3259 
   3260   /* Used when adding symbols.  */
   3261   struct ppc_link_hash_entry *dot_syms;
   3262 
   3263   /* Shortcuts to get to dynamic linker sections.  */
   3264   asection *glink;
   3265   asection *global_entry;
   3266   asection *sfpr;
   3267   asection *pltlocal;
   3268   asection *relpltlocal;
   3269   asection *brlt;
   3270   asection *relbrlt;
   3271   asection *glink_eh_frame;
   3272 
   3273   /* Shortcut to .__tls_get_addr and __tls_get_addr.  */
   3274   struct ppc_link_hash_entry *tls_get_addr;
   3275   struct ppc_link_hash_entry *tls_get_addr_fd;
   3276   struct ppc_link_hash_entry *tga_desc;
   3277   struct ppc_link_hash_entry *tga_desc_fd;
   3278   struct map_stub *tga_group;
   3279 
   3280   /* The size of reliplt used by got entry relocs.  */
   3281   bfd_size_type got_reli_size;
   3282 
   3283   /* DT_RELR array of section/r_offset.  */
   3284   size_t relr_alloc;
   3285   size_t relr_count;
   3286   struct
   3287   {
   3288     asection *sec;
   3289     bfd_vma off;
   3290   } *relr;
   3291 
   3292   /* Statistics.  */
   3293   unsigned long stub_count[ppc_stub_save_res];
   3294 
   3295   /* Number of stubs against global syms.  */
   3296   unsigned long stub_globals;
   3297 
   3298   /* Set if we're linking code with function descriptors.  */
   3299   unsigned int opd_abi:1;
   3300 
   3301   /* Support for multiple toc sections.  */
   3302   unsigned int do_multi_toc:1;
   3303   unsigned int multi_toc_needed:1;
   3304   unsigned int second_toc_pass:1;
   3305   unsigned int do_toc_opt:1;
   3306 
   3307   /* Set if tls optimization is enabled.  */
   3308   unsigned int do_tls_opt:1;
   3309 
   3310   /* Set if inline plt calls should be converted to direct calls.  */
   3311   unsigned int can_convert_all_inline_plt:1;
   3312 
   3313   /* Set if a stub_offset changed.  */
   3314   unsigned int stub_changed:1;
   3315 
   3316   /* Set on error.  */
   3317   unsigned int stub_error:1;
   3318 
   3319   /* Whether func_desc_adjust needs to be run over symbols.  */
   3320   unsigned int need_func_desc_adj:1;
   3321 
   3322   /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized.  */
   3323   unsigned int has_plt_localentry0:1;
   3324 
   3325   /* Whether calls are made via the PLT from NOTOC functions.  */
   3326   unsigned int notoc_plt:1;
   3327 
   3328   /* Whether any code linked seems to be Power10.  */
   3329   unsigned int has_power10_relocs:1;
   3330 
   3331   /* Incremented once for each stub sized.  */
   3332   unsigned int stub_id;
   3333 
   3334   /* Incremented every time we size stubs.  */
   3335   unsigned int stub_iteration;
   3336 
   3337 /* After 20 iterations of stub sizing we no longer allow stubs to
   3338    shrink.  This is to break out of a pathological case where adding
   3339    stubs or increasing their size on one iteration decreases section
   3340    gaps (perhaps due to alignment), which then results in smaller
   3341    stubs on the next iteration.  */
   3342 #define STUB_SHRINK_ITER 20
   3343 };
   3344 
   3345 /* Rename some of the generic section flags to better document how they
   3346    are used here.  */
   3347 
   3348 /* Nonzero if this section has TLS related relocations.  */
   3349 #define has_tls_reloc sec_flg0
   3350 
   3351 /* Nonzero if this section has a call to __tls_get_addr lacking marker
   3352    relocations.  */
   3353 #define nomark_tls_get_addr sec_flg1
   3354 
   3355 /* Nonzero if this section has any toc or got relocs.  */
   3356 #define has_toc_reloc sec_flg2
   3357 
   3358 /* Nonzero if this section has a call to another section that uses
   3359    the toc or got.  */
   3360 #define makes_toc_func_call sec_flg3
   3361 
   3362 /* Recursion protection when determining above flag.  */
   3363 #define call_check_in_progress sec_flg4
   3364 #define call_check_done sec_flg5
   3365 
   3366 /* Get the ppc64 ELF linker hash table from a link_info structure.  */
   3367 
   3368 #define ppc_hash_table(p) \
   3369   ((is_elf_hash_table ((p)->hash)					\
   3370     && elf_hash_table_id (elf_hash_table (p)) == PPC64_ELF_DATA)	\
   3371    ? (struct ppc_link_hash_table *) (p)->hash : NULL)
   3372 
   3373 #define ppc_stub_hash_lookup(table, string, create, copy) \
   3374   ((struct ppc_stub_hash_entry *) \
   3375    bfd_hash_lookup ((table), (string), (create), (copy)))
   3376 
   3377 #define ppc_branch_hash_lookup(table, string, create, copy) \
   3378   ((struct ppc_branch_hash_entry *) \
   3379    bfd_hash_lookup ((table), (string), (create), (copy)))
   3380 
   3381 /* Create an entry in the stub hash table.  */
   3382 
   3383 static struct bfd_hash_entry *
   3384 stub_hash_newfunc (struct bfd_hash_entry *entry,
   3385 		   struct bfd_hash_table *table,
   3386 		   const char *string)
   3387 {
   3388   /* Allocate the structure if it has not already been allocated by a
   3389      subclass.  */
   3390   if (entry == NULL)
   3391     {
   3392       entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
   3393       if (entry == NULL)
   3394 	return entry;
   3395     }
   3396 
   3397   /* Call the allocation method of the superclass.  */
   3398   entry = bfd_hash_newfunc (entry, table, string);
   3399   if (entry != NULL)
   3400     {
   3401       struct ppc_stub_hash_entry *eh;
   3402 
   3403       /* Initialize the local fields.  */
   3404       eh = (struct ppc_stub_hash_entry *) entry;
   3405       eh->type.main = ppc_stub_none;
   3406       eh->type.sub = ppc_stub_toc;
   3407       eh->type.r2save = 0;
   3408       eh->group = NULL;
   3409       eh->stub_offset = 0;
   3410       eh->target_value = 0;
   3411       eh->target_section = NULL;
   3412       eh->h = NULL;
   3413       eh->plt_ent = NULL;
   3414       eh->symtype = 0;
   3415       eh->other = 0;
   3416       eh->id = 0;
   3417     }
   3418 
   3419   return entry;
   3420 }
   3421 
   3422 /* Create an entry in the branch hash table.  */
   3423 
   3424 static struct bfd_hash_entry *
   3425 branch_hash_newfunc (struct bfd_hash_entry *entry,
   3426 		     struct bfd_hash_table *table,
   3427 		     const char *string)
   3428 {
   3429   /* Allocate the structure if it has not already been allocated by a
   3430      subclass.  */
   3431   if (entry == NULL)
   3432     {
   3433       entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
   3434       if (entry == NULL)
   3435 	return entry;
   3436     }
   3437 
   3438   /* Call the allocation method of the superclass.  */
   3439   entry = bfd_hash_newfunc (entry, table, string);
   3440   if (entry != NULL)
   3441     {
   3442       struct ppc_branch_hash_entry *eh;
   3443 
   3444       /* Initialize the local fields.  */
   3445       eh = (struct ppc_branch_hash_entry *) entry;
   3446       eh->offset = 0;
   3447       eh->iter = 0;
   3448     }
   3449 
   3450   return entry;
   3451 }
   3452 
   3453 /* Create an entry in a ppc64 ELF linker hash table.  */
   3454 
   3455 static struct bfd_hash_entry *
   3456 link_hash_newfunc (struct bfd_hash_entry *entry,
   3457 		   struct bfd_hash_table *table,
   3458 		   const char *string)
   3459 {
   3460   /* Allocate the structure if it has not already been allocated by a
   3461      subclass.  */
   3462   if (entry == NULL)
   3463     {
   3464       entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
   3465       if (entry == NULL)
   3466 	return entry;
   3467     }
   3468 
   3469   /* Call the allocation method of the superclass.  */
   3470   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
   3471   if (entry != NULL)
   3472     {
   3473       struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
   3474 
   3475       memset (&eh->u.stub_cache, 0,
   3476 	      (sizeof (struct ppc_link_hash_entry)
   3477 	       - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
   3478 
   3479       /* When making function calls, old ABI code references function entry
   3480 	 points (dot symbols), while new ABI code references the function
   3481 	 descriptor symbol.  We need to make any combination of reference and
   3482 	 definition work together, without breaking archive linking.
   3483 
   3484 	 For a defined function "foo" and an undefined call to "bar":
   3485 	 An old object defines "foo" and ".foo", references ".bar" (possibly
   3486 	 "bar" too).
   3487 	 A new object defines "foo" and references "bar".
   3488 
   3489 	 A new object thus has no problem with its undefined symbols being
   3490 	 satisfied by definitions in an old object.  On the other hand, the
   3491 	 old object won't have ".bar" satisfied by a new object.
   3492 
   3493 	 Keep a list of newly added dot-symbols.  */
   3494 
   3495       if (string[0] == '.')
   3496 	{
   3497 	  struct ppc_link_hash_table *htab;
   3498 
   3499 	  htab = (struct ppc_link_hash_table *) table;
   3500 	  eh->u.next_dot_sym = htab->dot_syms;
   3501 	  htab->dot_syms = eh;
   3502 	}
   3503     }
   3504 
   3505   return entry;
   3506 }
   3507 
   3508 struct tocsave_entry
   3509 {
   3510   asection *sec;
   3511   bfd_vma offset;
   3512 };
   3513 
   3514 static hashval_t
   3515 tocsave_htab_hash (const void *p)
   3516 {
   3517   const struct tocsave_entry *e = (const struct tocsave_entry *) p;
   3518   return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
   3519 }
   3520 
   3521 static int
   3522 tocsave_htab_eq (const void *p1, const void *p2)
   3523 {
   3524   const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
   3525   const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
   3526   return e1->sec == e2->sec && e1->offset == e2->offset;
   3527 }
   3528 
   3529 /* Destroy a ppc64 ELF linker hash table.  */
   3530 
   3531 static void
   3532 ppc64_elf_link_hash_table_free (bfd *obfd)
   3533 {
   3534   struct ppc_link_hash_table *htab;
   3535 
   3536   htab = (struct ppc_link_hash_table *) obfd->link.hash;
   3537   if (htab->tocsave_htab)
   3538     htab_delete (htab->tocsave_htab);
   3539   bfd_hash_table_free (&htab->branch_hash_table);
   3540   bfd_hash_table_free (&htab->stub_hash_table);
   3541   _bfd_elf_link_hash_table_free (obfd);
   3542 }
   3543 
   3544 /* Create a ppc64 ELF linker hash table.  */
   3545 
   3546 static struct bfd_link_hash_table *
   3547 ppc64_elf_link_hash_table_create (bfd *abfd)
   3548 {
   3549   struct ppc_link_hash_table *htab;
   3550   size_t amt = sizeof (struct ppc_link_hash_table);
   3551 
   3552   htab = bfd_zmalloc (amt);
   3553   if (htab == NULL)
   3554     return NULL;
   3555 
   3556   if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
   3557 				      sizeof (struct ppc_link_hash_entry),
   3558 				      PPC64_ELF_DATA))
   3559     {
   3560       free (htab);
   3561       return NULL;
   3562     }
   3563 
   3564   /* Init the stub hash table too.  */
   3565   if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
   3566 			    sizeof (struct ppc_stub_hash_entry)))
   3567     {
   3568       _bfd_elf_link_hash_table_free (abfd);
   3569       return NULL;
   3570     }
   3571 
   3572   /* And the branch hash table.  */
   3573   if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
   3574 			    sizeof (struct ppc_branch_hash_entry)))
   3575     {
   3576       bfd_hash_table_free (&htab->stub_hash_table);
   3577       _bfd_elf_link_hash_table_free (abfd);
   3578       return NULL;
   3579     }
   3580 
   3581   htab->tocsave_htab = htab_try_create (1024,
   3582 					tocsave_htab_hash,
   3583 					tocsave_htab_eq,
   3584 					NULL);
   3585   if (htab->tocsave_htab == NULL)
   3586     {
   3587       ppc64_elf_link_hash_table_free (abfd);
   3588       return NULL;
   3589     }
   3590   htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
   3591 
   3592   /* Initializing two fields of the union is just cosmetic.  We really
   3593      only care about glist, but when compiled on a 32-bit host the
   3594      bfd_vma fields are larger.  Setting the bfd_vma to zero makes
   3595      debugger inspection of these fields look nicer.  */
   3596   htab->elf.init_got_refcount.refcount = 0;
   3597   htab->elf.init_got_refcount.glist = NULL;
   3598   htab->elf.init_plt_refcount.refcount = 0;
   3599   htab->elf.init_plt_refcount.glist = NULL;
   3600   htab->elf.init_got_offset.offset = 0;
   3601   htab->elf.init_got_offset.glist = NULL;
   3602   htab->elf.init_plt_offset.offset = 0;
   3603   htab->elf.init_plt_offset.glist = NULL;
   3604 
   3605   return &htab->elf.root;
   3606 }
   3607 
   3608 /* Create sections for linker generated code.  */
   3609 
   3610 static bool
   3611 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
   3612 {
   3613   struct ppc_link_hash_table *htab;
   3614   flagword flags;
   3615 
   3616   htab = ppc_hash_table (info);
   3617 
   3618   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
   3619 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   3620   if (htab->params->save_restore_funcs)
   3621     {
   3622       /* Create .sfpr for code to save and restore fp regs.  */
   3623       htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
   3624 						       flags);
   3625       if (htab->sfpr == NULL
   3626 	  || !bfd_set_section_alignment (htab->sfpr, 2))
   3627 	return false;
   3628     }
   3629 
   3630   if (bfd_link_relocatable (info))
   3631     return true;
   3632 
   3633   /* Create .glink for lazy dynamic linking support.  */
   3634   htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
   3635 						    flags);
   3636   if (htab->glink == NULL
   3637       || !bfd_set_section_alignment (htab->glink, 3))
   3638     return false;
   3639 
   3640   /* The part of .glink used by global entry stubs, separate so that
   3641      it can be aligned appropriately without affecting htab->glink.  */
   3642   htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
   3643 							   flags);
   3644   if (htab->global_entry == NULL
   3645       || !bfd_set_section_alignment (htab->global_entry, 2))
   3646     return false;
   3647 
   3648   if (!info->no_ld_generated_unwind_info)
   3649     {
   3650       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
   3651 	       | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   3652       htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
   3653 								 ".eh_frame",
   3654 								 flags);
   3655       if (htab->glink_eh_frame == NULL
   3656 	  || !bfd_set_section_alignment (htab->glink_eh_frame, 2))
   3657 	return false;
   3658     }
   3659 
   3660   flags = SEC_ALLOC | SEC_LINKER_CREATED;
   3661   htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
   3662   if (htab->elf.iplt == NULL
   3663       || !bfd_set_section_alignment (htab->elf.iplt, 3))
   3664     return false;
   3665 
   3666   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
   3667 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   3668   htab->elf.irelplt
   3669     = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
   3670   if (htab->elf.irelplt == NULL
   3671       || !bfd_set_section_alignment (htab->elf.irelplt, 3))
   3672     return false;
   3673 
   3674   /* Create branch lookup table for plt_branch stubs.  */
   3675   flags = (SEC_ALLOC | SEC_LOAD
   3676 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   3677   htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
   3678 						   flags);
   3679   if (htab->brlt == NULL
   3680       || !bfd_set_section_alignment (htab->brlt, 3))
   3681     return false;
   3682 
   3683   /* Local plt entries, put in .branch_lt but a separate section for
   3684      convenience.  */
   3685   htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
   3686 						       flags);
   3687   if (htab->pltlocal == NULL
   3688       || !bfd_set_section_alignment (htab->pltlocal, 3))
   3689     return false;
   3690 
   3691   if (!bfd_link_pic (info))
   3692     return true;
   3693 
   3694   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
   3695 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   3696   htab->relbrlt
   3697     = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
   3698   if (htab->relbrlt == NULL
   3699       || !bfd_set_section_alignment (htab->relbrlt, 3))
   3700     return false;
   3701 
   3702   htab->relpltlocal
   3703     = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
   3704   if (htab->relpltlocal == NULL
   3705       || !bfd_set_section_alignment (htab->relpltlocal, 3))
   3706     return false;
   3707 
   3708   return true;
   3709 }
   3710 
   3711 /* Satisfy the ELF linker by filling in some fields in our fake bfd.  */
   3712 
   3713 bool
   3714 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
   3715 			 struct ppc64_elf_params *params)
   3716 {
   3717   struct ppc_link_hash_table *htab;
   3718 
   3719   elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
   3720 
   3721 /* Always hook our dynamic sections into the first bfd, which is the
   3722    linker created stub bfd.  This ensures that the GOT header is at
   3723    the start of the output TOC section.  */
   3724   htab = ppc_hash_table (info);
   3725   htab->elf.dynobj = params->stub_bfd;
   3726   htab->params = params;
   3727 
   3728   return create_linkage_sections (htab->elf.dynobj, info);
   3729 }
   3730 
   3731 /* Build a name for an entry in the stub hash table.  */
   3732 
   3733 static char *
   3734 ppc_stub_name (const asection *input_section,
   3735 	       const asection *sym_sec,
   3736 	       const struct ppc_link_hash_entry *h,
   3737 	       const Elf_Internal_Rela *rel)
   3738 {
   3739   char *stub_name;
   3740   ssize_t len;
   3741 
   3742   /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
   3743      offsets from a sym as a branch target?  In fact, we could
   3744      probably assume the addend is always zero.  */
   3745   BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
   3746 
   3747   if (h)
   3748     {
   3749       len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
   3750       stub_name = bfd_malloc (len);
   3751       if (stub_name == NULL)
   3752 	return stub_name;
   3753 
   3754       len = sprintf (stub_name, "%08x.%s+%x",
   3755 		     input_section->id & 0xffffffff,
   3756 		     h->elf.root.root.string,
   3757 		     (int) rel->r_addend & 0xffffffff);
   3758     }
   3759   else
   3760     {
   3761       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
   3762       stub_name = bfd_malloc (len);
   3763       if (stub_name == NULL)
   3764 	return stub_name;
   3765 
   3766       len = sprintf (stub_name, "%08x.%x:%x+%x",
   3767 		     input_section->id & 0xffffffff,
   3768 		     sym_sec->id & 0xffffffff,
   3769 		     (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
   3770 		     (int) rel->r_addend & 0xffffffff);
   3771     }
   3772   if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
   3773     stub_name[len - 2] = 0;
   3774   return stub_name;
   3775 }
   3776 
   3777 /* If mixing power10 with non-power10 code and --power10-stubs is not
   3778    specified (or is auto) then there may be multiple stub types for any
   3779    given symbol.  Up to three classes of stubs are stored in separate
   3780    stub_hash_table entries having the same key string.  The entries
   3781    will always be adjacent on entry->root.next chain, even if hash
   3782    table resizing occurs.  This function selects the correct entry to
   3783    use.  */
   3784 
   3785 static struct ppc_stub_hash_entry *
   3786 select_alt_stub (struct ppc_stub_hash_entry *entry,
   3787 		 enum elf_ppc64_reloc_type r_type)
   3788 {
   3789   enum ppc_stub_sub_type subt;
   3790 
   3791   switch (r_type)
   3792     {
   3793     case R_PPC64_REL24_NOTOC:
   3794       subt = ppc_stub_notoc;
   3795       break;
   3796     case R_PPC64_REL24_P9NOTOC:
   3797       subt = ppc_stub_p9notoc;
   3798       break;
   3799     default:
   3800       subt = ppc_stub_toc;
   3801       break;
   3802     }
   3803 
   3804   while (entry != NULL && entry->type.sub != subt)
   3805     {
   3806       const char *stub_name = entry->root.string;
   3807 
   3808       entry = (struct ppc_stub_hash_entry *) entry->root.next;
   3809       if (entry != NULL
   3810 	  && entry->root.string != stub_name)
   3811 	entry = NULL;
   3812     }
   3813 
   3814   return entry;
   3815 }
   3816 
   3817 /* Look up an entry in the stub hash.  Stub entries are cached because
   3818    creating the stub name takes a bit of time.  */
   3819 
   3820 static struct ppc_stub_hash_entry *
   3821 ppc_get_stub_entry (const asection *input_section,
   3822 		    const asection *sym_sec,
   3823 		    struct ppc_link_hash_entry *h,
   3824 		    const Elf_Internal_Rela *rel,
   3825 		    struct ppc_link_hash_table *htab)
   3826 {
   3827   struct ppc_stub_hash_entry *stub_entry;
   3828   struct map_stub *group;
   3829 
   3830   /* If this input section is part of a group of sections sharing one
   3831      stub section, then use the id of the first section in the group.
   3832      Stub names need to include a section id, as there may well be
   3833      more than one stub used to reach say, printf, and we need to
   3834      distinguish between them.  */
   3835   group = htab->sec_info[input_section->id].u.group;
   3836   if (group == NULL)
   3837     return NULL;
   3838 
   3839   if (h != NULL && h->u.stub_cache != NULL
   3840       && h->u.stub_cache->h == h
   3841       && h->u.stub_cache->group == group)
   3842     {
   3843       stub_entry = h->u.stub_cache;
   3844     }
   3845   else
   3846     {
   3847       char *stub_name;
   3848 
   3849       stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
   3850       if (stub_name == NULL)
   3851 	return NULL;
   3852 
   3853       stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
   3854 					 stub_name, false, false);
   3855       if (h != NULL)
   3856 	h->u.stub_cache = stub_entry;
   3857 
   3858       free (stub_name);
   3859     }
   3860 
   3861   if (stub_entry != NULL && htab->params->power10_stubs == -1)
   3862     stub_entry = select_alt_stub (stub_entry, ELF64_R_TYPE (rel->r_info));
   3863 
   3864   return stub_entry;
   3865 }
   3866 
   3867 /* Add a new stub entry to the stub hash.  Not all fields of the new
   3868    stub entry are initialised.  */
   3869 
   3870 static struct ppc_stub_hash_entry *
   3871 ppc_add_stub (const char *stub_name,
   3872 	      asection *section,
   3873 	      struct bfd_link_info *info)
   3874 {
   3875   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   3876   struct map_stub *group;
   3877   asection *link_sec;
   3878   asection *stub_sec;
   3879   struct ppc_stub_hash_entry *stub_entry;
   3880 
   3881   group = htab->sec_info[section->id].u.group;
   3882   link_sec = group->link_sec;
   3883   stub_sec = group->stub_sec;
   3884   if (stub_sec == NULL)
   3885     {
   3886       size_t namelen;
   3887       bfd_size_type len;
   3888       char *s_name;
   3889 
   3890       namelen = strlen (link_sec->name);
   3891       len = namelen + sizeof (STUB_SUFFIX);
   3892       s_name = bfd_alloc (htab->params->stub_bfd, len);
   3893       if (s_name == NULL)
   3894 	return NULL;
   3895 
   3896       memcpy (s_name, link_sec->name, namelen);
   3897       memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
   3898       stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
   3899       if (stub_sec == NULL)
   3900 	return NULL;
   3901       group->stub_sec = stub_sec;
   3902     }
   3903 
   3904   /* Enter this entry into the linker stub hash table.  */
   3905   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
   3906 				     true, false);
   3907   if (stub_entry == NULL)
   3908     {
   3909       /* xgettext:c-format */
   3910       _bfd_error_handler (_("%pB: cannot create stub entry %s"),
   3911 			  section->owner, stub_name);
   3912       return NULL;
   3913     }
   3914 
   3915   stub_entry->group = group;
   3916   stub_entry->stub_offset = 0;
   3917   return stub_entry;
   3918 }
   3919 
   3920 /* A stub has already been created, but it may not be the required
   3921    type.  We shouldn't be transitioning from plt_call to long_branch
   3922    stubs or vice versa, but we might be upgrading from plt_call to
   3923    plt_call with r2save for example.  */
   3924 
   3925 static bool
   3926 ppc_merge_stub (struct ppc_link_hash_table *htab,
   3927 		struct ppc_stub_hash_entry *stub_entry,
   3928 		struct ppc_stub_type stub_type,
   3929 		enum elf_ppc64_reloc_type r_type)
   3930 {
   3931   struct ppc_stub_type old_type = stub_entry->type;
   3932 
   3933   if (old_type.main == ppc_stub_save_res)
   3934     return true;
   3935 
   3936   if (htab->params->power10_stubs == -1)
   3937     {
   3938       /* For --power10-stubs=auto, don't merge _notoc and other
   3939 	 varieties of stubs.  */
   3940       struct ppc_stub_hash_entry *alt_stub;
   3941 
   3942       alt_stub = select_alt_stub (stub_entry, r_type);
   3943       if (alt_stub == NULL)
   3944 	{
   3945 	  alt_stub = ((struct ppc_stub_hash_entry *)
   3946 		      stub_hash_newfunc (NULL,
   3947 					 &htab->stub_hash_table,
   3948 					 stub_entry->root.string));
   3949 	  if (alt_stub == NULL)
   3950 	    return false;
   3951 
   3952 	  *alt_stub = *stub_entry;
   3953 	  stub_entry->root.next = &alt_stub->root;
   3954 
   3955 	  /* Sort notoc stubs first, then toc stubs, then p9notoc.
   3956 	     Not that it matters, this just puts smaller stubs first.  */
   3957 	  if (stub_type.sub == ppc_stub_notoc)
   3958 	    alt_stub = stub_entry;
   3959 	  else if (stub_type.sub == ppc_stub_p9notoc
   3960 		   && alt_stub->root.next
   3961 		   && alt_stub->root.next->string == alt_stub->root.string)
   3962 	    {
   3963 	      struct ppc_stub_hash_entry *next
   3964 		= (struct ppc_stub_hash_entry *) alt_stub->root.next;
   3965 	      alt_stub->type = next->type;
   3966 	      alt_stub = next;
   3967 	    }
   3968 	  alt_stub->type = stub_type;
   3969 	  return true;
   3970 	}
   3971       stub_entry = alt_stub;
   3972     }
   3973 
   3974   old_type = stub_entry->type;
   3975   if (old_type.main == ppc_stub_plt_branch)
   3976     old_type.main = ppc_stub_long_branch;
   3977 
   3978   if (old_type.main != stub_type.main
   3979       || (old_type.sub != stub_type.sub
   3980 	  && old_type.sub != ppc_stub_toc
   3981 	  && stub_type.sub != ppc_stub_toc))
   3982     abort ();
   3983 
   3984   stub_entry->type.sub |= stub_type.sub;
   3985   stub_entry->type.r2save |= stub_type.r2save;
   3986   return true;
   3987 }
   3988 
   3989 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
   3990    not already done.  */
   3991 
   3992 static bool
   3993 create_got_section (bfd *abfd, struct bfd_link_info *info)
   3994 {
   3995   asection *got, *relgot;
   3996   flagword flags;
   3997   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   3998 
   3999   if (!is_ppc64_elf (abfd))
   4000     return false;
   4001   if (htab == NULL)
   4002     return false;
   4003 
   4004   if (!htab->elf.sgot
   4005       && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
   4006     return false;
   4007 
   4008   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
   4009 	   | SEC_LINKER_CREATED);
   4010 
   4011   got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
   4012   if (!got
   4013       || !bfd_set_section_alignment (got, 3))
   4014     return false;
   4015 
   4016   relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
   4017 					       flags | SEC_READONLY);
   4018   if (!relgot
   4019       || !bfd_set_section_alignment (relgot, 3))
   4020     return false;
   4021 
   4022   ppc64_elf_tdata (abfd)->got = got;
   4023   ppc64_elf_tdata (abfd)->relgot = relgot;
   4024   return true;
   4025 }
   4026 
   4027 /* Follow indirect and warning symbol links.  */
   4028 
   4029 static inline struct bfd_link_hash_entry *
   4030 follow_link (struct bfd_link_hash_entry *h)
   4031 {
   4032   while (h->type == bfd_link_hash_indirect
   4033 	 || h->type == bfd_link_hash_warning)
   4034     h = h->u.i.link;
   4035   return h;
   4036 }
   4037 
   4038 static inline struct elf_link_hash_entry *
   4039 elf_follow_link (struct elf_link_hash_entry *h)
   4040 {
   4041   return (struct elf_link_hash_entry *) follow_link (&h->root);
   4042 }
   4043 
   4044 static inline struct ppc_link_hash_entry *
   4045 ppc_follow_link (struct ppc_link_hash_entry *h)
   4046 {
   4047   return ppc_elf_hash_entry (elf_follow_link (&h->elf));
   4048 }
   4049 
   4050 /* Merge PLT info on FROM with that on TO.  */
   4051 
   4052 static void
   4053 move_plt_plist (struct ppc_link_hash_entry *from,
   4054 		struct ppc_link_hash_entry *to)
   4055 {
   4056   if (from->elf.plt.plist != NULL)
   4057     {
   4058       if (to->elf.plt.plist != NULL)
   4059 	{
   4060 	  struct plt_entry **entp;
   4061 	  struct plt_entry *ent;
   4062 
   4063 	  for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
   4064 	    {
   4065 	      struct plt_entry *dent;
   4066 
   4067 	      for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
   4068 		if (dent->addend == ent->addend)
   4069 		  {
   4070 		    dent->plt.refcount += ent->plt.refcount;
   4071 		    *entp = ent->next;
   4072 		    break;
   4073 		  }
   4074 	      if (dent == NULL)
   4075 		entp = &ent->next;
   4076 	    }
   4077 	  *entp = to->elf.plt.plist;
   4078 	}
   4079 
   4080       to->elf.plt.plist = from->elf.plt.plist;
   4081       from->elf.plt.plist = NULL;
   4082     }
   4083 }
   4084 
   4085 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
   4086 
   4087 static void
   4088 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
   4089 				struct elf_link_hash_entry *dir,
   4090 				struct elf_link_hash_entry *ind)
   4091 {
   4092   struct ppc_link_hash_entry *edir, *eind;
   4093 
   4094   edir = ppc_elf_hash_entry (dir);
   4095   eind = ppc_elf_hash_entry (ind);
   4096 
   4097   edir->is_func |= eind->is_func;
   4098   edir->is_func_descriptor |= eind->is_func_descriptor;
   4099   edir->tls_mask |= eind->tls_mask;
   4100   if (eind->oh != NULL)
   4101     edir->oh = ppc_follow_link (eind->oh);
   4102 
   4103   if (edir->elf.versioned != versioned_hidden)
   4104     edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
   4105   edir->elf.ref_regular |= eind->elf.ref_regular;
   4106   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
   4107   edir->elf.non_got_ref |= eind->elf.non_got_ref;
   4108   edir->elf.needs_plt |= eind->elf.needs_plt;
   4109   edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
   4110 
   4111   /* If we were called to copy over info for a weak sym, don't copy
   4112      dyn_relocs, plt/got info, or dynindx.  We used to copy dyn_relocs
   4113      in order to simplify readonly_dynrelocs and save a field in the
   4114      symbol hash entry, but that means dyn_relocs can't be used in any
   4115      tests about a specific symbol, or affect other symbol flags which
   4116      are then tested.  */
   4117   if (eind->elf.root.type != bfd_link_hash_indirect)
   4118     return;
   4119 
   4120   /* Copy over any dynamic relocs we may have on the indirect sym.  */
   4121   if (ind->dyn_relocs != NULL)
   4122     {
   4123       if (dir->dyn_relocs != NULL)
   4124 	{
   4125 	  struct ppc_dyn_relocs **pp;
   4126 	  struct ppc_dyn_relocs *p;
   4127 
   4128 	  /* Add reloc counts against the indirect sym to the direct sym
   4129 	     list.  Merge any entries against the same section.  */
   4130 	  for (pp = (struct ppc_dyn_relocs **) &ind->dyn_relocs;
   4131 	       (p = *pp) != NULL;
   4132 	       )
   4133 	    {
   4134 	      struct ppc_dyn_relocs *q;
   4135 
   4136 	      for (q = (struct ppc_dyn_relocs *) dir->dyn_relocs;
   4137 		   q != NULL;
   4138 		   q = q->next)
   4139 		if (q->sec == p->sec)
   4140 		  {
   4141 		    q->count += p->count;
   4142 		    q->pc_count += p->pc_count;
   4143 		    q->rel_count += p->rel_count;
   4144 		    *pp = p->next;
   4145 		    break;
   4146 		  }
   4147 	      if (q == NULL)
   4148 		pp = &p->next;
   4149 	    }
   4150 	  *pp = (struct ppc_dyn_relocs *) dir->dyn_relocs;
   4151 	}
   4152 
   4153       dir->dyn_relocs = ind->dyn_relocs;
   4154       ind->dyn_relocs = NULL;
   4155     }
   4156 
   4157   /* Copy over got entries that we may have already seen to the
   4158      symbol which just became indirect.  */
   4159   if (eind->elf.got.glist != NULL)
   4160     {
   4161       if (edir->elf.got.glist != NULL)
   4162 	{
   4163 	  struct got_entry **entp;
   4164 	  struct got_entry *ent;
   4165 
   4166 	  for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
   4167 	    {
   4168 	      struct got_entry *dent;
   4169 
   4170 	      for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
   4171 		if (dent->addend == ent->addend
   4172 		    && dent->owner == ent->owner
   4173 		    && dent->tls_type == ent->tls_type)
   4174 		  {
   4175 		    dent->got.refcount += ent->got.refcount;
   4176 		    *entp = ent->next;
   4177 		    break;
   4178 		  }
   4179 	      if (dent == NULL)
   4180 		entp = &ent->next;
   4181 	    }
   4182 	  *entp = edir->elf.got.glist;
   4183 	}
   4184 
   4185       edir->elf.got.glist = eind->elf.got.glist;
   4186       eind->elf.got.glist = NULL;
   4187     }
   4188 
   4189   /* And plt entries.  */
   4190   move_plt_plist (eind, edir);
   4191 
   4192   if (eind->elf.dynindx != -1)
   4193     {
   4194       if (edir->elf.dynindx != -1)
   4195 	_bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
   4196 				edir->elf.dynstr_index);
   4197       edir->elf.dynindx = eind->elf.dynindx;
   4198       edir->elf.dynstr_index = eind->elf.dynstr_index;
   4199       eind->elf.dynindx = -1;
   4200       eind->elf.dynstr_index = 0;
   4201     }
   4202 }
   4203 
   4204 /* Find the function descriptor hash entry from the given function code
   4205    hash entry FH.  Link the entries via their OH fields.  */
   4206 
   4207 static struct ppc_link_hash_entry *
   4208 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
   4209 {
   4210   struct ppc_link_hash_entry *fdh = fh->oh;
   4211 
   4212   if (fdh == NULL)
   4213     {
   4214       const char *fd_name = fh->elf.root.root.string + 1;
   4215 
   4216       fdh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, fd_name,
   4217 						      false, false, false));
   4218       if (fdh == NULL)
   4219 	return fdh;
   4220 
   4221       fdh->is_func_descriptor = 1;
   4222       fdh->oh = fh;
   4223       fh->is_func = 1;
   4224       fh->oh = fdh;
   4225     }
   4226 
   4227   fdh = ppc_follow_link (fdh);
   4228   fdh->is_func_descriptor = 1;
   4229   fdh->oh = fh;
   4230   return fdh;
   4231 }
   4232 
   4233 /* Make a fake function descriptor sym for the undefined code sym FH.  */
   4234 
   4235 static struct ppc_link_hash_entry *
   4236 make_fdh (struct bfd_link_info *info,
   4237 	  struct ppc_link_hash_entry *fh)
   4238 {
   4239   bfd *abfd = fh->elf.root.u.undef.abfd;
   4240   struct bfd_link_hash_entry *bh = NULL;
   4241   struct ppc_link_hash_entry *fdh;
   4242   flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
   4243 		    ? BSF_WEAK
   4244 		    : BSF_GLOBAL);
   4245 
   4246   if (!_bfd_generic_link_add_one_symbol (info, abfd,
   4247 					 fh->elf.root.root.string + 1,
   4248 					 flags, bfd_und_section_ptr, 0,
   4249 					 NULL, false, false, &bh))
   4250     return NULL;
   4251 
   4252   fdh = (struct ppc_link_hash_entry *) bh;
   4253   fdh->elf.non_elf = 0;
   4254   fdh->fake = 1;
   4255   fdh->is_func_descriptor = 1;
   4256   fdh->oh = fh;
   4257   fh->is_func = 1;
   4258   fh->oh = fdh;
   4259   return fdh;
   4260 }
   4261 
   4262 /* Fix function descriptor symbols defined in .opd sections to be
   4263    function type.  */
   4264 
   4265 static bool
   4266 ppc64_elf_add_symbol_hook (bfd *ibfd,
   4267 			   struct bfd_link_info *info,
   4268 			   Elf_Internal_Sym *isym,
   4269 			   const char **name,
   4270 			   flagword *flags ATTRIBUTE_UNUSED,
   4271 			   asection **sec,
   4272 			   bfd_vma *value)
   4273 {
   4274   if (*sec != NULL
   4275       && strcmp ((*sec)->name, ".opd") == 0)
   4276     {
   4277       asection *code_sec;
   4278 
   4279       if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
   4280 	    || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
   4281 	isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
   4282 
   4283       /* If the symbol is a function defined in .opd, and the function
   4284 	 code is in a discarded group, let it appear to be undefined.  */
   4285       if (!bfd_link_relocatable (info)
   4286 	  && (*sec)->reloc_count != 0
   4287 	  && opd_entry_value (*sec, *value, &code_sec, NULL,
   4288 			      false) != (bfd_vma) -1
   4289 	  && discarded_section (code_sec))
   4290 	{
   4291 	  *sec = bfd_und_section_ptr;
   4292 	  isym->st_shndx = SHN_UNDEF;
   4293 	}
   4294     }
   4295   else if (*sec != NULL
   4296 	   && strcmp ((*sec)->name, ".toc") == 0
   4297 	   && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
   4298     {
   4299       struct ppc_link_hash_table *htab = ppc_hash_table (info);
   4300       if (htab != NULL)
   4301 	htab->params->object_in_toc = 1;
   4302     }
   4303 
   4304   if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
   4305     {
   4306       if (abiversion (ibfd) == 0)
   4307 	set_abiversion (ibfd, 2);
   4308       else if (abiversion (ibfd) == 1)
   4309 	{
   4310 	  _bfd_error_handler (_("symbol '%s' has invalid st_other"
   4311 				" for ABI version 1"), *name);
   4312 	  bfd_set_error (bfd_error_bad_value);
   4313 	  return false;
   4314 	}
   4315     }
   4316 
   4317   return true;
   4318 }
   4319 
   4320 /* Merge non-visibility st_other attributes: local entry point.  */
   4321 
   4322 static void
   4323 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
   4324 				  unsigned int st_other,
   4325 				  bool definition,
   4326 				  bool dynamic)
   4327 {
   4328   if (definition && (!dynamic || !h->def_regular))
   4329     h->other = ((st_other & ~ELF_ST_VISIBILITY (-1))
   4330 		| ELF_ST_VISIBILITY (h->other));
   4331 }
   4332 
   4333 /* Hook called on merging a symbol.  We use this to clear "fake" since
   4334    we now have a real symbol.  */
   4335 
   4336 static bool
   4337 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
   4338 			const Elf_Internal_Sym *isym,
   4339 			asection **psec ATTRIBUTE_UNUSED,
   4340 			bool newdef ATTRIBUTE_UNUSED,
   4341 			bool olddef ATTRIBUTE_UNUSED,
   4342 			bfd *oldbfd ATTRIBUTE_UNUSED,
   4343 			const asection *oldsec ATTRIBUTE_UNUSED)
   4344 {
   4345   ppc_elf_hash_entry (h)->fake = 0;
   4346   if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
   4347     ppc_elf_hash_entry (h)->non_zero_localentry = 1;
   4348   return true;
   4349 }
   4350 
   4351 /* This function makes an old ABI object reference to ".bar" cause the
   4352    inclusion of a new ABI object archive that defines "bar".
   4353    NAME is a symbol defined in an archive.  Return a symbol in the hash
   4354    table that might be satisfied by the archive symbols.  */
   4355 
   4356 static struct bfd_link_hash_entry *
   4357 ppc64_elf_archive_symbol_lookup (bfd *abfd,
   4358 				 struct bfd_link_info *info,
   4359 				 const char *name)
   4360 {
   4361   struct bfd_link_hash_entry *h;
   4362   char *dot_name;
   4363   size_t len;
   4364 
   4365   h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
   4366   if (h != NULL
   4367       && ppc_hash_table (info) != NULL
   4368       /* Don't return this sym if it is a fake function descriptor
   4369 	 created by add_symbol_adjust.  */
   4370       && !((struct ppc_link_hash_entry *) h)->fake)
   4371     return h;
   4372 
   4373   if (name[0] == '.')
   4374     return h;
   4375 
   4376   len = strlen (name);
   4377   dot_name = bfd_alloc (abfd, len + 2);
   4378   if (dot_name == NULL)
   4379     return (struct bfd_link_hash_entry *) -1;
   4380   dot_name[0] = '.';
   4381   memcpy (dot_name + 1, name, len + 1);
   4382   h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
   4383   bfd_release (abfd, dot_name);
   4384   if (h != NULL)
   4385     return h;
   4386 
   4387   if (strcmp (name, "__tls_get_addr_opt") == 0)
   4388     h = _bfd_elf_archive_symbol_lookup (abfd, info, "__tls_get_addr_desc");
   4389   return h;
   4390 }
   4391 
   4392 /* This function satisfies all old ABI object references to ".bar" if a
   4393    new ABI object defines "bar".  Well, at least, undefined dot symbols
   4394    are made weak.  This stops later archive searches from including an
   4395    object if we already have a function descriptor definition.  It also
   4396    prevents the linker complaining about undefined symbols.
   4397    We also check and correct mismatched symbol visibility here.  The
   4398    most restrictive visibility of the function descriptor and the
   4399    function entry symbol is used.  */
   4400 
   4401 static bool
   4402 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
   4403 {
   4404   struct ppc_link_hash_table *htab;
   4405   struct ppc_link_hash_entry *fdh;
   4406 
   4407   if (eh->elf.root.type == bfd_link_hash_warning)
   4408     eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
   4409 
   4410   if (eh->elf.root.type == bfd_link_hash_indirect)
   4411     return true;
   4412 
   4413   if (eh->elf.root.root.string[0] != '.')
   4414     abort ();
   4415 
   4416   htab = ppc_hash_table (info);
   4417   if (htab == NULL)
   4418     return false;
   4419 
   4420   fdh = lookup_fdh (eh, htab);
   4421   if (fdh == NULL
   4422       && !bfd_link_relocatable (info)
   4423       && (eh->elf.root.type == bfd_link_hash_undefined
   4424 	  || eh->elf.root.type == bfd_link_hash_undefweak)
   4425       && eh->elf.ref_regular)
   4426     {
   4427       /* Make an undefined function descriptor sym, in order to
   4428 	 pull in an --as-needed shared lib.  Archives are handled
   4429 	 elsewhere.  */
   4430       fdh = make_fdh (info, eh);
   4431       if (fdh == NULL)
   4432 	return false;
   4433     }
   4434 
   4435   if (fdh != NULL)
   4436     {
   4437       unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
   4438       unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
   4439 
   4440       /* Make both descriptor and entry symbol have the most
   4441 	 constraining visibility of either symbol.  */
   4442       if (entry_vis < descr_vis)
   4443 	fdh->elf.other += entry_vis - descr_vis;
   4444       else if (entry_vis > descr_vis)
   4445 	eh->elf.other += descr_vis - entry_vis;
   4446 
   4447       /* Propagate reference flags from entry symbol to function
   4448 	 descriptor symbol.  */
   4449       fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
   4450       fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
   4451       fdh->elf.ref_regular |= eh->elf.ref_regular;
   4452       fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
   4453 
   4454       if (!fdh->elf.forced_local
   4455 	  && fdh->elf.dynindx == -1
   4456 	  && fdh->elf.versioned != versioned_hidden
   4457 	  && (bfd_link_dll (info)
   4458 	      || fdh->elf.def_dynamic
   4459 	      || fdh->elf.ref_dynamic)
   4460 	  && (eh->elf.ref_regular
   4461 	      || eh->elf.def_regular))
   4462 	{
   4463 	  if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
   4464 	    return false;
   4465 	}
   4466     }
   4467 
   4468   return true;
   4469 }
   4470 
   4471 /* Set up opd section info and abiversion for IBFD, and process list
   4472    of dot-symbols we made in link_hash_newfunc.  */
   4473 
   4474 static bool
   4475 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
   4476 {
   4477   struct ppc_link_hash_table *htab;
   4478   struct ppc_link_hash_entry **p, *eh;
   4479   asection *opd = bfd_get_section_by_name (ibfd, ".opd");
   4480 
   4481   if (opd != NULL && opd->size != 0)
   4482     {
   4483       BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
   4484       ppc64_elf_section_data (opd)->sec_type = sec_opd;
   4485 
   4486       if (abiversion (ibfd) == 0)
   4487 	set_abiversion (ibfd, 1);
   4488       else if (abiversion (ibfd) >= 2)
   4489 	{
   4490 	  /* xgettext:c-format */
   4491 	  _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
   4492 			      ibfd, abiversion (ibfd));
   4493 	  bfd_set_error (bfd_error_bad_value);
   4494 	  return false;
   4495 	}
   4496     }
   4497 
   4498   if (is_ppc64_elf (info->output_bfd))
   4499     {
   4500       /* For input files without an explicit abiversion in e_flags
   4501 	 we should have flagged any with symbol st_other bits set
   4502 	 as ELFv1 and above flagged those with .opd as ELFv2.
   4503 	 Set the output abiversion if not yet set, and for any input
   4504 	 still ambiguous, take its abiversion from the output.
   4505 	 Differences in ABI are reported later.  */
   4506       if (abiversion (info->output_bfd) == 0)
   4507 	set_abiversion (info->output_bfd, abiversion (ibfd));
   4508       else if (abiversion (ibfd) == 0)
   4509 	set_abiversion (ibfd, abiversion (info->output_bfd));
   4510     }
   4511 
   4512   htab = ppc_hash_table (info);
   4513   if (htab == NULL)
   4514     return true;
   4515 
   4516   if (opd != NULL && opd->size != 0
   4517       && (ibfd->flags & DYNAMIC) == 0
   4518       && (opd->flags & SEC_RELOC) != 0
   4519       && opd->reloc_count != 0
   4520       && !bfd_is_abs_section (opd->output_section)
   4521       && info->gc_sections)
   4522     {
   4523       /* Garbage collection needs some extra help with .opd sections.
   4524 	 We don't want to necessarily keep everything referenced by
   4525 	 relocs in .opd, as that would keep all functions.  Instead,
   4526 	 if we reference an .opd symbol (a function descriptor), we
   4527 	 want to keep the function code symbol's section.  This is
   4528 	 easy for global symbols, but for local syms we need to keep
   4529 	 information about the associated function section.  */
   4530       bfd_size_type amt;
   4531       asection **opd_sym_map;
   4532       Elf_Internal_Shdr *symtab_hdr;
   4533       Elf_Internal_Rela *relocs, *rel_end, *rel;
   4534 
   4535       amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
   4536       opd_sym_map = bfd_zalloc (ibfd, amt);
   4537       if (opd_sym_map == NULL)
   4538 	return false;
   4539       ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
   4540       relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
   4541 					  info->keep_memory);
   4542       if (relocs == NULL)
   4543 	return false;
   4544       symtab_hdr = &elf_symtab_hdr (ibfd);
   4545       rel_end = relocs + opd->reloc_count - 1;
   4546       for (rel = relocs; rel < rel_end; rel++)
   4547 	{
   4548 	  enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
   4549 	  unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
   4550 
   4551 	  if (r_type == R_PPC64_ADDR64
   4552 	      && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
   4553 	      && r_symndx < symtab_hdr->sh_info)
   4554 	    {
   4555 	      Elf_Internal_Sym *isym;
   4556 	      asection *s;
   4557 
   4558 	      isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, ibfd,
   4559 					    r_symndx);
   4560 	      if (isym == NULL)
   4561 		{
   4562 		  if (elf_section_data (opd)->relocs != relocs)
   4563 		    free (relocs);
   4564 		  return false;
   4565 		}
   4566 
   4567 	      s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
   4568 	      if (s != NULL && s != opd)
   4569 		opd_sym_map[OPD_NDX (rel->r_offset)] = s;
   4570 	    }
   4571 	}
   4572       if (elf_section_data (opd)->relocs != relocs)
   4573 	free (relocs);
   4574     }
   4575 
   4576   p = &htab->dot_syms;
   4577   while ((eh = *p) != NULL)
   4578     {
   4579       *p = NULL;
   4580       if (&eh->elf == htab->elf.hgot)
   4581 	;
   4582       else if (htab->elf.hgot == NULL
   4583 	       && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
   4584 	htab->elf.hgot = &eh->elf;
   4585       else if (abiversion (ibfd) <= 1)
   4586 	{
   4587 	  htab->need_func_desc_adj = 1;
   4588 	  if (!add_symbol_adjust (eh, info))
   4589 	    return false;
   4590 	}
   4591       p = &eh->u.next_dot_sym;
   4592     }
   4593   return true;
   4594 }
   4595 
   4596 /* Undo hash table changes when an --as-needed input file is determined
   4597    not to be needed.  */
   4598 
   4599 static bool
   4600 ppc64_elf_notice_as_needed (bfd *ibfd,
   4601 			    struct bfd_link_info *info,
   4602 			    enum notice_asneeded_action act)
   4603 {
   4604   if (act == notice_not_needed)
   4605     {
   4606       struct ppc_link_hash_table *htab = ppc_hash_table (info);
   4607 
   4608       if (htab == NULL)
   4609 	return false;
   4610 
   4611       htab->dot_syms = NULL;
   4612     }
   4613   return _bfd_elf_notice_as_needed (ibfd, info, act);
   4614 }
   4615 
   4616 /* If --just-symbols against a final linked binary, then assume we need
   4617    toc adjusting stubs when calling functions defined there.  */
   4618 
   4619 static void
   4620 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
   4621 {
   4622   if ((sec->flags & SEC_CODE) != 0
   4623       && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
   4624       && is_ppc64_elf (sec->owner))
   4625     {
   4626       if (abiversion (sec->owner) >= 2
   4627 	  || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
   4628 	sec->has_toc_reloc = 1;
   4629     }
   4630   _bfd_elf_link_just_syms (sec, info);
   4631 }
   4632 
   4633 static struct plt_entry **
   4634 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
   4635 		       unsigned long r_symndx, bfd_vma r_addend, int tls_type)
   4636 {
   4637   struct got_entry **local_got_ents = elf_local_got_ents (abfd);
   4638   struct plt_entry **local_plt;
   4639   unsigned char *local_got_tls_masks;
   4640 
   4641   if (local_got_ents == NULL)
   4642     {
   4643       bfd_size_type size = symtab_hdr->sh_info;
   4644 
   4645       size *= (sizeof (*local_got_ents)
   4646 	       + sizeof (*local_plt)
   4647 	       + sizeof (*local_got_tls_masks));
   4648       local_got_ents = bfd_zalloc (abfd, size);
   4649       if (local_got_ents == NULL)
   4650 	return NULL;
   4651       elf_local_got_ents (abfd) = local_got_ents;
   4652     }
   4653 
   4654   if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
   4655     {
   4656       struct got_entry *ent;
   4657 
   4658       for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
   4659 	if (ent->addend == r_addend
   4660 	    && ent->owner == abfd
   4661 	    && ent->tls_type == tls_type)
   4662 	  break;
   4663       if (ent == NULL)
   4664 	{
   4665 	  size_t amt = sizeof (*ent);
   4666 	  ent = bfd_alloc (abfd, amt);
   4667 	  if (ent == NULL)
   4668 	    return false;
   4669 	  ent->next = local_got_ents[r_symndx];
   4670 	  ent->addend = r_addend;
   4671 	  ent->owner = abfd;
   4672 	  ent->tls_type = tls_type;
   4673 	  ent->is_indirect = false;
   4674 	  ent->got.refcount = 0;
   4675 	  local_got_ents[r_symndx] = ent;
   4676 	}
   4677       ent->got.refcount += 1;
   4678     }
   4679 
   4680   local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
   4681   local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
   4682   local_got_tls_masks[r_symndx] |= tls_type & 0xff;
   4683 
   4684   return local_plt + r_symndx;
   4685 }
   4686 
   4687 static bool
   4688 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
   4689 {
   4690   struct plt_entry *ent;
   4691 
   4692   for (ent = *plist; ent != NULL; ent = ent->next)
   4693     if (ent->addend == addend)
   4694       break;
   4695   if (ent == NULL)
   4696     {
   4697       size_t amt = sizeof (*ent);
   4698       ent = bfd_alloc (abfd, amt);
   4699       if (ent == NULL)
   4700 	return false;
   4701       ent->next = *plist;
   4702       ent->addend = addend;
   4703       ent->plt.refcount = 0;
   4704       *plist = ent;
   4705     }
   4706   ent->plt.refcount += 1;
   4707   return true;
   4708 }
   4709 
   4710 static bool
   4711 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
   4712 {
   4713   return (r_type == R_PPC64_REL24
   4714 	  || r_type == R_PPC64_REL24_NOTOC
   4715 	  || r_type == R_PPC64_REL24_P9NOTOC
   4716 	  || r_type == R_PPC64_REL14
   4717 	  || r_type == R_PPC64_REL14_BRTAKEN
   4718 	  || r_type == R_PPC64_REL14_BRNTAKEN
   4719 	  || r_type == R_PPC64_ADDR24
   4720 	  || r_type == R_PPC64_ADDR14
   4721 	  || r_type == R_PPC64_ADDR14_BRTAKEN
   4722 	  || r_type == R_PPC64_ADDR14_BRNTAKEN
   4723 	  || r_type == R_PPC64_PLTCALL
   4724 	  || r_type == R_PPC64_PLTCALL_NOTOC);
   4725 }
   4726 
   4727 /* Relocs on inline plt call sequence insns prior to the call.  */
   4728 
   4729 static bool
   4730 is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
   4731 {
   4732   return (r_type == R_PPC64_PLT16_HA
   4733 	  || r_type == R_PPC64_PLT16_HI
   4734 	  || r_type == R_PPC64_PLT16_LO
   4735 	  || r_type == R_PPC64_PLT16_LO_DS
   4736 	  || r_type == R_PPC64_PLT_PCREL34
   4737 	  || r_type == R_PPC64_PLT_PCREL34_NOTOC
   4738 	  || r_type == R_PPC64_PLTSEQ
   4739 	  || r_type == R_PPC64_PLTSEQ_NOTOC);
   4740 }
   4741 
   4742 /* Of relocs which might appear paired with TLSGD and TLSLD marker
   4743    relocs, return true for those that operate on a dword.  */
   4744 
   4745 static bool
   4746 is_8byte_reloc (enum elf_ppc64_reloc_type r_type)
   4747 {
   4748   return (r_type == R_PPC64_PLT_PCREL34
   4749 	  || r_type == R_PPC64_PLT_PCREL34_NOTOC
   4750 	  || r_type == R_PPC64_PLTCALL);
   4751 }
   4752 
   4753 /* Like bfd_reloc_offset_in_range but without a howto.  Return true
   4754    iff a field of SIZE bytes at OFFSET is within SEC limits.  */
   4755 
   4756 static bool
   4757 offset_in_range (asection *sec, bfd_vma offset, size_t size)
   4758 {
   4759   return offset <= sec->size && size <= sec->size - offset;
   4760 }
   4761 
   4762 /* Look through the relocs for a section during the first phase, and
   4763    calculate needed space in the global offset table, procedure
   4764    linkage table, and dynamic reloc sections.  */
   4765 
   4766 static bool
   4767 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
   4768 			asection *sec, const Elf_Internal_Rela *relocs)
   4769 {
   4770   struct ppc_link_hash_table *htab;
   4771   Elf_Internal_Shdr *symtab_hdr;
   4772   struct elf_link_hash_entry **sym_hashes;
   4773   const Elf_Internal_Rela *rel;
   4774   const Elf_Internal_Rela *rel_end;
   4775   asection *sreloc;
   4776   struct elf_link_hash_entry *tga, *dottga;
   4777   bool is_opd;
   4778 
   4779   if (bfd_link_relocatable (info))
   4780     return true;
   4781 
   4782   BFD_ASSERT (is_ppc64_elf (abfd));
   4783 
   4784   htab = ppc_hash_table (info);
   4785   if (htab == NULL)
   4786     return false;
   4787 
   4788   tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
   4789 			      false, false, true);
   4790   dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
   4791 				 false, false, true);
   4792   symtab_hdr = &elf_symtab_hdr (abfd);
   4793   sym_hashes = elf_sym_hashes (abfd);
   4794   sreloc = NULL;
   4795   is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
   4796   rel_end = relocs + sec->reloc_count;
   4797   for (rel = relocs; rel < rel_end; rel++)
   4798     {
   4799       unsigned long r_symndx;
   4800       struct elf_link_hash_entry *h;
   4801       Elf_Internal_Sym *isym;
   4802       enum elf_ppc64_reloc_type r_type;
   4803       int tls_type;
   4804       struct _ppc64_elf_section_data *ppc64_sec;
   4805       struct plt_entry **ifunc, **plt_list;
   4806 
   4807       r_symndx = ELF64_R_SYM (rel->r_info);
   4808       if (r_symndx < symtab_hdr->sh_info)
   4809 	{
   4810 	  h = NULL;
   4811 	  isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, abfd, r_symndx);
   4812 	  if (isym == NULL)
   4813 	    return false;
   4814 	}
   4815       else
   4816 	{
   4817 	  isym = NULL;
   4818 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   4819 	  h = elf_follow_link (h);
   4820 
   4821 	  if (h == htab->elf.hgot)
   4822 	    sec->has_toc_reloc = 1;
   4823 	}
   4824 
   4825       r_type = ELF64_R_TYPE (rel->r_info);
   4826       switch (r_type)
   4827 	{
   4828 	case R_PPC64_D34:
   4829 	case R_PPC64_D34_LO:
   4830 	case R_PPC64_D34_HI30:
   4831 	case R_PPC64_D34_HA30:
   4832 	case R_PPC64_D28:
   4833 	case R_PPC64_TPREL34:
   4834 	case R_PPC64_DTPREL34:
   4835 	case R_PPC64_PCREL34:
   4836 	case R_PPC64_GOT_PCREL34:
   4837 	case R_PPC64_GOT_TLSGD_PCREL34:
   4838 	case R_PPC64_GOT_TLSLD_PCREL34:
   4839 	case R_PPC64_GOT_TPREL_PCREL34:
   4840 	case R_PPC64_GOT_DTPREL_PCREL34:
   4841 	case R_PPC64_PLT_PCREL34:
   4842 	case R_PPC64_PLT_PCREL34_NOTOC:
   4843 	case R_PPC64_PCREL28:
   4844 	  htab->has_power10_relocs = 1;
   4845 	  break;
   4846 	default:
   4847 	  break;
   4848 	}
   4849 
   4850       switch (r_type)
   4851 	{
   4852 	case R_PPC64_PLT16_HA:
   4853 	case R_PPC64_GOT_TLSLD16_HA:
   4854 	case R_PPC64_GOT_TLSGD16_HA:
   4855 	case R_PPC64_GOT_TPREL16_HA:
   4856 	case R_PPC64_GOT_DTPREL16_HA:
   4857 	case R_PPC64_GOT16_HA:
   4858 	case R_PPC64_TOC16_HA:
   4859 	case R_PPC64_PLT16_LO:
   4860 	case R_PPC64_PLT16_LO_DS:
   4861 	case R_PPC64_GOT_TLSLD16_LO:
   4862 	case R_PPC64_GOT_TLSGD16_LO:
   4863 	case R_PPC64_GOT_TPREL16_LO_DS:
   4864 	case R_PPC64_GOT_DTPREL16_LO_DS:
   4865 	case R_PPC64_GOT16_LO:
   4866 	case R_PPC64_GOT16_LO_DS:
   4867 	case R_PPC64_TOC16_LO:
   4868 	case R_PPC64_TOC16_LO_DS:
   4869 	case R_PPC64_GOT_PCREL34:
   4870 	  ppc64_elf_tdata (abfd)->has_optrel = 1;
   4871 	  ppc64_elf_section_data (sec)->has_optrel = 1;
   4872 	  break;
   4873 	default:
   4874 	  break;
   4875 	}
   4876 
   4877       ifunc = NULL;
   4878       if (h != NULL)
   4879 	{
   4880 	  if (h->type == STT_GNU_IFUNC)
   4881 	    {
   4882 	      h->needs_plt = 1;
   4883 	      ifunc = &h->plt.plist;
   4884 	    }
   4885 	}
   4886       else
   4887 	{
   4888 	  if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
   4889 	    {
   4890 	      ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
   4891 					     rel->r_addend,
   4892 					     NON_GOT | PLT_IFUNC);
   4893 	      if (ifunc == NULL)
   4894 		return false;
   4895 	    }
   4896 	}
   4897 
   4898       tls_type = 0;
   4899       switch (r_type)
   4900 	{
   4901 	case R_PPC64_TLSGD:
   4902 	case R_PPC64_TLSLD:
   4903 	  /* These special tls relocs tie a call to __tls_get_addr with
   4904 	     its parameter symbol.  */
   4905 	  if (h != NULL)
   4906 	    ppc_elf_hash_entry (h)->tls_mask |= TLS_TLS | TLS_MARK;
   4907 	  else
   4908 	    if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
   4909 					rel->r_addend,
   4910 					NON_GOT | TLS_TLS | TLS_MARK))
   4911 	      return false;
   4912 	  sec->has_tls_reloc = 1;
   4913 	  break;
   4914 
   4915 	case R_PPC64_GOT_TLSLD16:
   4916 	case R_PPC64_GOT_TLSLD16_LO:
   4917 	case R_PPC64_GOT_TLSLD16_HI:
   4918 	case R_PPC64_GOT_TLSLD16_HA:
   4919 	case R_PPC64_GOT_TLSLD_PCREL34:
   4920 	  tls_type = TLS_TLS | TLS_LD;
   4921 	  goto dogottls;
   4922 
   4923 	case R_PPC64_GOT_TLSGD16:
   4924 	case R_PPC64_GOT_TLSGD16_LO:
   4925 	case R_PPC64_GOT_TLSGD16_HI:
   4926 	case R_PPC64_GOT_TLSGD16_HA:
   4927 	case R_PPC64_GOT_TLSGD_PCREL34:
   4928 	  tls_type = TLS_TLS | TLS_GD;
   4929 	  goto dogottls;
   4930 
   4931 	case R_PPC64_GOT_TPREL16_DS:
   4932 	case R_PPC64_GOT_TPREL16_LO_DS:
   4933 	case R_PPC64_GOT_TPREL16_HI:
   4934 	case R_PPC64_GOT_TPREL16_HA:
   4935 	case R_PPC64_GOT_TPREL_PCREL34:
   4936 	  if (bfd_link_dll (info))
   4937 	    info->flags |= DF_STATIC_TLS;
   4938 	  tls_type = TLS_TLS | TLS_TPREL;
   4939 	  goto dogottls;
   4940 
   4941 	case R_PPC64_GOT_DTPREL16_DS:
   4942 	case R_PPC64_GOT_DTPREL16_LO_DS:
   4943 	case R_PPC64_GOT_DTPREL16_HI:
   4944 	case R_PPC64_GOT_DTPREL16_HA:
   4945 	case R_PPC64_GOT_DTPREL_PCREL34:
   4946 	  tls_type = TLS_TLS | TLS_DTPREL;
   4947 	dogottls:
   4948 	  sec->has_tls_reloc = 1;
   4949 	  goto dogot;
   4950 
   4951 	case R_PPC64_GOT16:
   4952 	case R_PPC64_GOT16_LO:
   4953 	case R_PPC64_GOT16_HI:
   4954 	case R_PPC64_GOT16_HA:
   4955 	case R_PPC64_GOT16_DS:
   4956 	case R_PPC64_GOT16_LO_DS:
   4957 	case R_PPC64_GOT_PCREL34:
   4958 	dogot:
   4959 	  /* This symbol requires a global offset table entry.  */
   4960 	  sec->has_toc_reloc = 1;
   4961 	  if (r_type == R_PPC64_GOT_TLSLD16
   4962 	      || r_type == R_PPC64_GOT_TLSGD16
   4963 	      || r_type == R_PPC64_GOT_TPREL16_DS
   4964 	      || r_type == R_PPC64_GOT_DTPREL16_DS
   4965 	      || r_type == R_PPC64_GOT16
   4966 	      || r_type == R_PPC64_GOT16_DS)
   4967 	    {
   4968 	      htab->do_multi_toc = 1;
   4969 	      ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
   4970 	    }
   4971 
   4972 	  if (ppc64_elf_tdata (abfd)->got == NULL
   4973 	      && !create_got_section (abfd, info))
   4974 	    return false;
   4975 
   4976 	  if (h != NULL)
   4977 	    {
   4978 	      struct ppc_link_hash_entry *eh;
   4979 	      struct got_entry *ent;
   4980 
   4981 	      eh = ppc_elf_hash_entry (h);
   4982 	      for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
   4983 		if (ent->addend == rel->r_addend
   4984 		    && ent->owner == abfd
   4985 		    && ent->tls_type == tls_type)
   4986 		  break;
   4987 	      if (ent == NULL)
   4988 		{
   4989 		  size_t amt = sizeof (*ent);
   4990 		  ent = bfd_alloc (abfd, amt);
   4991 		  if (ent == NULL)
   4992 		    return false;
   4993 		  ent->next = eh->elf.got.glist;
   4994 		  ent->addend = rel->r_addend;
   4995 		  ent->owner = abfd;
   4996 		  ent->tls_type = tls_type;
   4997 		  ent->is_indirect = false;
   4998 		  ent->got.refcount = 0;
   4999 		  eh->elf.got.glist = ent;
   5000 		}
   5001 	      ent->got.refcount += 1;
   5002 	      eh->tls_mask |= tls_type;
   5003 	    }
   5004 	  else
   5005 	    /* This is a global offset table entry for a local symbol.  */
   5006 	    if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
   5007 					rel->r_addend, tls_type))
   5008 	      return false;
   5009 	  break;
   5010 
   5011 	case R_PPC64_PLT16_HA:
   5012 	case R_PPC64_PLT16_HI:
   5013 	case R_PPC64_PLT16_LO:
   5014 	case R_PPC64_PLT16_LO_DS:
   5015 	case R_PPC64_PLT_PCREL34:
   5016 	case R_PPC64_PLT_PCREL34_NOTOC:
   5017 	case R_PPC64_PLT32:
   5018 	case R_PPC64_PLT64:
   5019 	  /* This symbol requires a procedure linkage table entry.  */
   5020 	  plt_list = ifunc;
   5021 	  if (h != NULL)
   5022 	    {
   5023 	      h->needs_plt = 1;
   5024 	      if (h->root.root.string[0] == '.'
   5025 		  && h->root.root.string[1] != '\0')
   5026 		ppc_elf_hash_entry (h)->is_func = 1;
   5027 	      ppc_elf_hash_entry (h)->tls_mask |= PLT_KEEP;
   5028 	      plt_list = &h->plt.plist;
   5029 	    }
   5030 	  if (plt_list == NULL)
   5031 	    plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
   5032 					      rel->r_addend,
   5033 					      NON_GOT | PLT_KEEP);
   5034 	  if (!update_plt_info (abfd, plt_list, rel->r_addend))
   5035 	    return false;
   5036 	  break;
   5037 
   5038 	  /* The following relocations don't need to propagate the
   5039 	     relocation if linking a shared object since they are
   5040 	     section relative.  */
   5041 	case R_PPC64_SECTOFF:
   5042 	case R_PPC64_SECTOFF_LO:
   5043 	case R_PPC64_SECTOFF_HI:
   5044 	case R_PPC64_SECTOFF_HA:
   5045 	case R_PPC64_SECTOFF_DS:
   5046 	case R_PPC64_SECTOFF_LO_DS:
   5047 	case R_PPC64_DTPREL16:
   5048 	case R_PPC64_DTPREL16_LO:
   5049 	case R_PPC64_DTPREL16_HI:
   5050 	case R_PPC64_DTPREL16_HA:
   5051 	case R_PPC64_DTPREL16_DS:
   5052 	case R_PPC64_DTPREL16_LO_DS:
   5053 	case R_PPC64_DTPREL16_HIGH:
   5054 	case R_PPC64_DTPREL16_HIGHA:
   5055 	case R_PPC64_DTPREL16_HIGHER:
   5056 	case R_PPC64_DTPREL16_HIGHERA:
   5057 	case R_PPC64_DTPREL16_HIGHEST:
   5058 	case R_PPC64_DTPREL16_HIGHESTA:
   5059 	  break;
   5060 
   5061 	  /* Nor do these.  */
   5062 	case R_PPC64_REL16:
   5063 	case R_PPC64_REL16_LO:
   5064 	case R_PPC64_REL16_HI:
   5065 	case R_PPC64_REL16_HA:
   5066 	case R_PPC64_REL16_HIGH:
   5067 	case R_PPC64_REL16_HIGHA:
   5068 	case R_PPC64_REL16_HIGHER:
   5069 	case R_PPC64_REL16_HIGHERA:
   5070 	case R_PPC64_REL16_HIGHEST:
   5071 	case R_PPC64_REL16_HIGHESTA:
   5072 	case R_PPC64_REL16_HIGHER34:
   5073 	case R_PPC64_REL16_HIGHERA34:
   5074 	case R_PPC64_REL16_HIGHEST34:
   5075 	case R_PPC64_REL16_HIGHESTA34:
   5076 	case R_PPC64_REL16DX_HA:
   5077 	  break;
   5078 
   5079 	  /* Not supported as a dynamic relocation.  */
   5080 	case R_PPC64_ADDR64_LOCAL:
   5081 	  if (bfd_link_pic (info))
   5082 	    {
   5083 	      if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
   5084 		ppc_howto_init ();
   5085 	      /* xgettext:c-format */
   5086 	      info->callbacks->einfo (_("%H: %s reloc unsupported "
   5087 					"in shared libraries and PIEs\n"),
   5088 				      abfd, sec, rel->r_offset,
   5089 				      ppc64_elf_howto_table[r_type]->name);
   5090 	      bfd_set_error (bfd_error_bad_value);
   5091 	      return false;
   5092 	    }
   5093 	  break;
   5094 
   5095 	case R_PPC64_TOC16:
   5096 	case R_PPC64_TOC16_DS:
   5097 	  htab->do_multi_toc = 1;
   5098 	  ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
   5099 	  /* Fall through.  */
   5100 	case R_PPC64_TOC16_LO:
   5101 	case R_PPC64_TOC16_HI:
   5102 	case R_PPC64_TOC16_HA:
   5103 	case R_PPC64_TOC16_LO_DS:
   5104 	  sec->has_toc_reloc = 1;
   5105 	  if (h != NULL && bfd_link_executable (info))
   5106 	    {
   5107 	      /* We may need a copy reloc.  */
   5108 	      h->non_got_ref = 1;
   5109 	      /* Strongly prefer a copy reloc over a dynamic reloc.
   5110 		 glibc ld.so as of 2019-08 will error out if one of
   5111 		 these relocations is emitted.  */
   5112 	      h->needs_copy = 1;
   5113 	      goto dodyn;
   5114 	    }
   5115 	  break;
   5116 
   5117 	  /* Marker reloc.  */
   5118 	case R_PPC64_ENTRY:
   5119 	  break;
   5120 
   5121 	  /* This relocation describes the C++ object vtable hierarchy.
   5122 	     Reconstruct it for later use during GC.  */
   5123 	case R_PPC64_GNU_VTINHERIT:
   5124 	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
   5125 	    return false;
   5126 	  break;
   5127 
   5128 	  /* This relocation describes which C++ vtable entries are actually
   5129 	     used.  Record for later use during GC.  */
   5130 	case R_PPC64_GNU_VTENTRY:
   5131 	  if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
   5132 	    return false;
   5133 	  break;
   5134 
   5135 	case R_PPC64_REL14:
   5136 	case R_PPC64_REL14_BRTAKEN:
   5137 	case R_PPC64_REL14_BRNTAKEN:
   5138 	  {
   5139 	    asection *dest = NULL;
   5140 
   5141 	    /* Heuristic: If jumping outside our section, chances are
   5142 	       we are going to need a stub.  */
   5143 	    if (h != NULL)
   5144 	      {
   5145 		/* If the sym is weak it may be overridden later, so
   5146 		   don't assume we know where a weak sym lives.  */
   5147 		if (h->root.type == bfd_link_hash_defined)
   5148 		  dest = h->root.u.def.section;
   5149 	      }
   5150 	    else
   5151 	      dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
   5152 
   5153 	    if (dest != sec)
   5154 	      ppc64_elf_section_data (sec)->has_14bit_branch = 1;
   5155 	  }
   5156 	  goto rel24;
   5157 
   5158 	case R_PPC64_PLTCALL:
   5159 	case R_PPC64_PLTCALL_NOTOC:
   5160 	  ppc64_elf_section_data (sec)->has_pltcall = 1;
   5161 	  /* Fall through.  */
   5162 
   5163 	case R_PPC64_REL24:
   5164 	case R_PPC64_REL24_NOTOC:
   5165 	case R_PPC64_REL24_P9NOTOC:
   5166 	rel24:
   5167 	  plt_list = ifunc;
   5168 	  if (h != NULL)
   5169 	    {
   5170 	      h->needs_plt = 1;
   5171 	      if (h->root.root.string[0] == '.'
   5172 		  && h->root.root.string[1] != '\0')
   5173 		ppc_elf_hash_entry (h)->is_func = 1;
   5174 
   5175 	      if (h == tga || h == dottga)
   5176 		{
   5177 		  sec->has_tls_reloc = 1;
   5178 		  if (rel != relocs
   5179 		      && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
   5180 			  || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
   5181 		    /* We have a new-style __tls_get_addr call with
   5182 		       a marker reloc.  */
   5183 		    ;
   5184 		  else
   5185 		    /* Mark this section as having an old-style call.  */
   5186 		    sec->nomark_tls_get_addr = 1;
   5187 		}
   5188 	      plt_list = &h->plt.plist;
   5189 	    }
   5190 
   5191 	  /* We may need a .plt entry if the function this reloc
   5192 	     refers to is in a shared lib.  */
   5193 	  if (plt_list
   5194 	      && !update_plt_info (abfd, plt_list, rel->r_addend))
   5195 	    return false;
   5196 	  break;
   5197 
   5198 	case R_PPC64_ADDR14:
   5199 	case R_PPC64_ADDR14_BRNTAKEN:
   5200 	case R_PPC64_ADDR14_BRTAKEN:
   5201 	case R_PPC64_ADDR24:
   5202 	  goto dodyn;
   5203 
   5204 	case R_PPC64_TPREL64:
   5205 	  tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
   5206 	  if (bfd_link_dll (info))
   5207 	    info->flags |= DF_STATIC_TLS;
   5208 	  goto dotlstoc;
   5209 
   5210 	case R_PPC64_DTPMOD64:
   5211 	  if (rel + 1 < rel_end
   5212 	      && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
   5213 	      && rel[1].r_offset == rel->r_offset + 8)
   5214 	    tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
   5215 	  else
   5216 	    tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
   5217 	  goto dotlstoc;
   5218 
   5219 	case R_PPC64_DTPREL64:
   5220 	  tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
   5221 	  if (rel != relocs
   5222 	      && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
   5223 	      && rel[-1].r_offset == rel->r_offset - 8)
   5224 	    /* This is the second reloc of a dtpmod, dtprel pair.
   5225 	       Don't mark with TLS_DTPREL.  */
   5226 	    goto dodyn;
   5227 
   5228 	dotlstoc:
   5229 	  sec->has_tls_reloc = 1;
   5230 	  if (h != NULL)
   5231 	    ppc_elf_hash_entry (h)->tls_mask |= tls_type & 0xff;
   5232 	  else
   5233 	    if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
   5234 					rel->r_addend, tls_type))
   5235 	      return false;
   5236 
   5237 	  ppc64_sec = ppc64_elf_section_data (sec);
   5238 	  if (ppc64_sec->sec_type != sec_toc)
   5239 	    {
   5240 	      bfd_size_type amt;
   5241 
   5242 	      /* One extra to simplify get_tls_mask.  */
   5243 	      amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
   5244 	      ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
   5245 	      if (ppc64_sec->u.toc.symndx == NULL)
   5246 		return false;
   5247 	      amt = sec->size * sizeof (bfd_vma) / 8;
   5248 	      ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
   5249 	      if (ppc64_sec->u.toc.add == NULL)
   5250 		return false;
   5251 	      BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
   5252 	      ppc64_sec->sec_type = sec_toc;
   5253 	    }
   5254 	  BFD_ASSERT (rel->r_offset % 8 == 0);
   5255 	  ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
   5256 	  ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
   5257 
   5258 	  /* Mark the second slot of a GD or LD entry.
   5259 	     -1 to indicate GD and -2 to indicate LD.  */
   5260 	  if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
   5261 	    ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
   5262 	  else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
   5263 	    ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
   5264 	  goto dodyn;
   5265 
   5266 	case R_PPC64_TPREL16_HI:
   5267 	case R_PPC64_TPREL16_HA:
   5268 	case R_PPC64_TPREL16_HIGH:
   5269 	case R_PPC64_TPREL16_HIGHA:
   5270 	case R_PPC64_TPREL16_HIGHER:
   5271 	case R_PPC64_TPREL16_HIGHERA:
   5272 	case R_PPC64_TPREL16_HIGHEST:
   5273 	case R_PPC64_TPREL16_HIGHESTA:
   5274 	  sec->has_tls_reloc = 1;
   5275 	  /* Fall through.  */
   5276 	case R_PPC64_TPREL34:
   5277 	case R_PPC64_TPREL16:
   5278 	case R_PPC64_TPREL16_DS:
   5279 	case R_PPC64_TPREL16_LO:
   5280 	case R_PPC64_TPREL16_LO_DS:
   5281 	  if (bfd_link_dll (info))
   5282 	    info->flags |= DF_STATIC_TLS;
   5283 	  goto dodyn;
   5284 
   5285 	case R_PPC64_ADDR64:
   5286 	  if (is_opd
   5287 	      && rel + 1 < rel_end
   5288 	      && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
   5289 	    {
   5290 	      if (h != NULL)
   5291 		ppc_elf_hash_entry (h)->is_func = 1;
   5292 	    }
   5293 	  /* Fall through.  */
   5294 
   5295 	case R_PPC64_ADDR16:
   5296 	case R_PPC64_ADDR16_DS:
   5297 	case R_PPC64_ADDR16_HA:
   5298 	case R_PPC64_ADDR16_HI:
   5299 	case R_PPC64_ADDR16_HIGH:
   5300 	case R_PPC64_ADDR16_HIGHA:
   5301 	case R_PPC64_ADDR16_HIGHER:
   5302 	case R_PPC64_ADDR16_HIGHERA:
   5303 	case R_PPC64_ADDR16_HIGHEST:
   5304 	case R_PPC64_ADDR16_HIGHESTA:
   5305 	case R_PPC64_ADDR16_LO:
   5306 	case R_PPC64_ADDR16_LO_DS:
   5307 	case R_PPC64_D34:
   5308 	case R_PPC64_D34_LO:
   5309 	case R_PPC64_D34_HI30:
   5310 	case R_PPC64_D34_HA30:
   5311 	case R_PPC64_ADDR16_HIGHER34:
   5312 	case R_PPC64_ADDR16_HIGHERA34:
   5313 	case R_PPC64_ADDR16_HIGHEST34:
   5314 	case R_PPC64_ADDR16_HIGHESTA34:
   5315 	case R_PPC64_D28:
   5316 	  if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
   5317 	      && rel->r_addend == 0)
   5318 	    {
   5319 	      /* We may need a .plt entry if this reloc refers to a
   5320 		 function in a shared lib.  */
   5321 	      if (!update_plt_info (abfd, &h->plt.plist, 0))
   5322 		return false;
   5323 	      h->pointer_equality_needed = 1;
   5324 	    }
   5325 	  /* Fall through.  */
   5326 
   5327 	case R_PPC64_REL30:
   5328 	case R_PPC64_REL32:
   5329 	case R_PPC64_REL64:
   5330 	case R_PPC64_ADDR32:
   5331 	case R_PPC64_UADDR16:
   5332 	case R_PPC64_UADDR32:
   5333 	case R_PPC64_UADDR64:
   5334 	case R_PPC64_TOC:
   5335 	  if (h != NULL && bfd_link_executable (info))
   5336 	    /* We may need a copy reloc.  */
   5337 	    h->non_got_ref = 1;
   5338 
   5339 	  /* Don't propagate .opd relocs.  */
   5340 	  if (NO_OPD_RELOCS && is_opd)
   5341 	    break;
   5342 
   5343 	  /* Set up information for symbols that might need dynamic
   5344 	     relocations.  At this point in linking we have read all
   5345 	     the input files and resolved most symbols, but have not
   5346 	     yet decided whether symbols are dynamic or finalized
   5347 	     symbol flags.  In some cases we might be setting dynamic
   5348 	     reloc info for symbols that do not end up needing such.
   5349 	     That's OK, adjust_dynamic_symbol and allocate_dynrelocs
   5350 	     work together with this code.  */
   5351 	dodyn:
   5352 	  if ((h != NULL
   5353 	       && !SYMBOL_REFERENCES_LOCAL (info, h))
   5354 	      || (bfd_link_pic (info)
   5355 		  && (h != NULL
   5356 		      ? !bfd_is_abs_symbol (&h->root)
   5357 		      : isym->st_shndx != SHN_ABS)
   5358 		  && must_be_dyn_reloc (info, r_type))
   5359 	      || (!bfd_link_pic (info)
   5360 		  && ifunc != NULL))
   5361 	    {
   5362 	      /* We must copy these reloc types into the output file.
   5363 		 Create a reloc section in dynobj and make room for
   5364 		 this reloc.  */
   5365 	      if (sreloc == NULL)
   5366 		{
   5367 		  sreloc = _bfd_elf_make_dynamic_reloc_section
   5368 		    (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ true);
   5369 
   5370 		  if (sreloc == NULL)
   5371 		    return false;
   5372 		}
   5373 
   5374 	      /* If this is a global symbol, we count the number of
   5375 		 relocations we need for this symbol.  */
   5376 	      if (h != NULL)
   5377 		{
   5378 		  struct ppc_dyn_relocs *p;
   5379 		  struct ppc_dyn_relocs **head;
   5380 
   5381 		  head = (struct ppc_dyn_relocs **) &h->dyn_relocs;
   5382 		  p = *head;
   5383 		  if (p == NULL || p->sec != sec)
   5384 		    {
   5385 		      p = bfd_alloc (htab->elf.dynobj, sizeof *p);
   5386 		      if (p == NULL)
   5387 			return false;
   5388 		      p->next = *head;
   5389 		      *head = p;
   5390 		      p->sec = sec;
   5391 		      p->count = 0;
   5392 		      p->pc_count = 0;
   5393 		      p->rel_count = 0;
   5394 		    }
   5395 		  p->count += 1;
   5396 		  if (!must_be_dyn_reloc (info, r_type))
   5397 		    p->pc_count += 1;
   5398 		  if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
   5399 		      && rel->r_offset % 2 == 0
   5400 		      && sec->alignment_power != 0)
   5401 		    p->rel_count += 1;
   5402 		}
   5403 	      else
   5404 		{
   5405 		  /* Track dynamic relocs needed for local syms too.  */
   5406 		  struct ppc_local_dyn_relocs *p;
   5407 		  struct ppc_local_dyn_relocs **head;
   5408 		  bool is_ifunc;
   5409 		  asection *s;
   5410 		  void *vpp;
   5411 
   5412 		  s = bfd_section_from_elf_index (abfd, isym->st_shndx);
   5413 		  if (s == NULL)
   5414 		    s = sec;
   5415 
   5416 		  vpp = &elf_section_data (s)->local_dynrel;
   5417 		  head = (struct ppc_local_dyn_relocs **) vpp;
   5418 		  is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
   5419 		  p = *head;
   5420 		  if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
   5421 		    p = p->next;
   5422 		  if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
   5423 		    {
   5424 		      p = bfd_alloc (htab->elf.dynobj, sizeof *p);
   5425 		      if (p == NULL)
   5426 			return false;
   5427 		      p->next = *head;
   5428 		      *head = p;
   5429 		      p->sec = sec;
   5430 		      p->count = 0;
   5431 		      p->rel_count = 0;
   5432 		      p->ifunc = is_ifunc;
   5433 		    }
   5434 		  p->count += 1;
   5435 		  if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
   5436 		      && rel->r_offset % 2 == 0
   5437 		      && sec->alignment_power != 0)
   5438 		    p->rel_count += 1;
   5439 		}
   5440 	    }
   5441 	  break;
   5442 
   5443 	default:
   5444 	  break;
   5445 	}
   5446     }
   5447 
   5448   return true;
   5449 }
   5450 
   5451 /* Merge backend specific data from an object file to the output
   5452    object file when linking.  */
   5453 
   5454 static bool
   5455 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
   5456 {
   5457   bfd *obfd = info->output_bfd;
   5458   unsigned long iflags, oflags;
   5459 
   5460   if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
   5461     return true;
   5462 
   5463   if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
   5464     return true;
   5465 
   5466   if (!_bfd_generic_verify_endian_match (ibfd, info))
   5467     return false;
   5468 
   5469   iflags = elf_elfheader (ibfd)->e_flags;
   5470   oflags = elf_elfheader (obfd)->e_flags;
   5471 
   5472   if (iflags & ~EF_PPC64_ABI)
   5473     {
   5474       _bfd_error_handler
   5475 	/* xgettext:c-format */
   5476 	(_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
   5477       bfd_set_error (bfd_error_bad_value);
   5478       return false;
   5479     }
   5480   else if (iflags != oflags && iflags != 0)
   5481     {
   5482       _bfd_error_handler
   5483 	/* xgettext:c-format */
   5484 	(_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
   5485 	 ibfd, iflags, oflags);
   5486       bfd_set_error (bfd_error_bad_value);
   5487       return false;
   5488     }
   5489 
   5490   if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
   5491     return false;
   5492 
   5493   /* Merge Tag_compatibility attributes and any common GNU ones.  */
   5494   return _bfd_elf_merge_object_attributes (ibfd, info);
   5495 }
   5496 
   5497 static bool
   5498 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
   5499 {
   5500   /* Print normal ELF private data.  */
   5501   _bfd_elf_print_private_bfd_data (abfd, ptr);
   5502 
   5503   if (elf_elfheader (abfd)->e_flags != 0)
   5504     {
   5505       FILE *file = ptr;
   5506 
   5507       fprintf (file, _("private flags = 0x%lx:"),
   5508 	       elf_elfheader (abfd)->e_flags);
   5509 
   5510       if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
   5511 	fprintf (file, _(" [abiv%ld]"),
   5512 		 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
   5513       fputc ('\n', file);
   5514     }
   5515 
   5516   return true;
   5517 }
   5518 
   5519 /* OFFSET in OPD_SEC specifies a function descriptor.  Return the address
   5520    of the code entry point, and its section, which must be in the same
   5521    object as OPD_SEC.  Returns (bfd_vma) -1 on error.  */
   5522 
   5523 static bfd_vma
   5524 opd_entry_value (asection *opd_sec,
   5525 		 bfd_vma offset,
   5526 		 asection **code_sec,
   5527 		 bfd_vma *code_off,
   5528 		 bool in_code_sec)
   5529 {
   5530   bfd *opd_bfd = opd_sec->owner;
   5531   Elf_Internal_Rela *relocs;
   5532   Elf_Internal_Rela *lo, *hi, *look;
   5533   bfd_vma val;
   5534 
   5535   /* No relocs implies we are linking a --just-symbols object, or looking
   5536      at a final linked executable with addr2line or somesuch.  */
   5537   if (opd_sec->reloc_count == 0)
   5538     {
   5539       bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
   5540 
   5541       if (contents == NULL)
   5542 	{
   5543 	  if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
   5544 	    return (bfd_vma) -1;
   5545 	  ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
   5546 	}
   5547 
   5548       /* PR 17512: file: 64b9dfbb.  */
   5549       if (offset + 7 >= opd_sec->size || offset + 7 < offset)
   5550 	return (bfd_vma) -1;
   5551 
   5552       val = bfd_get_64 (opd_bfd, contents + offset);
   5553       if (code_sec != NULL)
   5554 	{
   5555 	  asection *sec, *likely = NULL;
   5556 
   5557 	  if (in_code_sec)
   5558 	    {
   5559 	      sec = *code_sec;
   5560 	      if (sec->vma <= val
   5561 		  && val < sec->vma + sec->size)
   5562 		likely = sec;
   5563 	      else
   5564 		val = -1;
   5565 	    }
   5566 	  else
   5567 	    for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
   5568 	      if (sec->vma <= val
   5569 		  && (sec->flags & SEC_LOAD) != 0
   5570 		  && (sec->flags & SEC_ALLOC) != 0)
   5571 		likely = sec;
   5572 	  if (likely != NULL)
   5573 	    {
   5574 	      *code_sec = likely;
   5575 	      if (code_off != NULL)
   5576 		*code_off = val - likely->vma;
   5577 	    }
   5578 	}
   5579       return val;
   5580     }
   5581 
   5582   BFD_ASSERT (is_ppc64_elf (opd_bfd));
   5583 
   5584   relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
   5585   if (relocs == NULL)
   5586     relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, true);
   5587   /* PR 17512: file: df8e1fd6.  */
   5588   if (relocs == NULL)
   5589     return (bfd_vma) -1;
   5590 
   5591   /* Go find the opd reloc at the sym address.  */
   5592   lo = relocs;
   5593   hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
   5594   val = (bfd_vma) -1;
   5595   while (lo < hi)
   5596     {
   5597       look = lo + (hi - lo) / 2;
   5598       if (look->r_offset < offset)
   5599 	lo = look + 1;
   5600       else if (look->r_offset > offset)
   5601 	hi = look;
   5602       else
   5603 	{
   5604 	  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
   5605 
   5606 	  if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
   5607 	      && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
   5608 	    {
   5609 	      unsigned long symndx = ELF64_R_SYM (look->r_info);
   5610 	      asection *sec = NULL;
   5611 
   5612 	      if (symndx >= symtab_hdr->sh_info
   5613 		  && elf_sym_hashes (opd_bfd) != NULL)
   5614 		{
   5615 		  struct elf_link_hash_entry **sym_hashes;
   5616 		  struct elf_link_hash_entry *rh;
   5617 
   5618 		  sym_hashes = elf_sym_hashes (opd_bfd);
   5619 		  rh = sym_hashes[symndx - symtab_hdr->sh_info];
   5620 		  if (rh != NULL)
   5621 		    {
   5622 		      rh = elf_follow_link (rh);
   5623 		      if (rh->root.type != bfd_link_hash_defined
   5624 			  && rh->root.type != bfd_link_hash_defweak)
   5625 			break;
   5626 		      if (rh->root.u.def.section->owner == opd_bfd)
   5627 			{
   5628 			  val = rh->root.u.def.value;
   5629 			  sec = rh->root.u.def.section;
   5630 			}
   5631 		    }
   5632 		}
   5633 
   5634 	      if (sec == NULL)
   5635 		{
   5636 		  Elf_Internal_Sym *sym;
   5637 
   5638 		  if (symndx < symtab_hdr->sh_info)
   5639 		    {
   5640 		      sym = (Elf_Internal_Sym *) symtab_hdr->contents;
   5641 		      if (sym == NULL)
   5642 			{
   5643 			  size_t symcnt = symtab_hdr->sh_info;
   5644 			  sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
   5645 						      symcnt, 0,
   5646 						      NULL, NULL, NULL);
   5647 			  if (sym == NULL)
   5648 			    break;
   5649 			  symtab_hdr->contents = (bfd_byte *) sym;
   5650 			}
   5651 		      sym += symndx;
   5652 		    }
   5653 		  else
   5654 		    {
   5655 		      sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
   5656 						  1, symndx,
   5657 						  NULL, NULL, NULL);
   5658 		      if (sym == NULL)
   5659 			break;
   5660 		    }
   5661 		  sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
   5662 		  if (sec == NULL)
   5663 		    break;
   5664 		  BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
   5665 		  val = sym->st_value;
   5666 		}
   5667 
   5668 	      val += look->r_addend;
   5669 	      if (code_off != NULL)
   5670 		*code_off = val;
   5671 	      if (code_sec != NULL)
   5672 		{
   5673 		  if (in_code_sec && *code_sec != sec)
   5674 		    return -1;
   5675 		  else
   5676 		    *code_sec = sec;
   5677 		}
   5678 	      if (sec->output_section != NULL)
   5679 		val += sec->output_section->vma + sec->output_offset;
   5680 	    }
   5681 	  break;
   5682 	}
   5683     }
   5684 
   5685   return val;
   5686 }
   5687 
   5688 /* If the ELF symbol SYM might be a function in SEC, return the
   5689    function size and set *CODE_OFF to the function's entry point,
   5690    otherwise return zero.  */
   5691 
   5692 static bfd_size_type
   5693 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
   5694 			      bfd_vma *code_off)
   5695 {
   5696   bfd_size_type size;
   5697   elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
   5698 
   5699   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
   5700 		     | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
   5701     return 0;
   5702 
   5703   size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
   5704 
   5705   /* In theory we should check that the symbol's type satisfies
   5706      _bfd_elf_is_function_type(), but there are some function-like
   5707      symbols which would fail this test.  (eg _start).  Instead
   5708      we check for hidden, local, notype symbols with zero size.
   5709      This type of symbol is generated by the annobin plugin for gcc
   5710      and clang, and should not be considered to be a function symbol.  */
   5711   if (size == 0
   5712       && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
   5713       && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
   5714       && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
   5715     return 0;
   5716 
   5717   if (strcmp (sym->section->name, ".opd") == 0)
   5718     {
   5719       struct _opd_sec_data *opd = get_opd_info (sym->section);
   5720       bfd_vma symval = sym->value;
   5721 
   5722       if (opd != NULL
   5723 	  && opd->adjust != NULL
   5724 	  && elf_section_data (sym->section)->relocs != NULL)
   5725 	{
   5726 	  /* opd_entry_value will use cached relocs that have been
   5727 	     adjusted, but with raw symbols.  That means both local
   5728 	     and global symbols need adjusting.  */
   5729 	  long adjust = opd->adjust[OPD_NDX (symval)];
   5730 	  if (adjust == -1)
   5731 	    return 0;
   5732 	  symval += adjust;
   5733 	}
   5734 
   5735       if (opd_entry_value (sym->section, symval,
   5736 			   &sec, code_off, true) == (bfd_vma) -1)
   5737 	return 0;
   5738       /* An old ABI binary with dot-syms has a size of 24 on the .opd
   5739 	 symbol.  This size has nothing to do with the code size of the
   5740 	 function, which is what we're supposed to return, but the
   5741 	 code size isn't available without looking up the dot-sym.
   5742 	 However, doing that would be a waste of time particularly
   5743 	 since elf_find_function will look at the dot-sym anyway.
   5744 	 Now, elf_find_function will keep the largest size of any
   5745 	 function sym found at the code address of interest, so return
   5746 	 1 here to avoid it incorrectly caching a larger function size
   5747 	 for a small function.  This does mean we return the wrong
   5748 	 size for a new-ABI function of size 24, but all that does is
   5749 	 disable caching for such functions.  */
   5750       if (size == 24)
   5751 	size = 1;
   5752     }
   5753   else
   5754     {
   5755       if (sym->section != sec)
   5756 	return 0;
   5757       *code_off = sym->value;
   5758     }
   5759 
   5760   /* Do not return 0 for the function's size.  */
   5761   return size ? size : 1;
   5762 }
   5763 
   5764 /* Return true if symbol is a strong function defined in an ELFv2
   5765    object with st_other localentry bits of zero, ie. its local entry
   5766    point coincides with its global entry point.  */
   5767 
   5768 static bool
   5769 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
   5770 {
   5771   return (h != NULL
   5772 	  && h->type == STT_FUNC
   5773 	  && h->root.type == bfd_link_hash_defined
   5774 	  && (STO_PPC64_LOCAL_MASK & h->other) == 0
   5775 	  && !ppc_elf_hash_entry (h)->non_zero_localentry
   5776 	  && is_ppc64_elf (h->root.u.def.section->owner)
   5777 	  && abiversion (h->root.u.def.section->owner) >= 2);
   5778 }
   5779 
   5780 /* Return true if symbol is defined in a regular object file.  */
   5781 
   5782 static bool
   5783 is_static_defined (struct elf_link_hash_entry *h)
   5784 {
   5785   return ((h->root.type == bfd_link_hash_defined
   5786 	   || h->root.type == bfd_link_hash_defweak)
   5787 	  && h->root.u.def.section != NULL
   5788 	  && h->root.u.def.section->output_section != NULL);
   5789 }
   5790 
   5791 /* If FDH is a function descriptor symbol, return the associated code
   5792    entry symbol if it is defined.  Return NULL otherwise.  */
   5793 
   5794 static struct ppc_link_hash_entry *
   5795 defined_code_entry (struct ppc_link_hash_entry *fdh)
   5796 {
   5797   if (fdh->is_func_descriptor)
   5798     {
   5799       struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
   5800       if (fh->elf.root.type == bfd_link_hash_defined
   5801 	  || fh->elf.root.type == bfd_link_hash_defweak)
   5802 	return fh;
   5803     }
   5804   return NULL;
   5805 }
   5806 
   5807 /* If FH is a function code entry symbol, return the associated
   5808    function descriptor symbol if it is defined.  Return NULL otherwise.  */
   5809 
   5810 static struct ppc_link_hash_entry *
   5811 defined_func_desc (struct ppc_link_hash_entry *fh)
   5812 {
   5813   if (fh->oh != NULL
   5814       && fh->oh->is_func_descriptor)
   5815     {
   5816       struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
   5817       if (fdh->elf.root.type == bfd_link_hash_defined
   5818 	  || fdh->elf.root.type == bfd_link_hash_defweak)
   5819 	return fdh;
   5820     }
   5821   return NULL;
   5822 }
   5823 
   5824 /* Given H is a symbol that satisfies is_static_defined, return the
   5825    value in the output file.  */
   5826 
   5827 static bfd_vma
   5828 defined_sym_val (struct elf_link_hash_entry *h)
   5829 {
   5830   return (h->root.u.def.section->output_section->vma
   5831 	  + h->root.u.def.section->output_offset
   5832 	  + h->root.u.def.value);
   5833 }
   5834 
   5835 /* Return true if H matches __tls_get_addr or one of its variants.  */
   5836 
   5837 static bool
   5838 is_tls_get_addr (struct elf_link_hash_entry *h,
   5839 		 struct ppc_link_hash_table *htab)
   5840 {
   5841   return (h == elf_hash_entry (htab->tls_get_addr_fd)
   5842 	  || h == elf_hash_entry (htab->tga_desc_fd)
   5843 	  || h == elf_hash_entry (htab->tls_get_addr)
   5844 	  || h == elf_hash_entry (htab->tga_desc));
   5845 }
   5846 
   5847 static bool func_desc_adjust (struct elf_link_hash_entry *, void *);
   5848 
   5849 /* Garbage collect sections, after first dealing with dot-symbols.  */
   5850 
   5851 static bool
   5852 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
   5853 {
   5854   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   5855 
   5856   if (htab != NULL && htab->need_func_desc_adj)
   5857     {
   5858       elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
   5859       htab->need_func_desc_adj = 0;
   5860     }
   5861   return bfd_elf_gc_sections (abfd, info);
   5862 }
   5863 
   5864 /* Mark all our entry sym sections, both opd and code section.  */
   5865 
   5866 static void
   5867 ppc64_elf_gc_keep (struct bfd_link_info *info)
   5868 {
   5869   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   5870   struct bfd_sym_chain *sym;
   5871 
   5872   if (htab == NULL)
   5873     return;
   5874 
   5875   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
   5876     {
   5877       struct ppc_link_hash_entry *eh, *fh;
   5878       asection *sec;
   5879 
   5880       eh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym->name,
   5881 						     false, false, true));
   5882       if (eh == NULL)
   5883 	continue;
   5884       if (eh->elf.root.type != bfd_link_hash_defined
   5885 	  && eh->elf.root.type != bfd_link_hash_defweak)
   5886 	continue;
   5887 
   5888       fh = defined_code_entry (eh);
   5889       if (fh != NULL)
   5890 	{
   5891 	  sec = fh->elf.root.u.def.section;
   5892 	  sec->flags |= SEC_KEEP;
   5893 	}
   5894       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
   5895 	       && opd_entry_value (eh->elf.root.u.def.section,
   5896 				   eh->elf.root.u.def.value,
   5897 				   &sec, NULL, false) != (bfd_vma) -1)
   5898 	sec->flags |= SEC_KEEP;
   5899 
   5900       sec = eh->elf.root.u.def.section;
   5901       sec->flags |= SEC_KEEP;
   5902     }
   5903 }
   5904 
   5905 /* Mark sections containing dynamically referenced symbols.  When
   5906    building shared libraries, we must assume that any visible symbol is
   5907    referenced.  */
   5908 
   5909 static bool
   5910 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
   5911 {
   5912   struct bfd_link_info *info = (struct bfd_link_info *) inf;
   5913   struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
   5914   struct ppc_link_hash_entry *fdh;
   5915   struct bfd_elf_dynamic_list *d = info->dynamic_list;
   5916 
   5917   /* Dynamic linking info is on the func descriptor sym.  */
   5918   fdh = defined_func_desc (eh);
   5919   if (fdh != NULL)
   5920     eh = fdh;
   5921 
   5922   if ((eh->elf.root.type == bfd_link_hash_defined
   5923        || eh->elf.root.type == bfd_link_hash_defweak)
   5924       && (!eh->elf.start_stop
   5925 	  || eh->elf.root.ldscript_def
   5926 	  || !info->start_stop_gc)
   5927       && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
   5928 	  || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
   5929 	      && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
   5930 	      && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
   5931 	      && (!bfd_link_executable (info)
   5932 		  || info->gc_keep_exported
   5933 		  || info->export_dynamic
   5934 		  || (eh->elf.dynamic
   5935 		      && d != NULL
   5936 		      && (*d->match) (&d->head, NULL,
   5937 				      eh->elf.root.root.string)))
   5938 	      && (eh->elf.versioned >= versioned
   5939 		  || !bfd_hide_sym_by_version (info->version_info,
   5940 					       eh->elf.root.root.string)))))
   5941     {
   5942       asection *code_sec;
   5943       struct ppc_link_hash_entry *fh;
   5944 
   5945       eh->elf.root.u.def.section->flags |= SEC_KEEP;
   5946 
   5947       /* Function descriptor syms cause the associated
   5948 	 function code sym section to be marked.  */
   5949       fh = defined_code_entry (eh);
   5950       if (fh != NULL)
   5951 	{
   5952 	  code_sec = fh->elf.root.u.def.section;
   5953 	  code_sec->flags |= SEC_KEEP;
   5954 	}
   5955       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
   5956 	       && opd_entry_value (eh->elf.root.u.def.section,
   5957 				   eh->elf.root.u.def.value,
   5958 				   &code_sec, NULL, false) != (bfd_vma) -1)
   5959 	code_sec->flags |= SEC_KEEP;
   5960     }
   5961 
   5962   return true;
   5963 }
   5964 
   5965 /* Return the section that should be marked against GC for a given
   5966    relocation.  */
   5967 
   5968 static asection *
   5969 ppc64_elf_gc_mark_hook (asection *sec,
   5970 			struct bfd_link_info *info,
   5971 			Elf_Internal_Rela *rel,
   5972 			struct elf_link_hash_entry *h,
   5973 			Elf_Internal_Sym *sym)
   5974 {
   5975   asection *rsec;
   5976 
   5977   /* Syms return NULL if we're marking .opd, so we avoid marking all
   5978      function sections, as all functions are referenced in .opd.  */
   5979   rsec = NULL;
   5980   if (get_opd_info (sec) != NULL)
   5981     return rsec;
   5982 
   5983   if (h != NULL)
   5984     {
   5985       enum elf_ppc64_reloc_type r_type;
   5986       struct ppc_link_hash_entry *eh, *fh, *fdh;
   5987 
   5988       r_type = ELF64_R_TYPE (rel->r_info);
   5989       switch (r_type)
   5990 	{
   5991 	case R_PPC64_GNU_VTINHERIT:
   5992 	case R_PPC64_GNU_VTENTRY:
   5993 	  break;
   5994 
   5995 	default:
   5996 	  switch (h->root.type)
   5997 	    {
   5998 	    case bfd_link_hash_defined:
   5999 	    case bfd_link_hash_defweak:
   6000 	      eh = ppc_elf_hash_entry (h);
   6001 	      fdh = defined_func_desc (eh);
   6002 	      if (fdh != NULL)
   6003 		{
   6004 		  /* -mcall-aixdesc code references the dot-symbol on
   6005 		     a call reloc.  Mark the function descriptor too
   6006 		     against garbage collection.  */
   6007 		  fdh->elf.mark = 1;
   6008 		  if (fdh->elf.is_weakalias)
   6009 		    weakdef (&fdh->elf)->mark = 1;
   6010 		  eh = fdh;
   6011 		}
   6012 
   6013 	      /* Function descriptor syms cause the associated
   6014 		 function code sym section to be marked.  */
   6015 	      fh = defined_code_entry (eh);
   6016 	      if (fh != NULL)
   6017 		{
   6018 		  /* They also mark their opd section.  */
   6019 		  eh->elf.root.u.def.section->gc_mark = 1;
   6020 
   6021 		  rsec = fh->elf.root.u.def.section;
   6022 		}
   6023 	      else if (get_opd_info (eh->elf.root.u.def.section) != NULL
   6024 		       && opd_entry_value (eh->elf.root.u.def.section,
   6025 					   eh->elf.root.u.def.value,
   6026 					   &rsec, NULL, false) != (bfd_vma) -1)
   6027 		eh->elf.root.u.def.section->gc_mark = 1;
   6028 	      else
   6029 		rsec = h->root.u.def.section;
   6030 	      break;
   6031 
   6032 	    case bfd_link_hash_common:
   6033 	      rsec = h->root.u.c.p->section;
   6034 	      break;
   6035 
   6036 	    default:
   6037 	      return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
   6038 	    }
   6039 	}
   6040     }
   6041   else
   6042     {
   6043       struct _opd_sec_data *opd;
   6044 
   6045       rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
   6046       opd = get_opd_info (rsec);
   6047       if (opd != NULL && opd->func_sec != NULL)
   6048 	{
   6049 	  rsec->gc_mark = 1;
   6050 
   6051 	  rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
   6052 	}
   6053     }
   6054 
   6055   return rsec;
   6056 }
   6057 
   6058 /* The maximum size of .sfpr.  */
   6059 #define SFPR_MAX (218*4)
   6060 
   6061 struct sfpr_def_parms
   6062 {
   6063   const char name[12];
   6064   unsigned char lo, hi;
   6065   bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
   6066   bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
   6067 };
   6068 
   6069 /* Auto-generate _save*, _rest* functions in .sfpr.
   6070    If STUB_SEC is non-null, define alias symbols in STUB_SEC
   6071    instead.  */
   6072 
   6073 static bool
   6074 sfpr_define (struct bfd_link_info *info,
   6075 	     const struct sfpr_def_parms *parm,
   6076 	     asection *stub_sec)
   6077 {
   6078   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   6079   unsigned int i;
   6080   size_t len = strlen (parm->name);
   6081   bool writing = false;
   6082   char sym[16];
   6083 
   6084   if (htab == NULL)
   6085     return false;
   6086 
   6087   memcpy (sym, parm->name, len);
   6088   sym[len + 2] = 0;
   6089 
   6090   for (i = parm->lo; i <= parm->hi; i++)
   6091     {
   6092       struct ppc_link_hash_entry *h;
   6093 
   6094       sym[len + 0] = i / 10 + '0';
   6095       sym[len + 1] = i % 10 + '0';
   6096       h = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym,
   6097 						    writing, true, true));
   6098       if (stub_sec != NULL)
   6099 	{
   6100 	  if (h != NULL
   6101 	      && h->elf.root.type == bfd_link_hash_defined
   6102 	      && h->elf.root.u.def.section == htab->sfpr)
   6103 	    {
   6104 	      struct elf_link_hash_entry *s;
   6105 	      char buf[32];
   6106 	      sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
   6107 	      s = elf_link_hash_lookup (&htab->elf, buf, true, true, false);
   6108 	      if (s == NULL)
   6109 		return false;
   6110 	      if (s->root.type == bfd_link_hash_new)
   6111 		{
   6112 		  s->root.type = bfd_link_hash_defined;
   6113 		  s->root.u.def.section = stub_sec;
   6114 		  s->root.u.def.value = (stub_sec->size - htab->sfpr->size
   6115 					 + h->elf.root.u.def.value);
   6116 		  s->ref_regular = 1;
   6117 		  s->def_regular = 1;
   6118 		  s->ref_regular_nonweak = 1;
   6119 		  s->forced_local = 1;
   6120 		  s->non_elf = 0;
   6121 		  s->root.linker_def = 1;
   6122 		}
   6123 	    }
   6124 	  continue;
   6125 	}
   6126       if (h != NULL)
   6127 	{
   6128 	  h->save_res = 1;
   6129 	  if (!h->elf.def_regular)
   6130 	    {
   6131 	      h->elf.root.type = bfd_link_hash_defined;
   6132 	      h->elf.root.u.def.section = htab->sfpr;
   6133 	      h->elf.root.u.def.value = htab->sfpr->size;
   6134 	      h->elf.type = STT_FUNC;
   6135 	      h->elf.def_regular = 1;
   6136 	      h->elf.non_elf = 0;
   6137 	      _bfd_elf_link_hash_hide_symbol (info, &h->elf, true);
   6138 	      writing = true;
   6139 	      if (htab->sfpr->contents == NULL)
   6140 		{
   6141 		  htab->sfpr->contents
   6142 		    = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
   6143 		  if (htab->sfpr->contents == NULL)
   6144 		    return false;
   6145 		}
   6146 	    }
   6147 	}
   6148       if (writing)
   6149 	{
   6150 	  bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
   6151 	  if (i != parm->hi)
   6152 	    p = (*parm->write_ent) (htab->elf.dynobj, p, i);
   6153 	  else
   6154 	    p = (*parm->write_tail) (htab->elf.dynobj, p, i);
   6155 	  htab->sfpr->size = p - htab->sfpr->contents;
   6156 	}
   6157     }
   6158 
   6159   return true;
   6160 }
   6161 
   6162 static bfd_byte *
   6163 savegpr0 (bfd *abfd, bfd_byte *p, int r)
   6164 {
   6165   bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6166   return p + 4;
   6167 }
   6168 
   6169 static bfd_byte *
   6170 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
   6171 {
   6172   p = savegpr0 (abfd, p, r);
   6173   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
   6174   p = p + 4;
   6175   bfd_put_32 (abfd, BLR, p);
   6176   return p + 4;
   6177 }
   6178 
   6179 static bfd_byte *
   6180 restgpr0 (bfd *abfd, bfd_byte *p, int r)
   6181 {
   6182   bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6183   return p + 4;
   6184 }
   6185 
   6186 static bfd_byte *
   6187 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
   6188 {
   6189   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
   6190   p = p + 4;
   6191   p = restgpr0 (abfd, p, r);
   6192   bfd_put_32 (abfd, MTLR_R0, p);
   6193   p = p + 4;
   6194   if (r == 29)
   6195     {
   6196       p = restgpr0 (abfd, p, 30);
   6197       p = restgpr0 (abfd, p, 31);
   6198     }
   6199   bfd_put_32 (abfd, BLR, p);
   6200   return p + 4;
   6201 }
   6202 
   6203 static bfd_byte *
   6204 savegpr1 (bfd *abfd, bfd_byte *p, int r)
   6205 {
   6206   bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6207   return p + 4;
   6208 }
   6209 
   6210 static bfd_byte *
   6211 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
   6212 {
   6213   p = savegpr1 (abfd, p, r);
   6214   bfd_put_32 (abfd, BLR, p);
   6215   return p + 4;
   6216 }
   6217 
   6218 static bfd_byte *
   6219 restgpr1 (bfd *abfd, bfd_byte *p, int r)
   6220 {
   6221   bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6222   return p + 4;
   6223 }
   6224 
   6225 static bfd_byte *
   6226 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
   6227 {
   6228   p = restgpr1 (abfd, p, r);
   6229   bfd_put_32 (abfd, BLR, p);
   6230   return p + 4;
   6231 }
   6232 
   6233 static bfd_byte *
   6234 savefpr (bfd *abfd, bfd_byte *p, int r)
   6235 {
   6236   bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6237   return p + 4;
   6238 }
   6239 
   6240 static bfd_byte *
   6241 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
   6242 {
   6243   p = savefpr (abfd, p, r);
   6244   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
   6245   p = p + 4;
   6246   bfd_put_32 (abfd, BLR, p);
   6247   return p + 4;
   6248 }
   6249 
   6250 static bfd_byte *
   6251 restfpr (bfd *abfd, bfd_byte *p, int r)
   6252 {
   6253   bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6254   return p + 4;
   6255 }
   6256 
   6257 static bfd_byte *
   6258 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
   6259 {
   6260   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
   6261   p = p + 4;
   6262   p = restfpr (abfd, p, r);
   6263   bfd_put_32 (abfd, MTLR_R0, p);
   6264   p = p + 4;
   6265   if (r == 29)
   6266     {
   6267       p = restfpr (abfd, p, 30);
   6268       p = restfpr (abfd, p, 31);
   6269     }
   6270   bfd_put_32 (abfd, BLR, p);
   6271   return p + 4;
   6272 }
   6273 
   6274 static bfd_byte *
   6275 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
   6276 {
   6277   p = savefpr (abfd, p, r);
   6278   bfd_put_32 (abfd, BLR, p);
   6279   return p + 4;
   6280 }
   6281 
   6282 static bfd_byte *
   6283 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
   6284 {
   6285   p = restfpr (abfd, p, r);
   6286   bfd_put_32 (abfd, BLR, p);
   6287   return p + 4;
   6288 }
   6289 
   6290 static bfd_byte *
   6291 savevr (bfd *abfd, bfd_byte *p, int r)
   6292 {
   6293   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
   6294   p = p + 4;
   6295   bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
   6296   return p + 4;
   6297 }
   6298 
   6299 static bfd_byte *
   6300 savevr_tail (bfd *abfd, bfd_byte *p, int r)
   6301 {
   6302   p = savevr (abfd, p, r);
   6303   bfd_put_32 (abfd, BLR, p);
   6304   return p + 4;
   6305 }
   6306 
   6307 static bfd_byte *
   6308 restvr (bfd *abfd, bfd_byte *p, int r)
   6309 {
   6310   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
   6311   p = p + 4;
   6312   bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
   6313   return p + 4;
   6314 }
   6315 
   6316 static bfd_byte *
   6317 restvr_tail (bfd *abfd, bfd_byte *p, int r)
   6318 {
   6319   p = restvr (abfd, p, r);
   6320   bfd_put_32 (abfd, BLR, p);
   6321   return p + 4;
   6322 }
   6323 
   6324 #define STDU_R1_0R1	0xf8210001
   6325 #define ADDI_R1_R1	0x38210000
   6326 
   6327 /* Emit prologue of wrapper preserving regs around a call to
   6328    __tls_get_addr_opt.  */
   6329 
   6330 static bfd_byte *
   6331 tls_get_addr_prologue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
   6332 {
   6333   unsigned int i;
   6334 
   6335   bfd_put_32 (obfd, MFLR_R0, p);
   6336   p += 4;
   6337   bfd_put_32 (obfd, STD_R0_0R1 + 16, p);
   6338   p += 4;
   6339 
   6340   if (htab->opd_abi)
   6341     {
   6342       for (i = 4; i < 12; i++)
   6343 	{
   6344 	  bfd_put_32 (obfd,
   6345 		      STD_R0_0R1 | i << 21 | (-(13 - i) * 8 & 0xffff), p);
   6346 	  p += 4;
   6347 	}
   6348       bfd_put_32 (obfd, STDU_R1_0R1 | (-128 & 0xffff), p);
   6349       p += 4;
   6350     }
   6351   else
   6352     {
   6353       for (i = 4; i < 12; i++)
   6354 	{
   6355 	  bfd_put_32 (obfd,
   6356 		      STD_R0_0R1 | i << 21 | (-(12 - i) * 8 & 0xffff), p);
   6357 	  p += 4;
   6358 	}
   6359       bfd_put_32 (obfd, STDU_R1_0R1 | (-96 & 0xffff), p);
   6360       p += 4;
   6361     }
   6362   return p;
   6363 }
   6364 
   6365 /* Emit epilogue of wrapper preserving regs around a call to
   6366    __tls_get_addr_opt.  */
   6367 
   6368 static bfd_byte *
   6369 tls_get_addr_epilogue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
   6370 {
   6371   unsigned int i;
   6372 
   6373   if (htab->opd_abi)
   6374     {
   6375       for (i = 4; i < 12; i++)
   6376 	{
   6377 	  bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (128 - (13 - i) * 8), p);
   6378 	  p += 4;
   6379 	}
   6380       bfd_put_32 (obfd, ADDI_R1_R1 | 128, p);
   6381       p += 4;
   6382     }
   6383   else
   6384     {
   6385       for (i = 4; i < 12; i++)
   6386 	{
   6387 	  bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (96 - (12 - i) * 8), p);
   6388 	  p += 4;
   6389 	}
   6390       bfd_put_32 (obfd, ADDI_R1_R1 | 96, p);
   6391       p += 4;
   6392     }
   6393   bfd_put_32 (obfd, LD_R0_0R1 | 16, p);
   6394   p += 4;
   6395   bfd_put_32 (obfd, MTLR_R0, p);
   6396   p += 4;
   6397   bfd_put_32 (obfd, BLR, p);
   6398   p += 4;
   6399   return p;
   6400 }
   6401 
   6402 /* Called via elf_link_hash_traverse to transfer dynamic linking
   6403    information on function code symbol entries to their corresponding
   6404    function descriptor symbol entries.  Must not be called twice for
   6405    any given code symbol.  */
   6406 
   6407 static bool
   6408 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
   6409 {
   6410   struct bfd_link_info *info;
   6411   struct ppc_link_hash_table *htab;
   6412   struct ppc_link_hash_entry *fh;
   6413   struct ppc_link_hash_entry *fdh;
   6414   bool force_local;
   6415 
   6416   fh = ppc_elf_hash_entry (h);
   6417   if (fh->elf.root.type == bfd_link_hash_indirect)
   6418     return true;
   6419 
   6420   if (!fh->is_func)
   6421     return true;
   6422 
   6423   if (fh->elf.root.root.string[0] != '.'
   6424       || fh->elf.root.root.string[1] == '\0')
   6425     return true;
   6426 
   6427   info = inf;
   6428   htab = ppc_hash_table (info);
   6429   if (htab == NULL)
   6430     return false;
   6431 
   6432   /* Find the corresponding function descriptor symbol.  */
   6433   fdh = lookup_fdh (fh, htab);
   6434 
   6435   /* Resolve undefined references to dot-symbols as the value
   6436      in the function descriptor, if we have one in a regular object.
   6437      This is to satisfy cases like ".quad .foo".  Calls to functions
   6438      in dynamic objects are handled elsewhere.  */
   6439   if ((fh->elf.root.type == bfd_link_hash_undefined
   6440        || fh->elf.root.type == bfd_link_hash_undefweak)
   6441       && (fdh->elf.root.type == bfd_link_hash_defined
   6442 	  || fdh->elf.root.type == bfd_link_hash_defweak)
   6443       && get_opd_info (fdh->elf.root.u.def.section) != NULL
   6444       && opd_entry_value (fdh->elf.root.u.def.section,
   6445 			  fdh->elf.root.u.def.value,
   6446 			  &fh->elf.root.u.def.section,
   6447 			  &fh->elf.root.u.def.value, false) != (bfd_vma) -1)
   6448     {
   6449       fh->elf.root.type = fdh->elf.root.type;
   6450       fh->elf.forced_local = 1;
   6451       fh->elf.def_regular = fdh->elf.def_regular;
   6452       fh->elf.def_dynamic = fdh->elf.def_dynamic;
   6453     }
   6454 
   6455   if (!fh->elf.dynamic)
   6456     {
   6457       struct plt_entry *ent;
   6458 
   6459       for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
   6460 	if (ent->plt.refcount > 0)
   6461 	  break;
   6462       if (ent == NULL)
   6463 	{
   6464 	  if (fdh != NULL && fdh->fake)
   6465 	    _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
   6466 	  return true;
   6467 	}
   6468     }
   6469 
   6470   /* Create a descriptor as undefined if necessary.  */
   6471   if (fdh == NULL
   6472       && !bfd_link_executable (info)
   6473       && (fh->elf.root.type == bfd_link_hash_undefined
   6474 	  || fh->elf.root.type == bfd_link_hash_undefweak))
   6475     {
   6476       fdh = make_fdh (info, fh);
   6477       if (fdh == NULL)
   6478 	return false;
   6479     }
   6480 
   6481   /* We can't support overriding of symbols on a fake descriptor.  */
   6482   if (fdh != NULL
   6483       && fdh->fake
   6484       && (fh->elf.root.type == bfd_link_hash_defined
   6485 	  || fh->elf.root.type == bfd_link_hash_defweak))
   6486     _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
   6487 
   6488   /* Transfer dynamic linking information to the function descriptor.  */
   6489   if (fdh != NULL)
   6490     {
   6491       fdh->elf.ref_regular |= fh->elf.ref_regular;
   6492       fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
   6493       fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
   6494       fdh->elf.non_got_ref |= fh->elf.non_got_ref;
   6495       fdh->elf.dynamic |= fh->elf.dynamic;
   6496       fdh->elf.needs_plt |= (fh->elf.needs_plt
   6497 			     || fh->elf.type == STT_FUNC
   6498 			     || fh->elf.type == STT_GNU_IFUNC);
   6499       move_plt_plist (fh, fdh);
   6500 
   6501       if (!fdh->elf.forced_local
   6502 	  && fh->elf.dynindx != -1)
   6503 	if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
   6504 	  return false;
   6505     }
   6506 
   6507   /* Now that the info is on the function descriptor, clear the
   6508      function code sym info.  Any function code syms for which we
   6509      don't have a definition in a regular file, we force local.
   6510      This prevents a shared library from exporting syms that have
   6511      been imported from another library.  Function code syms that
   6512      are really in the library we must leave global to prevent the
   6513      linker dragging in a definition from a static library.  */
   6514   force_local = (!fh->elf.def_regular
   6515 		 || fdh == NULL
   6516 		 || !fdh->elf.def_regular
   6517 		 || fdh->elf.forced_local);
   6518   _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
   6519 
   6520   return true;
   6521 }
   6522 
   6523 static const struct sfpr_def_parms save_res_funcs[] =
   6524   {
   6525     { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
   6526     { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
   6527     { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
   6528     { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
   6529     { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
   6530     { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
   6531     { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
   6532     { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
   6533     { "._savef", 14, 31, savefpr, savefpr1_tail },
   6534     { "._restf", 14, 31, restfpr, restfpr1_tail },
   6535     { "_savevr_", 20, 31, savevr, savevr_tail },
   6536     { "_restvr_", 20, 31, restvr, restvr_tail }
   6537   };
   6538 
   6539 /* Called near the start of bfd_elf_size_dynamic_sections.  We use
   6540    this hook to a) run the edit functions in this file, b) provide
   6541    some gcc support functions, and c) transfer dynamic linking
   6542    information gathered so far on function code symbol entries, to
   6543    their corresponding function descriptor symbol entries.  */
   6544 
   6545 static bool
   6546 ppc64_elf_edit (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
   6547 {
   6548   struct ppc_link_hash_table *htab;
   6549 
   6550   htab = ppc_hash_table (info);
   6551   if (htab == NULL)
   6552     return false;
   6553 
   6554   /* Call back into the linker, which then runs the edit functions.  */
   6555   htab->params->edit ();
   6556 
   6557   /* Provide any missing _save* and _rest* functions.  */
   6558   if (htab->sfpr != NULL)
   6559     {
   6560       unsigned int i;
   6561 
   6562       htab->sfpr->size = 0;
   6563       for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
   6564 	if (!sfpr_define (info, &save_res_funcs[i], NULL))
   6565 	  return false;
   6566       if (htab->sfpr->size == 0)
   6567 	htab->sfpr->flags |= SEC_EXCLUDE;
   6568     }
   6569 
   6570   if (bfd_link_relocatable (info))
   6571     return true;
   6572 
   6573   if (htab->elf.hgot != NULL)
   6574     {
   6575       _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, true);
   6576       /* Make .TOC. defined so as to prevent it being made dynamic.
   6577 	 The wrong value here is fixed later in ppc64_elf_set_toc.  */
   6578       if (!htab->elf.hgot->def_regular
   6579 	  || htab->elf.hgot->root.type != bfd_link_hash_defined)
   6580 	{
   6581 	  htab->elf.hgot->root.type = bfd_link_hash_defined;
   6582 	  htab->elf.hgot->root.u.def.value = 0;
   6583 	  htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
   6584 	  htab->elf.hgot->def_regular = 1;
   6585 	  htab->elf.hgot->root.linker_def = 1;
   6586 	}
   6587       htab->elf.hgot->type = STT_OBJECT;
   6588       htab->elf.hgot->other
   6589 	= (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
   6590     }
   6591 
   6592   return true;
   6593 }
   6594 
   6595 /* Return true if we have dynamic relocs against H or any of its weak
   6596    aliases, that apply to read-only sections.  Cannot be used after
   6597    size_dynamic_sections.  */
   6598 
   6599 static bool
   6600 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
   6601 {
   6602   struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
   6603   do
   6604     {
   6605       if (_bfd_elf_readonly_dynrelocs (&eh->elf))
   6606 	return true;
   6607       eh = ppc_elf_hash_entry (eh->elf.u.alias);
   6608     }
   6609   while (eh != NULL && &eh->elf != h);
   6610 
   6611   return false;
   6612 }
   6613 
   6614 /* Return whether EH has pc-relative dynamic relocs.  */
   6615 
   6616 static bool
   6617 pc_dynrelocs (struct ppc_link_hash_entry *eh)
   6618 {
   6619   struct ppc_dyn_relocs *p;
   6620 
   6621   for (p = (struct ppc_dyn_relocs *) eh->elf.dyn_relocs; p != NULL; p = p->next)
   6622     if (p->pc_count != 0)
   6623       return true;
   6624   return false;
   6625 }
   6626 
   6627 /* Return true if a global entry stub will be created for H.  Valid
   6628    for ELFv2 before plt entries have been allocated.  */
   6629 
   6630 static bool
   6631 global_entry_stub (struct elf_link_hash_entry *h)
   6632 {
   6633   struct plt_entry *pent;
   6634 
   6635   if (!h->pointer_equality_needed
   6636       || h->def_regular)
   6637     return false;
   6638 
   6639   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
   6640     if (pent->plt.refcount > 0
   6641 	&& pent->addend == 0)
   6642       return true;
   6643 
   6644   return false;
   6645 }
   6646 
   6647 /* Adjust a symbol defined by a dynamic object and referenced by a
   6648    regular object.  The current definition is in some section of the
   6649    dynamic object, but we're not including those sections.  We have to
   6650    change the definition to something the rest of the link can
   6651    understand.  */
   6652 
   6653 static bool
   6654 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
   6655 				 struct elf_link_hash_entry *h)
   6656 {
   6657   struct ppc_link_hash_table *htab;
   6658   asection *s, *srel;
   6659 
   6660   htab = ppc_hash_table (info);
   6661   if (htab == NULL)
   6662     return false;
   6663 
   6664   /* Deal with function syms.  */
   6665   if (h->type == STT_FUNC
   6666       || h->type == STT_GNU_IFUNC
   6667       || h->needs_plt)
   6668     {
   6669       bool local = (ppc_elf_hash_entry (h)->save_res
   6670 		    || SYMBOL_CALLS_LOCAL (info, h)
   6671 		    || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
   6672       /* Discard dyn_relocs when non-pic if we've decided that a
   6673 	 function symbol is local and not an ifunc.  We keep dynamic
   6674 	 relocs for ifuncs when local rather than always emitting a
   6675 	 plt call stub for them and defining the symbol on the call
   6676 	 stub.  We can't do that for ELFv1 anyway (a function symbol
   6677 	 is defined on a descriptor, not code) and it can be faster at
   6678 	 run-time due to not needing to bounce through a stub.  The
   6679 	 dyn_relocs for ifuncs will be applied even in a static
   6680 	 executable.  */
   6681       if (!bfd_link_pic (info)
   6682 	  && h->type != STT_GNU_IFUNC
   6683 	  && local)
   6684 	h->dyn_relocs = NULL;
   6685 
   6686       /* Clear procedure linkage table information for any symbol that
   6687 	 won't need a .plt entry.  */
   6688       struct plt_entry *ent;
   6689       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
   6690 	if (ent->plt.refcount > 0)
   6691 	  break;
   6692       if (ent == NULL
   6693 	  || (h->type != STT_GNU_IFUNC
   6694 	      && local
   6695 	      && (htab->can_convert_all_inline_plt
   6696 		  || (ppc_elf_hash_entry (h)->tls_mask
   6697 		      & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
   6698 	{
   6699 	  h->plt.plist = NULL;
   6700 	  h->needs_plt = 0;
   6701 	  h->pointer_equality_needed = 0;
   6702 	}
   6703       else if (abiversion (info->output_bfd) >= 2)
   6704 	{
   6705 	  /* Taking a function's address in a read/write section
   6706 	     doesn't require us to define the function symbol in the
   6707 	     executable on a global entry stub.  A dynamic reloc can
   6708 	     be used instead.  The reason we prefer a few more dynamic
   6709 	     relocs is that calling via a global entry stub costs a
   6710 	     few more instructions, and pointer_equality_needed causes
   6711 	     extra work in ld.so when resolving these symbols.  */
   6712 	  if (global_entry_stub (h))
   6713 	    {
   6714 	      if (!_bfd_elf_readonly_dynrelocs (h))
   6715 		{
   6716 		  h->pointer_equality_needed = 0;
   6717 		  /* If we haven't seen a branch reloc and the symbol
   6718 		     isn't an ifunc then we don't need a plt entry.  */
   6719 		  if (!h->needs_plt)
   6720 		    h->plt.plist = NULL;
   6721 		}
   6722 	      else if (!bfd_link_pic (info))
   6723 		/* We are going to be defining the function symbol on the
   6724 		   plt stub, so no dyn_relocs needed when non-pic.  */
   6725 		h->dyn_relocs = NULL;
   6726 	    }
   6727 
   6728 	  /* ELFv2 function symbols can't have copy relocs.  */
   6729 	  return true;
   6730 	}
   6731       else if (!h->needs_plt
   6732 	       && !_bfd_elf_readonly_dynrelocs (h))
   6733 	{
   6734 	  /* If we haven't seen a branch reloc and the symbol isn't an
   6735 	     ifunc then we don't need a plt entry.  */
   6736 	  h->plt.plist = NULL;
   6737 	  h->pointer_equality_needed = 0;
   6738 	  return true;
   6739 	}
   6740     }
   6741   else
   6742     h->plt.plist = NULL;
   6743 
   6744   /* If this is a weak symbol, and there is a real definition, the
   6745      processor independent code will have arranged for us to see the
   6746      real definition first, and we can just use the same value.  */
   6747   if (h->is_weakalias)
   6748     {
   6749       struct elf_link_hash_entry *def = weakdef (h);
   6750       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
   6751       h->root.u.def.section = def->root.u.def.section;
   6752       h->root.u.def.value = def->root.u.def.value;
   6753       if (def->root.u.def.section == htab->elf.sdynbss
   6754 	  || def->root.u.def.section == htab->elf.sdynrelro)
   6755 	h->dyn_relocs = NULL;
   6756       return true;
   6757     }
   6758 
   6759   /* If we are creating a shared library, we must presume that the
   6760      only references to the symbol are via the global offset table.
   6761      For such cases we need not do anything here; the relocations will
   6762      be handled correctly by relocate_section.  */
   6763   if (!bfd_link_executable (info))
   6764     return true;
   6765 
   6766   /* If there are no references to this symbol that do not use the
   6767      GOT, we don't need to generate a copy reloc.  */
   6768   if (!h->non_got_ref)
   6769     return true;
   6770 
   6771   /* Don't generate a copy reloc for symbols defined in the executable.  */
   6772   if (!h->def_dynamic || !h->ref_regular || h->def_regular
   6773 
   6774       /* If -z nocopyreloc was given, don't generate them either.  */
   6775       || info->nocopyreloc
   6776 
   6777       /* If we don't find any dynamic relocs in read-only sections, then
   6778 	 we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
   6779       || (ELIMINATE_COPY_RELOCS
   6780 	  && !h->needs_copy
   6781 	  && !alias_readonly_dynrelocs (h))
   6782 
   6783       /* Protected variables do not work with .dynbss.  The copy in
   6784 	 .dynbss won't be used by the shared library with the protected
   6785 	 definition for the variable.  Text relocations are preferable
   6786 	 to an incorrect program.  */
   6787       || h->protected_def)
   6788     return true;
   6789 
   6790   if (h->type == STT_FUNC
   6791       || h->type == STT_GNU_IFUNC)
   6792     {
   6793       /* .dynbss copies of function symbols only work if we have
   6794 	 ELFv1 dot-symbols.  ELFv1 compilers since 2004 default to not
   6795 	 use dot-symbols and set the function symbol size to the text
   6796 	 size of the function rather than the size of the descriptor.
   6797 	 That's wrong for copying a descriptor.  */
   6798       if (ppc_elf_hash_entry (h)->oh == NULL
   6799 	  || !(h->size == 24 || h->size == 16))
   6800 	return true;
   6801 
   6802       /* We should never get here, but unfortunately there are old
   6803 	 versions of gcc (circa gcc-3.2) that improperly for the
   6804 	 ELFv1 ABI put initialized function pointers, vtable refs and
   6805 	 suchlike in read-only sections.  Allow them to proceed, but
   6806 	 warn that this might break at runtime.  */
   6807       info->callbacks->einfo
   6808 	(_("%P: copy reloc against `%pT' requires lazy plt linking; "
   6809 	   "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
   6810 	 h->root.root.string);
   6811     }
   6812 
   6813   /* This is a reference to a symbol defined by a dynamic object which
   6814      is not a function.  */
   6815 
   6816   /* We must allocate the symbol in our .dynbss section, which will
   6817      become part of the .bss section of the executable.  There will be
   6818      an entry for this symbol in the .dynsym section.  The dynamic
   6819      object will contain position independent code, so all references
   6820      from the dynamic object to this symbol will go through the global
   6821      offset table.  The dynamic linker will use the .dynsym entry to
   6822      determine the address it must put in the global offset table, so
   6823      both the dynamic object and the regular object will refer to the
   6824      same memory location for the variable.  */
   6825   if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
   6826     {
   6827       s = htab->elf.sdynrelro;
   6828       srel = htab->elf.sreldynrelro;
   6829     }
   6830   else
   6831     {
   6832       s = htab->elf.sdynbss;
   6833       srel = htab->elf.srelbss;
   6834     }
   6835   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
   6836     {
   6837       /* We must generate a R_PPC64_COPY reloc to tell the dynamic
   6838 	 linker to copy the initial value out of the dynamic object
   6839 	 and into the runtime process image.  */
   6840       srel->size += sizeof (Elf64_External_Rela);
   6841       h->needs_copy = 1;
   6842     }
   6843 
   6844   /* We no longer want dyn_relocs.  */
   6845   h->dyn_relocs = NULL;
   6846   return _bfd_elf_adjust_dynamic_copy (info, h, s);
   6847 }
   6848 
   6849 /* If given a function descriptor symbol, hide both the function code
   6850    sym and the descriptor.  */
   6851 static void
   6852 ppc64_elf_hide_symbol (struct bfd_link_info *info,
   6853 		       struct elf_link_hash_entry *h,
   6854 		       bool force_local)
   6855 {
   6856   struct ppc_link_hash_entry *eh;
   6857   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
   6858 
   6859   if (ppc_hash_table (info) == NULL)
   6860     return;
   6861 
   6862   eh = ppc_elf_hash_entry (h);
   6863   if (eh->is_func_descriptor)
   6864     {
   6865       struct ppc_link_hash_entry *fh = eh->oh;
   6866 
   6867       if (fh == NULL)
   6868 	{
   6869 	  const char *p, *q;
   6870 	  struct elf_link_hash_table *htab = elf_hash_table (info);
   6871 	  char save;
   6872 
   6873 	  /* We aren't supposed to use alloca in BFD because on
   6874 	     systems which do not have alloca the version in libiberty
   6875 	     calls xmalloc, which might cause the program to crash
   6876 	     when it runs out of memory.  This function doesn't have a
   6877 	     return status, so there's no way to gracefully return an
   6878 	     error.  So cheat.  We know that string[-1] can be safely
   6879 	     accessed;  It's either a string in an ELF string table,
   6880 	     or allocated in an objalloc structure.  */
   6881 
   6882 	  p = eh->elf.root.root.string - 1;
   6883 	  save = *p;
   6884 	  *(char *) p = '.';
   6885 	  fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
   6886 							 false, false));
   6887 	  *(char *) p = save;
   6888 
   6889 	  /* Unfortunately, if it so happens that the string we were
   6890 	     looking for was allocated immediately before this string,
   6891 	     then we overwrote the string terminator.  That's the only
   6892 	     reason the lookup should fail.  */
   6893 	  if (fh == NULL)
   6894 	    {
   6895 	      q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
   6896 	      while (q >= eh->elf.root.root.string && *q == *p)
   6897 		--q, --p;
   6898 	      if (q < eh->elf.root.root.string && *p == '.')
   6899 		fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
   6900 							       false, false));
   6901 	    }
   6902 	  if (fh != NULL)
   6903 	    {
   6904 	      eh->oh = fh;
   6905 	      fh->oh = eh;
   6906 	    }
   6907 	}
   6908       if (fh != NULL)
   6909 	_bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
   6910     }
   6911 }
   6912 
   6913 static bool
   6914 get_sym_h (struct elf_link_hash_entry **hp,
   6915 	   Elf_Internal_Sym **symp,
   6916 	   asection **symsecp,
   6917 	   unsigned char **tls_maskp,
   6918 	   Elf_Internal_Sym **locsymsp,
   6919 	   unsigned long r_symndx,
   6920 	   bfd *ibfd)
   6921 {
   6922   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
   6923 
   6924   if (r_symndx >= symtab_hdr->sh_info)
   6925     {
   6926       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
   6927       struct elf_link_hash_entry *h;
   6928 
   6929       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   6930       h = elf_follow_link (h);
   6931 
   6932       if (hp != NULL)
   6933 	*hp = h;
   6934 
   6935       if (symp != NULL)
   6936 	*symp = NULL;
   6937 
   6938       if (symsecp != NULL)
   6939 	{
   6940 	  asection *symsec = NULL;
   6941 	  if (h->root.type == bfd_link_hash_defined
   6942 	      || h->root.type == bfd_link_hash_defweak)
   6943 	    symsec = h->root.u.def.section;
   6944 	  *symsecp = symsec;
   6945 	}
   6946 
   6947       if (tls_maskp != NULL)
   6948 	*tls_maskp = &ppc_elf_hash_entry (h)->tls_mask;
   6949     }
   6950   else
   6951     {
   6952       Elf_Internal_Sym *sym;
   6953       Elf_Internal_Sym *locsyms = *locsymsp;
   6954 
   6955       if (locsyms == NULL)
   6956 	{
   6957 	  locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
   6958 	  if (locsyms == NULL)
   6959 	    locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
   6960 					    symtab_hdr->sh_info,
   6961 					    0, NULL, NULL, NULL);
   6962 	  if (locsyms == NULL)
   6963 	    return false;
   6964 	  *locsymsp = locsyms;
   6965 	}
   6966       sym = locsyms + r_symndx;
   6967 
   6968       if (hp != NULL)
   6969 	*hp = NULL;
   6970 
   6971       if (symp != NULL)
   6972 	*symp = sym;
   6973 
   6974       if (symsecp != NULL)
   6975 	*symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
   6976 
   6977       if (tls_maskp != NULL)
   6978 	{
   6979 	  struct got_entry **lgot_ents;
   6980 	  unsigned char *tls_mask;
   6981 
   6982 	  tls_mask = NULL;
   6983 	  lgot_ents = elf_local_got_ents (ibfd);
   6984 	  if (lgot_ents != NULL)
   6985 	    {
   6986 	      struct plt_entry **local_plt = (struct plt_entry **)
   6987 		(lgot_ents + symtab_hdr->sh_info);
   6988 	      unsigned char *lgot_masks = (unsigned char *)
   6989 		(local_plt + symtab_hdr->sh_info);
   6990 	      tls_mask = &lgot_masks[r_symndx];
   6991 	    }
   6992 	  *tls_maskp = tls_mask;
   6993 	}
   6994     }
   6995   return true;
   6996 }
   6997 
   6998 /* Returns TLS_MASKP for the given REL symbol.  Function return is 0 on
   6999    error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
   7000    type suitable for optimization, and 1 otherwise.  */
   7001 
   7002 static int
   7003 get_tls_mask (unsigned char **tls_maskp,
   7004 	      unsigned long *toc_symndx,
   7005 	      bfd_vma *toc_addend,
   7006 	      Elf_Internal_Sym **locsymsp,
   7007 	      const Elf_Internal_Rela *rel,
   7008 	      bfd *ibfd)
   7009 {
   7010   unsigned long r_symndx;
   7011   int next_r;
   7012   struct elf_link_hash_entry *h;
   7013   Elf_Internal_Sym *sym;
   7014   asection *sec;
   7015   bfd_vma off;
   7016 
   7017   r_symndx = ELF64_R_SYM (rel->r_info);
   7018   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
   7019     return 0;
   7020 
   7021   if ((*tls_maskp != NULL
   7022        && (**tls_maskp & TLS_TLS) != 0
   7023        && **tls_maskp != (TLS_TLS | TLS_MARK))
   7024       || sec == NULL
   7025       || ppc64_elf_section_data (sec) == NULL
   7026       || ppc64_elf_section_data (sec)->sec_type != sec_toc)
   7027     return 1;
   7028 
   7029   /* Look inside a TOC section too.  */
   7030   if (h != NULL)
   7031     {
   7032       BFD_ASSERT (h->root.type == bfd_link_hash_defined);
   7033       off = h->root.u.def.value;
   7034     }
   7035   else
   7036     off = sym->st_value;
   7037   off += rel->r_addend;
   7038   BFD_ASSERT (off % 8 == 0);
   7039   r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
   7040   next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
   7041   if (toc_symndx != NULL)
   7042     *toc_symndx = r_symndx;
   7043   if (toc_addend != NULL)
   7044     *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
   7045   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
   7046     return 0;
   7047   if ((h == NULL || is_static_defined (h))
   7048       && (next_r == -1 || next_r == -2))
   7049     return 1 - next_r;
   7050   return 1;
   7051 }
   7052 
   7053 /* Find (or create) an entry in the tocsave hash table.  */
   7054 
   7055 static struct tocsave_entry *
   7056 tocsave_find (struct ppc_link_hash_table *htab,
   7057 	      enum insert_option insert,
   7058 	      Elf_Internal_Sym **local_syms,
   7059 	      const Elf_Internal_Rela *irela,
   7060 	      bfd *ibfd)
   7061 {
   7062   unsigned long r_indx;
   7063   struct elf_link_hash_entry *h;
   7064   Elf_Internal_Sym *sym;
   7065   struct tocsave_entry ent, *p;
   7066   hashval_t hash;
   7067   struct tocsave_entry **slot;
   7068 
   7069   r_indx = ELF64_R_SYM (irela->r_info);
   7070   if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
   7071     return NULL;
   7072   if (ent.sec == NULL || ent.sec->output_section == NULL)
   7073     {
   7074       _bfd_error_handler
   7075 	(_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
   7076       return NULL;
   7077     }
   7078 
   7079   if (h != NULL)
   7080     ent.offset = h->root.u.def.value;
   7081   else
   7082     ent.offset = sym->st_value;
   7083   ent.offset += irela->r_addend;
   7084 
   7085   hash = tocsave_htab_hash (&ent);
   7086   slot = ((struct tocsave_entry **)
   7087 	  htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
   7088   if (slot == NULL)
   7089     return NULL;
   7090 
   7091   if (*slot == NULL)
   7092     {
   7093       p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
   7094       if (p == NULL)
   7095 	return NULL;
   7096       *p = ent;
   7097       *slot = p;
   7098     }
   7099   return *slot;
   7100 }
   7101 
   7102 /* Adjust all global syms defined in opd sections.  In gcc generated
   7103    code for the old ABI, these will already have been done.  */
   7104 
   7105 static bool
   7106 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
   7107 {
   7108   struct ppc_link_hash_entry *eh;
   7109   asection *sym_sec;
   7110   struct _opd_sec_data *opd;
   7111 
   7112   if (h->root.type == bfd_link_hash_indirect)
   7113     return true;
   7114 
   7115   if (h->root.type != bfd_link_hash_defined
   7116       && h->root.type != bfd_link_hash_defweak)
   7117     return true;
   7118 
   7119   eh = ppc_elf_hash_entry (h);
   7120   if (eh->adjust_done)
   7121     return true;
   7122 
   7123   sym_sec = eh->elf.root.u.def.section;
   7124   opd = get_opd_info (sym_sec);
   7125   if (opd != NULL && opd->adjust != NULL)
   7126     {
   7127       long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
   7128       if (adjust == -1)
   7129 	{
   7130 	  /* This entry has been deleted.  */
   7131 	  asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
   7132 	  if (dsec == NULL)
   7133 	    {
   7134 	      for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
   7135 		if (discarded_section (dsec))
   7136 		  {
   7137 		    ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
   7138 		    break;
   7139 		  }
   7140 	    }
   7141 	  eh->elf.root.u.def.value = 0;
   7142 	  eh->elf.root.u.def.section = dsec;
   7143 	}
   7144       else
   7145 	eh->elf.root.u.def.value += adjust;
   7146       eh->adjust_done = 1;
   7147     }
   7148   return true;
   7149 }
   7150 
   7151 /* Handles decrementing dynamic reloc counts for the reloc specified by
   7152    R_INFO in section SEC.  If LOCAL_SYMS is NULL, then H and SYM
   7153    have already been determined.  */
   7154 
   7155 static bool
   7156 dec_dynrel_count (const Elf_Internal_Rela *rel,
   7157 		  asection *sec,
   7158 		  struct bfd_link_info *info,
   7159 		  Elf_Internal_Sym **local_syms,
   7160 		  struct elf_link_hash_entry *h,
   7161 		  Elf_Internal_Sym *sym)
   7162 {
   7163   enum elf_ppc64_reloc_type r_type;
   7164   asection *sym_sec = NULL;
   7165 
   7166   /* Can this reloc be dynamic?  This switch, and later tests here
   7167      should be kept in sync with the code in check_relocs.  */
   7168   r_type = ELF64_R_TYPE (rel->r_info);
   7169   switch (r_type)
   7170     {
   7171     default:
   7172       return true;
   7173 
   7174     case R_PPC64_TOC16:
   7175     case R_PPC64_TOC16_DS:
   7176     case R_PPC64_TOC16_LO:
   7177     case R_PPC64_TOC16_HI:
   7178     case R_PPC64_TOC16_HA:
   7179     case R_PPC64_TOC16_LO_DS:
   7180       if (h == NULL)
   7181 	return true;
   7182       break;
   7183 
   7184     case R_PPC64_TPREL16:
   7185     case R_PPC64_TPREL16_LO:
   7186     case R_PPC64_TPREL16_HI:
   7187     case R_PPC64_TPREL16_HA:
   7188     case R_PPC64_TPREL16_DS:
   7189     case R_PPC64_TPREL16_LO_DS:
   7190     case R_PPC64_TPREL16_HIGH:
   7191     case R_PPC64_TPREL16_HIGHA:
   7192     case R_PPC64_TPREL16_HIGHER:
   7193     case R_PPC64_TPREL16_HIGHERA:
   7194     case R_PPC64_TPREL16_HIGHEST:
   7195     case R_PPC64_TPREL16_HIGHESTA:
   7196     case R_PPC64_TPREL64:
   7197     case R_PPC64_TPREL34:
   7198     case R_PPC64_DTPMOD64:
   7199     case R_PPC64_DTPREL64:
   7200     case R_PPC64_ADDR64:
   7201     case R_PPC64_REL30:
   7202     case R_PPC64_REL32:
   7203     case R_PPC64_REL64:
   7204     case R_PPC64_ADDR14:
   7205     case R_PPC64_ADDR14_BRNTAKEN:
   7206     case R_PPC64_ADDR14_BRTAKEN:
   7207     case R_PPC64_ADDR16:
   7208     case R_PPC64_ADDR16_DS:
   7209     case R_PPC64_ADDR16_HA:
   7210     case R_PPC64_ADDR16_HI:
   7211     case R_PPC64_ADDR16_HIGH:
   7212     case R_PPC64_ADDR16_HIGHA:
   7213     case R_PPC64_ADDR16_HIGHER:
   7214     case R_PPC64_ADDR16_HIGHERA:
   7215     case R_PPC64_ADDR16_HIGHEST:
   7216     case R_PPC64_ADDR16_HIGHESTA:
   7217     case R_PPC64_ADDR16_LO:
   7218     case R_PPC64_ADDR16_LO_DS:
   7219     case R_PPC64_ADDR24:
   7220     case R_PPC64_ADDR32:
   7221     case R_PPC64_UADDR16:
   7222     case R_PPC64_UADDR32:
   7223     case R_PPC64_UADDR64:
   7224     case R_PPC64_TOC:
   7225     case R_PPC64_D34:
   7226     case R_PPC64_D34_LO:
   7227     case R_PPC64_D34_HI30:
   7228     case R_PPC64_D34_HA30:
   7229     case R_PPC64_ADDR16_HIGHER34:
   7230     case R_PPC64_ADDR16_HIGHERA34:
   7231     case R_PPC64_ADDR16_HIGHEST34:
   7232     case R_PPC64_ADDR16_HIGHESTA34:
   7233     case R_PPC64_D28:
   7234       break;
   7235     }
   7236 
   7237   if (local_syms != NULL)
   7238     {
   7239       unsigned long r_symndx;
   7240       bfd *ibfd = sec->owner;
   7241 
   7242       r_symndx = ELF64_R_SYM (rel->r_info);
   7243       if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
   7244 	return false;
   7245     }
   7246 
   7247   if ((h != NULL
   7248        && !SYMBOL_REFERENCES_LOCAL (info, h))
   7249       || (bfd_link_pic (info)
   7250 	  && (h != NULL
   7251 	      ? !bfd_is_abs_symbol (&h->root)
   7252 	      : sym_sec != bfd_abs_section_ptr)
   7253 	  && must_be_dyn_reloc (info, r_type))
   7254       || (!bfd_link_pic (info)
   7255 	  && (h != NULL
   7256 	      ? h->type == STT_GNU_IFUNC
   7257 	      : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
   7258     ;
   7259   else
   7260     return true;
   7261 
   7262   if (h != NULL)
   7263     {
   7264       struct ppc_dyn_relocs *p;
   7265       struct ppc_dyn_relocs **pp;
   7266       pp = (struct ppc_dyn_relocs **) &h->dyn_relocs;
   7267 
   7268       /* elf_gc_sweep may have already removed all dyn relocs associated
   7269 	 with local syms for a given section.  Also, symbol flags are
   7270 	 changed by elf_gc_sweep_symbol, confusing the test above.  Don't
   7271 	 report a dynreloc miscount.  */
   7272       if (*pp == NULL && info->gc_sections)
   7273 	return true;
   7274 
   7275       while ((p = *pp) != NULL)
   7276 	{
   7277 	  if (p->sec == sec)
   7278 	    {
   7279 	      if (!must_be_dyn_reloc (info, r_type))
   7280 		p->pc_count -= 1;
   7281 	      if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
   7282 		  && rel->r_offset % 2 == 0
   7283 		  && sec->alignment_power != 0)
   7284 		p->rel_count -= 1;
   7285 	      p->count -= 1;
   7286 	      if (p->count == 0)
   7287 		*pp = p->next;
   7288 	      return true;
   7289 	    }
   7290 	  pp = &p->next;
   7291 	}
   7292     }
   7293   else
   7294     {
   7295       struct ppc_local_dyn_relocs *p;
   7296       struct ppc_local_dyn_relocs **pp;
   7297       void *vpp;
   7298       bool is_ifunc;
   7299 
   7300       if (local_syms == NULL)
   7301 	sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
   7302       if (sym_sec == NULL)
   7303 	sym_sec = sec;
   7304 
   7305       vpp = &elf_section_data (sym_sec)->local_dynrel;
   7306       pp = (struct ppc_local_dyn_relocs **) vpp;
   7307 
   7308       if (*pp == NULL && info->gc_sections)
   7309 	return true;
   7310 
   7311       is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
   7312       while ((p = *pp) != NULL)
   7313 	{
   7314 	  if (p->sec == sec && p->ifunc == is_ifunc)
   7315 	    {
   7316 	      if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
   7317 		  && rel->r_offset % 2 == 0
   7318 		  && sec->alignment_power != 0)
   7319 		p->rel_count -= 1;
   7320 	      p->count -= 1;
   7321 	      if (p->count == 0)
   7322 		*pp = p->next;
   7323 	      return true;
   7324 	    }
   7325 	  pp = &p->next;
   7326 	}
   7327     }
   7328 
   7329   /* xgettext:c-format */
   7330   _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
   7331 		      sec->owner, sec);
   7332   bfd_set_error (bfd_error_bad_value);
   7333   return false;
   7334 }
   7335 
   7336 /* Remove unused Official Procedure Descriptor entries.  Currently we
   7337    only remove those associated with functions in discarded link-once
   7338    sections, or weakly defined functions that have been overridden.  It
   7339    would be possible to remove many more entries for statically linked
   7340    applications.  */
   7341 
   7342 bool
   7343 ppc64_elf_edit_opd (struct bfd_link_info *info)
   7344 {
   7345   bfd *ibfd;
   7346   bool some_edited = false;
   7347   asection *need_pad = NULL;
   7348   struct ppc_link_hash_table *htab;
   7349 
   7350   htab = ppc_hash_table (info);
   7351   if (htab == NULL)
   7352     return false;
   7353 
   7354   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   7355     {
   7356       asection *sec;
   7357       Elf_Internal_Rela *relstart, *rel, *relend;
   7358       Elf_Internal_Shdr *symtab_hdr;
   7359       Elf_Internal_Sym *local_syms;
   7360       struct _opd_sec_data *opd;
   7361       bool need_edit, add_aux_fields, broken;
   7362       bfd_size_type cnt_16b = 0;
   7363 
   7364       if (!is_ppc64_elf (ibfd))
   7365 	continue;
   7366 
   7367       sec = bfd_get_section_by_name (ibfd, ".opd");
   7368       if (sec == NULL || sec->size == 0)
   7369 	continue;
   7370 
   7371       if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   7372 	continue;
   7373 
   7374       if (sec->output_section == bfd_abs_section_ptr)
   7375 	continue;
   7376 
   7377       /* Look through the section relocs.  */
   7378       if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
   7379 	continue;
   7380 
   7381       local_syms = NULL;
   7382       symtab_hdr = &elf_symtab_hdr (ibfd);
   7383 
   7384       /* Read the relocations.  */
   7385       relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   7386 					    info->keep_memory);
   7387       if (relstart == NULL)
   7388 	return false;
   7389 
   7390       /* First run through the relocs to check they are sane, and to
   7391 	 determine whether we need to edit this opd section.  */
   7392       need_edit = false;
   7393       broken = false;
   7394       need_pad = sec;
   7395       relend = relstart + sec->reloc_count;
   7396       for (rel = relstart; rel < relend; )
   7397 	{
   7398 	  enum elf_ppc64_reloc_type r_type;
   7399 	  unsigned long r_symndx;
   7400 	  asection *sym_sec;
   7401 	  struct elf_link_hash_entry *h;
   7402 	  Elf_Internal_Sym *sym;
   7403 	  bfd_vma offset;
   7404 
   7405 	  /* .opd contains an array of 16 or 24 byte entries.  We're
   7406 	     only interested in the reloc pointing to a function entry
   7407 	     point.  */
   7408 	  offset = rel->r_offset;
   7409 	  if (rel + 1 == relend
   7410 	      || rel[1].r_offset != offset + 8)
   7411 	    {
   7412 	      /* If someone messes with .opd alignment then after a
   7413 		 "ld -r" we might have padding in the middle of .opd.
   7414 		 Also, there's nothing to prevent someone putting
   7415 		 something silly in .opd with the assembler.  No .opd
   7416 		 optimization for them!  */
   7417 	    broken_opd:
   7418 	      _bfd_error_handler
   7419 		(_("%pB: .opd is not a regular array of opd entries"), ibfd);
   7420 	      broken = true;
   7421 	      break;
   7422 	    }
   7423 
   7424 	  if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
   7425 	      || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
   7426 	    {
   7427 	      _bfd_error_handler
   7428 		/* xgettext:c-format */
   7429 		(_("%pB: unexpected reloc type %u in .opd section"),
   7430 		 ibfd, r_type);
   7431 	      broken = true;
   7432 	      break;
   7433 	    }
   7434 
   7435 	  r_symndx = ELF64_R_SYM (rel->r_info);
   7436 	  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   7437 			  r_symndx, ibfd))
   7438 	    goto error_ret;
   7439 
   7440 	  if (sym_sec == NULL || sym_sec->owner == NULL)
   7441 	    {
   7442 	      const char *sym_name;
   7443 	      if (h != NULL)
   7444 		sym_name = h->root.root.string;
   7445 	      else
   7446 		sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
   7447 					     sym_sec);
   7448 
   7449 	      _bfd_error_handler
   7450 		/* xgettext:c-format */
   7451 		(_("%pB: undefined sym `%s' in .opd section"),
   7452 		 ibfd, sym_name);
   7453 	      broken = true;
   7454 	      break;
   7455 	    }
   7456 
   7457 	  /* opd entries are always for functions defined in the
   7458 	     current input bfd.  If the symbol isn't defined in the
   7459 	     input bfd, then we won't be using the function in this
   7460 	     bfd;  It must be defined in a linkonce section in another
   7461 	     bfd, or is weak.  It's also possible that we are
   7462 	     discarding the function due to a linker script /DISCARD/,
   7463 	     which we test for via the output_section.  */
   7464 	  if (sym_sec->owner != ibfd
   7465 	      || sym_sec->output_section == bfd_abs_section_ptr)
   7466 	    need_edit = true;
   7467 
   7468 	  rel += 2;
   7469 	  if (rel + 1 == relend
   7470 	      || (rel + 2 < relend
   7471 		  && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
   7472 	    ++rel;
   7473 
   7474 	  if (rel == relend)
   7475 	    {
   7476 	      if (sec->size == offset + 24)
   7477 		{
   7478 		  need_pad = NULL;
   7479 		  break;
   7480 		}
   7481 	      if (sec->size == offset + 16)
   7482 		{
   7483 		  cnt_16b++;
   7484 		  break;
   7485 		}
   7486 	      goto broken_opd;
   7487 	    }
   7488 	  else if (rel + 1 < relend
   7489 		   && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
   7490 		   && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
   7491 	    {
   7492 	      if (rel[0].r_offset == offset + 16)
   7493 		cnt_16b++;
   7494 	      else if (rel[0].r_offset != offset + 24)
   7495 		goto broken_opd;
   7496 	    }
   7497 	  else
   7498 	    goto broken_opd;
   7499 	}
   7500 
   7501       add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
   7502 
   7503       if (!broken && (need_edit || add_aux_fields))
   7504 	{
   7505 	  Elf_Internal_Rela *write_rel;
   7506 	  Elf_Internal_Shdr *rel_hdr;
   7507 	  bfd_byte *rptr, *wptr;
   7508 	  bfd_byte *new_contents;
   7509 	  bfd_size_type amt;
   7510 
   7511 	  new_contents = NULL;
   7512 	  amt = OPD_NDX (sec->size) * sizeof (long);
   7513 	  opd = &ppc64_elf_section_data (sec)->u.opd;
   7514 	  opd->adjust = bfd_zalloc (sec->owner, amt);
   7515 	  if (opd->adjust == NULL)
   7516 	    return false;
   7517 
   7518 	  /* This seems a waste of time as input .opd sections are all
   7519 	     zeros as generated by gcc, but I suppose there's no reason
   7520 	     this will always be so.  We might start putting something in
   7521 	     the third word of .opd entries.  */
   7522 	  if ((sec->flags & SEC_IN_MEMORY) == 0)
   7523 	    {
   7524 	      bfd_byte *loc;
   7525 	      if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
   7526 		{
   7527 		  free (loc);
   7528 		error_ret:
   7529 		  if (symtab_hdr->contents != (unsigned char *) local_syms)
   7530 		    free (local_syms);
   7531 		  if (elf_section_data (sec)->relocs != relstart)
   7532 		    free (relstart);
   7533 		  return false;
   7534 		}
   7535 	      sec->contents = loc;
   7536 	      sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
   7537 	    }
   7538 
   7539 	  elf_section_data (sec)->relocs = relstart;
   7540 
   7541 	  new_contents = sec->contents;
   7542 	  if (add_aux_fields)
   7543 	    {
   7544 	      new_contents = bfd_malloc (sec->size + cnt_16b * 8);
   7545 	      if (new_contents == NULL)
   7546 		return false;
   7547 	      need_pad = NULL;
   7548 	    }
   7549 	  wptr = new_contents;
   7550 	  rptr = sec->contents;
   7551 	  write_rel = relstart;
   7552 	  for (rel = relstart; rel < relend; )
   7553 	    {
   7554 	      unsigned long r_symndx;
   7555 	      asection *sym_sec;
   7556 	      struct elf_link_hash_entry *h;
   7557 	      struct ppc_link_hash_entry *fdh = NULL;
   7558 	      Elf_Internal_Sym *sym;
   7559 	      long opd_ent_size;
   7560 	      Elf_Internal_Rela *next_rel;
   7561 	      bool skip;
   7562 
   7563 	      r_symndx = ELF64_R_SYM (rel->r_info);
   7564 	      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   7565 			      r_symndx, ibfd))
   7566 		goto error_ret;
   7567 
   7568 	      next_rel = rel + 2;
   7569 	      if (next_rel + 1 == relend
   7570 		  || (next_rel + 2 < relend
   7571 		      && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
   7572 		++next_rel;
   7573 
   7574 	      /* See if the .opd entry is full 24 byte or
   7575 		 16 byte (with fd_aux entry overlapped with next
   7576 		 fd_func).  */
   7577 	      opd_ent_size = 24;
   7578 	      if (next_rel == relend)
   7579 		{
   7580 		  if (sec->size == rel->r_offset + 16)
   7581 		    opd_ent_size = 16;
   7582 		}
   7583 	      else if (next_rel->r_offset == rel->r_offset + 16)
   7584 		opd_ent_size = 16;
   7585 
   7586 	      if (h != NULL
   7587 		  && h->root.root.string[0] == '.')
   7588 		{
   7589 		  fdh = ppc_elf_hash_entry (h)->oh;
   7590 		  if (fdh != NULL)
   7591 		    {
   7592 		      fdh = ppc_follow_link (fdh);
   7593 		      if (fdh->elf.root.type != bfd_link_hash_defined
   7594 			  && fdh->elf.root.type != bfd_link_hash_defweak)
   7595 			fdh = NULL;
   7596 		    }
   7597 		}
   7598 
   7599 	      skip = (sym_sec->owner != ibfd
   7600 		      || sym_sec->output_section == bfd_abs_section_ptr);
   7601 	      if (skip)
   7602 		{
   7603 		  if (fdh != NULL && sym_sec->owner == ibfd)
   7604 		    {
   7605 		      /* Arrange for the function descriptor sym
   7606 			 to be dropped.  */
   7607 		      fdh->elf.root.u.def.value = 0;
   7608 		      fdh->elf.root.u.def.section = sym_sec;
   7609 		    }
   7610 		  opd->adjust[OPD_NDX (rel->r_offset)] = -1;
   7611 
   7612 		  if (NO_OPD_RELOCS || bfd_link_relocatable (info))
   7613 		    rel = next_rel;
   7614 		  else
   7615 		    while (1)
   7616 		      {
   7617 			if (!dec_dynrel_count (rel, sec, info,
   7618 					       NULL, h, sym))
   7619 			  goto error_ret;
   7620 
   7621 			if (++rel == next_rel)
   7622 			  break;
   7623 
   7624 			r_symndx = ELF64_R_SYM (rel->r_info);
   7625 			if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   7626 					r_symndx, ibfd))
   7627 			  goto error_ret;
   7628 		      }
   7629 		}
   7630 	      else
   7631 		{
   7632 		  /* We'll be keeping this opd entry.  */
   7633 		  long adjust;
   7634 
   7635 		  if (fdh != NULL)
   7636 		    {
   7637 		      /* Redefine the function descriptor symbol to
   7638 			 this location in the opd section.  It is
   7639 			 necessary to update the value here rather
   7640 			 than using an array of adjustments as we do
   7641 			 for local symbols, because various places
   7642 			 in the generic ELF code use the value
   7643 			 stored in u.def.value.  */
   7644 		      fdh->elf.root.u.def.value = wptr - new_contents;
   7645 		      fdh->adjust_done = 1;
   7646 		    }
   7647 
   7648 		  /* Local syms are a bit tricky.  We could
   7649 		     tweak them as they can be cached, but
   7650 		     we'd need to look through the local syms
   7651 		     for the function descriptor sym which we
   7652 		     don't have at the moment.  So keep an
   7653 		     array of adjustments.  */
   7654 		  adjust = (wptr - new_contents) - (rptr - sec->contents);
   7655 		  opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
   7656 
   7657 		  if (wptr != rptr)
   7658 		    memcpy (wptr, rptr, opd_ent_size);
   7659 		  wptr += opd_ent_size;
   7660 		  if (add_aux_fields && opd_ent_size == 16)
   7661 		    {
   7662 		      memset (wptr, '\0', 8);
   7663 		      wptr += 8;
   7664 		    }
   7665 
   7666 		  /* We need to adjust any reloc offsets to point to the
   7667 		     new opd entries.  */
   7668 		  for ( ; rel != next_rel; ++rel)
   7669 		    {
   7670 		      rel->r_offset += adjust;
   7671 		      if (write_rel != rel)
   7672 			memcpy (write_rel, rel, sizeof (*rel));
   7673 		      ++write_rel;
   7674 		    }
   7675 		}
   7676 
   7677 	      rptr += opd_ent_size;
   7678 	    }
   7679 
   7680 	  sec->size = wptr - new_contents;
   7681 	  sec->reloc_count = write_rel - relstart;
   7682 	  if (add_aux_fields)
   7683 	    {
   7684 	      free (sec->contents);
   7685 	      sec->contents = new_contents;
   7686 	    }
   7687 
   7688 	  /* Fudge the header size too, as this is used later in
   7689 	     elf_bfd_final_link if we are emitting relocs.  */
   7690 	  rel_hdr = _bfd_elf_single_rel_hdr (sec);
   7691 	  rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
   7692 	  some_edited = true;
   7693 	}
   7694       else if (elf_section_data (sec)->relocs != relstart)
   7695 	free (relstart);
   7696 
   7697       if (local_syms != NULL
   7698 	  && symtab_hdr->contents != (unsigned char *) local_syms)
   7699 	{
   7700 	  if (!info->keep_memory)
   7701 	    free (local_syms);
   7702 	  else
   7703 	    symtab_hdr->contents = (unsigned char *) local_syms;
   7704 	}
   7705     }
   7706 
   7707   if (some_edited)
   7708     elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
   7709 
   7710   /* If we are doing a final link and the last .opd entry is just 16 byte
   7711      long, add a 8 byte padding after it.  */
   7712   if (need_pad != NULL && !bfd_link_relocatable (info))
   7713     {
   7714       bfd_byte *p;
   7715 
   7716       if ((need_pad->flags & SEC_IN_MEMORY) == 0)
   7717 	{
   7718 	  BFD_ASSERT (need_pad->size > 0);
   7719 
   7720 	  p = bfd_malloc (need_pad->size + 8);
   7721 	  if (p == NULL)
   7722 	    return false;
   7723 
   7724 	  if (!bfd_get_section_contents (need_pad->owner, need_pad,
   7725 					 p, 0, need_pad->size))
   7726 	    return false;
   7727 
   7728 	  need_pad->contents = p;
   7729 	  need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
   7730 	}
   7731       else
   7732 	{
   7733 	  p = bfd_realloc (need_pad->contents, need_pad->size + 8);
   7734 	  if (p == NULL)
   7735 	    return false;
   7736 
   7737 	  need_pad->contents = p;
   7738 	}
   7739 
   7740       memset (need_pad->contents + need_pad->size, 0, 8);
   7741       need_pad->size += 8;
   7742     }
   7743 
   7744   return true;
   7745 }
   7746 
   7747 /* Analyze inline PLT call relocations to see whether calls to locally
   7748    defined functions can be converted to direct calls.  */
   7749 
   7750 bool
   7751 ppc64_elf_inline_plt (struct bfd_link_info *info)
   7752 {
   7753   struct ppc_link_hash_table *htab;
   7754   bfd *ibfd;
   7755   asection *sec;
   7756   bfd_vma low_vma, high_vma, limit;
   7757 
   7758   htab = ppc_hash_table (info);
   7759   if (htab == NULL)
   7760     return false;
   7761 
   7762   /* A bl insn can reach -0x2000000 to 0x1fffffc.  The limit is
   7763      reduced somewhat to cater for possible stubs that might be added
   7764      between the call and its destination.  */
   7765   if (htab->params->group_size < 0)
   7766     {
   7767       limit = -htab->params->group_size;
   7768       if (limit == 1)
   7769 	limit = 0x1e00000;
   7770     }
   7771   else
   7772     {
   7773       limit = htab->params->group_size;
   7774       if (limit == 1)
   7775 	limit = 0x1c00000;
   7776     }
   7777 
   7778   low_vma = -1;
   7779   high_vma = 0;
   7780   for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
   7781     if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
   7782       {
   7783 	if (low_vma > sec->vma)
   7784 	  low_vma = sec->vma;
   7785 	if (high_vma < sec->vma + sec->size)
   7786 	  high_vma = sec->vma + sec->size;
   7787       }
   7788 
   7789   /* If a "bl" can reach anywhere in local code sections, then we can
   7790      convert all inline PLT sequences to direct calls when the symbol
   7791      is local.  */
   7792   if (high_vma - low_vma < limit)
   7793     {
   7794       htab->can_convert_all_inline_plt = 1;
   7795       return true;
   7796     }
   7797 
   7798   /* Otherwise, go looking through relocs for cases where a direct
   7799      call won't reach.  Mark the symbol on any such reloc to disable
   7800      the optimization and keep the PLT entry as it seems likely that
   7801      this will be better than creating trampolines.  Note that this
   7802      will disable the optimization for all inline PLT calls to a
   7803      particular symbol, not just those that won't reach.  The
   7804      difficulty in doing a more precise optimization is that the
   7805      linker needs to make a decision depending on whether a
   7806      particular R_PPC64_PLTCALL insn can be turned into a direct
   7807      call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
   7808      the sequence, and there is nothing that ties those relocs
   7809      together except their symbol.  */
   7810 
   7811   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   7812     {
   7813       Elf_Internal_Shdr *symtab_hdr;
   7814       Elf_Internal_Sym *local_syms;
   7815 
   7816       if (!is_ppc64_elf (ibfd))
   7817 	continue;
   7818 
   7819       local_syms = NULL;
   7820       symtab_hdr = &elf_symtab_hdr (ibfd);
   7821 
   7822       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   7823 	if (ppc64_elf_section_data (sec)->has_pltcall
   7824 	    && !bfd_is_abs_section (sec->output_section))
   7825 	  {
   7826 	    Elf_Internal_Rela *relstart, *rel, *relend;
   7827 
   7828 	    /* Read the relocations.  */
   7829 	    relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   7830 						  info->keep_memory);
   7831 	    if (relstart == NULL)
   7832 	      return false;
   7833 
   7834 	    relend = relstart + sec->reloc_count;
   7835 	    for (rel = relstart; rel < relend; rel++)
   7836 	      {
   7837 		enum elf_ppc64_reloc_type r_type;
   7838 		unsigned long r_symndx;
   7839 		asection *sym_sec;
   7840 		struct elf_link_hash_entry *h;
   7841 		Elf_Internal_Sym *sym;
   7842 		unsigned char *tls_maskp;
   7843 
   7844 		r_type = ELF64_R_TYPE (rel->r_info);
   7845 		if (r_type != R_PPC64_PLTCALL
   7846 		    && r_type != R_PPC64_PLTCALL_NOTOC)
   7847 		  continue;
   7848 
   7849 		r_symndx = ELF64_R_SYM (rel->r_info);
   7850 		if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
   7851 				r_symndx, ibfd))
   7852 		  {
   7853 		    if (elf_section_data (sec)->relocs != relstart)
   7854 		      free (relstart);
   7855 		    if (symtab_hdr->contents != (bfd_byte *) local_syms)
   7856 		      free (local_syms);
   7857 		    return false;
   7858 		  }
   7859 
   7860 		if (sym_sec != NULL && sym_sec->output_section != NULL)
   7861 		  {
   7862 		    bfd_vma from, to;
   7863 		    if (h != NULL)
   7864 		      to = h->root.u.def.value;
   7865 		    else
   7866 		      to = sym->st_value;
   7867 		    to += (rel->r_addend
   7868 			   + sym_sec->output_offset
   7869 			   + sym_sec->output_section->vma);
   7870 		    from = (rel->r_offset
   7871 			    + sec->output_offset
   7872 			    + sec->output_section->vma);
   7873 		    if (to - from + limit < 2 * limit
   7874 			&& !(r_type == R_PPC64_PLTCALL_NOTOC
   7875 			     && (((h ? h->other : sym->st_other)
   7876 				  & STO_PPC64_LOCAL_MASK)
   7877 				 > 1 << STO_PPC64_LOCAL_BIT)))
   7878 		      *tls_maskp &= ~PLT_KEEP;
   7879 		  }
   7880 	      }
   7881 	    if (elf_section_data (sec)->relocs != relstart)
   7882 	      free (relstart);
   7883 	  }
   7884 
   7885       if (local_syms != NULL
   7886 	  && symtab_hdr->contents != (unsigned char *) local_syms)
   7887 	{
   7888 	  if (!info->keep_memory)
   7889 	    free (local_syms);
   7890 	  else
   7891 	    symtab_hdr->contents = (unsigned char *) local_syms;
   7892 	}
   7893     }
   7894 
   7895   return true;
   7896 }
   7897 
   7898 /* Set htab->tls_get_addr and various other info specific to TLS.
   7899    This needs to run before dynamic symbols are processed in
   7900    bfd_elf_size_dynamic_sections.  */
   7901 
   7902 bool
   7903 ppc64_elf_tls_setup (struct bfd_link_info *info)
   7904 {
   7905   struct ppc_link_hash_table *htab;
   7906   struct elf_link_hash_entry *tga, *tga_fd, *desc, *desc_fd;
   7907 
   7908   htab = ppc_hash_table (info);
   7909   if (htab == NULL)
   7910     return false;
   7911 
   7912   /* Move dynamic linking info to the function descriptor sym.  */
   7913   if (htab->need_func_desc_adj)
   7914     {
   7915       elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
   7916       htab->need_func_desc_adj = 0;
   7917     }
   7918 
   7919   if (abiversion (info->output_bfd) == 1)
   7920     htab->opd_abi = 1;
   7921 
   7922   if (htab->params->no_multi_toc)
   7923     htab->do_multi_toc = 0;
   7924   else if (!htab->do_multi_toc)
   7925     htab->params->no_multi_toc = 1;
   7926 
   7927   /* Default to --no-plt-localentry, as this option can cause problems
   7928      with symbol interposition.  For example, glibc libpthread.so and
   7929      libc.so duplicate many pthread symbols, with a fallback
   7930      implementation in libc.so.  In some cases the fallback does more
   7931      work than the pthread implementation.  __pthread_condattr_destroy
   7932      is one such symbol: the libpthread.so implementation is
   7933      localentry:0 while the libc.so implementation is localentry:8.
   7934      An app that "cleverly" uses dlopen to only load necessary
   7935      libraries at runtime may omit loading libpthread.so when not
   7936      running multi-threaded, which then results in the libc.so
   7937      fallback symbols being used and ld.so complaining.  Now there
   7938      are workarounds in ld (see non_zero_localentry) to detect the
   7939      pthread situation, but that may not be the only case where
   7940      --plt-localentry can cause trouble.  */
   7941   if (htab->params->plt_localentry0 < 0)
   7942     htab->params->plt_localentry0 = 0;
   7943   if (htab->params->plt_localentry0 && htab->has_power10_relocs)
   7944     {
   7945       /* The issue is that __glink_PLTresolve saves r2, which is done
   7946 	 because glibc ld.so _dl_runtime_resolve restores r2 to support
   7947 	 a glibc plt call optimisation where global entry code is
   7948 	 skipped on calls that resolve to the same binary.  The
   7949 	 __glink_PLTresolve save of r2 is incompatible with code
   7950 	 making tail calls, because the tail call might go via the
   7951 	 resolver and thus overwrite the proper saved r2.  */
   7952       _bfd_error_handler (_("warning: --plt-localentry is incompatible with "
   7953 			    "power10 pc-relative code"));
   7954       htab->params->plt_localentry0 = 0;
   7955     }
   7956   if (htab->params->plt_localentry0
   7957       && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
   7958 			       false, false, false) == NULL)
   7959     _bfd_error_handler
   7960       (_("warning: --plt-localentry is especially dangerous without "
   7961 	 "ld.so support to detect ABI violations"));
   7962 
   7963   tga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
   7964 			      false, false, true);
   7965   htab->tls_get_addr = ppc_elf_hash_entry (tga);
   7966   tga_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
   7967 				 false, false, true);
   7968   htab->tls_get_addr_fd = ppc_elf_hash_entry (tga_fd);
   7969 
   7970   desc = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_desc",
   7971 			       false, false, true);
   7972   htab->tga_desc = ppc_elf_hash_entry (desc);
   7973   desc_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_desc",
   7974 				  false, false, true);
   7975   htab->tga_desc_fd = ppc_elf_hash_entry (desc_fd);
   7976 
   7977   if (htab->params->tls_get_addr_opt)
   7978     {
   7979       struct elf_link_hash_entry *opt, *opt_fd;
   7980 
   7981       opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
   7982 				  false, false, true);
   7983       opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
   7984 				     false, false, true);
   7985       if (opt_fd != NULL
   7986 	  && (opt_fd->root.type == bfd_link_hash_defined
   7987 	      || opt_fd->root.type == bfd_link_hash_defweak))
   7988 	{
   7989 	  /* If glibc supports an optimized __tls_get_addr call stub,
   7990 	     signalled by the presence of __tls_get_addr_opt, and we'll
   7991 	     be calling __tls_get_addr via a plt call stub, then
   7992 	     make __tls_get_addr point to __tls_get_addr_opt.  */
   7993 	  if (!(htab->elf.dynamic_sections_created
   7994 		&& tga_fd != NULL
   7995 		&& (tga_fd->type == STT_FUNC
   7996 		    || tga_fd->needs_plt)
   7997 		&& !(SYMBOL_CALLS_LOCAL (info, tga_fd)
   7998 		     || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd))))
   7999 	    tga_fd = NULL;
   8000 	  if (!(htab->elf.dynamic_sections_created
   8001 		&& desc_fd != NULL
   8002 		&& (desc_fd->type == STT_FUNC
   8003 		    || desc_fd->needs_plt)
   8004 		&& !(SYMBOL_CALLS_LOCAL (info, desc_fd)
   8005 		     || UNDEFWEAK_NO_DYNAMIC_RELOC (info, desc_fd))))
   8006 	    desc_fd = NULL;
   8007 
   8008 	  if (tga_fd != NULL || desc_fd != NULL)
   8009 	    {
   8010 	      struct plt_entry *ent = NULL;
   8011 
   8012 	      if (tga_fd != NULL)
   8013 		for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
   8014 		  if (ent->plt.refcount > 0)
   8015 		    break;
   8016 	      if (ent == NULL && desc_fd != NULL)
   8017 		for (ent = desc_fd->plt.plist; ent != NULL; ent = ent->next)
   8018 		  if (ent->plt.refcount > 0)
   8019 		    break;
   8020 	      if (ent != NULL)
   8021 		{
   8022 		  if (tga_fd != NULL)
   8023 		    {
   8024 		      tga_fd->root.type = bfd_link_hash_indirect;
   8025 		      tga_fd->root.u.i.link = &opt_fd->root;
   8026 		      tga_fd->root.u.i.warning = NULL;
   8027 		      ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
   8028 		    }
   8029 		  if (desc_fd != NULL)
   8030 		    {
   8031 		      desc_fd->root.type = bfd_link_hash_indirect;
   8032 		      desc_fd->root.u.i.link = &opt_fd->root;
   8033 		      desc_fd->root.u.i.warning = NULL;
   8034 		      ppc64_elf_copy_indirect_symbol (info, opt_fd, desc_fd);
   8035 		    }
   8036 		  opt_fd->mark = 1;
   8037 		  if (opt_fd->dynindx != -1)
   8038 		    {
   8039 		      /* Use __tls_get_addr_opt in dynamic relocations.  */
   8040 		      opt_fd->dynindx = -1;
   8041 		      _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
   8042 					      opt_fd->dynstr_index);
   8043 		      if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
   8044 			return false;
   8045 		    }
   8046 		  if (tga_fd != NULL)
   8047 		    {
   8048 		      htab->tls_get_addr_fd = ppc_elf_hash_entry (opt_fd);
   8049 		      tga = elf_hash_entry (htab->tls_get_addr);
   8050 		      if (opt != NULL && tga != NULL)
   8051 			{
   8052 			  tga->root.type = bfd_link_hash_indirect;
   8053 			  tga->root.u.i.link = &opt->root;
   8054 			  tga->root.u.i.warning = NULL;
   8055 			  ppc64_elf_copy_indirect_symbol (info, opt, tga);
   8056 			  opt->mark = 1;
   8057 			  _bfd_elf_link_hash_hide_symbol (info, opt,
   8058 							  tga->forced_local);
   8059 			  htab->tls_get_addr = ppc_elf_hash_entry (opt);
   8060 			}
   8061 		      htab->tls_get_addr_fd->oh = htab->tls_get_addr;
   8062 		      htab->tls_get_addr_fd->is_func_descriptor = 1;
   8063 		      if (htab->tls_get_addr != NULL)
   8064 			{
   8065 			  htab->tls_get_addr->oh = htab->tls_get_addr_fd;
   8066 			  htab->tls_get_addr->is_func = 1;
   8067 			}
   8068 		    }
   8069 		  if (desc_fd != NULL)
   8070 		    {
   8071 		      htab->tga_desc_fd = ppc_elf_hash_entry (opt_fd);
   8072 		      if (opt != NULL && desc != NULL)
   8073 			{
   8074 			  desc->root.type = bfd_link_hash_indirect;
   8075 			  desc->root.u.i.link = &opt->root;
   8076 			  desc->root.u.i.warning = NULL;
   8077 			  ppc64_elf_copy_indirect_symbol (info, opt, desc);
   8078 			  opt->mark = 1;
   8079 			  _bfd_elf_link_hash_hide_symbol (info, opt,
   8080 							  desc->forced_local);
   8081 			  htab->tga_desc = ppc_elf_hash_entry (opt);
   8082 			}
   8083 		      htab->tga_desc_fd->oh = htab->tga_desc;
   8084 		      htab->tga_desc_fd->is_func_descriptor = 1;
   8085 		      if (htab->tga_desc != NULL)
   8086 			{
   8087 			  htab->tga_desc->oh = htab->tga_desc_fd;
   8088 			  htab->tga_desc->is_func = 1;
   8089 			}
   8090 		    }
   8091 		}
   8092 	    }
   8093 	}
   8094       else if (htab->params->tls_get_addr_opt < 0)
   8095 	htab->params->tls_get_addr_opt = 0;
   8096     }
   8097 
   8098   if (htab->tga_desc_fd != NULL
   8099       && htab->params->tls_get_addr_opt
   8100       && htab->params->no_tls_get_addr_regsave == -1)
   8101     htab->params->no_tls_get_addr_regsave = 0;
   8102 
   8103   return true;
   8104 }
   8105 
   8106 /* Return TRUE iff REL is a branch reloc with a global symbol matching
   8107    any of HASH1, HASH2, HASH3, or HASH4.  */
   8108 
   8109 static bool
   8110 branch_reloc_hash_match (bfd *ibfd,
   8111 			 Elf_Internal_Rela *rel,
   8112 			 struct ppc_link_hash_entry *hash1,
   8113 			 struct ppc_link_hash_entry *hash2,
   8114 			 struct ppc_link_hash_entry *hash3,
   8115 			 struct ppc_link_hash_entry *hash4)
   8116 {
   8117   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
   8118   enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
   8119   unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
   8120 
   8121   if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
   8122     {
   8123       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
   8124       struct elf_link_hash_entry *h;
   8125 
   8126       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   8127       h = elf_follow_link (h);
   8128       if (h == elf_hash_entry (hash1)
   8129 	  || h == elf_hash_entry (hash2)
   8130 	  || h == elf_hash_entry (hash3)
   8131 	  || h == elf_hash_entry (hash4))
   8132 	return true;
   8133     }
   8134   return false;
   8135 }
   8136 
   8137 /* Run through all the TLS relocs looking for optimization
   8138    opportunities.  The linker has been hacked (see ppc64elf.em) to do
   8139    a preliminary section layout so that we know the TLS segment
   8140    offsets.  We can't optimize earlier because some optimizations need
   8141    to know the tp offset, and we need to optimize before allocating
   8142    dynamic relocations.  */
   8143 
   8144 bool
   8145 ppc64_elf_tls_optimize (struct bfd_link_info *info)
   8146 {
   8147   bfd *ibfd;
   8148   asection *sec;
   8149   struct ppc_link_hash_table *htab;
   8150   unsigned char *toc_ref;
   8151   int pass;
   8152 
   8153   if (!bfd_link_executable (info))
   8154     return true;
   8155 
   8156   htab = ppc_hash_table (info);
   8157   if (htab == NULL)
   8158     return false;
   8159 
   8160   htab->do_tls_opt = 1;
   8161 
   8162   /* Make two passes over the relocs.  On the first pass, mark toc
   8163      entries involved with tls relocs, and check that tls relocs
   8164      involved in setting up a tls_get_addr call are indeed followed by
   8165      such a call.  If they are not, we can't do any tls optimization.
   8166      On the second pass twiddle tls_mask flags to notify
   8167      relocate_section that optimization can be done, and adjust got
   8168      and plt refcounts.  */
   8169   toc_ref = NULL;
   8170   for (pass = 0; pass < 2; ++pass)
   8171     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   8172       {
   8173 	Elf_Internal_Sym *locsyms = NULL;
   8174 	asection *toc = bfd_get_section_by_name (ibfd, ".toc");
   8175 
   8176 	for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   8177 	  if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
   8178 	    {
   8179 	      Elf_Internal_Rela *relstart, *rel, *relend;
   8180 	      bool found_tls_get_addr_arg = 0;
   8181 
   8182 	      /* Read the relocations.  */
   8183 	      relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   8184 						    info->keep_memory);
   8185 	      if (relstart == NULL)
   8186 		{
   8187 		  free (toc_ref);
   8188 		  return false;
   8189 		}
   8190 
   8191 	      relend = relstart + sec->reloc_count;
   8192 	      for (rel = relstart; rel < relend; rel++)
   8193 		{
   8194 		  enum elf_ppc64_reloc_type r_type;
   8195 		  unsigned long r_symndx;
   8196 		  struct elf_link_hash_entry *h;
   8197 		  Elf_Internal_Sym *sym;
   8198 		  asection *sym_sec;
   8199 		  unsigned char *tls_mask;
   8200 		  unsigned int tls_set, tls_clear, tls_type = 0;
   8201 		  bfd_vma value;
   8202 		  bool ok_tprel, is_local;
   8203 		  long toc_ref_index = 0;
   8204 		  int expecting_tls_get_addr = 0;
   8205 		  bool ret = false;
   8206 
   8207 		  r_symndx = ELF64_R_SYM (rel->r_info);
   8208 		  if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
   8209 				  r_symndx, ibfd))
   8210 		    {
   8211 		    err_free_rel:
   8212 		      if (elf_section_data (sec)->relocs != relstart)
   8213 			free (relstart);
   8214 		      free (toc_ref);
   8215 		      if (elf_symtab_hdr (ibfd).contents
   8216 			  != (unsigned char *) locsyms)
   8217 			free (locsyms);
   8218 		      return ret;
   8219 		    }
   8220 
   8221 		  if (h != NULL)
   8222 		    {
   8223 		      if (h->root.type == bfd_link_hash_defined
   8224 			  || h->root.type == bfd_link_hash_defweak)
   8225 			value = h->root.u.def.value;
   8226 		      else if (h->root.type == bfd_link_hash_undefweak)
   8227 			value = 0;
   8228 		      else
   8229 			{
   8230 			  found_tls_get_addr_arg = 0;
   8231 			  continue;
   8232 			}
   8233 		    }
   8234 		  else
   8235 		    /* Symbols referenced by TLS relocs must be of type
   8236 		       STT_TLS.  So no need for .opd local sym adjust.  */
   8237 		    value = sym->st_value;
   8238 
   8239 		  ok_tprel = false;
   8240 		  is_local = SYMBOL_REFERENCES_LOCAL (info, h);
   8241 		  if (is_local)
   8242 		    {
   8243 		      if (h != NULL
   8244 			  && h->root.type == bfd_link_hash_undefweak)
   8245 			ok_tprel = true;
   8246 		      else if (sym_sec != NULL
   8247 			       && sym_sec->output_section != NULL)
   8248 			{
   8249 			  value += sym_sec->output_offset;
   8250 			  value += sym_sec->output_section->vma;
   8251 			  value -= htab->elf.tls_sec->vma + TP_OFFSET;
   8252 			  /* Note that even though the prefix insns
   8253 			     allow a 1<<33 offset we use the same test
   8254 			     as for addis;addi.  There may be a mix of
   8255 			     pcrel and non-pcrel code and the decision
   8256 			     to optimise is per symbol, not per TLS
   8257 			     sequence.  */
   8258 			  ok_tprel = value + 0x80008000ULL < 1ULL << 32;
   8259 			}
   8260 		    }
   8261 
   8262 		  r_type = ELF64_R_TYPE (rel->r_info);
   8263 		  /* If this section has old-style __tls_get_addr calls
   8264 		     without marker relocs, then check that each
   8265 		     __tls_get_addr call reloc is preceded by a reloc
   8266 		     that conceivably belongs to the __tls_get_addr arg
   8267 		     setup insn.  If we don't find matching arg setup
   8268 		     relocs, don't do any tls optimization.  */
   8269 		  if (pass == 0
   8270 		      && sec->nomark_tls_get_addr
   8271 		      && h != NULL
   8272 		      && is_tls_get_addr (h, htab)
   8273 		      && !found_tls_get_addr_arg
   8274 		      && is_branch_reloc (r_type))
   8275 		    {
   8276 		      info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
   8277 						"TLS optimization disabled\n"),
   8278 					      ibfd, sec, rel->r_offset);
   8279 		      ret = true;
   8280 		      goto err_free_rel;
   8281 		    }
   8282 
   8283 		  found_tls_get_addr_arg = 0;
   8284 		  switch (r_type)
   8285 		    {
   8286 		    case R_PPC64_GOT_TLSLD16:
   8287 		    case R_PPC64_GOT_TLSLD16_LO:
   8288 		    case R_PPC64_GOT_TLSLD_PCREL34:
   8289 		      expecting_tls_get_addr = 1;
   8290 		      found_tls_get_addr_arg = 1;
   8291 		      /* Fall through.  */
   8292 
   8293 		    case R_PPC64_GOT_TLSLD16_HI:
   8294 		    case R_PPC64_GOT_TLSLD16_HA:
   8295 		      /* These relocs should never be against a symbol
   8296 			 defined in a shared lib.  Leave them alone if
   8297 			 that turns out to be the case.  */
   8298 		      if (!is_local)
   8299 			continue;
   8300 
   8301 		      /* LD -> LE */
   8302 		      tls_set = 0;
   8303 		      tls_clear = TLS_LD;
   8304 		      tls_type = TLS_TLS | TLS_LD;
   8305 		      break;
   8306 
   8307 		    case R_PPC64_GOT_TLSGD16:
   8308 		    case R_PPC64_GOT_TLSGD16_LO:
   8309 		    case R_PPC64_GOT_TLSGD_PCREL34:
   8310 		      expecting_tls_get_addr = 1;
   8311 		      found_tls_get_addr_arg = 1;
   8312 		      /* Fall through. */
   8313 
   8314 		    case R_PPC64_GOT_TLSGD16_HI:
   8315 		    case R_PPC64_GOT_TLSGD16_HA:
   8316 		      if (ok_tprel)
   8317 			/* GD -> LE */
   8318 			tls_set = 0;
   8319 		      else
   8320 			/* GD -> IE */
   8321 			tls_set = TLS_TLS | TLS_GDIE;
   8322 		      tls_clear = TLS_GD;
   8323 		      tls_type = TLS_TLS | TLS_GD;
   8324 		      break;
   8325 
   8326 		    case R_PPC64_GOT_TPREL_PCREL34:
   8327 		    case R_PPC64_GOT_TPREL16_DS:
   8328 		    case R_PPC64_GOT_TPREL16_LO_DS:
   8329 		    case R_PPC64_GOT_TPREL16_HI:
   8330 		    case R_PPC64_GOT_TPREL16_HA:
   8331 		      if (ok_tprel)
   8332 			{
   8333 			  /* IE -> LE */
   8334 			  tls_set = 0;
   8335 			  tls_clear = TLS_TPREL;
   8336 			  tls_type = TLS_TLS | TLS_TPREL;
   8337 			  break;
   8338 			}
   8339 		      continue;
   8340 
   8341 		    case R_PPC64_TLSLD:
   8342 		      if (!is_local)
   8343 			continue;
   8344 		      /* Fall through.  */
   8345 		    case R_PPC64_TLSGD:
   8346 		      if (rel + 1 < relend
   8347 			  && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
   8348 			{
   8349 			  if (pass != 0
   8350 			      && (ELF64_R_TYPE (rel[1].r_info)
   8351 				  != R_PPC64_PLTSEQ)
   8352 			      && (ELF64_R_TYPE (rel[1].r_info)
   8353 				  != R_PPC64_PLTSEQ_NOTOC))
   8354 			    {
   8355 			      r_symndx = ELF64_R_SYM (rel[1].r_info);
   8356 			      if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
   8357 					      r_symndx, ibfd))
   8358 				goto err_free_rel;
   8359 			      if (h != NULL)
   8360 				{
   8361 				  struct plt_entry *ent = NULL;
   8362 
   8363 				  for (ent = h->plt.plist;
   8364 				       ent != NULL;
   8365 				       ent = ent->next)
   8366 				    if (ent->addend == rel[1].r_addend)
   8367 				      break;
   8368 
   8369 				  if (ent != NULL
   8370 				      && ent->plt.refcount > 0)
   8371 				    ent->plt.refcount -= 1;
   8372 				}
   8373 			    }
   8374 			  continue;
   8375 			}
   8376 		      found_tls_get_addr_arg = 1;
   8377 		      /* Fall through.  */
   8378 
   8379 		    case R_PPC64_TLS:
   8380 		    case R_PPC64_TOC16:
   8381 		    case R_PPC64_TOC16_LO:
   8382 		      if (sym_sec == NULL || sym_sec != toc)
   8383 			continue;
   8384 
   8385 		      /* Mark this toc entry as referenced by a TLS
   8386 			 code sequence.  We can do that now in the
   8387 			 case of R_PPC64_TLS, and after checking for
   8388 			 tls_get_addr for the TOC16 relocs.  */
   8389 		      if (toc_ref == NULL)
   8390 			toc_ref
   8391 			  = bfd_zmalloc (toc->output_section->rawsize / 8);
   8392 		      if (toc_ref == NULL)
   8393 			goto err_free_rel;
   8394 
   8395 		      if (h != NULL)
   8396 			value = h->root.u.def.value;
   8397 		      else
   8398 			value = sym->st_value;
   8399 		      value += rel->r_addend;
   8400 		      if (value % 8 != 0)
   8401 			continue;
   8402 		      BFD_ASSERT (value < toc->size
   8403 				  && toc->output_offset % 8 == 0);
   8404 		      toc_ref_index = (value + toc->output_offset) / 8;
   8405 		      if (r_type == R_PPC64_TLS
   8406 			  || r_type == R_PPC64_TLSGD
   8407 			  || r_type == R_PPC64_TLSLD)
   8408 			{
   8409 			  toc_ref[toc_ref_index] = 1;
   8410 			  continue;
   8411 			}
   8412 
   8413 		      if (pass != 0 && toc_ref[toc_ref_index] == 0)
   8414 			continue;
   8415 
   8416 		      tls_set = 0;
   8417 		      tls_clear = 0;
   8418 		      expecting_tls_get_addr = 2;
   8419 		      break;
   8420 
   8421 		    case R_PPC64_TPREL64:
   8422 		      if (pass == 0
   8423 			  || sec != toc
   8424 			  || toc_ref == NULL
   8425 			  || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
   8426 			continue;
   8427 		      if (ok_tprel)
   8428 			{
   8429 			  /* IE -> LE */
   8430 			  tls_set = TLS_EXPLICIT;
   8431 			  tls_clear = TLS_TPREL;
   8432 			  break;
   8433 			}
   8434 		      continue;
   8435 
   8436 		    case R_PPC64_DTPMOD64:
   8437 		      if (pass == 0
   8438 			  || sec != toc
   8439 			  || toc_ref == NULL
   8440 			  || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
   8441 			continue;
   8442 		      if (rel + 1 < relend
   8443 			  && (rel[1].r_info
   8444 			      == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
   8445 			  && rel[1].r_offset == rel->r_offset + 8)
   8446 			{
   8447 			  if (ok_tprel)
   8448 			    /* GD -> LE */
   8449 			    tls_set = TLS_EXPLICIT | TLS_GD;
   8450 			  else
   8451 			    /* GD -> IE */
   8452 			    tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
   8453 			  tls_clear = TLS_GD;
   8454 			}
   8455 		      else
   8456 			{
   8457 			  if (!is_local)
   8458 			    continue;
   8459 
   8460 			  /* LD -> LE */
   8461 			  tls_set = TLS_EXPLICIT;
   8462 			  tls_clear = TLS_LD;
   8463 			}
   8464 		      break;
   8465 
   8466 		    case R_PPC64_TPREL16_HA:
   8467 		      if (pass == 0)
   8468 			{
   8469 			  unsigned char buf[4];
   8470 			  unsigned int insn;
   8471 			  bfd_vma off = rel->r_offset & ~3;
   8472 			  if (!bfd_get_section_contents (ibfd, sec, buf,
   8473 							 off, 4))
   8474 			    goto err_free_rel;
   8475 			  insn = bfd_get_32 (ibfd, buf);
   8476 			  /* addis rt,13,imm */
   8477 			  if ((insn & ((0x3fu << 26) | 0x1f << 16))
   8478 			      != ((15u << 26) | (13 << 16)))
   8479 			    {
   8480 			      /* xgettext:c-format */
   8481 			      info->callbacks->minfo
   8482 				(_("%H: warning: %s unexpected insn %#x.\n"),
   8483 				 ibfd, sec, off, "R_PPC64_TPREL16_HA", insn);
   8484 			      htab->do_tls_opt = 0;
   8485 			    }
   8486 			}
   8487 		      continue;
   8488 
   8489 		    case R_PPC64_TPREL16_HI:
   8490 		    case R_PPC64_TPREL16_HIGH:
   8491 		    case R_PPC64_TPREL16_HIGHA:
   8492 		    case R_PPC64_TPREL16_HIGHER:
   8493 		    case R_PPC64_TPREL16_HIGHERA:
   8494 		    case R_PPC64_TPREL16_HIGHEST:
   8495 		    case R_PPC64_TPREL16_HIGHESTA:
   8496 		      /* These can all be used in sequences along with
   8497 			 TPREL16_LO or TPREL16_LO_DS in ways we aren't
   8498 			 able to verify easily.  */
   8499 		      htab->do_tls_opt = 0;
   8500 		      continue;
   8501 
   8502 		    default:
   8503 		      continue;
   8504 		    }
   8505 
   8506 		  if (pass == 0)
   8507 		    {
   8508 		      if (!expecting_tls_get_addr
   8509 			  || !sec->nomark_tls_get_addr)
   8510 			continue;
   8511 
   8512 		      if (rel + 1 < relend
   8513 			  && branch_reloc_hash_match (ibfd, rel + 1,
   8514 						      htab->tls_get_addr_fd,
   8515 						      htab->tga_desc_fd,
   8516 						      htab->tls_get_addr,
   8517 						      htab->tga_desc))
   8518 			{
   8519 			  if (expecting_tls_get_addr == 2)
   8520 			    {
   8521 			      /* Check for toc tls entries.  */
   8522 			      unsigned char *toc_tls;
   8523 			      int retval;
   8524 
   8525 			      retval = get_tls_mask (&toc_tls, NULL, NULL,
   8526 						     &locsyms,
   8527 						     rel, ibfd);
   8528 			      if (retval == 0)
   8529 				goto err_free_rel;
   8530 			      if (toc_tls != NULL)
   8531 				{
   8532 				  if ((*toc_tls & TLS_TLS) != 0
   8533 				      && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
   8534 				    found_tls_get_addr_arg = 1;
   8535 				  if (retval > 1)
   8536 				    toc_ref[toc_ref_index] = 1;
   8537 				}
   8538 			    }
   8539 			  continue;
   8540 			}
   8541 
   8542 		      /* Uh oh, we didn't find the expected call.  We
   8543 			 could just mark this symbol to exclude it
   8544 			 from tls optimization but it's safer to skip
   8545 			 the entire optimization.  */
   8546 		      /* xgettext:c-format */
   8547 		      info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
   8548 						"TLS optimization disabled\n"),
   8549 					      ibfd, sec, rel->r_offset);
   8550 		      ret = true;
   8551 		      goto err_free_rel;
   8552 		    }
   8553 
   8554 		  /* If we don't have old-style __tls_get_addr calls
   8555 		     without TLSGD/TLSLD marker relocs, and we haven't
   8556 		     found a new-style __tls_get_addr call with a
   8557 		     marker for this symbol, then we either have a
   8558 		     broken object file or an -mlongcall style
   8559 		     indirect call to __tls_get_addr without a marker.
   8560 		     Disable optimization in this case.  */
   8561 		  if ((tls_clear & (TLS_GD | TLS_LD)) != 0
   8562 		      && (tls_set & TLS_EXPLICIT) == 0
   8563 		      && !sec->nomark_tls_get_addr
   8564 		      && ((*tls_mask & (TLS_TLS | TLS_MARK))
   8565 			  != (TLS_TLS | TLS_MARK)))
   8566 		    continue;
   8567 
   8568 		  if (expecting_tls_get_addr == 1 + !sec->nomark_tls_get_addr)
   8569 		    {
   8570 		      struct plt_entry *ent = NULL;
   8571 
   8572 		      if (htab->tls_get_addr_fd != NULL)
   8573 			for (ent = htab->tls_get_addr_fd->elf.plt.plist;
   8574 			     ent != NULL;
   8575 			     ent = ent->next)
   8576 			  if (ent->addend == 0)
   8577 			    break;
   8578 
   8579 		      if (ent == NULL && htab->tga_desc_fd != NULL)
   8580 			for (ent = htab->tga_desc_fd->elf.plt.plist;
   8581 			     ent != NULL;
   8582 			     ent = ent->next)
   8583 			  if (ent->addend == 0)
   8584 			    break;
   8585 
   8586 		      if (ent == NULL && htab->tls_get_addr != NULL)
   8587 			for (ent = htab->tls_get_addr->elf.plt.plist;
   8588 			     ent != NULL;
   8589 			     ent = ent->next)
   8590 			  if (ent->addend == 0)
   8591 			    break;
   8592 
   8593 		      if (ent == NULL && htab->tga_desc != NULL)
   8594 			for (ent = htab->tga_desc->elf.plt.plist;
   8595 			     ent != NULL;
   8596 			     ent = ent->next)
   8597 			  if (ent->addend == 0)
   8598 			    break;
   8599 
   8600 		      if (ent != NULL
   8601 			  && ent->plt.refcount > 0)
   8602 			ent->plt.refcount -= 1;
   8603 		    }
   8604 
   8605 		  if (tls_clear == 0)
   8606 		    continue;
   8607 
   8608 		  if ((tls_set & TLS_EXPLICIT) == 0)
   8609 		    {
   8610 		      struct got_entry *ent;
   8611 
   8612 		      /* Adjust got entry for this reloc.  */
   8613 		      if (h != NULL)
   8614 			ent = h->got.glist;
   8615 		      else
   8616 			ent = elf_local_got_ents (ibfd)[r_symndx];
   8617 
   8618 		      for (; ent != NULL; ent = ent->next)
   8619 			if (ent->addend == rel->r_addend
   8620 			    && ent->owner == ibfd
   8621 			    && ent->tls_type == tls_type)
   8622 			  break;
   8623 		      if (ent == NULL)
   8624 			abort ();
   8625 
   8626 		      if (tls_set == 0)
   8627 			{
   8628 			  /* We managed to get rid of a got entry.  */
   8629 			  if (ent->got.refcount > 0)
   8630 			    ent->got.refcount -= 1;
   8631 			}
   8632 		    }
   8633 		  else
   8634 		    {
   8635 		      /* If we got rid of a DTPMOD/DTPREL reloc pair then
   8636 			 we'll lose one or two dyn relocs.  */
   8637 		      if (!dec_dynrel_count (rel, sec, info,
   8638 					     NULL, h, sym))
   8639 			return false;
   8640 
   8641 		      if (tls_set == (TLS_EXPLICIT | TLS_GD))
   8642 			{
   8643 			  if (!dec_dynrel_count (rel + 1, sec, info,
   8644 						 NULL, h, sym))
   8645 			    return false;
   8646 			}
   8647 		    }
   8648 
   8649 		  *tls_mask |= tls_set & 0xff;
   8650 		  *tls_mask &= ~tls_clear;
   8651 		}
   8652 
   8653 	      if (elf_section_data (sec)->relocs != relstart)
   8654 		free (relstart);
   8655 	    }
   8656 
   8657 	if (locsyms != NULL
   8658 	    && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
   8659 	  {
   8660 	    if (!info->keep_memory)
   8661 	      free (locsyms);
   8662 	    else
   8663 	      elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
   8664 	  }
   8665       }
   8666 
   8667   free (toc_ref);
   8668   return true;
   8669 }
   8670 
   8671 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
   8672    the values of any global symbols in a toc section that has been
   8673    edited.  Globals in toc sections should be a rarity, so this function
   8674    sets a flag if any are found in toc sections other than the one just
   8675    edited, so that further hash table traversals can be avoided.  */
   8676 
   8677 struct adjust_toc_info
   8678 {
   8679   asection *toc;
   8680   unsigned long *skip;
   8681   bool global_toc_syms;
   8682 };
   8683 
   8684 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
   8685 
   8686 static bool
   8687 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
   8688 {
   8689   struct ppc_link_hash_entry *eh;
   8690   struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
   8691   unsigned long i;
   8692 
   8693   if (h->root.type != bfd_link_hash_defined
   8694       && h->root.type != bfd_link_hash_defweak)
   8695     return true;
   8696 
   8697   eh = ppc_elf_hash_entry (h);
   8698   if (eh->adjust_done)
   8699     return true;
   8700 
   8701   if (eh->elf.root.u.def.section == toc_inf->toc)
   8702     {
   8703       if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
   8704 	i = toc_inf->toc->rawsize >> 3;
   8705       else
   8706 	i = eh->elf.root.u.def.value >> 3;
   8707 
   8708       if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
   8709 	{
   8710 	  _bfd_error_handler
   8711 	    (_("%s defined on removed toc entry"), eh->elf.root.root.string);
   8712 	  do
   8713 	    ++i;
   8714 	  while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
   8715 	  eh->elf.root.u.def.value = (bfd_vma) i << 3;
   8716 	}
   8717 
   8718       eh->elf.root.u.def.value -= toc_inf->skip[i];
   8719       eh->adjust_done = 1;
   8720     }
   8721   else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
   8722     toc_inf->global_toc_syms = true;
   8723 
   8724   return true;
   8725 }
   8726 
   8727 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
   8728    on a _LO variety toc/got reloc.  */
   8729 
   8730 static bool
   8731 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
   8732 {
   8733   return ((insn & (0x3fu << 26)) == 12u << 26 /* addic */
   8734 	  || (insn & (0x3fu << 26)) == 14u << 26 /* addi */
   8735 	  || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
   8736 	  || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
   8737 	  || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
   8738 	  || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
   8739 	  || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
   8740 	  || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
   8741 	  || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
   8742 	  || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
   8743 	  || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
   8744 	  || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
   8745 	  || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
   8746 	  || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
   8747 	  || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
   8748 	  || (insn & (0x3fu << 26)) == 56u << 26 /* lq,lfq */
   8749 	  || ((insn & (0x3fu << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
   8750 	      /* Exclude lfqu by testing reloc.  If relocs are ever
   8751 		 defined for the reduced D field in psq_lu then those
   8752 		 will need testing too.  */
   8753 	      && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
   8754 	  || ((insn & (0x3fu << 26)) == 58u << 26 /* ld,lwa */
   8755 	      && (insn & 1) == 0)
   8756 	  || (insn & (0x3fu << 26)) == 60u << 26 /* stfq */
   8757 	  || ((insn & (0x3fu << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
   8758 	      /* Exclude stfqu.  psq_stu as above for psq_lu.  */
   8759 	      && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
   8760 	  || ((insn & (0x3fu << 26)) == 62u << 26 /* std,stq */
   8761 	      && (insn & 1) == 0));
   8762 }
   8763 
   8764 /* PCREL_OPT in one instance flags to the linker that a pair of insns:
   8765      pld ra,symbol@got@pcrel
   8766      load/store rt,off(ra)
   8767    or
   8768      pla ra,symbol@pcrel
   8769      load/store rt,off(ra)
   8770    may be translated to
   8771      pload/pstore rt,symbol+off@pcrel
   8772      nop.
   8773    This function returns true if the optimization is possible, placing
   8774    the prefix insn in *PINSN1, a NOP in *PINSN2 and the offset in *POFF.
   8775 
   8776    On entry to this function, the linker has already determined that
   8777    the pld can be replaced with pla: *PINSN1 is that pla insn,
   8778    while *PINSN2 is the second instruction.  */
   8779 
   8780 static bool
   8781 xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2, bfd_signed_vma *poff)
   8782 {
   8783   uint64_t insn1 = *pinsn1;
   8784   uint64_t insn2 = *pinsn2;
   8785   bfd_signed_vma off;
   8786 
   8787   if ((insn2 & (63ULL << 58)) == 1ULL << 58)
   8788     {
   8789       /* Check that regs match.  */
   8790       if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
   8791 	return false;
   8792 
   8793       /* P8LS or PMLS form, non-pcrel.  */
   8794       if ((insn2 & (-1ULL << 50) & ~(1ULL << 56)) != (1ULL << 58))
   8795 	return false;
   8796 
   8797       *pinsn1 = (insn2 & ~(31 << 16) & ~0x3ffff0000ffffULL) | (1ULL << 52);
   8798       *pinsn2 = PNOP;
   8799       off = ((insn2 >> 16) & 0x3ffff0000ULL) | (insn2 & 0xffff);
   8800       *poff = (off ^ 0x200000000ULL) - 0x200000000ULL;
   8801       return true;
   8802     }
   8803 
   8804   insn2 >>= 32;
   8805 
   8806   /* Check that regs match.  */
   8807   if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
   8808     return false;
   8809 
   8810   switch ((insn2 >> 26) & 63)
   8811     {
   8812     default:
   8813       return false;
   8814 
   8815     case 32: /* lwz */
   8816     case 34: /* lbz */
   8817     case 36: /* stw */
   8818     case 38: /* stb */
   8819     case 40: /* lhz */
   8820     case 42: /* lha */
   8821     case 44: /* sth */
   8822     case 48: /* lfs */
   8823     case 50: /* lfd */
   8824     case 52: /* stfs */
   8825     case 54: /* stfd */
   8826       /* These are the PMLS cases, where we just need to tack a prefix
   8827 	 on the insn.  */
   8828       insn1 = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
   8829 	       | (insn2 & ((63ULL << 26) | (31ULL << 21))));
   8830       off = insn2 & 0xffff;
   8831       break;
   8832 
   8833     case 58: /* lwa, ld */
   8834       if ((insn2 & 1) != 0)
   8835 	return false;
   8836       insn1 = ((1ULL << 58) | (1ULL << 52)
   8837 	       | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
   8838 	       | (insn2 & (31ULL << 21)));
   8839       off = insn2 & 0xfffc;
   8840       break;
   8841 
   8842     case 57: /* lxsd, lxssp */
   8843       if ((insn2 & 3) < 2)
   8844 	return false;
   8845       insn1 = ((1ULL << 58) | (1ULL << 52)
   8846 	       | ((40ULL | (insn2 & 3)) << 26)
   8847 	       | (insn2 & (31ULL << 21)));
   8848       off = insn2 & 0xfffc;
   8849       break;
   8850 
   8851     case 61: /* stxsd, stxssp, lxv, stxv  */
   8852       if ((insn2 & 3) == 0)
   8853 	return false;
   8854       else if ((insn2 & 3) >= 2)
   8855 	{
   8856 	  insn1 = ((1ULL << 58) | (1ULL << 52)
   8857 		   | ((44ULL | (insn2 & 3)) << 26)
   8858 		   | (insn2 & (31ULL << 21)));
   8859 	  off = insn2 & 0xfffc;
   8860 	}
   8861       else
   8862 	{
   8863 	  insn1 = ((1ULL << 58) | (1ULL << 52)
   8864 		   | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
   8865 		   | (insn2 & (31ULL << 21)));
   8866 	  off = insn2 & 0xfff0;
   8867 	}
   8868       break;
   8869 
   8870     case 56: /* lq */
   8871       insn1 = ((1ULL << 58) | (1ULL << 52)
   8872 	       | (insn2 & ((63ULL << 26) | (31ULL << 21))));
   8873       off = insn2 & 0xffff;
   8874       break;
   8875 
   8876     case 6: /* lxvp, stxvp */
   8877       if ((insn2 & 0xe) != 0)
   8878 	return false;
   8879       insn1 = ((1ULL << 58) | (1ULL << 52)
   8880 	       | ((insn2 & 1) == 0 ? 58ULL << 26 : 62ULL << 26)
   8881 	       | (insn2 & (31ULL << 21)));
   8882       off = insn2 & 0xfff0;
   8883       break;
   8884 
   8885     case 62: /* std, stq */
   8886       if ((insn2 & 1) != 0)
   8887 	return false;
   8888       insn1 = ((1ULL << 58) | (1ULL << 52)
   8889 	       | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
   8890 	       | (insn2 & (31ULL << 21)));
   8891       off = insn2 & 0xfffc;
   8892       break;
   8893     }
   8894 
   8895   *pinsn1 = insn1;
   8896   *pinsn2 = (uint64_t) NOP << 32;
   8897   *poff = (off ^ 0x8000) - 0x8000;
   8898   return true;
   8899 }
   8900 
   8901 /* Examine all relocs referencing .toc sections in order to remove
   8902    unused .toc entries.  */
   8903 
   8904 bool
   8905 ppc64_elf_edit_toc (struct bfd_link_info *info)
   8906 {
   8907   bfd *ibfd;
   8908   struct adjust_toc_info toc_inf;
   8909   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   8910 
   8911   htab->do_toc_opt = 1;
   8912   toc_inf.global_toc_syms = true;
   8913   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   8914     {
   8915       asection *toc, *sec;
   8916       Elf_Internal_Shdr *symtab_hdr;
   8917       Elf_Internal_Sym *local_syms;
   8918       Elf_Internal_Rela *relstart, *rel, *toc_relocs;
   8919       unsigned long *skip, *drop;
   8920       unsigned char *used;
   8921       unsigned char *keep, last, some_unused;
   8922 
   8923       if (!is_ppc64_elf (ibfd))
   8924 	continue;
   8925 
   8926       toc = bfd_get_section_by_name (ibfd, ".toc");
   8927       if (toc == NULL
   8928 	  || toc->size == 0
   8929 	  || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
   8930 	  || discarded_section (toc))
   8931 	continue;
   8932 
   8933       toc_relocs = NULL;
   8934       local_syms = NULL;
   8935       symtab_hdr = &elf_symtab_hdr (ibfd);
   8936 
   8937       /* Look at sections dropped from the final link.  */
   8938       skip = NULL;
   8939       relstart = NULL;
   8940       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   8941 	{
   8942 	  if (sec->reloc_count == 0
   8943 	      || !discarded_section (sec)
   8944 	      || get_opd_info (sec)
   8945 	      || (sec->flags & SEC_ALLOC) == 0
   8946 	      || (sec->flags & SEC_DEBUGGING) != 0)
   8947 	    continue;
   8948 
   8949 	  relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, false);
   8950 	  if (relstart == NULL)
   8951 	    goto error_ret;
   8952 
   8953 	  /* Run through the relocs to see which toc entries might be
   8954 	     unused.  */
   8955 	  for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
   8956 	    {
   8957 	      enum elf_ppc64_reloc_type r_type;
   8958 	      unsigned long r_symndx;
   8959 	      asection *sym_sec;
   8960 	      struct elf_link_hash_entry *h;
   8961 	      Elf_Internal_Sym *sym;
   8962 	      bfd_vma val;
   8963 
   8964 	      r_type = ELF64_R_TYPE (rel->r_info);
   8965 	      switch (r_type)
   8966 		{
   8967 		default:
   8968 		  continue;
   8969 
   8970 		case R_PPC64_TOC16:
   8971 		case R_PPC64_TOC16_LO:
   8972 		case R_PPC64_TOC16_HI:
   8973 		case R_PPC64_TOC16_HA:
   8974 		case R_PPC64_TOC16_DS:
   8975 		case R_PPC64_TOC16_LO_DS:
   8976 		  break;
   8977 		}
   8978 
   8979 	      r_symndx = ELF64_R_SYM (rel->r_info);
   8980 	      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   8981 			      r_symndx, ibfd))
   8982 		goto error_ret;
   8983 
   8984 	      if (sym_sec != toc)
   8985 		continue;
   8986 
   8987 	      if (h != NULL)
   8988 		val = h->root.u.def.value;
   8989 	      else
   8990 		val = sym->st_value;
   8991 	      val += rel->r_addend;
   8992 
   8993 	      if (val >= toc->size)
   8994 		continue;
   8995 
   8996 	      /* Anything in the toc ought to be aligned to 8 bytes.
   8997 		 If not, don't mark as unused.  */
   8998 	      if (val & 7)
   8999 		continue;
   9000 
   9001 	      if (skip == NULL)
   9002 		{
   9003 		  skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
   9004 		  if (skip == NULL)
   9005 		    goto error_ret;
   9006 		}
   9007 
   9008 	      skip[val >> 3] = ref_from_discarded;
   9009 	    }
   9010 
   9011 	  if (elf_section_data (sec)->relocs != relstart)
   9012 	    free (relstart);
   9013 	}
   9014 
   9015       /* For largetoc loads of address constants, we can convert
   9016 	 .  addis rx,2,addr@got@ha
   9017 	 .  ld ry,addr@got@l(rx)
   9018 	 to
   9019 	 .  addis rx,2,addr@toc@ha
   9020 	 .  addi ry,rx,addr@toc@l
   9021 	 when addr is within 2G of the toc pointer.  This then means
   9022 	 that the word storing "addr" in the toc is no longer needed.  */
   9023 
   9024       if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
   9025 	  && toc->output_section->rawsize < (bfd_vma) 1 << 31
   9026 	  && toc->reloc_count != 0)
   9027 	{
   9028 	  /* Read toc relocs.  */
   9029 	  toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
   9030 						  info->keep_memory);
   9031 	  if (toc_relocs == NULL)
   9032 	    goto error_ret;
   9033 
   9034 	  for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
   9035 	    {
   9036 	      enum elf_ppc64_reloc_type r_type;
   9037 	      unsigned long r_symndx;
   9038 	      asection *sym_sec;
   9039 	      struct elf_link_hash_entry *h;
   9040 	      Elf_Internal_Sym *sym;
   9041 	      bfd_vma val, addr;
   9042 
   9043 	      r_type = ELF64_R_TYPE (rel->r_info);
   9044 	      if (r_type != R_PPC64_ADDR64)
   9045 		continue;
   9046 
   9047 	      r_symndx = ELF64_R_SYM (rel->r_info);
   9048 	      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   9049 			      r_symndx, ibfd))
   9050 		goto error_ret;
   9051 
   9052 	      if (sym_sec == NULL
   9053 		  || sym_sec->output_section == NULL
   9054 		  || discarded_section (sym_sec))
   9055 		continue;
   9056 
   9057 	      if (!SYMBOL_REFERENCES_LOCAL (info, h)
   9058 		  || (bfd_link_pic (info)
   9059 		      && sym_sec == bfd_abs_section_ptr))
   9060 		continue;
   9061 
   9062 	      if (h != NULL)
   9063 		{
   9064 		  if (h->type == STT_GNU_IFUNC)
   9065 		    continue;
   9066 		  val = h->root.u.def.value;
   9067 		}
   9068 	      else
   9069 		{
   9070 		  if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
   9071 		    continue;
   9072 		  val = sym->st_value;
   9073 		}
   9074 	      val += rel->r_addend;
   9075 	      val += sym_sec->output_section->vma + sym_sec->output_offset;
   9076 
   9077 	      /* We don't yet know the exact toc pointer value, but we
   9078 		 know it will be somewhere in the toc section.  Don't
   9079 		 optimize if the difference from any possible toc
   9080 		 pointer is outside [ff..f80008000, 7fff7fff].  */
   9081 	      addr = toc->output_section->vma + TOC_BASE_OFF;
   9082 	      if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
   9083 		continue;
   9084 
   9085 	      addr = toc->output_section->vma + toc->output_section->rawsize;
   9086 	      if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
   9087 		continue;
   9088 
   9089 	      if (skip == NULL)
   9090 		{
   9091 		  skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
   9092 		  if (skip == NULL)
   9093 		    goto error_ret;
   9094 		}
   9095 
   9096 	      skip[rel->r_offset >> 3]
   9097 		|= can_optimize | ((rel - toc_relocs) << 2);
   9098 	    }
   9099 	}
   9100 
   9101       if (skip == NULL)
   9102 	continue;
   9103 
   9104       used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
   9105       if (used == NULL)
   9106 	{
   9107 	error_ret:
   9108 	  if (symtab_hdr->contents != (unsigned char *) local_syms)
   9109 	    free (local_syms);
   9110 	  if (sec != NULL
   9111 	      && elf_section_data (sec)->relocs != relstart)
   9112 	    free (relstart);
   9113 	  if (elf_section_data (toc)->relocs != toc_relocs)
   9114 	    free (toc_relocs);
   9115 	  free (skip);
   9116 	  return false;
   9117 	}
   9118 
   9119       /* Now check all kept sections that might reference the toc.
   9120 	 Check the toc itself last.  */
   9121       for (sec = (ibfd->sections == toc && toc->next ? toc->next
   9122 		  : ibfd->sections);
   9123 	   sec != NULL;
   9124 	   sec = (sec == toc ? NULL
   9125 		  : sec->next == NULL ? toc
   9126 		  : sec->next == toc && toc->next ? toc->next
   9127 		  : sec->next))
   9128 	{
   9129 	  int repeat;
   9130 
   9131 	  if (sec->reloc_count == 0
   9132 	      || discarded_section (sec)
   9133 	      || get_opd_info (sec)
   9134 	      || (sec->flags & SEC_ALLOC) == 0
   9135 	      || (sec->flags & SEC_DEBUGGING) != 0)
   9136 	    continue;
   9137 
   9138 	  relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   9139 						info->keep_memory);
   9140 	  if (relstart == NULL)
   9141 	    {
   9142 	      free (used);
   9143 	      goto error_ret;
   9144 	    }
   9145 
   9146 	  /* Mark toc entries referenced as used.  */
   9147 	  do
   9148 	    {
   9149 	      repeat = 0;
   9150 	      for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
   9151 		{
   9152 		  enum elf_ppc64_reloc_type r_type;
   9153 		  unsigned long r_symndx;
   9154 		  asection *sym_sec;
   9155 		  struct elf_link_hash_entry *h;
   9156 		  Elf_Internal_Sym *sym;
   9157 		  bfd_vma val;
   9158 
   9159 		  r_type = ELF64_R_TYPE (rel->r_info);
   9160 		  switch (r_type)
   9161 		    {
   9162 		    case R_PPC64_TOC16:
   9163 		    case R_PPC64_TOC16_LO:
   9164 		    case R_PPC64_TOC16_HI:
   9165 		    case R_PPC64_TOC16_HA:
   9166 		    case R_PPC64_TOC16_DS:
   9167 		    case R_PPC64_TOC16_LO_DS:
   9168 		      /* In case we're taking addresses of toc entries.  */
   9169 		    case R_PPC64_ADDR64:
   9170 		      break;
   9171 
   9172 		    default:
   9173 		      continue;
   9174 		    }
   9175 
   9176 		  r_symndx = ELF64_R_SYM (rel->r_info);
   9177 		  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   9178 				  r_symndx, ibfd))
   9179 		    {
   9180 		      free (used);
   9181 		      goto error_ret;
   9182 		    }
   9183 
   9184 		  if (sym_sec != toc)
   9185 		    continue;
   9186 
   9187 		  if (h != NULL)
   9188 		    val = h->root.u.def.value;
   9189 		  else
   9190 		    val = sym->st_value;
   9191 		  val += rel->r_addend;
   9192 
   9193 		  if (val >= toc->size)
   9194 		    continue;
   9195 
   9196 		  if ((skip[val >> 3] & can_optimize) != 0)
   9197 		    {
   9198 		      bfd_vma off;
   9199 		      unsigned char opc;
   9200 
   9201 		      switch (r_type)
   9202 			{
   9203 			case R_PPC64_TOC16_HA:
   9204 			  break;
   9205 
   9206 			case R_PPC64_TOC16_LO_DS:
   9207 			  off = rel->r_offset;
   9208 			  off += (bfd_big_endian (ibfd) ? -2 : 3);
   9209 			  if (!bfd_get_section_contents (ibfd, sec, &opc,
   9210 							 off, 1))
   9211 			    {
   9212 			      free (used);
   9213 			      goto error_ret;
   9214 			    }
   9215 			  if ((opc & (0x3f << 2)) == (58u << 2))
   9216 			    break;
   9217 			  /* Fall through.  */
   9218 
   9219 			default:
   9220 			  /* Wrong sort of reloc, or not a ld.  We may
   9221 			     as well clear ref_from_discarded too.  */
   9222 			  skip[val >> 3] = 0;
   9223 			}
   9224 		    }
   9225 
   9226 		  if (sec != toc)
   9227 		    used[val >> 3] = 1;
   9228 		  /* For the toc section, we only mark as used if this
   9229 		     entry itself isn't unused.  */
   9230 		  else if ((used[rel->r_offset >> 3]
   9231 			    || !(skip[rel->r_offset >> 3] & ref_from_discarded))
   9232 			   && !used[val >> 3])
   9233 		    {
   9234 		      /* Do all the relocs again, to catch reference
   9235 			 chains.  */
   9236 		      repeat = 1;
   9237 		      used[val >> 3] = 1;
   9238 		    }
   9239 		}
   9240 	    }
   9241 	  while (repeat);
   9242 
   9243 	  if (elf_section_data (sec)->relocs != relstart)
   9244 	    free (relstart);
   9245 	}
   9246 
   9247       /* Merge the used and skip arrays.  Assume that TOC
   9248 	 doublewords not appearing as either used or unused belong
   9249 	 to an entry more than one doubleword in size.  */
   9250       for (drop = skip, keep = used, last = 0, some_unused = 0;
   9251 	   drop < skip + (toc->size + 7) / 8;
   9252 	   ++drop, ++keep)
   9253 	{
   9254 	  if (*keep)
   9255 	    {
   9256 	      *drop &= ~ref_from_discarded;
   9257 	      if ((*drop & can_optimize) != 0)
   9258 		some_unused = 1;
   9259 	      last = 0;
   9260 	    }
   9261 	  else if ((*drop & ref_from_discarded) != 0)
   9262 	    {
   9263 	      some_unused = 1;
   9264 	      last = ref_from_discarded;
   9265 	    }
   9266 	  else
   9267 	    *drop = last;
   9268 	}
   9269 
   9270       free (used);
   9271 
   9272       if (some_unused)
   9273 	{
   9274 	  bfd_byte *contents, *src;
   9275 	  unsigned long off;
   9276 	  Elf_Internal_Sym *sym;
   9277 	  bool local_toc_syms = false;
   9278 
   9279 	  /* Shuffle the toc contents, and at the same time convert the
   9280 	     skip array from booleans into offsets.  */
   9281 	  if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
   9282 	    goto error_ret;
   9283 
   9284 	  elf_section_data (toc)->this_hdr.contents = contents;
   9285 
   9286 	  for (src = contents, off = 0, drop = skip;
   9287 	       src < contents + toc->size;
   9288 	       src += 8, ++drop)
   9289 	    {
   9290 	      if ((*drop & (can_optimize | ref_from_discarded)) != 0)
   9291 		off += 8;
   9292 	      else if (off != 0)
   9293 		{
   9294 		  *drop = off;
   9295 		  memcpy (src - off, src, 8);
   9296 		}
   9297 	    }
   9298 	  *drop = off;
   9299 	  toc->rawsize = toc->size;
   9300 	  toc->size = src - contents - off;
   9301 
   9302 	  /* Adjust addends for relocs against the toc section sym,
   9303 	     and optimize any accesses we can.  */
   9304 	  for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   9305 	    {
   9306 	      if (sec->reloc_count == 0
   9307 		  || discarded_section (sec))
   9308 		continue;
   9309 
   9310 	      relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   9311 						    info->keep_memory);
   9312 	      if (relstart == NULL)
   9313 		goto error_ret;
   9314 
   9315 	      for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
   9316 		{
   9317 		  enum elf_ppc64_reloc_type r_type;
   9318 		  unsigned long r_symndx;
   9319 		  asection *sym_sec;
   9320 		  struct elf_link_hash_entry *h;
   9321 		  bfd_vma val;
   9322 
   9323 		  r_type = ELF64_R_TYPE (rel->r_info);
   9324 		  switch (r_type)
   9325 		    {
   9326 		    default:
   9327 		      continue;
   9328 
   9329 		    case R_PPC64_TOC16:
   9330 		    case R_PPC64_TOC16_LO:
   9331 		    case R_PPC64_TOC16_HI:
   9332 		    case R_PPC64_TOC16_HA:
   9333 		    case R_PPC64_TOC16_DS:
   9334 		    case R_PPC64_TOC16_LO_DS:
   9335 		    case R_PPC64_ADDR64:
   9336 		      break;
   9337 		    }
   9338 
   9339 		  r_symndx = ELF64_R_SYM (rel->r_info);
   9340 		  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   9341 				  r_symndx, ibfd))
   9342 		    goto error_ret;
   9343 
   9344 		  if (sym_sec != toc)
   9345 		    continue;
   9346 
   9347 		  if (h != NULL)
   9348 		    val = h->root.u.def.value;
   9349 		  else
   9350 		    {
   9351 		      val = sym->st_value;
   9352 		      if (val != 0)
   9353 			local_toc_syms = true;
   9354 		    }
   9355 
   9356 		  val += rel->r_addend;
   9357 
   9358 		  if (val > toc->rawsize)
   9359 		    val = toc->rawsize;
   9360 		  else if ((skip[val >> 3] & ref_from_discarded) != 0)
   9361 		    continue;
   9362 		  else if ((skip[val >> 3] & can_optimize) != 0)
   9363 		    {
   9364 		      Elf_Internal_Rela *tocrel
   9365 			= toc_relocs + (skip[val >> 3] >> 2);
   9366 		      unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
   9367 
   9368 		      switch (r_type)
   9369 			{
   9370 			case R_PPC64_TOC16_HA:
   9371 			  rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
   9372 			  break;
   9373 
   9374 			case R_PPC64_TOC16_LO_DS:
   9375 			  rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
   9376 			  break;
   9377 
   9378 			default:
   9379 			  if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
   9380 			    ppc_howto_init ();
   9381 			  info->callbacks->einfo
   9382 			    /* xgettext:c-format */
   9383 			    (_("%H: %s references "
   9384 			       "optimized away TOC entry\n"),
   9385 			     ibfd, sec, rel->r_offset,
   9386 			     ppc64_elf_howto_table[r_type]->name);
   9387 			  bfd_set_error (bfd_error_bad_value);
   9388 			  goto error_ret;
   9389 			}
   9390 		      rel->r_addend = tocrel->r_addend;
   9391 		      elf_section_data (sec)->relocs = relstart;
   9392 		      continue;
   9393 		    }
   9394 
   9395 		  if (h != NULL || sym->st_value != 0)
   9396 		    continue;
   9397 
   9398 		  rel->r_addend -= skip[val >> 3];
   9399 		  elf_section_data (sec)->relocs = relstart;
   9400 		}
   9401 
   9402 	      if (elf_section_data (sec)->relocs != relstart)
   9403 		free (relstart);
   9404 	    }
   9405 
   9406 	  /* We shouldn't have local or global symbols defined in the TOC,
   9407 	     but handle them anyway.  */
   9408 	  if (local_syms != NULL)
   9409 	    for (sym = local_syms;
   9410 		 sym < local_syms + symtab_hdr->sh_info;
   9411 		 ++sym)
   9412 	      if (sym->st_value != 0
   9413 		  && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
   9414 		{
   9415 		  unsigned long i;
   9416 
   9417 		  if (sym->st_value > toc->rawsize)
   9418 		    i = toc->rawsize >> 3;
   9419 		  else
   9420 		    i = sym->st_value >> 3;
   9421 
   9422 		  if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
   9423 		    {
   9424 		      if (local_toc_syms)
   9425 			_bfd_error_handler
   9426 			  (_("%s defined on removed toc entry"),
   9427 			   bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
   9428 		      do
   9429 			++i;
   9430 		      while ((skip[i] & (ref_from_discarded | can_optimize)));
   9431 		      sym->st_value = (bfd_vma) i << 3;
   9432 		    }
   9433 
   9434 		  sym->st_value -= skip[i];
   9435 		  symtab_hdr->contents = (unsigned char *) local_syms;
   9436 		}
   9437 
   9438 	  /* Adjust any global syms defined in this toc input section.  */
   9439 	  if (toc_inf.global_toc_syms)
   9440 	    {
   9441 	      toc_inf.toc = toc;
   9442 	      toc_inf.skip = skip;
   9443 	      toc_inf.global_toc_syms = false;
   9444 	      elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
   9445 				      &toc_inf);
   9446 	    }
   9447 
   9448 	  if (toc->reloc_count != 0)
   9449 	    {
   9450 	      Elf_Internal_Shdr *rel_hdr;
   9451 	      Elf_Internal_Rela *wrel;
   9452 	      bfd_size_type sz;
   9453 
   9454 	      /* Remove unused toc relocs, and adjust those we keep.  */
   9455 	      if (toc_relocs == NULL)
   9456 		toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
   9457 							info->keep_memory);
   9458 	      if (toc_relocs == NULL)
   9459 		goto error_ret;
   9460 
   9461 	      wrel = toc_relocs;
   9462 	      for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
   9463 		if ((skip[rel->r_offset >> 3]
   9464 		     & (ref_from_discarded | can_optimize)) == 0)
   9465 		  {
   9466 		    wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
   9467 		    wrel->r_info = rel->r_info;
   9468 		    wrel->r_addend = rel->r_addend;
   9469 		    ++wrel;
   9470 		  }
   9471 		else if (!dec_dynrel_count (rel, toc, info,
   9472 					    &local_syms, NULL, NULL))
   9473 		  goto error_ret;
   9474 
   9475 	      elf_section_data (toc)->relocs = toc_relocs;
   9476 	      toc->reloc_count = wrel - toc_relocs;
   9477 	      rel_hdr = _bfd_elf_single_rel_hdr (toc);
   9478 	      sz = rel_hdr->sh_entsize;
   9479 	      rel_hdr->sh_size = toc->reloc_count * sz;
   9480 	    }
   9481 	}
   9482       else if (elf_section_data (toc)->relocs != toc_relocs)
   9483 	free (toc_relocs);
   9484 
   9485       if (local_syms != NULL
   9486 	  && symtab_hdr->contents != (unsigned char *) local_syms)
   9487 	{
   9488 	  if (!info->keep_memory)
   9489 	    free (local_syms);
   9490 	  else
   9491 	    symtab_hdr->contents = (unsigned char *) local_syms;
   9492 	}
   9493       free (skip);
   9494     }
   9495 
   9496   /* Look for cases where we can change an indirect GOT access to
   9497      a GOT relative or PC relative access, possibly reducing the
   9498      number of GOT entries.  */
   9499   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   9500     {
   9501       asection *sec;
   9502       Elf_Internal_Shdr *symtab_hdr;
   9503       Elf_Internal_Sym *local_syms;
   9504       Elf_Internal_Rela *relstart, *rel;
   9505       bfd_vma got;
   9506 
   9507       if (!is_ppc64_elf (ibfd))
   9508 	continue;
   9509 
   9510       if (!ppc64_elf_tdata (ibfd)->has_optrel)
   9511 	continue;
   9512 
   9513       sec = ppc64_elf_tdata (ibfd)->got;
   9514       got = 0;
   9515       if (sec != NULL)
   9516 	got = sec->output_section->vma + sec->output_offset + 0x8000;
   9517 
   9518       local_syms = NULL;
   9519       symtab_hdr = &elf_symtab_hdr (ibfd);
   9520 
   9521       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   9522 	{
   9523 	  if (sec->reloc_count == 0
   9524 	      || !ppc64_elf_section_data (sec)->has_optrel
   9525 	      || discarded_section (sec))
   9526 	    continue;
   9527 
   9528 	  relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   9529 						info->keep_memory);
   9530 	  if (relstart == NULL)
   9531 	    {
   9532 	    got_error_ret:
   9533 	      if (symtab_hdr->contents != (unsigned char *) local_syms)
   9534 		free (local_syms);
   9535 	      if (sec != NULL
   9536 		  && elf_section_data (sec)->relocs != relstart)
   9537 		free (relstart);
   9538 	      return false;
   9539 	    }
   9540 
   9541 	  for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
   9542 	    {
   9543 	      enum elf_ppc64_reloc_type r_type;
   9544 	      unsigned long r_symndx;
   9545 	      Elf_Internal_Sym *sym;
   9546 	      asection *sym_sec;
   9547 	      struct elf_link_hash_entry *h;
   9548 	      struct got_entry *ent;
   9549 	      bfd_vma val, pc;
   9550 	      unsigned char buf[8];
   9551 	      unsigned int insn;
   9552 	      enum {no_check, check_lo, check_ha} insn_check;
   9553 
   9554 	      r_type = ELF64_R_TYPE (rel->r_info);
   9555 	      switch (r_type)
   9556 		{
   9557 		default:
   9558 		  insn_check = no_check;
   9559 		  break;
   9560 
   9561 		case R_PPC64_PLT16_HA:
   9562 		case R_PPC64_GOT_TLSLD16_HA:
   9563 		case R_PPC64_GOT_TLSGD16_HA:
   9564 		case R_PPC64_GOT_TPREL16_HA:
   9565 		case R_PPC64_GOT_DTPREL16_HA:
   9566 		case R_PPC64_GOT16_HA:
   9567 		case R_PPC64_TOC16_HA:
   9568 		  insn_check = check_ha;
   9569 		  break;
   9570 
   9571 		case R_PPC64_PLT16_LO:
   9572 		case R_PPC64_PLT16_LO_DS:
   9573 		case R_PPC64_GOT_TLSLD16_LO:
   9574 		case R_PPC64_GOT_TLSGD16_LO:
   9575 		case R_PPC64_GOT_TPREL16_LO_DS:
   9576 		case R_PPC64_GOT_DTPREL16_LO_DS:
   9577 		case R_PPC64_GOT16_LO:
   9578 		case R_PPC64_GOT16_LO_DS:
   9579 		case R_PPC64_TOC16_LO:
   9580 		case R_PPC64_TOC16_LO_DS:
   9581 		  insn_check = check_lo;
   9582 		  break;
   9583 		}
   9584 
   9585 	      if (insn_check != no_check)
   9586 		{
   9587 		  bfd_vma off = rel->r_offset & ~3;
   9588 
   9589 		  if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
   9590 		    goto got_error_ret;
   9591 
   9592 		  insn = bfd_get_32 (ibfd, buf);
   9593 		  if (insn_check == check_lo
   9594 		      ? !ok_lo_toc_insn (insn, r_type)
   9595 		      : ((insn & ((0x3fu << 26) | 0x1f << 16))
   9596 			 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
   9597 		    {
   9598 		      char str[12];
   9599 
   9600 		      ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
   9601 		      sprintf (str, "%#08x", insn);
   9602 		      info->callbacks->einfo
   9603 			/* xgettext:c-format */
   9604 			(_("%H: got/toc optimization is not supported for"
   9605 			   " %s instruction\n"),
   9606 			 ibfd, sec, rel->r_offset & ~3, str);
   9607 		      continue;
   9608 		    }
   9609 		}
   9610 
   9611 	      switch (r_type)
   9612 		{
   9613 		/* Note that we don't delete GOT entries for
   9614 		   R_PPC64_GOT16_DS since we'd need a lot more
   9615 		   analysis.  For starters, the preliminary layout is
   9616 		   before the GOT, PLT, dynamic sections and stubs are
   9617 		   laid out.  Then we'd need to allow for changes in
   9618 		   distance between sections caused by alignment.  */
   9619 		default:
   9620 		  continue;
   9621 
   9622 		case R_PPC64_GOT16_HA:
   9623 		case R_PPC64_GOT16_LO_DS:
   9624 		case R_PPC64_GOT_PCREL34:
   9625 		  break;
   9626 		}
   9627 
   9628 	      r_symndx = ELF64_R_SYM (rel->r_info);
   9629 	      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   9630 			      r_symndx, ibfd))
   9631 		goto got_error_ret;
   9632 
   9633 	      if (sym_sec == NULL
   9634 		  || sym_sec->output_section == NULL
   9635 		  || discarded_section (sym_sec))
   9636 		continue;
   9637 
   9638 	      if ((h ? h->type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC)
   9639 		continue;
   9640 
   9641 	      if (!SYMBOL_REFERENCES_LOCAL (info, h)
   9642 		  || (bfd_link_pic (info)
   9643 		      && sym_sec == bfd_abs_section_ptr))
   9644 		continue;
   9645 
   9646 	      if (h != NULL)
   9647 		val = h->root.u.def.value;
   9648 	      else
   9649 		val = sym->st_value;
   9650 	      val += rel->r_addend;
   9651 	      val += sym_sec->output_section->vma + sym_sec->output_offset;
   9652 
   9653 /* Fudge factor to allow for the fact that the preliminary layout
   9654    isn't exact.  Reduce limits by this factor.  */
   9655 #define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
   9656 
   9657 	      switch (r_type)
   9658 		{
   9659 		default:
   9660 		  continue;
   9661 
   9662 		case R_PPC64_GOT16_HA:
   9663 		  if (val - got + LIMIT_ADJUST (0x80008000ULL)
   9664 		      >= LIMIT_ADJUST (0x100000000ULL))
   9665 		    continue;
   9666 
   9667 		  if (!bfd_get_section_contents (ibfd, sec, buf,
   9668 						 rel->r_offset & ~3, 4))
   9669 		    goto got_error_ret;
   9670 		  insn = bfd_get_32 (ibfd, buf);
   9671 		  if (((insn & ((0x3fu << 26) | 0x1f << 16))
   9672 		       != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
   9673 		    continue;
   9674 		  break;
   9675 
   9676 		case R_PPC64_GOT16_LO_DS:
   9677 		  if (val - got + LIMIT_ADJUST (0x80008000ULL)
   9678 		      >= LIMIT_ADJUST (0x100000000ULL))
   9679 		    continue;
   9680 		  if (!bfd_get_section_contents (ibfd, sec, buf,
   9681 						 rel->r_offset & ~3, 4))
   9682 		    goto got_error_ret;
   9683 		  insn = bfd_get_32 (ibfd, buf);
   9684 		  if ((insn & (0x3fu << 26 | 0x3)) != 58u << 26 /* ld */)
   9685 		    continue;
   9686 		  break;
   9687 
   9688 		case R_PPC64_GOT_PCREL34:
   9689 		  pc = rel->r_offset;
   9690 		  pc += sec->output_section->vma + sec->output_offset;
   9691 		  if (val - pc + LIMIT_ADJUST (1ULL << 33)
   9692 		      >= LIMIT_ADJUST (1ULL << 34))
   9693 		    continue;
   9694 		  if (!bfd_get_section_contents (ibfd, sec, buf,
   9695 						 rel->r_offset & ~3, 8))
   9696 		    goto got_error_ret;
   9697 		  insn = bfd_get_32 (ibfd, buf);
   9698 		  if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
   9699 		    continue;
   9700 		  insn = bfd_get_32 (ibfd, buf + 4);
   9701 		  if ((insn & (0x3fu << 26)) != 57u << 26)
   9702 		    continue;
   9703 		  break;
   9704 		}
   9705 #undef LIMIT_ADJUST
   9706 
   9707 	      if (h != NULL)
   9708 		ent = h->got.glist;
   9709 	      else
   9710 		{
   9711 		  struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
   9712 		  ent = local_got_ents[r_symndx];
   9713 		}
   9714 	      for (; ent != NULL; ent = ent->next)
   9715 		if (ent->addend == rel->r_addend
   9716 		    && ent->owner == ibfd
   9717 		    && ent->tls_type == 0)
   9718 		  break;
   9719 	      BFD_ASSERT (ent && ent->got.refcount > 0);
   9720 	      ent->got.refcount -= 1;
   9721 	    }
   9722 
   9723 	  if (elf_section_data (sec)->relocs != relstart)
   9724 	    free (relstart);
   9725 	}
   9726 
   9727       if (local_syms != NULL
   9728 	  && symtab_hdr->contents != (unsigned char *) local_syms)
   9729 	{
   9730 	  if (!info->keep_memory)
   9731 	    free (local_syms);
   9732 	  else
   9733 	    symtab_hdr->contents = (unsigned char *) local_syms;
   9734 	}
   9735     }
   9736 
   9737   return true;
   9738 }
   9739 
   9740 /* Return true iff input section I references the TOC using
   9741    instructions limited to +/-32k offsets.  */
   9742 
   9743 bool
   9744 ppc64_elf_has_small_toc_reloc (asection *i)
   9745 {
   9746   return (is_ppc64_elf (i->owner)
   9747 	  && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
   9748 }
   9749 
   9750 /* Allocate space for one GOT entry.  */
   9751 
   9752 static void
   9753 allocate_got (struct elf_link_hash_entry *h,
   9754 	      struct bfd_link_info *info,
   9755 	      struct got_entry *gent)
   9756 {
   9757   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   9758   struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
   9759   int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
   9760 		 ? 16 : 8);
   9761   int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
   9762 		  ? 2 : 1) * sizeof (Elf64_External_Rela);
   9763   asection *got = ppc64_elf_tdata (gent->owner)->got;
   9764 
   9765   gent->got.offset = got->size;
   9766   got->size += entsize;
   9767 
   9768   if (h->type == STT_GNU_IFUNC)
   9769     {
   9770       htab->elf.irelplt->size += rentsize;
   9771       htab->got_reli_size += rentsize;
   9772     }
   9773   else if (((bfd_link_pic (info)
   9774 	     && (gent->tls_type == 0
   9775 		 ? !info->enable_dt_relr
   9776 		 : !(bfd_link_executable (info)
   9777 		     && SYMBOL_REFERENCES_LOCAL (info, h)))
   9778 	     && !bfd_is_abs_symbol (&h->root))
   9779 	    || (htab->elf.dynamic_sections_created
   9780 		&& h->dynindx != -1
   9781 		&& !SYMBOL_REFERENCES_LOCAL (info, h)))
   9782 	   && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
   9783     {
   9784       asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
   9785       relgot->size += rentsize;
   9786     }
   9787 }
   9788 
   9789 /* This function merges got entries in the same toc group.  */
   9790 
   9791 static void
   9792 merge_got_entries (struct got_entry **pent)
   9793 {
   9794   struct got_entry *ent, *ent2;
   9795 
   9796   for (ent = *pent; ent != NULL; ent = ent->next)
   9797     if (!ent->is_indirect)
   9798       for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
   9799 	if (!ent2->is_indirect
   9800 	    && ent2->addend == ent->addend
   9801 	    && ent2->tls_type == ent->tls_type
   9802 	    && elf_gp (ent2->owner) == elf_gp (ent->owner))
   9803 	  {
   9804 	    ent2->is_indirect = true;
   9805 	    ent2->got.ent = ent;
   9806 	  }
   9807 }
   9808 
   9809 /* If H is undefined, make it dynamic if that makes sense.  */
   9810 
   9811 static bool
   9812 ensure_undef_dynamic (struct bfd_link_info *info,
   9813 		      struct elf_link_hash_entry *h)
   9814 {
   9815   struct elf_link_hash_table *htab = elf_hash_table (info);
   9816 
   9817   if (htab->dynamic_sections_created
   9818       && ((info->dynamic_undefined_weak != 0
   9819 	   && h->root.type == bfd_link_hash_undefweak)
   9820 	  || h->root.type == bfd_link_hash_undefined)
   9821       && h->dynindx == -1
   9822       && !h->forced_local
   9823       && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
   9824     return bfd_elf_link_record_dynamic_symbol (info, h);
   9825   return true;
   9826 }
   9827 
   9828 /* Choose whether to use htab->iplt or htab->pltlocal rather than the
   9829    usual htab->elf.splt section for a PLT entry.  */
   9830 
   9831 static inline
   9832 bool use_local_plt (struct bfd_link_info *info,
   9833 			   struct elf_link_hash_entry *h)
   9834 {
   9835   return (h == NULL
   9836 	  || h->dynindx == -1
   9837 	  || !elf_hash_table (info)->dynamic_sections_created);
   9838 }
   9839 
   9840 /* Allocate space in .plt, .got and associated reloc sections for
   9841    dynamic relocs.  */
   9842 
   9843 static bool
   9844 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
   9845 {
   9846   struct bfd_link_info *info;
   9847   struct ppc_link_hash_table *htab;
   9848   asection *s;
   9849   struct ppc_link_hash_entry *eh;
   9850   struct got_entry **pgent, *gent;
   9851 
   9852   if (h->root.type == bfd_link_hash_indirect)
   9853     return true;
   9854 
   9855   info = (struct bfd_link_info *) inf;
   9856   htab = ppc_hash_table (info);
   9857   if (htab == NULL)
   9858     return false;
   9859 
   9860   eh = ppc_elf_hash_entry (h);
   9861   /* Run through the TLS GD got entries first if we're changing them
   9862      to TPREL.  */
   9863   if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
   9864     for (gent = h->got.glist; gent != NULL; gent = gent->next)
   9865       if (gent->got.refcount > 0
   9866 	  && (gent->tls_type & TLS_GD) != 0)
   9867 	{
   9868 	  /* This was a GD entry that has been converted to TPREL.  If
   9869 	     there happens to be a TPREL entry we can use that one.  */
   9870 	  struct got_entry *ent;
   9871 	  for (ent = h->got.glist; ent != NULL; ent = ent->next)
   9872 	    if (ent->got.refcount > 0
   9873 		&& (ent->tls_type & TLS_TPREL) != 0
   9874 		&& ent->addend == gent->addend
   9875 		&& ent->owner == gent->owner)
   9876 	      {
   9877 		gent->got.refcount = 0;
   9878 		break;
   9879 	      }
   9880 
   9881 	  /* If not, then we'll be using our own TPREL entry.  */
   9882 	  if (gent->got.refcount != 0)
   9883 	    gent->tls_type = TLS_TLS | TLS_TPREL;
   9884 	}
   9885 
   9886   /* Remove any list entry that won't generate a word in the GOT before
   9887      we call merge_got_entries.  Otherwise we risk merging to empty
   9888      entries.  */
   9889   pgent = &h->got.glist;
   9890   while ((gent = *pgent) != NULL)
   9891     if (gent->got.refcount > 0)
   9892       {
   9893 	if ((gent->tls_type & TLS_LD) != 0
   9894 	    && SYMBOL_REFERENCES_LOCAL (info, h))
   9895 	  {
   9896 	    ppc64_tlsld_got (gent->owner)->got.refcount += 1;
   9897 	    *pgent = gent->next;
   9898 	  }
   9899 	else
   9900 	  pgent = &gent->next;
   9901       }
   9902     else
   9903       *pgent = gent->next;
   9904 
   9905   if (!htab->do_multi_toc)
   9906     merge_got_entries (&h->got.glist);
   9907 
   9908   for (gent = h->got.glist; gent != NULL; gent = gent->next)
   9909     if (!gent->is_indirect)
   9910       {
   9911 	/* Ensure we catch all the cases where this symbol should
   9912 	   be made dynamic.  */
   9913 	if (!ensure_undef_dynamic (info, h))
   9914 	  return false;
   9915 
   9916 	if (!is_ppc64_elf (gent->owner))
   9917 	  abort ();
   9918 
   9919 	allocate_got (h, info, gent);
   9920       }
   9921 
   9922   /* If no dynamic sections we can't have dynamic relocs, except for
   9923      IFUNCs which are handled even in static executables.  */
   9924   if (!htab->elf.dynamic_sections_created
   9925       && h->type != STT_GNU_IFUNC)
   9926     h->dyn_relocs = NULL;
   9927 
   9928   /* Discard relocs on undefined symbols that must be local.  */
   9929   else if (h->root.type == bfd_link_hash_undefined
   9930 	   && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
   9931     h->dyn_relocs = NULL;
   9932 
   9933   /* Also discard relocs on undefined weak syms with non-default
   9934      visibility, or when dynamic_undefined_weak says so.  */
   9935   else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
   9936     h->dyn_relocs = NULL;
   9937 
   9938   if (h->dyn_relocs != NULL)
   9939     {
   9940       struct ppc_dyn_relocs *p, **pp;
   9941 
   9942       /* In the shared -Bsymbolic case, discard space allocated for
   9943 	 dynamic pc-relative relocs against symbols which turn out to
   9944 	 be defined in regular objects.  For the normal shared case,
   9945 	 discard space for relocs that have become local due to symbol
   9946 	 visibility changes.  */
   9947       if (bfd_link_pic (info))
   9948 	{
   9949 	  /* Relocs that use pc_count are those that appear on a call
   9950 	     insn, or certain REL relocs (see must_be_dyn_reloc) that
   9951 	     can be generated via assembly.  We want calls to
   9952 	     protected symbols to resolve directly to the function
   9953 	     rather than going via the plt.  If people want function
   9954 	     pointer comparisons to work as expected then they should
   9955 	     avoid writing weird assembly.  */
   9956 	  if (SYMBOL_CALLS_LOCAL (info, h))
   9957 	    {
   9958 	      for (pp = (struct ppc_dyn_relocs **) &h->dyn_relocs;
   9959 		   (p = *pp) != NULL;
   9960 		   )
   9961 		{
   9962 		  p->count -= p->pc_count;
   9963 		  p->pc_count = 0;
   9964 		  if (p->count == 0)
   9965 		    *pp = p->next;
   9966 		  else
   9967 		    pp = &p->next;
   9968 		}
   9969 	    }
   9970 
   9971 	  if (h->dyn_relocs != NULL)
   9972 	    {
   9973 	      /* Ensure we catch all the cases where this symbol
   9974 		 should be made dynamic.  */
   9975 	      if (!ensure_undef_dynamic (info, h))
   9976 		return false;
   9977 	    }
   9978 	}
   9979 
   9980       /* For a fixed position executable, discard space for
   9981 	 relocs against symbols which are not dynamic.  */
   9982       else if (h->type != STT_GNU_IFUNC)
   9983 	{
   9984 	  if ((h->dynamic_adjusted
   9985 	       || (h->ref_regular
   9986 		   && h->root.type == bfd_link_hash_undefweak
   9987 		   && (info->dynamic_undefined_weak > 0
   9988 		       || !_bfd_elf_readonly_dynrelocs (h))))
   9989 	      && !h->def_regular
   9990 	      && !ELF_COMMON_DEF_P (h))
   9991 	    {
   9992 	      /* Ensure we catch all the cases where this symbol
   9993 		 should be made dynamic.  */
   9994 	      if (!ensure_undef_dynamic (info, h))
   9995 		return false;
   9996 
   9997 	      /* But if that didn't work out, discard dynamic relocs.  */
   9998 	      if (h->dynindx == -1)
   9999 		h->dyn_relocs = NULL;
   10000 	    }
   10001 	  else
   10002 	    h->dyn_relocs = NULL;
   10003 	}
   10004 
   10005       /* Finally, allocate space.  */
   10006       for (p = (struct ppc_dyn_relocs *) h->dyn_relocs; p != NULL; p = p->next)
   10007 	if (!discarded_section (p->sec))
   10008 	  {
   10009 	    unsigned int count;
   10010 	    asection *sreloc = elf_section_data (p->sec)->sreloc;
   10011 	    if (eh->elf.type == STT_GNU_IFUNC)
   10012 	      sreloc = htab->elf.irelplt;
   10013 	    count = p->count;
   10014 	    if (info->enable_dt_relr
   10015 		&& ((!NO_OPD_RELOCS
   10016 		     && ppc64_elf_section_data (p->sec)->sec_type == sec_opd)
   10017 		    || (eh->elf.type != STT_GNU_IFUNC
   10018 			&& SYMBOL_REFERENCES_LOCAL (info, h))))
   10019 	      count -= p->rel_count;
   10020 	    sreloc->size += count * sizeof (Elf64_External_Rela);
   10021 	  }
   10022     }
   10023 
   10024   /* We might need a PLT entry when the symbol
   10025      a) is dynamic, or
   10026      b) is an ifunc, or
   10027      c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
   10028      d) has plt16 relocs and we are linking statically.  */
   10029   if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
   10030       || h->type == STT_GNU_IFUNC
   10031       || (h->needs_plt && h->dynamic_adjusted)
   10032       || (h->needs_plt
   10033 	  && h->def_regular
   10034 	  && !htab->elf.dynamic_sections_created
   10035 	  && !htab->can_convert_all_inline_plt
   10036 	  && (ppc_elf_hash_entry (h)->tls_mask
   10037 	      & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
   10038     {
   10039       struct plt_entry *pent;
   10040       bool doneone = false;
   10041       for (pent = h->plt.plist; pent != NULL; pent = pent->next)
   10042 	if (pent->plt.refcount > 0)
   10043 	  {
   10044 	    if (!ensure_undef_dynamic (info, h))
   10045 	      return false;
   10046 
   10047 	    if (use_local_plt (info, h))
   10048 	      {
   10049 		if (h->type == STT_GNU_IFUNC)
   10050 		  {
   10051 		    s = htab->elf.iplt;
   10052 		    pent->plt.offset = s->size;
   10053 		    s->size += PLT_ENTRY_SIZE (htab);
   10054 		    s = htab->elf.irelplt;
   10055 		  }
   10056 		else
   10057 		  {
   10058 		    s = htab->pltlocal;
   10059 		    pent->plt.offset = s->size;
   10060 		    s->size += LOCAL_PLT_ENTRY_SIZE (htab);
   10061 		    s = NULL;
   10062 		    if (bfd_link_pic (info)
   10063 			&& !(info->enable_dt_relr && !htab->opd_abi))
   10064 		      s = htab->relpltlocal;
   10065 		  }
   10066 	      }
   10067 	    else
   10068 	      {
   10069 		/* If this is the first .plt entry, make room for the special
   10070 		   first entry.  */
   10071 		s = htab->elf.splt;
   10072 		if (s->size == 0)
   10073 		  s->size += PLT_INITIAL_ENTRY_SIZE (htab);
   10074 
   10075 		pent->plt.offset = s->size;
   10076 
   10077 		/* Make room for this entry.  */
   10078 		s->size += PLT_ENTRY_SIZE (htab);
   10079 
   10080 		/* Make room for the .glink code.  */
   10081 		s = htab->glink;
   10082 		if (s->size == 0)
   10083 		  s->size += GLINK_PLTRESOLVE_SIZE (htab);
   10084 		if (htab->opd_abi)
   10085 		  {
   10086 		    /* We need bigger stubs past index 32767.  */
   10087 		    if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
   10088 		      s->size += 4;
   10089 		    s->size += 2*4;
   10090 		  }
   10091 		else
   10092 		  s->size += 4;
   10093 
   10094 		/* We also need to make an entry in the .rela.plt section.  */
   10095 		s = htab->elf.srelplt;
   10096 	      }
   10097 	    if (s != NULL)
   10098 	      s->size += sizeof (Elf64_External_Rela);
   10099 	    doneone = true;
   10100 	  }
   10101 	else
   10102 	  pent->plt.offset = (bfd_vma) -1;
   10103       if (!doneone)
   10104 	{
   10105 	  h->plt.plist = NULL;
   10106 	  h->needs_plt = 0;
   10107 	}
   10108     }
   10109   else
   10110     {
   10111       h->plt.plist = NULL;
   10112       h->needs_plt = 0;
   10113     }
   10114 
   10115   return true;
   10116 }
   10117 
   10118 #define PPC_LO(v) ((v) & 0xffff)
   10119 #define PPC_HI(v) (((v) >> 16) & 0xffff)
   10120 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
   10121 #define D34(v) \
   10122   ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
   10123 #define HA34(v) ((v + (1ULL << 33)) >> 34)
   10124 
   10125 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
   10126    to set up space for global entry stubs.  These are put in glink,
   10127    after the branch table.  */
   10128 
   10129 static bool
   10130 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
   10131 {
   10132   struct bfd_link_info *info;
   10133   struct ppc_link_hash_table *htab;
   10134   struct plt_entry *pent;
   10135   asection *s, *plt;
   10136 
   10137   if (h->root.type == bfd_link_hash_indirect)
   10138     return true;
   10139 
   10140   if (!h->pointer_equality_needed)
   10141     return true;
   10142 
   10143   if (h->def_regular)
   10144     return true;
   10145 
   10146   info = inf;
   10147   htab = ppc_hash_table (info);
   10148   if (htab == NULL)
   10149     return false;
   10150 
   10151   s = htab->global_entry;
   10152   plt = htab->elf.splt;
   10153   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
   10154     if (pent->plt.offset != (bfd_vma) -1
   10155 	&& pent->addend == 0)
   10156       {
   10157 	/* For ELFv2, if this symbol is not defined in a regular file
   10158 	   and we are not generating a shared library or pie, then we
   10159 	   need to define the symbol in the executable on a call stub.
   10160 	   This is to avoid text relocations.  */
   10161 	bfd_vma off, stub_align, stub_off, stub_size;
   10162 	unsigned int align_power;
   10163 
   10164 	stub_size = 16;
   10165 	stub_off = s->size;
   10166 	if (htab->params->plt_stub_align >= 0)
   10167 	  align_power = htab->params->plt_stub_align;
   10168 	else
   10169 	  align_power = -htab->params->plt_stub_align;
   10170 	/* Setting section alignment is delayed until we know it is
   10171 	   non-empty.  Otherwise the .text output section will be
   10172 	   aligned at least to plt_stub_align even when no global
   10173 	   entry stubs are needed.  */
   10174 	if (s->alignment_power < align_power)
   10175 	  s->alignment_power = align_power;
   10176 	stub_align = (bfd_vma) 1 << align_power;
   10177 	if (htab->params->plt_stub_align >= 0
   10178 	    || ((((stub_off + stub_size - 1) & -stub_align)
   10179 		 - (stub_off & -stub_align))
   10180 		> ((stub_size - 1) & -stub_align)))
   10181 	  stub_off = (stub_off + stub_align - 1) & -stub_align;
   10182 	off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
   10183 	off -= stub_off + s->output_offset + s->output_section->vma;
   10184 	/* Note that for --plt-stub-align negative we have a possible
   10185 	   dependency between stub offset and size.  Break that
   10186 	   dependency by assuming the max stub size when calculating
   10187 	   the stub offset.  */
   10188 	if (PPC_HA (off) == 0)
   10189 	  stub_size -= 4;
   10190 	h->root.type = bfd_link_hash_defined;
   10191 	h->root.u.def.section = s;
   10192 	h->root.u.def.value = stub_off;
   10193 	s->size = stub_off + stub_size;
   10194 	break;
   10195       }
   10196   return true;
   10197 }
   10198 
   10199 /* Set the sizes of the dynamic sections.  */
   10200 
   10201 static bool
   10202 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
   10203 				 struct bfd_link_info *info)
   10204 {
   10205   struct ppc_link_hash_table *htab;
   10206   bfd *dynobj;
   10207   asection *s;
   10208   bool relocs;
   10209   bfd *ibfd;
   10210   struct got_entry *first_tlsld;
   10211 
   10212   htab = ppc_hash_table (info);
   10213   if (htab == NULL)
   10214     return false;
   10215 
   10216   dynobj = htab->elf.dynobj;
   10217   if (dynobj == NULL)
   10218     abort ();
   10219 
   10220   if (htab->elf.dynamic_sections_created)
   10221     {
   10222       /* Set the contents of the .interp section to the interpreter.  */
   10223       if (bfd_link_executable (info) && !info->nointerp)
   10224 	{
   10225 	  s = bfd_get_linker_section (dynobj, ".interp");
   10226 	  if (s == NULL)
   10227 	    abort ();
   10228 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
   10229 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
   10230 	}
   10231     }
   10232 
   10233   /* Set up .got offsets for local syms, and space for local dynamic
   10234      relocs.  */
   10235   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   10236     {
   10237       struct got_entry **lgot_ents;
   10238       struct got_entry **end_lgot_ents;
   10239       struct plt_entry **local_plt;
   10240       struct plt_entry **end_local_plt;
   10241       unsigned char *lgot_masks;
   10242       bfd_size_type locsymcount;
   10243       Elf_Internal_Shdr *symtab_hdr;
   10244       Elf_Internal_Sym *local_syms;
   10245       Elf_Internal_Sym *isym;
   10246 
   10247       if (!is_ppc64_elf (ibfd))
   10248 	continue;
   10249 
   10250       for (s = ibfd->sections; s != NULL; s = s->next)
   10251 	{
   10252 	  struct ppc_local_dyn_relocs *p;
   10253 
   10254 	  for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
   10255 	    {
   10256 	      if (discarded_section (p->sec))
   10257 		{
   10258 		  /* Input section has been discarded, either because
   10259 		     it is a copy of a linkonce section or due to
   10260 		     linker script /DISCARD/, so we'll be discarding
   10261 		     the relocs too.  */
   10262 		}
   10263 	      else if (p->count != 0)
   10264 		{
   10265 		  unsigned int count;
   10266 		  asection *srel;
   10267 
   10268 		  count = p->count;
   10269 		  if (info->enable_dt_relr
   10270 		      && ((!NO_OPD_RELOCS
   10271 			   && (ppc64_elf_section_data (p->sec)->sec_type
   10272 			       == sec_opd))
   10273 			  || !p->ifunc))
   10274 		    count -= p->rel_count;
   10275 		  srel = elf_section_data (p->sec)->sreloc;
   10276 		  if (p->ifunc)
   10277 		    srel = htab->elf.irelplt;
   10278 		  srel->size += count * sizeof (Elf64_External_Rela);
   10279 		  if ((p->sec->output_section->flags & SEC_READONLY) != 0)
   10280 		    info->flags |= DF_TEXTREL;
   10281 		}
   10282 	    }
   10283 	}
   10284 
   10285       lgot_ents = elf_local_got_ents (ibfd);
   10286       if (!lgot_ents)
   10287 	continue;
   10288 
   10289       symtab_hdr = &elf_symtab_hdr (ibfd);
   10290       locsymcount = symtab_hdr->sh_info;
   10291       end_lgot_ents = lgot_ents + locsymcount;
   10292       local_plt = (struct plt_entry **) end_lgot_ents;
   10293       end_local_plt = local_plt + locsymcount;
   10294       lgot_masks = (unsigned char *) end_local_plt;
   10295       local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
   10296       if (local_syms == NULL && locsymcount != 0)
   10297 	{
   10298 	  local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
   10299 					     0, NULL, NULL, NULL);
   10300 	  if (local_syms == NULL)
   10301 	    return false;
   10302 	}
   10303       s = ppc64_elf_tdata (ibfd)->got;
   10304       for (isym = local_syms;
   10305 	   lgot_ents < end_lgot_ents;
   10306 	   ++lgot_ents, ++lgot_masks, isym++)
   10307 	{
   10308 	  struct got_entry **pent, *ent;
   10309 
   10310 	  pent = lgot_ents;
   10311 	  while ((ent = *pent) != NULL)
   10312 	    if (ent->got.refcount > 0)
   10313 	      {
   10314 		if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
   10315 		  {
   10316 		    ppc64_tlsld_got (ibfd)->got.refcount += 1;
   10317 		    *pent = ent->next;
   10318 		  }
   10319 		else
   10320 		  {
   10321 		    unsigned int ent_size = 8;
   10322 		    unsigned int rel_size = sizeof (Elf64_External_Rela);
   10323 
   10324 		    ent->got.offset = s->size;
   10325 		    if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
   10326 		      {
   10327 			ent_size *= 2;
   10328 			rel_size *= 2;
   10329 		      }
   10330 		    s->size += ent_size;
   10331 		    if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
   10332 		      {
   10333 			htab->elf.irelplt->size += rel_size;
   10334 			htab->got_reli_size += rel_size;
   10335 		      }
   10336 		    else if (bfd_link_pic (info)
   10337 			     && (ent->tls_type == 0
   10338 				 ? !info->enable_dt_relr
   10339 				 : !bfd_link_executable (info))
   10340 			     && isym->st_shndx != SHN_ABS)
   10341 		      {
   10342 			asection *srel = ppc64_elf_tdata (ibfd)->relgot;
   10343 			srel->size += rel_size;
   10344 		      }
   10345 		    pent = &ent->next;
   10346 		  }
   10347 	      }
   10348 	    else
   10349 	      *pent = ent->next;
   10350 	}
   10351       if (local_syms != NULL
   10352 	  && symtab_hdr->contents != (unsigned char *) local_syms)
   10353 	{
   10354 	  if (!info->keep_memory)
   10355 	    free (local_syms);
   10356 	  else
   10357 	    symtab_hdr->contents = (unsigned char *) local_syms;
   10358 	}
   10359 
   10360       /* Allocate space for plt calls to local syms.  */
   10361       lgot_masks = (unsigned char *) end_local_plt;
   10362       for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
   10363 	{
   10364 	  struct plt_entry *ent;
   10365 
   10366 	  for (ent = *local_plt; ent != NULL; ent = ent->next)
   10367 	    if (ent->plt.refcount > 0)
   10368 	      {
   10369 		if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
   10370 		  {
   10371 		    s = htab->elf.iplt;
   10372 		    ent->plt.offset = s->size;
   10373 		    s->size += PLT_ENTRY_SIZE (htab);
   10374 		    htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
   10375 		  }
   10376 		else if (htab->can_convert_all_inline_plt
   10377 			 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
   10378 		  ent->plt.offset = (bfd_vma) -1;
   10379 		else
   10380 		  {
   10381 		    s = htab->pltlocal;
   10382 		    ent->plt.offset = s->size;
   10383 		    s->size += LOCAL_PLT_ENTRY_SIZE (htab);
   10384 		    if (bfd_link_pic (info)
   10385 			&& !(info->enable_dt_relr && !htab->opd_abi))
   10386 		      htab->relpltlocal->size += sizeof (Elf64_External_Rela);
   10387 		  }
   10388 	      }
   10389 	    else
   10390 	      ent->plt.offset = (bfd_vma) -1;
   10391 	}
   10392     }
   10393 
   10394   /* Allocate global sym .plt and .got entries, and space for global
   10395      sym dynamic relocs.  */
   10396   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
   10397 
   10398   if (!htab->opd_abi && !bfd_link_pic (info))
   10399     elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
   10400 
   10401   first_tlsld = NULL;
   10402   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   10403     {
   10404       struct got_entry *ent;
   10405 
   10406       if (!is_ppc64_elf (ibfd))
   10407 	continue;
   10408 
   10409       ent = ppc64_tlsld_got (ibfd);
   10410       if (ent->got.refcount > 0)
   10411 	{
   10412 	  if (!htab->do_multi_toc && first_tlsld != NULL)
   10413 	    {
   10414 	      ent->is_indirect = true;
   10415 	      ent->got.ent = first_tlsld;
   10416 	    }
   10417 	  else
   10418 	    {
   10419 	      if (first_tlsld == NULL)
   10420 		first_tlsld = ent;
   10421 	      s = ppc64_elf_tdata (ibfd)->got;
   10422 	      ent->got.offset = s->size;
   10423 	      ent->owner = ibfd;
   10424 	      s->size += 16;
   10425 	      if (bfd_link_dll (info))
   10426 		{
   10427 		  asection *srel = ppc64_elf_tdata (ibfd)->relgot;
   10428 		  srel->size += sizeof (Elf64_External_Rela);
   10429 		}
   10430 	    }
   10431 	}
   10432       else
   10433 	ent->got.offset = (bfd_vma) -1;
   10434     }
   10435 
   10436   /* We now have determined the sizes of the various dynamic sections.
   10437      Allocate memory for them.  */
   10438   relocs = false;
   10439   for (s = dynobj->sections; s != NULL; s = s->next)
   10440     {
   10441       if ((s->flags & SEC_LINKER_CREATED) == 0)
   10442 	continue;
   10443 
   10444       if (s == htab->brlt || s == htab->relbrlt || s == htab->elf.srelrdyn)
   10445 	/* These haven't been allocated yet;  don't strip.  */
   10446 	continue;
   10447       else if (s == htab->elf.sgot
   10448 	       || s == htab->elf.splt
   10449 	       || s == htab->elf.iplt
   10450 	       || s == htab->pltlocal
   10451 	       || s == htab->glink
   10452 	       || s == htab->global_entry
   10453 	       || s == htab->elf.sdynbss
   10454 	       || s == htab->elf.sdynrelro)
   10455 	{
   10456 	  /* Strip this section if we don't need it; see the
   10457 	     comment below.  */
   10458 	}
   10459       else if (s == htab->glink_eh_frame)
   10460 	{
   10461 	  if (!bfd_is_abs_section (s->output_section))
   10462 	    /* Not sized yet.  */
   10463 	    continue;
   10464 	}
   10465       else if (startswith (s->name, ".rela"))
   10466 	{
   10467 	  if (s->size != 0)
   10468 	    {
   10469 	      if (s != htab->elf.srelplt)
   10470 		relocs = true;
   10471 
   10472 	      /* We use the reloc_count field as a counter if we need
   10473 		 to copy relocs into the output file.  */
   10474 	      s->reloc_count = 0;
   10475 	    }
   10476 	}
   10477       else
   10478 	{
   10479 	  /* It's not one of our sections, so don't allocate space.  */
   10480 	  continue;
   10481 	}
   10482 
   10483       if (s->size == 0)
   10484 	{
   10485 	  /* If we don't need this section, strip it from the
   10486 	     output file.  This is mostly to handle .rela.bss and
   10487 	     .rela.plt.  We must create both sections in
   10488 	     create_dynamic_sections, because they must be created
   10489 	     before the linker maps input sections to output
   10490 	     sections.  The linker does that before
   10491 	     adjust_dynamic_symbol is called, and it is that
   10492 	     function which decides whether anything needs to go
   10493 	     into these sections.  */
   10494 	  s->flags |= SEC_EXCLUDE;
   10495 	  continue;
   10496 	}
   10497 
   10498       if (bfd_is_abs_section (s->output_section))
   10499 	_bfd_error_handler (_("warning: discarding dynamic section %s"),
   10500 			    s->name);
   10501 
   10502       if ((s->flags & SEC_HAS_CONTENTS) == 0)
   10503 	continue;
   10504 
   10505       /* Allocate memory for the section contents.  We use bfd_zalloc
   10506 	 here in case unused entries are not reclaimed before the
   10507 	 section's contents are written out.  This should not happen,
   10508 	 but this way if it does we get a R_PPC64_NONE reloc in .rela
   10509 	 sections instead of garbage.
   10510 	 We also rely on the section contents being zero when writing
   10511 	 the GOT and .dynrelro.  */
   10512       s->contents = bfd_zalloc (dynobj, s->size);
   10513       if (s->contents == NULL)
   10514 	return false;
   10515     }
   10516 
   10517   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   10518     {
   10519       if (!is_ppc64_elf (ibfd))
   10520 	continue;
   10521 
   10522       s = ppc64_elf_tdata (ibfd)->got;
   10523       if (s != NULL && s != htab->elf.sgot)
   10524 	{
   10525 	  if (s->size == 0)
   10526 	    s->flags |= SEC_EXCLUDE;
   10527 	  else
   10528 	    {
   10529 	      s->contents = bfd_zalloc (ibfd, s->size);
   10530 	      if (s->contents == NULL)
   10531 		return false;
   10532 	    }
   10533 	}
   10534       s = ppc64_elf_tdata (ibfd)->relgot;
   10535       if (s != NULL)
   10536 	{
   10537 	  if (s->size == 0)
   10538 	    s->flags |= SEC_EXCLUDE;
   10539 	  else
   10540 	    {
   10541 	      s->contents = bfd_zalloc (ibfd, s->size);
   10542 	      if (s->contents == NULL)
   10543 		return false;
   10544 	      relocs = true;
   10545 	      s->reloc_count = 0;
   10546 	    }
   10547 	}
   10548     }
   10549 
   10550   if (htab->elf.dynamic_sections_created)
   10551     {
   10552       bool tls_opt;
   10553 
   10554       /* Add some entries to the .dynamic section.  We fill in the
   10555 	 values later, in ppc64_elf_finish_dynamic_sections, but we
   10556 	 must add the entries now so that we get the correct size for
   10557 	 the .dynamic section.  The DT_DEBUG entry is filled in by the
   10558 	 dynamic linker and used by the debugger.  */
   10559 #define add_dynamic_entry(TAG, VAL) \
   10560   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
   10561 
   10562       if (bfd_link_executable (info))
   10563 	{
   10564 	  if (!add_dynamic_entry (DT_DEBUG, 0))
   10565 	    return false;
   10566 	}
   10567 
   10568       if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
   10569 	{
   10570 	  if (!add_dynamic_entry (DT_PLTGOT, 0)
   10571 	      || !add_dynamic_entry (DT_PLTRELSZ, 0)
   10572 	      || !add_dynamic_entry (DT_PLTREL, DT_RELA)
   10573 	      || !add_dynamic_entry (DT_JMPREL, 0)
   10574 	      || !add_dynamic_entry (DT_PPC64_GLINK, 0))
   10575 	    return false;
   10576 	}
   10577 
   10578       if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
   10579 	{
   10580 	  if (!add_dynamic_entry (DT_PPC64_OPD, 0)
   10581 	      || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
   10582 	    return false;
   10583 	}
   10584 
   10585       tls_opt = (htab->params->tls_get_addr_opt
   10586 		 && ((htab->tls_get_addr_fd != NULL
   10587 		      && htab->tls_get_addr_fd->elf.plt.plist != NULL)
   10588 		     || (htab->tga_desc_fd != NULL
   10589 			 && htab->tga_desc_fd->elf.plt.plist != NULL)));
   10590       if (tls_opt || !htab->opd_abi)
   10591 	{
   10592 	  if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
   10593 	    return false;
   10594 	}
   10595 
   10596       if (relocs)
   10597 	{
   10598 	  if (!add_dynamic_entry (DT_RELA, 0)
   10599 	      || !add_dynamic_entry (DT_RELASZ, 0)
   10600 	      || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
   10601 	    return false;
   10602 
   10603 	  /* If any dynamic relocs apply to a read-only section,
   10604 	     then we need a DT_TEXTREL entry.  */
   10605 	  if ((info->flags & DF_TEXTREL) == 0)
   10606 	    elf_link_hash_traverse (&htab->elf,
   10607 				    _bfd_elf_maybe_set_textrel, info);
   10608 
   10609 	  if ((info->flags & DF_TEXTREL) != 0)
   10610 	    {
   10611 	      if (!add_dynamic_entry (DT_TEXTREL, 0))
   10612 		return false;
   10613 	    }
   10614 	}
   10615     }
   10616 #undef add_dynamic_entry
   10617 
   10618   return true;
   10619 }
   10620 
   10621 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
   10622 
   10623 static bool
   10624 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
   10625 {
   10626   if (h->plt.plist != NULL
   10627       && !h->def_regular
   10628       && !h->pointer_equality_needed)
   10629     return false;
   10630 
   10631   return _bfd_elf_hash_symbol (h);
   10632 }
   10633 
   10634 /* Determine the type of stub needed, if any, for a call.  */
   10635 
   10636 static inline enum ppc_stub_main_type
   10637 ppc_type_of_stub (asection *input_sec,
   10638 		  const Elf_Internal_Rela *rel,
   10639 		  struct ppc_link_hash_entry **hash,
   10640 		  struct plt_entry **plt_ent,
   10641 		  bfd_vma destination,
   10642 		  unsigned long local_off)
   10643 {
   10644   struct ppc_link_hash_entry *h = *hash;
   10645   bfd_vma location;
   10646   bfd_vma branch_offset;
   10647   bfd_vma max_branch_offset;
   10648   enum elf_ppc64_reloc_type r_type;
   10649 
   10650   if (h != NULL)
   10651     {
   10652       struct plt_entry *ent;
   10653       struct ppc_link_hash_entry *fdh = h;
   10654       if (h->oh != NULL
   10655 	  && h->oh->is_func_descriptor)
   10656 	{
   10657 	  fdh = ppc_follow_link (h->oh);
   10658 	  *hash = fdh;
   10659 	}
   10660 
   10661       for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
   10662 	if (ent->addend == rel->r_addend
   10663 	    && ent->plt.offset != (bfd_vma) -1)
   10664 	  {
   10665 	    *plt_ent = ent;
   10666 	    return ppc_stub_plt_call;
   10667 	  }
   10668 
   10669       /* Here, we know we don't have a plt entry.  If we don't have a
   10670 	 either a defined function descriptor or a defined entry symbol
   10671 	 in a regular object file, then it is pointless trying to make
   10672 	 any other type of stub.  */
   10673       if (!is_static_defined (&fdh->elf)
   10674 	  && !is_static_defined (&h->elf))
   10675 	return ppc_stub_none;
   10676     }
   10677   else if (elf_local_got_ents (input_sec->owner) != NULL)
   10678     {
   10679       Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
   10680       struct plt_entry **local_plt = (struct plt_entry **)
   10681 	elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
   10682       unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
   10683 
   10684       if (local_plt[r_symndx] != NULL)
   10685 	{
   10686 	  struct plt_entry *ent;
   10687 
   10688 	  for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
   10689 	    if (ent->addend == rel->r_addend
   10690 		&& ent->plt.offset != (bfd_vma) -1)
   10691 	      {
   10692 		*plt_ent = ent;
   10693 		return ppc_stub_plt_call;
   10694 	      }
   10695 	}
   10696     }
   10697 
   10698   /* Determine where the call point is.  */
   10699   location = (input_sec->output_offset
   10700 	      + input_sec->output_section->vma
   10701 	      + rel->r_offset);
   10702 
   10703   branch_offset = destination - location;
   10704   r_type = ELF64_R_TYPE (rel->r_info);
   10705 
   10706   /* Determine if a long branch stub is needed.  */
   10707   max_branch_offset = 1 << 25;
   10708   if (r_type == R_PPC64_REL14
   10709       || r_type == R_PPC64_REL14_BRTAKEN
   10710       || r_type == R_PPC64_REL14_BRNTAKEN)
   10711     max_branch_offset = 1 << 15;
   10712 
   10713   if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
   10714     /* We need a stub.  Figure out whether a long_branch or plt_branch
   10715        is needed later.  */
   10716     return ppc_stub_long_branch;
   10717 
   10718   return ppc_stub_none;
   10719 }
   10720 
   10721 /* Gets the address of a label (1:) in r11 and builds an offset in r12,
   10722    then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
   10723    .	mflr	%r12
   10724    .	bcl	20,31,1f
   10725    .1:	mflr	%r11
   10726    .	mtlr	%r12
   10727    .	lis	%r12,xxx-1b@highest
   10728    .	ori	%r12,%r12,xxx-1b@higher
   10729    .	sldi	%r12,%r12,32
   10730    .	oris	%r12,%r12,xxx-1b@high
   10731    .	ori	%r12,%r12,xxx-1b@l
   10732    .	add/ldx	%r12,%r11,%r12  */
   10733 
   10734 static bfd_byte *
   10735 build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bool load)
   10736 {
   10737   bfd_put_32 (abfd, MFLR_R12, p);
   10738   p += 4;
   10739   bfd_put_32 (abfd, BCL_20_31, p);
   10740   p += 4;
   10741   bfd_put_32 (abfd, MFLR_R11, p);
   10742   p += 4;
   10743   bfd_put_32 (abfd, MTLR_R12, p);
   10744   p += 4;
   10745   if (off + 0x8000 < 0x10000)
   10746     {
   10747       if (load)
   10748 	bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
   10749       else
   10750 	bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
   10751       p += 4;
   10752     }
   10753   else if (off + 0x80008000ULL < 0x100000000ULL)
   10754     {
   10755       bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
   10756       p += 4;
   10757       if (load)
   10758 	bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
   10759       else
   10760 	bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
   10761       p += 4;
   10762     }
   10763   else
   10764     {
   10765       if (off + 0x800000000000ULL < 0x1000000000000ULL)
   10766 	{
   10767 	  bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
   10768 	  p += 4;
   10769 	}
   10770       else
   10771 	{
   10772 	  bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
   10773 	  p += 4;
   10774 	  if (((off >> 32) & 0xffff) != 0)
   10775 	    {
   10776 	      bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
   10777 	      p += 4;
   10778 	    }
   10779 	}
   10780       if (((off >> 32) & 0xffffffffULL) != 0)
   10781 	{
   10782 	  bfd_put_32 (abfd, SLDI_R12_R12_32, p);
   10783 	  p += 4;
   10784 	}
   10785       if (PPC_HI (off) != 0)
   10786 	{
   10787 	  bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
   10788 	  p += 4;
   10789 	}
   10790       if (PPC_LO (off) != 0)
   10791 	{
   10792 	  bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
   10793 	  p += 4;
   10794 	}
   10795       if (load)
   10796 	bfd_put_32 (abfd, LDX_R12_R11_R12, p);
   10797       else
   10798 	bfd_put_32 (abfd, ADD_R12_R11_R12, p);
   10799       p += 4;
   10800     }
   10801   return p;
   10802 }
   10803 
   10804 static unsigned int
   10805 size_offset (bfd_vma off)
   10806 {
   10807   unsigned int size;
   10808   if (off + 0x8000 < 0x10000)
   10809     size = 4;
   10810   else if (off + 0x80008000ULL < 0x100000000ULL)
   10811     size = 8;
   10812   else
   10813     {
   10814       if (off + 0x800000000000ULL < 0x1000000000000ULL)
   10815 	size = 4;
   10816       else
   10817 	{
   10818 	  size = 4;
   10819 	  if (((off >> 32) & 0xffff) != 0)
   10820 	    size += 4;
   10821 	}
   10822       if (((off >> 32) & 0xffffffffULL) != 0)
   10823 	size += 4;
   10824       if (PPC_HI (off) != 0)
   10825 	size += 4;
   10826       if (PPC_LO (off) != 0)
   10827 	size += 4;
   10828       size += 4;
   10829     }
   10830   return size + 16;
   10831 }
   10832 
   10833 static unsigned int
   10834 num_relocs_for_offset (bfd_vma off)
   10835 {
   10836   unsigned int num_rel;
   10837   if (off + 0x8000 < 0x10000)
   10838     num_rel = 1;
   10839   else if (off + 0x80008000ULL < 0x100000000ULL)
   10840     num_rel = 2;
   10841   else
   10842     {
   10843       num_rel = 1;
   10844       if (off + 0x800000000000ULL >= 0x1000000000000ULL
   10845 	  && ((off >> 32) & 0xffff) != 0)
   10846 	num_rel += 1;
   10847       if (PPC_HI (off) != 0)
   10848 	num_rel += 1;
   10849       if (PPC_LO (off) != 0)
   10850 	num_rel += 1;
   10851     }
   10852   return num_rel;
   10853 }
   10854 
   10855 static Elf_Internal_Rela *
   10856 emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
   10857 			bfd_vma roff, bfd_vma targ, bfd_vma off)
   10858 {
   10859   bfd_vma relative_targ = targ - (roff - 8);
   10860   if (bfd_big_endian (info->output_bfd))
   10861     roff += 2;
   10862   r->r_offset = roff;
   10863   r->r_addend = relative_targ + roff;
   10864   if (off + 0x8000 < 0x10000)
   10865     r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
   10866   else if (off + 0x80008000ULL < 0x100000000ULL)
   10867     {
   10868       r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
   10869       ++r;
   10870       roff += 4;
   10871       r->r_offset = roff;
   10872       r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
   10873       r->r_addend = relative_targ + roff;
   10874     }
   10875   else
   10876     {
   10877       if (off + 0x800000000000ULL < 0x1000000000000ULL)
   10878 	r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
   10879       else
   10880 	{
   10881 	  r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
   10882 	  if (((off >> 32) & 0xffff) != 0)
   10883 	    {
   10884 	      ++r;
   10885 	      roff += 4;
   10886 	      r->r_offset = roff;
   10887 	      r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
   10888 	      r->r_addend = relative_targ + roff;
   10889 	    }
   10890 	}
   10891       if (((off >> 32) & 0xffffffffULL) != 0)
   10892 	roff += 4;
   10893       if (PPC_HI (off) != 0)
   10894 	{
   10895 	  ++r;
   10896 	  roff += 4;
   10897 	  r->r_offset = roff;
   10898 	  r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
   10899 	  r->r_addend = relative_targ + roff;
   10900 	}
   10901       if (PPC_LO (off) != 0)
   10902 	{
   10903 	  ++r;
   10904 	  roff += 4;
   10905 	  r->r_offset = roff;
   10906 	  r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
   10907 	  r->r_addend = relative_targ + roff;
   10908 	}
   10909     }
   10910   return r;
   10911 }
   10912 
   10913 static bfd_byte *
   10914 build_power10_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
   10915 		      bool load)
   10916 {
   10917   uint64_t insn;
   10918   if (off - odd + (1ULL << 33) < 1ULL << 34)
   10919     {
   10920       off -= odd;
   10921       if (odd)
   10922 	{
   10923 	  bfd_put_32 (abfd, NOP, p);
   10924 	  p += 4;
   10925 	}
   10926       if (load)
   10927 	insn = PLD_R12_PC;
   10928       else
   10929 	insn = PADDI_R12_PC;
   10930       insn |= D34 (off);
   10931       bfd_put_32 (abfd, insn >> 32, p);
   10932       p += 4;
   10933       bfd_put_32 (abfd, insn, p);
   10934     }
   10935   /* The minimum value for paddi is -0x200000000.  The minimum value
   10936      for li is -0x8000, which when shifted by 34 and added gives a
   10937      minimum value of -0x2000200000000.  The maximum value is
   10938      0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1.  */
   10939   else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
   10940     {
   10941       off -= 8 - odd;
   10942       bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
   10943       p += 4;
   10944       if (!odd)
   10945 	{
   10946 	  bfd_put_32 (abfd, SLDI_R11_R11_34, p);
   10947 	  p += 4;
   10948 	}
   10949       insn = PADDI_R12_PC | D34 (off);
   10950       bfd_put_32 (abfd, insn >> 32, p);
   10951       p += 4;
   10952       bfd_put_32 (abfd, insn, p);
   10953       p += 4;
   10954       if (odd)
   10955 	{
   10956 	  bfd_put_32 (abfd, SLDI_R11_R11_34, p);
   10957 	  p += 4;
   10958 	}
   10959       if (load)
   10960 	bfd_put_32 (abfd, LDX_R12_R11_R12, p);
   10961       else
   10962 	bfd_put_32 (abfd, ADD_R12_R11_R12, p);
   10963     }
   10964   else
   10965     {
   10966       off -= odd + 8;
   10967       bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
   10968       p += 4;
   10969       bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
   10970       p += 4;
   10971       if (odd)
   10972 	{
   10973 	  bfd_put_32 (abfd, SLDI_R11_R11_34, p);
   10974 	  p += 4;
   10975 	}
   10976       insn = PADDI_R12_PC | D34 (off);
   10977       bfd_put_32 (abfd, insn >> 32, p);
   10978       p += 4;
   10979       bfd_put_32 (abfd, insn, p);
   10980       p += 4;
   10981       if (!odd)
   10982 	{
   10983 	  bfd_put_32 (abfd, SLDI_R11_R11_34, p);
   10984 	  p += 4;
   10985 	}
   10986       if (load)
   10987 	bfd_put_32 (abfd, LDX_R12_R11_R12, p);
   10988       else
   10989 	bfd_put_32 (abfd, ADD_R12_R11_R12, p);
   10990     }
   10991   p += 4;
   10992   return p;
   10993 }
   10994 
   10995 static unsigned int
   10996 size_power10_offset (bfd_vma off, int odd)
   10997 {
   10998   if (off - odd + (1ULL << 33) < 1ULL << 34)
   10999     return odd + 8;
   11000   else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
   11001     return 20;
   11002   else
   11003     return 24;
   11004 }
   11005 
   11006 static unsigned int
   11007 num_relocs_for_power10_offset (bfd_vma off, int odd)
   11008 {
   11009   if (off - odd + (1ULL << 33) < 1ULL << 34)
   11010     return 1;
   11011   else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
   11012     return 2;
   11013   else
   11014     return 3;
   11015 }
   11016 
   11017 static Elf_Internal_Rela *
   11018 emit_relocs_for_power10_offset (struct bfd_link_info *info,
   11019 				Elf_Internal_Rela *r, bfd_vma roff,
   11020 				bfd_vma targ, bfd_vma off, int odd)
   11021 {
   11022   if (off - odd + (1ULL << 33) < 1ULL << 34)
   11023     roff += odd;
   11024   else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
   11025     {
   11026       int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
   11027       r->r_offset = roff + d_offset;
   11028       r->r_addend = targ + 8 - odd - d_offset;
   11029       r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
   11030       ++r;
   11031       roff += 8 - odd;
   11032     }
   11033   else
   11034     {
   11035       int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
   11036       r->r_offset = roff + d_offset;
   11037       r->r_addend = targ + 8 + odd - d_offset;
   11038       r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
   11039       ++r;
   11040       roff += 4;
   11041       r->r_offset = roff + d_offset;
   11042       r->r_addend = targ + 4 + odd - d_offset;
   11043       r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
   11044       ++r;
   11045       roff += 4 + odd;
   11046     }
   11047   r->r_offset = roff;
   11048   r->r_addend = targ;
   11049   r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
   11050   return r;
   11051 }
   11052 
   11053 /* Emit .eh_frame opcode to advance pc by DELTA.  */
   11054 
   11055 static bfd_byte *
   11056 eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
   11057 {
   11058   delta /= 4;
   11059   if (delta < 64)
   11060     *eh++ = DW_CFA_advance_loc + delta;
   11061   else if (delta < 256)
   11062     {
   11063       *eh++ = DW_CFA_advance_loc1;
   11064       *eh++ = delta;
   11065     }
   11066   else if (delta < 65536)
   11067     {
   11068       *eh++ = DW_CFA_advance_loc2;
   11069       bfd_put_16 (abfd, delta, eh);
   11070       eh += 2;
   11071     }
   11072   else
   11073     {
   11074       *eh++ = DW_CFA_advance_loc4;
   11075       bfd_put_32 (abfd, delta, eh);
   11076       eh += 4;
   11077     }
   11078   return eh;
   11079 }
   11080 
   11081 /* Size of required .eh_frame opcode to advance pc by DELTA.  */
   11082 
   11083 static unsigned int
   11084 eh_advance_size (unsigned int delta)
   11085 {
   11086   if (delta < 64 * 4)
   11087     /* DW_CFA_advance_loc+[1..63].  */
   11088     return 1;
   11089   if (delta < 256 * 4)
   11090     /* DW_CFA_advance_loc1, byte.  */
   11091     return 2;
   11092   if (delta < 65536 * 4)
   11093     /* DW_CFA_advance_loc2, 2 bytes.  */
   11094     return 3;
   11095   /* DW_CFA_advance_loc4, 4 bytes.  */
   11096   return 5;
   11097 }
   11098 
   11099 /* With power7 weakly ordered memory model, it is possible for ld.so
   11100    to update a plt entry in one thread and have another thread see a
   11101    stale zero toc entry.  To avoid this we need some sort of acquire
   11102    barrier in the call stub.  One solution is to make the load of the
   11103    toc word seem to appear to depend on the load of the function entry
   11104    word.  Another solution is to test for r2 being zero, and branch to
   11105    the appropriate glink entry if so.
   11106 
   11107    .	fake dep barrier	compare
   11108    .	ld 12,xxx(2)		ld 12,xxx(2)
   11109    .	mtctr 12		mtctr 12
   11110    .	xor 11,12,12		ld 2,xxx+8(2)
   11111    .	add 2,2,11		cmpldi 2,0
   11112    .	ld 2,xxx+8(2)		bnectr+
   11113    .	bctr			b <glink_entry>
   11114 
   11115    The solution involving the compare turns out to be faster, so
   11116    that's what we use unless the branch won't reach.  */
   11117 
   11118 #define ALWAYS_USE_FAKE_DEP 0
   11119 #define ALWAYS_EMIT_R2SAVE 0
   11120 
   11121 static inline unsigned int
   11122 plt_stub_size (struct ppc_link_hash_table *htab,
   11123 	       struct ppc_stub_hash_entry *stub_entry,
   11124 	       bfd_vma off,
   11125 	       unsigned int odd)
   11126 {
   11127   unsigned size;
   11128 
   11129   if (stub_entry->type.sub == ppc_stub_notoc)
   11130     {
   11131       size = 8 + size_power10_offset (off, odd);
   11132       if (stub_entry->type.r2save)
   11133 	size += 4;
   11134     }
   11135   else if (stub_entry->type.sub == ppc_stub_p9notoc)
   11136     {
   11137       size = 8 + size_offset (off - 8);
   11138       if (stub_entry->type.r2save)
   11139 	size += 4;
   11140     }
   11141   else
   11142     {
   11143       size = 12;
   11144       if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
   11145 	size += 4;
   11146       if (PPC_HA (off) != 0)
   11147 	size += 4;
   11148       if (htab->opd_abi)
   11149 	{
   11150 	  size += 4;
   11151 	  if (htab->params->plt_static_chain)
   11152 	    size += 4;
   11153 	  if (htab->params->plt_thread_safe
   11154 	      && htab->elf.dynamic_sections_created
   11155 	      && stub_entry->h != NULL
   11156 	      && stub_entry->h->elf.dynindx != -1)
   11157 	    size += 8;
   11158 	  if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain)
   11159 	      != PPC_HA (off))
   11160 	    size += 4;
   11161 	}
   11162     }
   11163   if (stub_entry->h != NULL
   11164       && is_tls_get_addr (&stub_entry->h->elf, htab)
   11165       && htab->params->tls_get_addr_opt)
   11166     {
   11167       if (!htab->params->no_tls_get_addr_regsave)
   11168 	{
   11169 	  size += 30 * 4;
   11170 	  if (stub_entry->type.r2save)
   11171 	    size += 4;
   11172 	}
   11173       else
   11174 	{
   11175 	  size += 7 * 4;
   11176 	  if (stub_entry->type.r2save)
   11177 	    size += 6 * 4;
   11178 	}
   11179     }
   11180   return size;
   11181 }
   11182 
   11183 /* Depending on the sign of plt_stub_align:
   11184    If positive, return the padding to align to a 2**plt_stub_align
   11185    boundary.
   11186    If negative, if this stub would cross fewer 2**plt_stub_align
   11187    boundaries if we align, then return the padding needed to do so.  */
   11188 
   11189 static inline unsigned int
   11190 plt_stub_pad (int plt_stub_align,
   11191 	      bfd_vma stub_off,
   11192 	      unsigned int stub_size)
   11193 {
   11194   unsigned int stub_align;
   11195 
   11196   if (plt_stub_align >= 0)
   11197     stub_align = 1u << plt_stub_align;
   11198   else
   11199     {
   11200       stub_align = 1u << -plt_stub_align;
   11201       if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
   11202 	  <= ((stub_size - 1) & -stub_align))
   11203 	return 0;
   11204     }
   11205   return stub_align - 1 - ((stub_off - 1) & (stub_align - 1));
   11206 }
   11207 
   11208 /* Build a toc using .plt call stub.  */
   11209 
   11210 static inline bfd_byte *
   11211 build_plt_stub (struct ppc_link_hash_table *htab,
   11212 		struct ppc_stub_hash_entry *stub_entry,
   11213 		bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
   11214 {
   11215   bfd *obfd = htab->params->stub_bfd;
   11216   bool plt_load_toc = htab->opd_abi;
   11217   bool plt_static_chain = htab->params->plt_static_chain;
   11218   bool plt_thread_safe = (htab->params->plt_thread_safe
   11219 			  && htab->elf.dynamic_sections_created
   11220 			  && stub_entry->h != NULL
   11221 			  && stub_entry->h->elf.dynindx != -1);
   11222   bool use_fake_dep = plt_thread_safe;
   11223   bfd_vma cmp_branch_off = 0;
   11224 
   11225   if (!ALWAYS_USE_FAKE_DEP
   11226       && plt_load_toc
   11227       && plt_thread_safe
   11228       && !(stub_entry->h != NULL
   11229 	   && is_tls_get_addr (&stub_entry->h->elf, htab)
   11230 	   && htab->params->tls_get_addr_opt))
   11231     {
   11232       bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
   11233       bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
   11234 			  / PLT_ENTRY_SIZE (htab));
   11235       bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
   11236       bfd_vma to, from;
   11237 
   11238       if (pltindex > 32768)
   11239 	glinkoff += (pltindex - 32768) * 4;
   11240       to = (glinkoff
   11241 	    + htab->glink->output_offset
   11242 	    + htab->glink->output_section->vma);
   11243       from = (p - stub_entry->group->stub_sec->contents
   11244 	      + 4 * (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
   11245 	      + 4 * (PPC_HA (offset) != 0)
   11246 	      + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
   11247 		     != PPC_HA (offset))
   11248 	      + 4 * (plt_static_chain != 0)
   11249 	      + 20
   11250 	      + stub_entry->group->stub_sec->output_offset
   11251 	      + stub_entry->group->stub_sec->output_section->vma);
   11252       cmp_branch_off = to - from;
   11253       use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
   11254     }
   11255 
   11256   if (PPC_HA (offset) != 0)
   11257     {
   11258       if (r != NULL)
   11259 	{
   11260 	  if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
   11261 	    r[0].r_offset += 4;
   11262 	  r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
   11263 	  r[1].r_offset = r[0].r_offset + 4;
   11264 	  r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
   11265 	  r[1].r_addend = r[0].r_addend;
   11266 	  if (plt_load_toc)
   11267 	    {
   11268 	      if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
   11269 		{
   11270 		  r[2].r_offset = r[1].r_offset + 4;
   11271 		  r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
   11272 		  r[2].r_addend = r[0].r_addend;
   11273 		}
   11274 	      else
   11275 		{
   11276 		  r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
   11277 		  r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
   11278 		  r[2].r_addend = r[0].r_addend + 8;
   11279 		  if (plt_static_chain)
   11280 		    {
   11281 		      r[3].r_offset = r[2].r_offset + 4;
   11282 		      r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
   11283 		      r[3].r_addend = r[0].r_addend + 16;
   11284 		    }
   11285 		}
   11286 	    }
   11287 	}
   11288       if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
   11289 	bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),	p += 4;
   11290       if (plt_load_toc)
   11291 	{
   11292 	  bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p),	p += 4;
   11293 	  bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p),	p += 4;
   11294 	}
   11295       else
   11296 	{
   11297 	  bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p),	p += 4;
   11298 	  bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p),	p += 4;
   11299 	}
   11300       if (plt_load_toc
   11301 	  && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
   11302 	{
   11303 	  bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p),	p += 4;
   11304 	  offset = 0;
   11305 	}
   11306       bfd_put_32 (obfd, MTCTR_R12, p),				p += 4;
   11307       if (plt_load_toc)
   11308 	{
   11309 	  if (use_fake_dep)
   11310 	    {
   11311 	      bfd_put_32 (obfd, XOR_R2_R12_R12, p),		p += 4;
   11312 	      bfd_put_32 (obfd, ADD_R11_R11_R2, p),		p += 4;
   11313 	    }
   11314 	  bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
   11315 	  if (plt_static_chain)
   11316 	    bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
   11317 	}
   11318     }
   11319   else
   11320     {
   11321       if (r != NULL)
   11322 	{
   11323 	  if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
   11324 	    r[0].r_offset += 4;
   11325 	  r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
   11326 	  if (plt_load_toc)
   11327 	    {
   11328 	      if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
   11329 		{
   11330 		  r[1].r_offset = r[0].r_offset + 4;
   11331 		  r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
   11332 		  r[1].r_addend = r[0].r_addend;
   11333 		}
   11334 	      else
   11335 		{
   11336 		  r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
   11337 		  r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
   11338 		  r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
   11339 		  if (plt_static_chain)
   11340 		    {
   11341 		      r[2].r_offset = r[1].r_offset + 4;
   11342 		      r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
   11343 		      r[2].r_addend = r[0].r_addend + 8;
   11344 		    }
   11345 		}
   11346 	    }
   11347 	}
   11348       if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
   11349 	bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),	p += 4;
   11350       bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p),	p += 4;
   11351       if (plt_load_toc
   11352 	  && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
   11353 	{
   11354 	  bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p),	p += 4;
   11355 	  offset = 0;
   11356 	}
   11357       bfd_put_32 (obfd, MTCTR_R12, p),				p += 4;
   11358       if (plt_load_toc)
   11359 	{
   11360 	  if (use_fake_dep)
   11361 	    {
   11362 	      bfd_put_32 (obfd, XOR_R11_R12_R12, p),		p += 4;
   11363 	      bfd_put_32 (obfd, ADD_R2_R2_R11, p),		p += 4;
   11364 	    }
   11365 	  if (plt_static_chain)
   11366 	    bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
   11367 	  bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
   11368 	}
   11369     }
   11370   if (plt_load_toc && plt_thread_safe && !use_fake_dep)
   11371     {
   11372       bfd_put_32 (obfd, CMPLDI_R2_0, p),			p += 4;
   11373       bfd_put_32 (obfd, BNECTR_P4, p),				p += 4;
   11374       bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
   11375     }
   11376   else
   11377     bfd_put_32 (obfd, BCTR, p),					p += 4;
   11378   return p;
   11379 }
   11380 
   11381 /* Build a special .plt call stub for __tls_get_addr.  */
   11382 
   11383 #define LD_R0_0R3	0xe8030000
   11384 #define LD_R12_0R3	0xe9830000
   11385 #define MR_R0_R3	0x7c601b78
   11386 #define CMPDI_R0_0	0x2c200000
   11387 #define ADD_R3_R12_R13	0x7c6c6a14
   11388 #define BEQLR		0x4d820020
   11389 #define MR_R3_R0	0x7c030378
   11390 #define BCTRL		0x4e800421
   11391 
   11392 static bfd_byte *
   11393 build_tls_get_addr_head (struct ppc_link_hash_table *htab,
   11394 			 struct ppc_stub_hash_entry *stub_entry,
   11395 			 bfd_byte *p)
   11396 {
   11397   bfd *obfd = htab->params->stub_bfd;
   11398 
   11399   bfd_put_32 (obfd, LD_R0_0R3 + 0, p),		p += 4;
   11400   bfd_put_32 (obfd, LD_R12_0R3 + 8, p),		p += 4;
   11401   bfd_put_32 (obfd, CMPDI_R0_0, p),		p += 4;
   11402   bfd_put_32 (obfd, MR_R0_R3, p),		p += 4;
   11403   bfd_put_32 (obfd, ADD_R3_R12_R13, p),		p += 4;
   11404   bfd_put_32 (obfd, BEQLR, p),			p += 4;
   11405   bfd_put_32 (obfd, MR_R3_R0, p),		p += 4;
   11406 
   11407   if (!htab->params->no_tls_get_addr_regsave)
   11408     p = tls_get_addr_prologue (obfd, p, htab);
   11409   else if (stub_entry->type.r2save)
   11410     {
   11411       bfd_put_32 (obfd, MFLR_R0, p);
   11412       p += 4;
   11413       bfd_put_32 (obfd, STD_R0_0R1 + STK_LINKER (htab), p);
   11414       p += 4;
   11415     }
   11416   return p;
   11417 }
   11418 
   11419 static bfd_byte *
   11420 build_tls_get_addr_tail (struct ppc_link_hash_table *htab,
   11421 			 struct ppc_stub_hash_entry *stub_entry,
   11422 			 bfd_byte *p,
   11423 			 bfd_byte *loc)
   11424 {
   11425   bfd *obfd = htab->params->stub_bfd;
   11426 
   11427   if (!htab->params->no_tls_get_addr_regsave)
   11428     {
   11429       bfd_put_32 (obfd, BCTRL, p - 4);
   11430 
   11431       if (stub_entry->type.r2save)
   11432 	{
   11433 	  bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
   11434 	  p += 4;
   11435 	}
   11436       p = tls_get_addr_epilogue (obfd, p, htab);
   11437     }
   11438   else if (stub_entry->type.r2save)
   11439     {
   11440       bfd_put_32 (obfd, BCTRL, p - 4);
   11441 
   11442       bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
   11443       p += 4;
   11444       bfd_put_32 (obfd, LD_R0_0R1 + STK_LINKER (htab), p);
   11445       p += 4;
   11446       bfd_put_32 (obfd, MTLR_R0, p);
   11447       p += 4;
   11448       bfd_put_32 (obfd, BLR, p);
   11449       p += 4;
   11450     }
   11451 
   11452   if (htab->glink_eh_frame != NULL
   11453       && htab->glink_eh_frame->size != 0)
   11454     {
   11455       bfd_byte *base, *eh;
   11456 
   11457       base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
   11458       eh = base + stub_entry->group->eh_size;
   11459 
   11460       if (!htab->params->no_tls_get_addr_regsave)
   11461 	{
   11462 	  unsigned int cfa_updt, delta, i;
   11463 
   11464 	  /* After the bctrl, lr has been modified so we need to emit
   11465 	     .eh_frame info saying the return address is on the stack.  In
   11466 	     fact we must put the EH info at or before the call rather
   11467 	     than after it, because the EH info for a call needs to be
   11468 	     specified by that point.
   11469 	     See libgcc/unwind-dw2.c execute_cfa_program.
   11470 	     Any stack pointer update must be described immediately after
   11471 	     the instruction making the change, and since the stdu occurs
   11472 	     after saving regs we put all the reg saves and the cfa
   11473 	     change there.  */
   11474 	  cfa_updt = stub_entry->stub_offset + 18 * 4;
   11475 	  delta = cfa_updt - stub_entry->group->lr_restore;
   11476 	  stub_entry->group->lr_restore
   11477 	    = stub_entry->stub_offset + (p - loc) - 4;
   11478 	  eh = eh_advance (htab->elf.dynobj, eh, delta);
   11479 	  *eh++ = DW_CFA_def_cfa_offset;
   11480 	  if (htab->opd_abi)
   11481 	    {
   11482 	      *eh++ = 128;
   11483 	      *eh++ = 1;
   11484 	    }
   11485 	  else
   11486 	    *eh++ = 96;
   11487 	  *eh++ = DW_CFA_offset_extended_sf;
   11488 	  *eh++ = 65;
   11489 	  *eh++ = (-16 / 8) & 0x7f;
   11490 	  for (i = 4; i < 12; i++)
   11491 	    {
   11492 	      *eh++ = DW_CFA_offset + i;
   11493 	      *eh++ = (htab->opd_abi ? 13 : 12) - i;
   11494 	    }
   11495 	  *eh++ = (DW_CFA_advance_loc
   11496 		   + (stub_entry->group->lr_restore - 8 - cfa_updt) / 4);
   11497 	  *eh++ = DW_CFA_def_cfa_offset;
   11498 	  *eh++ = 0;
   11499 	  for (i = 4; i < 12; i++)
   11500 	    *eh++ = DW_CFA_restore + i;
   11501 	  *eh++ = DW_CFA_advance_loc + 2;
   11502 	  *eh++ = DW_CFA_restore_extended;
   11503 	  *eh++ = 65;
   11504 	  stub_entry->group->eh_size = eh - base;
   11505 	}
   11506       else if (stub_entry->type.r2save)
   11507 	{
   11508 	  unsigned int lr_used, delta;
   11509 
   11510 	  lr_used = stub_entry->stub_offset + (p - 20 - loc);
   11511 	  delta = lr_used - stub_entry->group->lr_restore;
   11512 	  stub_entry->group->lr_restore = lr_used + 16;
   11513 	  eh = eh_advance (htab->elf.dynobj, eh, delta);
   11514 	  *eh++ = DW_CFA_offset_extended_sf;
   11515 	  *eh++ = 65;
   11516 	  *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
   11517 	  *eh++ = DW_CFA_advance_loc + 4;
   11518 	  *eh++ = DW_CFA_restore_extended;
   11519 	  *eh++ = 65;
   11520 	  stub_entry->group->eh_size = eh - base;
   11521 	}
   11522     }
   11523   return p;
   11524 }
   11525 
   11526 static Elf_Internal_Rela *
   11527 get_relocs (asection *sec, int count)
   11528 {
   11529   Elf_Internal_Rela *relocs;
   11530   struct bfd_elf_section_data *elfsec_data;
   11531 
   11532   elfsec_data = elf_section_data (sec);
   11533   relocs = elfsec_data->relocs;
   11534   if (relocs == NULL)
   11535     {
   11536       bfd_size_type relsize;
   11537       relsize = sec->reloc_count * sizeof (*relocs);
   11538       relocs = bfd_alloc (sec->owner, relsize);
   11539       if (relocs == NULL)
   11540 	return NULL;
   11541       elfsec_data->relocs = relocs;
   11542       elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
   11543 					  sizeof (Elf_Internal_Shdr));
   11544       if (elfsec_data->rela.hdr == NULL)
   11545 	return NULL;
   11546       elfsec_data->rela.hdr->sh_size = (sec->reloc_count
   11547 					* sizeof (Elf64_External_Rela));
   11548       elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
   11549       sec->reloc_count = 0;
   11550     }
   11551   relocs += sec->reloc_count;
   11552   sec->reloc_count += count;
   11553   return relocs;
   11554 }
   11555 
   11556 /* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
   11557    forms, to the equivalent relocs against the global symbol given by
   11558    STUB_ENTRY->H.  */
   11559 
   11560 static bool
   11561 use_global_in_relocs (struct ppc_link_hash_table *htab,
   11562 		      struct ppc_stub_hash_entry *stub_entry,
   11563 		      Elf_Internal_Rela *r, unsigned int num_rel)
   11564 {
   11565   struct elf_link_hash_entry **hashes;
   11566   unsigned long symndx;
   11567   struct ppc_link_hash_entry *h;
   11568   bfd_vma symval;
   11569 
   11570   /* Relocs are always against symbols in their own object file.  Fake
   11571      up global sym hashes for the stub bfd (which has no symbols).  */
   11572   hashes = elf_sym_hashes (htab->params->stub_bfd);
   11573   if (hashes == NULL)
   11574     {
   11575       bfd_size_type hsize;
   11576 
   11577       /* When called the first time, stub_globals will contain the
   11578 	 total number of symbols seen during stub sizing.  After
   11579 	 allocating, stub_globals is used as an index to fill the
   11580 	 hashes array.  */
   11581       hsize = (htab->stub_globals + 1) * sizeof (*hashes);
   11582       hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
   11583       if (hashes == NULL)
   11584 	return false;
   11585       elf_sym_hashes (htab->params->stub_bfd) = hashes;
   11586       htab->stub_globals = 1;
   11587     }
   11588   symndx = htab->stub_globals++;
   11589   h = stub_entry->h;
   11590   hashes[symndx] = &h->elf;
   11591   if (h->oh != NULL && h->oh->is_func)
   11592     h = ppc_follow_link (h->oh);
   11593   BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
   11594 	      || h->elf.root.type == bfd_link_hash_defweak);
   11595   symval = defined_sym_val (&h->elf);
   11596   while (num_rel-- != 0)
   11597     {
   11598       r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
   11599       if (h->elf.root.u.def.section != stub_entry->target_section)
   11600 	{
   11601 	  /* H is an opd symbol.  The addend must be zero, and the
   11602 	     branch reloc is the only one we can convert.  */
   11603 	  r->r_addend = 0;
   11604 	  break;
   11605 	}
   11606       else
   11607 	r->r_addend -= symval;
   11608       --r;
   11609     }
   11610   return true;
   11611 }
   11612 
   11613 static bfd_vma
   11614 get_r2off (struct bfd_link_info *info,
   11615 	   struct ppc_stub_hash_entry *stub_entry)
   11616 {
   11617   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   11618   bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
   11619 
   11620   if (r2off == 0)
   11621     {
   11622       /* Support linking -R objects.  Get the toc pointer from the
   11623 	 opd entry.  */
   11624       char buf[8];
   11625       if (!htab->opd_abi)
   11626 	return r2off;
   11627       asection *opd = stub_entry->h->elf.root.u.def.section;
   11628       bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
   11629 
   11630       if (strcmp (opd->name, ".opd") != 0
   11631 	  || opd->reloc_count != 0)
   11632 	{
   11633 	  info->callbacks->einfo
   11634 	    (_("%P: cannot find opd entry toc for `%pT'\n"),
   11635 	     stub_entry->h->elf.root.root.string);
   11636 	  bfd_set_error (bfd_error_bad_value);
   11637 	  return (bfd_vma) -1;
   11638 	}
   11639       if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
   11640 	return (bfd_vma) -1;
   11641       r2off = bfd_get_64 (opd->owner, buf);
   11642       r2off -= elf_gp (info->output_bfd);
   11643     }
   11644   r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
   11645   return r2off;
   11646 }
   11647 
   11648 /* Debug dump.  */
   11649 
   11650 static void
   11651 dump_stub (const char *header,
   11652 	   struct ppc_stub_hash_entry *stub_entry,
   11653 	   size_t end_offset)
   11654 {
   11655   const char *t1, *t2, *t3;
   11656   switch (stub_entry->type.main)
   11657     {
   11658     case ppc_stub_none:		t1 = "none";		break;
   11659     case ppc_stub_long_branch:	t1 = "long_branch";	break;
   11660     case ppc_stub_plt_branch:	t1 = "plt_branch";	break;
   11661     case ppc_stub_plt_call:	t1 = "plt_call";	break;
   11662     case ppc_stub_global_entry:	t1 = "global_entry";	break;
   11663     case ppc_stub_save_res:	t1 = "save_res";	break;
   11664     default:			t1 = "???";		break;
   11665     }
   11666   switch (stub_entry->type.sub)
   11667     {
   11668     case ppc_stub_toc:		t2 = "toc";		break;
   11669     case ppc_stub_notoc:	t2 = "notoc";		break;
   11670     case ppc_stub_p9notoc:	t2 = "p9notoc";		break;
   11671     default:			t2 = "???";		break;
   11672     }
   11673   t3 = stub_entry->type.r2save ? "r2save" : "";
   11674   fprintf (stderr, "%s id = %u type = %s:%s:%s\n",
   11675 	   header, stub_entry->id, t1, t2, t3);
   11676   fprintf (stderr, "name = %s\n", stub_entry->root.string);
   11677   fprintf (stderr, "offset = 0x%" PRIx64 ":", stub_entry->stub_offset);
   11678   for (size_t i = stub_entry->stub_offset; i < end_offset; i += 4)
   11679     {
   11680       asection *stub_sec = stub_entry->group->stub_sec;
   11681       uint32_t *p = (uint32_t *) (stub_sec->contents + i);
   11682       fprintf (stderr, " %08x", (uint32_t) bfd_get_32 (stub_sec->owner, p));
   11683     }
   11684   fprintf (stderr, "\n");
   11685 }
   11686 
   11687 static bool
   11688 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
   11689 {
   11690   struct ppc_stub_hash_entry *stub_entry;
   11691   struct ppc_branch_hash_entry *br_entry;
   11692   struct bfd_link_info *info;
   11693   struct ppc_link_hash_table *htab;
   11694   bfd *obfd;
   11695   bfd_byte *loc;
   11696   bfd_byte *p, *relp;
   11697   bfd_vma targ, off;
   11698   Elf_Internal_Rela *r;
   11699   asection *plt;
   11700   int num_rel;
   11701   int odd;
   11702   bool is_tga;
   11703 
   11704   /* Massage our args to the form they really have.  */
   11705   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
   11706   info = in_arg;
   11707 
   11708   /* Fail if the target section could not be assigned to an output
   11709      section.  The user should fix his linker script.  */
   11710   if (stub_entry->target_section != NULL
   11711       && stub_entry->target_section->output_section == NULL
   11712       && info->non_contiguous_regions)
   11713     info->callbacks->einfo (_("%F%P: Could not assign '%pA' to an output section. "
   11714 			      "Retry without --enable-non-contiguous-regions.\n"),
   11715 			    stub_entry->target_section);
   11716 
   11717   /* Same for the group.  */
   11718   if (stub_entry->group->stub_sec != NULL
   11719       && stub_entry->group->stub_sec->output_section == NULL
   11720       && info->non_contiguous_regions)
   11721     info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
   11722 			      "output section. Retry without "
   11723 			      "--enable-non-contiguous-regions.\n"),
   11724 			    stub_entry->group->stub_sec,
   11725 			    stub_entry->target_section);
   11726 
   11727   htab = ppc_hash_table (info);
   11728   if (htab == NULL)
   11729     return false;
   11730 
   11731   struct _ppc64_elf_section_data *esd
   11732     = ppc64_elf_section_data (stub_entry->group->stub_sec);
   11733   ++htab->stub_id;
   11734   if (stub_entry->id != htab->stub_id
   11735       || (stub_entry->type.main != ppc_stub_save_res
   11736 	  && stub_entry->stub_offset < stub_entry->group->stub_sec->size))
   11737     {
   11738       BFD_ASSERT (0);
   11739       if (stub_entry->id != htab->stub_id)
   11740 	fprintf (stderr, "Expected id %u, got %u\n",
   11741 		 htab->stub_id, stub_entry->id);
   11742       if (stub_entry->stub_offset < stub_entry->group->stub_sec->size)
   11743 	fprintf (stderr, "Expected offset >= %" PRIx64 ", got %"
   11744 		 PRIx64 "\n", stub_entry->group->stub_sec->size,
   11745 		 stub_entry->stub_offset);
   11746       if (esd->sec_type == sec_stub)
   11747 	dump_stub ("Previous:", esd->u.last_ent, stub_entry->stub_offset);
   11748       dump_stub ("Current:", stub_entry, 0);
   11749     }
   11750   if (esd->sec_type == sec_normal)
   11751     esd->sec_type = sec_stub;
   11752   if (esd->sec_type == sec_stub)
   11753     esd->u.last_ent = stub_entry;
   11754   loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
   11755 
   11756   htab->stub_count[stub_entry->type.main - 1] += 1;
   11757   if (stub_entry->type.main == ppc_stub_long_branch
   11758       && stub_entry->type.sub == ppc_stub_toc)
   11759     {
   11760       /* Branches are relative.  This is where we are going to.  */
   11761       targ = (stub_entry->target_value
   11762 	      + stub_entry->target_section->output_offset
   11763 	      + stub_entry->target_section->output_section->vma);
   11764       targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
   11765 
   11766       /* And this is where we are coming from.  */
   11767       off = (stub_entry->stub_offset
   11768 	     + stub_entry->group->stub_sec->output_offset
   11769 	     + stub_entry->group->stub_sec->output_section->vma);
   11770       off = targ - off;
   11771 
   11772       p = loc;
   11773       obfd = htab->params->stub_bfd;
   11774       if (stub_entry->type.r2save)
   11775 	{
   11776 	  bfd_vma r2off = get_r2off (info, stub_entry);
   11777 
   11778 	  if (r2off == (bfd_vma) -1)
   11779 	    {
   11780 	      htab->stub_error = true;
   11781 	      return false;
   11782 	    }
   11783 	  bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
   11784 	  p += 4;
   11785 	  if (PPC_HA (r2off) != 0)
   11786 	    {
   11787 	      bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
   11788 	      p += 4;
   11789 	    }
   11790 	  if (PPC_LO (r2off) != 0)
   11791 	    {
   11792 	      bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
   11793 	      p += 4;
   11794 	    }
   11795 	  off -= p - loc;
   11796 	}
   11797       bfd_put_32 (obfd, B_DOT | (off & 0x3fffffc), p);
   11798       p += 4;
   11799 
   11800       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
   11801 	{
   11802 	  _bfd_error_handler
   11803 	    (_("long branch stub `%s' offset overflow"),
   11804 	     stub_entry->root.string);
   11805 	  htab->stub_error = true;
   11806 	  return false;
   11807 	}
   11808 
   11809       if (info->emitrelocations)
   11810 	{
   11811 	  r = get_relocs (stub_entry->group->stub_sec, 1);
   11812 	  if (r == NULL)
   11813 	    return false;
   11814 	  r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
   11815 	  r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
   11816 	  r->r_addend = targ;
   11817 	  if (stub_entry->h != NULL
   11818 	      && !use_global_in_relocs (htab, stub_entry, r, 1))
   11819 	    return false;
   11820 	}
   11821     }
   11822   else if (stub_entry->type.main == ppc_stub_plt_branch
   11823 	   && stub_entry->type.sub == ppc_stub_toc)
   11824     {
   11825       br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
   11826 					 stub_entry->root.string + 9,
   11827 					 false, false);
   11828       if (br_entry == NULL)
   11829 	{
   11830 	  _bfd_error_handler (_("can't find branch stub `%s'"),
   11831 			      stub_entry->root.string);
   11832 	  htab->stub_error = true;
   11833 	  return false;
   11834 	}
   11835 
   11836       targ = (stub_entry->target_value
   11837 	      + stub_entry->target_section->output_offset
   11838 	      + stub_entry->target_section->output_section->vma);
   11839       if (!stub_entry->type.r2save)
   11840 	targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
   11841 
   11842       bfd_put_64 (htab->brlt->owner, targ,
   11843 		  htab->brlt->contents + br_entry->offset);
   11844 
   11845       if (br_entry->iter == htab->stub_iteration)
   11846 	{
   11847 	  br_entry->iter = 0;
   11848 
   11849 	  if (htab->relbrlt != NULL && !info->enable_dt_relr)
   11850 	    {
   11851 	      /* Create a reloc for the branch lookup table entry.  */
   11852 	      Elf_Internal_Rela rela;
   11853 	      bfd_byte *rl;
   11854 
   11855 	      rela.r_offset = (br_entry->offset
   11856 			       + htab->brlt->output_offset
   11857 			       + htab->brlt->output_section->vma);
   11858 	      rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   11859 	      rela.r_addend = targ;
   11860 
   11861 	      rl = htab->relbrlt->contents;
   11862 	      rl += (htab->relbrlt->reloc_count++
   11863 		     * sizeof (Elf64_External_Rela));
   11864 	      bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
   11865 	    }
   11866 	  else if (info->emitrelocations)
   11867 	    {
   11868 	      r = get_relocs (htab->brlt, 1);
   11869 	      if (r == NULL)
   11870 		return false;
   11871 	      /* brlt, being SEC_LINKER_CREATED does not go through the
   11872 		 normal reloc processing.  Symbols and offsets are not
   11873 		 translated from input file to output file form, so
   11874 		 set up the offset per the output file.  */
   11875 	      r->r_offset = (br_entry->offset
   11876 			     + htab->brlt->output_offset
   11877 			     + htab->brlt->output_section->vma);
   11878 	      r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   11879 	      r->r_addend = targ;
   11880 	    }
   11881 	}
   11882 
   11883       targ = (br_entry->offset
   11884 	      + htab->brlt->output_offset
   11885 	      + htab->brlt->output_section->vma);
   11886 
   11887       off = (elf_gp (info->output_bfd)
   11888 	     + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
   11889       off = targ - off;
   11890 
   11891       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
   11892 	{
   11893 	  info->callbacks->einfo
   11894 	    (_("%P: linkage table error against `%pT'\n"),
   11895 	     stub_entry->root.string);
   11896 	  bfd_set_error (bfd_error_bad_value);
   11897 	  htab->stub_error = true;
   11898 	  return false;
   11899 	}
   11900 
   11901       if (info->emitrelocations)
   11902 	{
   11903 	  r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
   11904 	  if (r == NULL)
   11905 	    return false;
   11906 	  r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
   11907 	  if (bfd_big_endian (info->output_bfd))
   11908 	    r[0].r_offset += 2;
   11909 	  if (stub_entry->type.r2save)
   11910 	    r[0].r_offset += 4;
   11911 	  r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
   11912 	  r[0].r_addend = targ;
   11913 	  if (PPC_HA (off) != 0)
   11914 	    {
   11915 	      r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
   11916 	      r[1].r_offset = r[0].r_offset + 4;
   11917 	      r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
   11918 	      r[1].r_addend = r[0].r_addend;
   11919 	    }
   11920 	}
   11921 
   11922       p = loc;
   11923       obfd = htab->params->stub_bfd;
   11924       if (!stub_entry->type.r2save)
   11925 	{
   11926 	  if (PPC_HA (off) != 0)
   11927 	    {
   11928 	      bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
   11929 	      p += 4;
   11930 	      bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
   11931 	    }
   11932 	  else
   11933 	    bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
   11934 	}
   11935       else
   11936 	{
   11937 	  bfd_vma r2off = get_r2off (info, stub_entry);
   11938 
   11939 	  if (r2off == (bfd_vma) -1)
   11940 	    {
   11941 	      htab->stub_error = true;
   11942 	      return false;
   11943 	    }
   11944 
   11945 	  bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
   11946 	  p += 4;
   11947 	  if (PPC_HA (off) != 0)
   11948 	    {
   11949 	      bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
   11950 	      p += 4;
   11951 	      bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
   11952 	    }
   11953 	  else
   11954 	    bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
   11955 
   11956 	  if (PPC_HA (r2off) != 0)
   11957 	    {
   11958 	      p += 4;
   11959 	      bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
   11960 	    }
   11961 	  if (PPC_LO (r2off) != 0)
   11962 	    {
   11963 	      p += 4;
   11964 	      bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
   11965 	    }
   11966 	}
   11967       p += 4;
   11968       bfd_put_32 (obfd, MTCTR_R12, p);
   11969       p += 4;
   11970       bfd_put_32 (obfd, BCTR, p);
   11971       p += 4;
   11972     }
   11973   else if (stub_entry->type.sub >= ppc_stub_notoc)
   11974     {
   11975       bool is_plt = stub_entry->type.main == ppc_stub_plt_call;
   11976       p = loc;
   11977       off = (stub_entry->stub_offset
   11978 	     + stub_entry->group->stub_sec->output_offset
   11979 	     + stub_entry->group->stub_sec->output_section->vma);
   11980       obfd = htab->params->stub_bfd;
   11981       is_tga = (is_plt
   11982 		&& stub_entry->h != NULL
   11983 		&& is_tls_get_addr (&stub_entry->h->elf, htab)
   11984 		&& htab->params->tls_get_addr_opt);
   11985       if (is_tga)
   11986 	{
   11987 	  p = build_tls_get_addr_head (htab, stub_entry, p);
   11988 	  off += p - loc;
   11989 	}
   11990       if (stub_entry->type.r2save)
   11991 	{
   11992 	  off += 4;
   11993 	  bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
   11994 	  p += 4;
   11995 	}
   11996       if (is_plt)
   11997 	{
   11998 	  targ = stub_entry->plt_ent->plt.offset & ~1;
   11999 	  if (targ >= (bfd_vma) -2)
   12000 	    abort ();
   12001 
   12002 	  plt = htab->elf.splt;
   12003 	  if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
   12004 	    {
   12005 	      if (stub_entry->symtype == STT_GNU_IFUNC)
   12006 		plt = htab->elf.iplt;
   12007 	      else
   12008 		plt = htab->pltlocal;
   12009 	    }
   12010 	  targ += plt->output_offset + plt->output_section->vma;
   12011 	}
   12012       else
   12013 	targ = (stub_entry->target_value
   12014 		+ stub_entry->target_section->output_offset
   12015 		+ stub_entry->target_section->output_section->vma);
   12016       odd = off & 4;
   12017       off = targ - off;
   12018 
   12019       relp = p;
   12020       num_rel = 0;
   12021       if (stub_entry->type.sub == ppc_stub_notoc)
   12022 	p = build_power10_offset (obfd, p, off, odd, is_plt);
   12023       else
   12024 	{
   12025 	  if (htab->glink_eh_frame != NULL
   12026 	      && htab->glink_eh_frame->size != 0)
   12027 	    {
   12028 	      bfd_byte *base, *eh;
   12029 	      unsigned int lr_used, delta;
   12030 
   12031 	      base = (htab->glink_eh_frame->contents
   12032 		      + stub_entry->group->eh_base + 17);
   12033 	      eh = base + stub_entry->group->eh_size;
   12034 	      lr_used = stub_entry->stub_offset + (p - loc) + 8;
   12035 	      delta = lr_used - stub_entry->group->lr_restore;
   12036 	      stub_entry->group->lr_restore = lr_used + 8;
   12037 	      eh = eh_advance (htab->elf.dynobj, eh, delta);
   12038 	      *eh++ = DW_CFA_register;
   12039 	      *eh++ = 65;
   12040 	      *eh++ = 12;
   12041 	      *eh++ = DW_CFA_advance_loc + 2;
   12042 	      *eh++ = DW_CFA_restore_extended;
   12043 	      *eh++ = 65;
   12044 	      stub_entry->group->eh_size = eh - base;
   12045 	    }
   12046 
   12047 	  /* The notoc stubs calculate their target (either a PLT entry or
   12048 	     the global entry point of a function) relative to the PC
   12049 	     returned by the "bcl" two instructions past the start of the
   12050 	     sequence emitted by build_offset.  The offset is therefore 8
   12051 	     less than calculated from the start of the sequence.  */
   12052 	  off -= 8;
   12053 	  p = build_offset (obfd, p, off, is_plt);
   12054 	}
   12055 
   12056       if (stub_entry->type.main == ppc_stub_long_branch)
   12057 	{
   12058 	  bfd_vma from;
   12059 	  num_rel = 1;
   12060 	  from = (stub_entry->stub_offset
   12061 		  + stub_entry->group->stub_sec->output_offset
   12062 		  + stub_entry->group->stub_sec->output_section->vma
   12063 		  + (p - loc));
   12064 	  bfd_put_32 (obfd, B_DOT | ((targ - from) & 0x3fffffc), p);
   12065 	}
   12066       else
   12067 	{
   12068 	  bfd_put_32 (obfd, MTCTR_R12, p);
   12069 	  p += 4;
   12070 	  bfd_put_32 (obfd, BCTR, p);
   12071 	}
   12072       p += 4;
   12073 
   12074       if (is_tga)
   12075 	p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
   12076 
   12077       if (info->emitrelocations)
   12078 	{
   12079 	  bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
   12080 	  if (stub_entry->type.sub == ppc_stub_notoc)
   12081 	    num_rel += num_relocs_for_power10_offset (off, odd);
   12082 	  else
   12083 	    {
   12084 	      num_rel += num_relocs_for_offset (off);
   12085 	      roff += 16;
   12086 	    }
   12087 	  r = get_relocs (stub_entry->group->stub_sec, num_rel);
   12088 	  if (r == NULL)
   12089 	    return false;
   12090 	  if (stub_entry->type.sub == ppc_stub_notoc)
   12091 	    r = emit_relocs_for_power10_offset (info, r, roff, targ, off, odd);
   12092 	  else
   12093 	    r = emit_relocs_for_offset (info, r, roff, targ, off);
   12094 	  if (stub_entry->type.main == ppc_stub_long_branch)
   12095 	    {
   12096 	      ++r;
   12097 	      roff = p - 4 - stub_entry->group->stub_sec->contents;
   12098 	      r->r_offset = roff;
   12099 	      r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
   12100 	      r->r_addend = targ;
   12101 	      if (stub_entry->h != NULL
   12102 		  && !use_global_in_relocs (htab, stub_entry, r, num_rel))
   12103 		return false;
   12104 	    }
   12105 	}
   12106     }
   12107   else if (stub_entry->type.main == ppc_stub_plt_call)
   12108     {
   12109       if (stub_entry->h != NULL
   12110 	  && stub_entry->h->is_func_descriptor
   12111 	  && stub_entry->h->oh != NULL)
   12112 	{
   12113 	  struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
   12114 
   12115 	  /* If the old-ABI "dot-symbol" is undefined make it weak so
   12116 	     we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.  */
   12117 	  if (fh->elf.root.type == bfd_link_hash_undefined
   12118 	      && (stub_entry->h->elf.root.type == bfd_link_hash_defined
   12119 		  || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
   12120 	    fh->elf.root.type = bfd_link_hash_undefweak;
   12121 	}
   12122 
   12123       /* Now build the stub.  */
   12124       targ = stub_entry->plt_ent->plt.offset & ~1;
   12125       if (targ >= (bfd_vma) -2)
   12126 	abort ();
   12127 
   12128       plt = htab->elf.splt;
   12129       if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
   12130 	{
   12131 	  if (stub_entry->symtype == STT_GNU_IFUNC)
   12132 	    plt = htab->elf.iplt;
   12133 	  else
   12134 	    plt = htab->pltlocal;
   12135 	}
   12136       targ += plt->output_offset + plt->output_section->vma;
   12137 
   12138       off = (elf_gp (info->output_bfd)
   12139 	     + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
   12140       off = targ - off;
   12141 
   12142       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
   12143 	{
   12144 	  info->callbacks->einfo
   12145 	    /* xgettext:c-format */
   12146 	    (_("%P: linkage table error against `%pT'\n"),
   12147 	     stub_entry->h != NULL
   12148 	     ? stub_entry->h->elf.root.root.string
   12149 	     : "<local sym>");
   12150 	  bfd_set_error (bfd_error_bad_value);
   12151 	  htab->stub_error = true;
   12152 	  return false;
   12153 	}
   12154 
   12155       r = NULL;
   12156       if (info->emitrelocations)
   12157 	{
   12158 	  r = get_relocs (stub_entry->group->stub_sec,
   12159 			  ((PPC_HA (off) != 0)
   12160 			   + (htab->opd_abi
   12161 			      ? 2 + (htab->params->plt_static_chain
   12162 				     && PPC_HA (off + 16) == PPC_HA (off))
   12163 			      : 1)));
   12164 	  if (r == NULL)
   12165 	    return false;
   12166 	  r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
   12167 	  if (bfd_big_endian (info->output_bfd))
   12168 	    r[0].r_offset += 2;
   12169 	  r[0].r_addend = targ;
   12170 	}
   12171       p = loc;
   12172       obfd = htab->params->stub_bfd;
   12173       is_tga = (stub_entry->h != NULL
   12174 		&& is_tls_get_addr (&stub_entry->h->elf, htab)
   12175 		&& htab->params->tls_get_addr_opt);
   12176       if (is_tga)
   12177 	{
   12178 	  p = build_tls_get_addr_head (htab, stub_entry, p);
   12179 	  if (r != NULL)
   12180 	    r[0].r_offset += p - loc;
   12181 	}
   12182       p = build_plt_stub (htab, stub_entry, p, off, r);
   12183       if (is_tga)
   12184 	p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
   12185     }
   12186   else if (stub_entry->type.main == ppc_stub_save_res)
   12187     return true;
   12188   else
   12189     {
   12190       BFD_FAIL ();
   12191       return false;
   12192     }
   12193 
   12194   stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
   12195 
   12196   if (htab->params->emit_stub_syms)
   12197     {
   12198       struct elf_link_hash_entry *h;
   12199       size_t len1, len2;
   12200       char *name;
   12201       const char *const stub_str[] = { "long_branch",
   12202 				       "plt_branch",
   12203 				       "plt_call" };
   12204 
   12205       len1 = strlen (stub_str[stub_entry->type.main - 1]);
   12206       len2 = strlen (stub_entry->root.string);
   12207       name = bfd_malloc (len1 + len2 + 2);
   12208       if (name == NULL)
   12209 	return false;
   12210       memcpy (name, stub_entry->root.string, 9);
   12211       memcpy (name + 9, stub_str[stub_entry->type.main - 1], len1);
   12212       memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
   12213       h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
   12214       if (h == NULL)
   12215 	return false;
   12216       if (h->root.type == bfd_link_hash_new)
   12217 	{
   12218 	  h->root.type = bfd_link_hash_defined;
   12219 	  h->root.u.def.section = stub_entry->group->stub_sec;
   12220 	  h->root.u.def.value = stub_entry->stub_offset;
   12221 	  h->ref_regular = 1;
   12222 	  h->def_regular = 1;
   12223 	  h->ref_regular_nonweak = 1;
   12224 	  h->forced_local = 1;
   12225 	  h->non_elf = 0;
   12226 	  h->root.linker_def = 1;
   12227 	}
   12228     }
   12229 
   12230   return true;
   12231 }
   12232 
   12233 /* As above, but don't actually build the stub.  Just bump offset so
   12234    we know stub section sizes, and select plt_branch stubs where
   12235    long_branch stubs won't do.  */
   12236 
   12237 static bool
   12238 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
   12239 {
   12240   struct ppc_stub_hash_entry *stub_entry;
   12241   struct bfd_link_info *info;
   12242   struct ppc_link_hash_table *htab;
   12243   asection *plt;
   12244   bfd_vma targ, off, r2off;
   12245   unsigned int size, pad, extra, lr_used, delta, odd;
   12246   bfd_vma stub_offset;
   12247 
   12248   /* Massage our args to the form they really have.  */
   12249   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
   12250   info = in_arg;
   12251 
   12252   htab = ppc_hash_table (info);
   12253   if (htab == NULL)
   12254     return false;
   12255 
   12256   /* Fail if the target section could not be assigned to an output
   12257      section.  The user should fix his linker script.  */
   12258   if (stub_entry->target_section != NULL
   12259       && stub_entry->target_section->output_section == NULL
   12260       && info->non_contiguous_regions)
   12261     info->callbacks->einfo (_("%F%P: Could not assign %pA to an output section. "
   12262 			      "Retry without --enable-non-contiguous-regions.\n"),
   12263 			    stub_entry->target_section);
   12264 
   12265   /* Same for the group.  */
   12266   if (stub_entry->group->stub_sec != NULL
   12267       && stub_entry->group->stub_sec->output_section == NULL
   12268       && info->non_contiguous_regions)
   12269     info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
   12270 			      "output section. Retry without "
   12271 			      "--enable-non-contiguous-regions.\n"),
   12272 			    stub_entry->group->stub_sec,
   12273 			    stub_entry->target_section);
   12274 
   12275   /* Make a note of the offset within the stubs for this entry.  */
   12276   stub_offset = stub_entry->group->stub_sec->size;
   12277   if (htab->stub_iteration > STUB_SHRINK_ITER
   12278       && stub_entry->stub_offset > stub_offset)
   12279     stub_offset = stub_entry->stub_offset;
   12280   stub_entry->id = ++htab->stub_id;
   12281 
   12282   if (stub_entry->h != NULL
   12283       && stub_entry->h->save_res
   12284       && stub_entry->h->elf.root.type == bfd_link_hash_defined
   12285       && stub_entry->h->elf.root.u.def.section == htab->sfpr)
   12286     {
   12287       /* Don't make stubs to out-of-line register save/restore
   12288 	 functions.  Instead, emit copies of the functions.  */
   12289       stub_entry->group->needs_save_res = 1;
   12290       stub_entry->type.main = ppc_stub_save_res;
   12291       stub_entry->type.sub = ppc_stub_toc;
   12292       stub_entry->type.r2save = 0;
   12293       return true;
   12294     }
   12295 
   12296   if (stub_entry->type.main == ppc_stub_plt_branch)
   12297     {
   12298       /* Reset the stub type from the plt branch variant in case we now
   12299 	 can reach with a shorter stub.  */
   12300       stub_entry->type.main = ppc_stub_long_branch;
   12301     }
   12302 
   12303   if (stub_entry->type.main == ppc_stub_long_branch
   12304       && stub_entry->type.sub == ppc_stub_toc)
   12305     {
   12306       targ = (stub_entry->target_value
   12307 	      + stub_entry->target_section->output_offset
   12308 	      + stub_entry->target_section->output_section->vma);
   12309       targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
   12310       off = (stub_offset
   12311 	     + stub_entry->group->stub_sec->output_offset
   12312 	     + stub_entry->group->stub_sec->output_section->vma);
   12313 
   12314       size = 4;
   12315       r2off = 0;
   12316       if (stub_entry->type.r2save)
   12317 	{
   12318 	  r2off = get_r2off (info, stub_entry);
   12319 	  if (r2off == (bfd_vma) -1)
   12320 	    {
   12321 	      htab->stub_error = true;
   12322 	      return false;
   12323 	    }
   12324 	  size = 8;
   12325 	  if (PPC_HA (r2off) != 0)
   12326 	    size += 4;
   12327 	  if (PPC_LO (r2off) != 0)
   12328 	    size += 4;
   12329 	  off += size - 4;
   12330 	}
   12331       off = targ - off;
   12332 
   12333       /* If the branch offset is too big, use a ppc_stub_plt_branch.
   12334 	 Do the same for -R objects without function descriptors.  */
   12335       if ((stub_entry->type.r2save
   12336 	   && r2off == 0
   12337 	   && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
   12338 	  || off + (1 << 25) >= (bfd_vma) (1 << 26))
   12339 	{
   12340 	  struct ppc_branch_hash_entry *br_entry;
   12341 
   12342 	  br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
   12343 					     stub_entry->root.string + 9,
   12344 					     true, false);
   12345 	  if (br_entry == NULL)
   12346 	    {
   12347 	      _bfd_error_handler (_("can't build branch stub `%s'"),
   12348 				  stub_entry->root.string);
   12349 	      htab->stub_error = true;
   12350 	      return false;
   12351 	    }
   12352 
   12353 	  if (br_entry->iter != htab->stub_iteration)
   12354 	    {
   12355 	      br_entry->iter = htab->stub_iteration;
   12356 	      br_entry->offset = htab->brlt->size;
   12357 	      htab->brlt->size += 8;
   12358 
   12359 	      if (htab->relbrlt != NULL && !info->enable_dt_relr)
   12360 		htab->relbrlt->size += sizeof (Elf64_External_Rela);
   12361 	      else if (info->emitrelocations)
   12362 		{
   12363 		  htab->brlt->reloc_count += 1;
   12364 		  htab->brlt->flags |= SEC_RELOC;
   12365 		}
   12366 	    }
   12367 
   12368 	  targ = (br_entry->offset
   12369 		  + htab->brlt->output_offset
   12370 		  + htab->brlt->output_section->vma);
   12371 	  off = (elf_gp (info->output_bfd)
   12372 		 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
   12373 	  off = targ - off;
   12374 
   12375 	  if (info->emitrelocations)
   12376 	    {
   12377 	      stub_entry->group->stub_sec->reloc_count
   12378 		+= 1 + (PPC_HA (off) != 0);
   12379 	      stub_entry->group->stub_sec->flags |= SEC_RELOC;
   12380 	    }
   12381 
   12382 	  stub_entry->type.main = ppc_stub_plt_branch;
   12383 	  if (!stub_entry->type.r2save)
   12384 	    {
   12385 	      size = 12;
   12386 	      if (PPC_HA (off) != 0)
   12387 		size = 16;
   12388 	    }
   12389 	  else
   12390 	    {
   12391 	      size = 16;
   12392 	      if (PPC_HA (off) != 0)
   12393 		size += 4;
   12394 
   12395 	      if (PPC_HA (r2off) != 0)
   12396 		size += 4;
   12397 	      if (PPC_LO (r2off) != 0)
   12398 		size += 4;
   12399 	    }
   12400 	  pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
   12401 	  stub_offset += pad;
   12402 	}
   12403       else if (info->emitrelocations)
   12404 	{
   12405 	  stub_entry->group->stub_sec->reloc_count += 1;
   12406 	  stub_entry->group->stub_sec->flags |= SEC_RELOC;
   12407 	}
   12408     }
   12409   else if (stub_entry->type.main == ppc_stub_long_branch)
   12410     {
   12411       off = (stub_offset
   12412 	     + stub_entry->group->stub_sec->output_offset
   12413 	     + stub_entry->group->stub_sec->output_section->vma);
   12414       size = 0;
   12415       if (stub_entry->type.r2save)
   12416 	size = 4;
   12417       off += size;
   12418       targ = (stub_entry->target_value
   12419 	      + stub_entry->target_section->output_offset
   12420 	      + stub_entry->target_section->output_section->vma);
   12421       odd = off & 4;
   12422       off = targ - off;
   12423 
   12424       if (stub_entry->type.sub == ppc_stub_notoc)
   12425 	extra = size_power10_offset (off, odd);
   12426       else
   12427 	extra = size_offset (off - 8);
   12428       /* Include branch insn plus those in the offset sequence.  */
   12429       size += 4 + extra;
   12430 
   12431       /* If the branch can't reach, use a plt_branch.
   12432 	 The branch insn is at the end, or "extra" bytes along.  So
   12433 	 its offset will be "extra" bytes less that that already
   12434 	 calculated.  */
   12435       if (off - extra + (1 << 25) >= (bfd_vma) (1 << 26))
   12436 	{
   12437 	  stub_entry->type.main = ppc_stub_plt_branch;
   12438 	  size += 4;
   12439 	  pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
   12440 	  if (pad != 0)
   12441 	    {
   12442 	      stub_offset += pad;
   12443 	      off -= pad;
   12444 	      odd ^= pad & 4;
   12445 	      size -= extra;
   12446 	      if (stub_entry->type.sub == ppc_stub_notoc)
   12447 		extra = size_power10_offset (off, odd);
   12448 	      else
   12449 		extra = size_offset (off - 8);
   12450 	      size += extra;
   12451 	    }
   12452 	}
   12453       else if (info->emitrelocations)
   12454 	stub_entry->group->stub_sec->reloc_count +=1;
   12455 
   12456       if (info->emitrelocations)
   12457 	{
   12458 	  unsigned int num_rel;
   12459 	  if (stub_entry->type.sub == ppc_stub_notoc)
   12460 	    num_rel = num_relocs_for_power10_offset (off, odd);
   12461 	  else
   12462 	    num_rel = num_relocs_for_offset (off - 8);
   12463 	  stub_entry->group->stub_sec->reloc_count += num_rel;
   12464 	  stub_entry->group->stub_sec->flags |= SEC_RELOC;
   12465 	}
   12466 
   12467       if (stub_entry->type.sub != ppc_stub_notoc)
   12468 	{
   12469 	  /* After the bcl, lr has been modified so we need to emit
   12470 	     .eh_frame info saying the return address is in r12.  */
   12471 	  lr_used = stub_offset + 8;
   12472 	  if (stub_entry->type.r2save)
   12473 	    lr_used += 4;
   12474 	  /* The eh_frame info will consist of a DW_CFA_advance_loc or
   12475 	     variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
   12476 	     DW_CFA_restore_extended 65.  */
   12477 	  delta = lr_used - stub_entry->group->lr_restore;
   12478 	  stub_entry->group->eh_size += eh_advance_size (delta) + 6;
   12479 	  stub_entry->group->lr_restore = lr_used + 8;
   12480 	}
   12481     }
   12482   else if (stub_entry->type.sub >= ppc_stub_notoc)
   12483     {
   12484       BFD_ASSERT (stub_entry->type.main == ppc_stub_plt_call);
   12485       lr_used = 0;
   12486       if (stub_entry->h != NULL
   12487 	  && is_tls_get_addr (&stub_entry->h->elf, htab)
   12488 	  && htab->params->tls_get_addr_opt)
   12489 	{
   12490 	  lr_used += 7 * 4;
   12491 	  if (!htab->params->no_tls_get_addr_regsave)
   12492 	    lr_used += 11 * 4;
   12493 	  else if (stub_entry->type.r2save)
   12494 	    lr_used += 2 * 4;
   12495 	}
   12496       if (stub_entry->type.r2save)
   12497 	lr_used += 4;
   12498       targ = stub_entry->plt_ent->plt.offset & ~1;
   12499       if (targ >= (bfd_vma) -2)
   12500 	abort ();
   12501 
   12502       plt = htab->elf.splt;
   12503       if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
   12504 	{
   12505 	  if (stub_entry->symtype == STT_GNU_IFUNC)
   12506 	    plt = htab->elf.iplt;
   12507 	  else
   12508 	    plt = htab->pltlocal;
   12509 	}
   12510       targ += plt->output_offset + plt->output_section->vma;
   12511       off = (stub_offset
   12512 	     + stub_entry->group->stub_sec->output_offset
   12513 	     + stub_entry->group->stub_sec->output_section->vma
   12514 	     + lr_used);
   12515       odd = off & 4;
   12516       off = targ - off;
   12517 
   12518       size = plt_stub_size (htab, stub_entry, off, odd);
   12519       pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
   12520       if (pad != 0)
   12521 	{
   12522 	  stub_offset += pad;
   12523 	  off -= pad;
   12524 	  odd ^= pad & 4;
   12525 	  size = plt_stub_size (htab, stub_entry, off, odd);
   12526 	}
   12527 
   12528       if (info->emitrelocations)
   12529 	{
   12530 	  unsigned int num_rel;
   12531 	  if (stub_entry->type.sub == ppc_stub_notoc)
   12532 	    num_rel = num_relocs_for_power10_offset (off, odd);
   12533 	  else
   12534 	    num_rel = num_relocs_for_offset (off - 8);
   12535 	  stub_entry->group->stub_sec->reloc_count += num_rel;
   12536 	  stub_entry->group->stub_sec->flags |= SEC_RELOC;
   12537 	}
   12538 
   12539       if (stub_entry->type.sub != ppc_stub_notoc)
   12540 	{
   12541 	  /* After the bcl, lr has been modified so we need to emit
   12542 	     .eh_frame info saying the return address is in r12.  */
   12543 	  lr_used += stub_offset + 8;
   12544 	  /* The eh_frame info will consist of a DW_CFA_advance_loc or
   12545 	     variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
   12546 	     DW_CFA_restore_extended 65.  */
   12547 	  delta = lr_used - stub_entry->group->lr_restore;
   12548 	  stub_entry->group->eh_size += eh_advance_size (delta) + 6;
   12549 	  stub_entry->group->lr_restore = lr_used + 8;
   12550 	}
   12551       if (stub_entry->h != NULL
   12552 	  && is_tls_get_addr (&stub_entry->h->elf, htab)
   12553 	  && htab->params->tls_get_addr_opt)
   12554 	{
   12555 	  if (!htab->params->no_tls_get_addr_regsave)
   12556 	    {
   12557 	      unsigned int cfa_updt = stub_offset + 18 * 4;
   12558 	      delta = cfa_updt - stub_entry->group->lr_restore;
   12559 	      stub_entry->group->eh_size += eh_advance_size (delta);
   12560 	      stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
   12561 	      stub_entry->group->lr_restore = stub_offset + size - 4;
   12562 	    }
   12563 	  else if (stub_entry->type.r2save)
   12564 	    {
   12565 	      lr_used = stub_offset + size - 20;
   12566 	      delta = lr_used - stub_entry->group->lr_restore;
   12567 	      stub_entry->group->eh_size += eh_advance_size (delta) + 6;
   12568 	      stub_entry->group->lr_restore = stub_offset + size - 4;
   12569 	    }
   12570 	}
   12571     }
   12572   else if (stub_entry->type.main == ppc_stub_plt_call)
   12573     {
   12574       targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
   12575       if (targ >= (bfd_vma) -2)
   12576 	abort ();
   12577       plt = htab->elf.splt;
   12578       if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
   12579 	{
   12580 	  if (stub_entry->symtype == STT_GNU_IFUNC)
   12581 	    plt = htab->elf.iplt;
   12582 	  else
   12583 	    plt = htab->pltlocal;
   12584 	}
   12585       targ += plt->output_offset + plt->output_section->vma;
   12586 
   12587       off = (elf_gp (info->output_bfd)
   12588 	     + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
   12589       off = targ - off;
   12590 
   12591       size = plt_stub_size (htab, stub_entry, off, 0);
   12592       pad = plt_stub_pad (htab->params->plt_stub_align, stub_offset, size);
   12593       stub_offset += pad;
   12594 
   12595       if (info->emitrelocations)
   12596 	{
   12597 	  stub_entry->group->stub_sec->reloc_count
   12598 	    += ((PPC_HA (off) != 0)
   12599 		+ (htab->opd_abi
   12600 		   ? 2 + (htab->params->plt_static_chain
   12601 			  && PPC_HA (off + 16) == PPC_HA (off))
   12602 		   : 1));
   12603 	  stub_entry->group->stub_sec->flags |= SEC_RELOC;
   12604 	}
   12605 
   12606       if (stub_entry->h != NULL
   12607 	  && is_tls_get_addr (&stub_entry->h->elf, htab)
   12608 	  && htab->params->tls_get_addr_opt
   12609 	  && stub_entry->type.r2save)
   12610 	{
   12611 	  if (!htab->params->no_tls_get_addr_regsave)
   12612 	    {
   12613 	      /* Adjustments to r1 need to be described.  */
   12614 	      unsigned int cfa_updt = stub_offset + 18 * 4;
   12615 	      delta = cfa_updt - stub_entry->group->lr_restore;
   12616 	      stub_entry->group->eh_size += eh_advance_size (delta);
   12617 	      stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
   12618 	    }
   12619 	  else
   12620 	    {
   12621 	      lr_used = stub_offset + size - 20;
   12622 	      /* The eh_frame info will consist of a DW_CFA_advance_loc
   12623 		 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
   12624 		 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65.  */
   12625 	      delta = lr_used - stub_entry->group->lr_restore;
   12626 	      stub_entry->group->eh_size += eh_advance_size (delta) + 6;
   12627 	    }
   12628 	  stub_entry->group->lr_restore = stub_offset + size - 4;
   12629 	}
   12630     }
   12631   else
   12632     {
   12633       BFD_FAIL ();
   12634       return false;
   12635     }
   12636 
   12637   if (stub_entry->stub_offset != stub_offset)
   12638     htab->stub_changed = true;
   12639   stub_entry->stub_offset = stub_offset;
   12640   stub_entry->group->stub_sec->size = stub_offset + size;
   12641   return true;
   12642 }
   12643 
   12644 /* Set up various things so that we can make a list of input sections
   12645    for each output section included in the link.  Returns -1 on error,
   12646    0 when no stubs will be needed, and 1 on success.  */
   12647 
   12648 int
   12649 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
   12650 {
   12651   unsigned int id;
   12652   size_t amt;
   12653   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   12654 
   12655   if (htab == NULL)
   12656     return -1;
   12657 
   12658   htab->sec_info_arr_size = _bfd_section_id;
   12659   amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
   12660   htab->sec_info = bfd_zmalloc (amt);
   12661   if (htab->sec_info == NULL)
   12662     return -1;
   12663 
   12664   /* Set toc_off for com, und, abs and ind sections.  */
   12665   for (id = 0; id < 3; id++)
   12666     htab->sec_info[id].toc_off = TOC_BASE_OFF;
   12667 
   12668   return 1;
   12669 }
   12670 
   12671 /* Set up for first pass at multitoc partitioning.  */
   12672 
   12673 void
   12674 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
   12675 {
   12676   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   12677 
   12678   htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
   12679   htab->toc_bfd = NULL;
   12680   htab->toc_first_sec = NULL;
   12681 }
   12682 
   12683 /* The linker repeatedly calls this function for each TOC input section
   12684    and linker generated GOT section.  Group input bfds such that the toc
   12685    within a group is less than 64k in size.  */
   12686 
   12687 bool
   12688 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
   12689 {
   12690   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   12691   bfd_vma addr, off, limit;
   12692 
   12693   if (htab == NULL)
   12694     return false;
   12695 
   12696   if (!htab->second_toc_pass)
   12697     {
   12698       /* Keep track of the first .toc or .got section for this input bfd.  */
   12699       bool new_bfd = htab->toc_bfd != isec->owner;
   12700 
   12701       if (new_bfd)
   12702 	{
   12703 	  htab->toc_bfd = isec->owner;
   12704 	  htab->toc_first_sec = isec;
   12705 	}
   12706 
   12707       addr = isec->output_offset + isec->output_section->vma;
   12708       off = addr - htab->toc_curr;
   12709       limit = 0x80008000;
   12710       if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
   12711 	limit = 0x10000;
   12712       if (off + isec->size > limit)
   12713 	{
   12714 	  addr = (htab->toc_first_sec->output_offset
   12715 		  + htab->toc_first_sec->output_section->vma);
   12716 	  htab->toc_curr = addr;
   12717 	  htab->toc_curr &= -TOC_BASE_ALIGN;
   12718 	}
   12719 
   12720       /* toc_curr is the base address of this toc group.  Set elf_gp
   12721 	 for the input section to be the offset relative to the
   12722 	 output toc base plus 0x8000.  Making the input elf_gp an
   12723 	 offset allows us to move the toc as a whole without
   12724 	 recalculating input elf_gp.  */
   12725       off = htab->toc_curr - elf_gp (info->output_bfd);
   12726       off += TOC_BASE_OFF;
   12727 
   12728       /* Die if someone uses a linker script that doesn't keep input
   12729 	 file .toc and .got together.  */
   12730       if (new_bfd
   12731 	  && elf_gp (isec->owner) != 0
   12732 	  && elf_gp (isec->owner) != off)
   12733 	return false;
   12734 
   12735       elf_gp (isec->owner) = off;
   12736       return true;
   12737     }
   12738 
   12739   /* During the second pass toc_first_sec points to the start of
   12740      a toc group, and toc_curr is used to track the old elf_gp.
   12741      We use toc_bfd to ensure we only look at each bfd once.  */
   12742   if (htab->toc_bfd == isec->owner)
   12743     return true;
   12744   htab->toc_bfd = isec->owner;
   12745 
   12746   if (htab->toc_first_sec == NULL
   12747       || htab->toc_curr != elf_gp (isec->owner))
   12748     {
   12749       htab->toc_curr = elf_gp (isec->owner);
   12750       htab->toc_first_sec = isec;
   12751     }
   12752   addr = (htab->toc_first_sec->output_offset
   12753 	  + htab->toc_first_sec->output_section->vma);
   12754   off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
   12755   elf_gp (isec->owner) = off;
   12756 
   12757   return true;
   12758 }
   12759 
   12760 /* Called via elf_link_hash_traverse to merge GOT entries for global
   12761    symbol H.  */
   12762 
   12763 static bool
   12764 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
   12765 {
   12766   if (h->root.type == bfd_link_hash_indirect)
   12767     return true;
   12768 
   12769   merge_got_entries (&h->got.glist);
   12770 
   12771   return true;
   12772 }
   12773 
   12774 /* Called via elf_link_hash_traverse to allocate GOT entries for global
   12775    symbol H.  */
   12776 
   12777 static bool
   12778 reallocate_got (struct elf_link_hash_entry *h, void *inf)
   12779 {
   12780   struct got_entry *gent;
   12781 
   12782   if (h->root.type == bfd_link_hash_indirect)
   12783     return true;
   12784 
   12785   for (gent = h->got.glist; gent != NULL; gent = gent->next)
   12786     if (!gent->is_indirect)
   12787       allocate_got (h, (struct bfd_link_info *) inf, gent);
   12788   return true;
   12789 }
   12790 
   12791 /* Called on the first multitoc pass after the last call to
   12792    ppc64_elf_next_toc_section.  This function removes duplicate GOT
   12793    entries.  */
   12794 
   12795 bool
   12796 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
   12797 {
   12798   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   12799   struct bfd *ibfd, *ibfd2;
   12800   bool done_something;
   12801 
   12802   htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
   12803 
   12804   if (!htab->do_multi_toc)
   12805     return false;
   12806 
   12807   /* Merge global sym got entries within a toc group.  */
   12808   elf_link_hash_traverse (&htab->elf, merge_global_got, info);
   12809 
   12810   /* And tlsld_got.  */
   12811   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   12812     {
   12813       struct got_entry *ent, *ent2;
   12814 
   12815       if (!is_ppc64_elf (ibfd))
   12816 	continue;
   12817 
   12818       ent = ppc64_tlsld_got (ibfd);
   12819       if (!ent->is_indirect
   12820 	  && ent->got.offset != (bfd_vma) -1)
   12821 	{
   12822 	  for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
   12823 	    {
   12824 	      if (!is_ppc64_elf (ibfd2))
   12825 		continue;
   12826 
   12827 	      ent2 = ppc64_tlsld_got (ibfd2);
   12828 	      if (!ent2->is_indirect
   12829 		  && ent2->got.offset != (bfd_vma) -1
   12830 		  && elf_gp (ibfd2) == elf_gp (ibfd))
   12831 		{
   12832 		  ent2->is_indirect = true;
   12833 		  ent2->got.ent = ent;
   12834 		}
   12835 	    }
   12836 	}
   12837     }
   12838 
   12839   /* Zap sizes of got sections.  */
   12840   htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
   12841   htab->elf.irelplt->size -= htab->got_reli_size;
   12842   htab->got_reli_size = 0;
   12843 
   12844   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   12845     {
   12846       asection *got, *relgot;
   12847 
   12848       if (!is_ppc64_elf (ibfd))
   12849 	continue;
   12850 
   12851       got = ppc64_elf_tdata (ibfd)->got;
   12852       if (got != NULL)
   12853 	{
   12854 	  got->rawsize = got->size;
   12855 	  got->size = 0;
   12856 	  relgot = ppc64_elf_tdata (ibfd)->relgot;
   12857 	  relgot->rawsize = relgot->size;
   12858 	  relgot->size = 0;
   12859 	}
   12860     }
   12861 
   12862   /* Now reallocate the got, local syms first.  We don't need to
   12863      allocate section contents again since we never increase size.  */
   12864   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   12865     {
   12866       struct got_entry **lgot_ents;
   12867       struct got_entry **end_lgot_ents;
   12868       struct plt_entry **local_plt;
   12869       struct plt_entry **end_local_plt;
   12870       unsigned char *lgot_masks;
   12871       bfd_size_type locsymcount;
   12872       Elf_Internal_Shdr *symtab_hdr;
   12873       asection *s;
   12874       Elf_Internal_Sym *local_syms;
   12875       Elf_Internal_Sym *isym;
   12876 
   12877       if (!is_ppc64_elf (ibfd))
   12878 	continue;
   12879 
   12880       lgot_ents = elf_local_got_ents (ibfd);
   12881       if (!lgot_ents)
   12882 	continue;
   12883 
   12884       symtab_hdr = &elf_symtab_hdr (ibfd);
   12885       locsymcount = symtab_hdr->sh_info;
   12886       end_lgot_ents = lgot_ents + locsymcount;
   12887       local_plt = (struct plt_entry **) end_lgot_ents;
   12888       end_local_plt = local_plt + locsymcount;
   12889       lgot_masks = (unsigned char *) end_local_plt;
   12890       local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
   12891       if (local_syms == NULL && locsymcount != 0)
   12892 	{
   12893 	  local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
   12894 					     0, NULL, NULL, NULL);
   12895 	  if (local_syms == NULL)
   12896 	    return false;
   12897 	}
   12898       s = ppc64_elf_tdata (ibfd)->got;
   12899       for (isym = local_syms;
   12900 	   lgot_ents < end_lgot_ents;
   12901 	   ++lgot_ents, ++lgot_masks, isym++)
   12902 	{
   12903 	  struct got_entry *ent;
   12904 
   12905 	  for (ent = *lgot_ents; ent != NULL; ent = ent->next)
   12906 	    {
   12907 	      unsigned int ent_size = 8;
   12908 	      unsigned int rel_size = sizeof (Elf64_External_Rela);
   12909 
   12910 	      ent->got.offset = s->size;
   12911 	      if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
   12912 		{
   12913 		  ent_size *= 2;
   12914 		  rel_size *= 2;
   12915 		}
   12916 	      s->size += ent_size;
   12917 	      if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
   12918 		{
   12919 		  htab->elf.irelplt->size += rel_size;
   12920 		  htab->got_reli_size += rel_size;
   12921 		}
   12922 	      else if (bfd_link_pic (info)
   12923 		       && (ent->tls_type == 0
   12924 			   ? !info->enable_dt_relr
   12925 			   : !bfd_link_executable (info))
   12926 		       && isym->st_shndx != SHN_ABS)
   12927 		{
   12928 		  asection *srel = ppc64_elf_tdata (ibfd)->relgot;
   12929 		  srel->size += rel_size;
   12930 		}
   12931 	    }
   12932 	}
   12933     }
   12934 
   12935   elf_link_hash_traverse (&htab->elf, reallocate_got, info);
   12936 
   12937   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   12938     {
   12939       struct got_entry *ent;
   12940 
   12941       if (!is_ppc64_elf (ibfd))
   12942 	continue;
   12943 
   12944       ent = ppc64_tlsld_got (ibfd);
   12945       if (!ent->is_indirect
   12946 	  && ent->got.offset != (bfd_vma) -1)
   12947 	{
   12948 	  asection *s = ppc64_elf_tdata (ibfd)->got;
   12949 	  ent->got.offset = s->size;
   12950 	  s->size += 16;
   12951 	  if (bfd_link_dll (info))
   12952 	    {
   12953 	      asection *srel = ppc64_elf_tdata (ibfd)->relgot;
   12954 	      srel->size += sizeof (Elf64_External_Rela);
   12955 	    }
   12956 	}
   12957     }
   12958 
   12959   done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
   12960   if (!done_something)
   12961     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   12962       {
   12963 	asection *got;
   12964 
   12965 	if (!is_ppc64_elf (ibfd))
   12966 	  continue;
   12967 
   12968 	got = ppc64_elf_tdata (ibfd)->got;
   12969 	if (got != NULL)
   12970 	  {
   12971 	    done_something = got->rawsize != got->size;
   12972 	    if (done_something)
   12973 	      break;
   12974 	  }
   12975       }
   12976 
   12977   if (done_something)
   12978     (*htab->params->layout_sections_again) ();
   12979 
   12980   /* Set up for second pass over toc sections to recalculate elf_gp
   12981      on input sections.  */
   12982   htab->toc_bfd = NULL;
   12983   htab->toc_first_sec = NULL;
   12984   htab->second_toc_pass = true;
   12985   return done_something;
   12986 }
   12987 
   12988 /* Called after second pass of multitoc partitioning.  */
   12989 
   12990 void
   12991 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
   12992 {
   12993   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   12994 
   12995   /* After the second pass, toc_curr tracks the TOC offset used
   12996      for code sections below in ppc64_elf_next_input_section.  */
   12997   htab->toc_curr = TOC_BASE_OFF;
   12998 }
   12999 
   13000 /* No toc references were found in ISEC.  If the code in ISEC makes no
   13001    calls, then there's no need to use toc adjusting stubs when branching
   13002    into ISEC.  Actually, indirect calls from ISEC are OK as they will
   13003    load r2.  Returns -1 on error, 0 for no stub needed, 1 for stub
   13004    needed, and 2 if a cyclical call-graph was found but no other reason
   13005    for a stub was detected.  If called from the top level, a return of
   13006    2 means the same as a return of 0.  */
   13007 
   13008 static int
   13009 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
   13010 {
   13011   int ret;
   13012 
   13013   /* Mark this section as checked.  */
   13014   isec->call_check_done = 1;
   13015 
   13016   /* We know none of our code bearing sections will need toc stubs.  */
   13017   if ((isec->flags & SEC_LINKER_CREATED) != 0)
   13018     return 0;
   13019 
   13020   if (isec->size == 0)
   13021     return 0;
   13022 
   13023   if (isec->output_section == NULL)
   13024     return 0;
   13025 
   13026   ret = 0;
   13027   if (isec->reloc_count != 0)
   13028     {
   13029       Elf_Internal_Rela *relstart, *rel;
   13030       Elf_Internal_Sym *local_syms;
   13031       struct ppc_link_hash_table *htab;
   13032 
   13033       relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
   13034 					    info->keep_memory);
   13035       if (relstart == NULL)
   13036 	return -1;
   13037 
   13038       /* Look for branches to outside of this section.  */
   13039       local_syms = NULL;
   13040       htab = ppc_hash_table (info);
   13041       if (htab == NULL)
   13042 	return -1;
   13043 
   13044       for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
   13045 	{
   13046 	  enum elf_ppc64_reloc_type r_type;
   13047 	  unsigned long r_symndx;
   13048 	  struct elf_link_hash_entry *h;
   13049 	  struct ppc_link_hash_entry *eh;
   13050 	  Elf_Internal_Sym *sym;
   13051 	  asection *sym_sec;
   13052 	  struct _opd_sec_data *opd;
   13053 	  bfd_vma sym_value;
   13054 	  bfd_vma dest;
   13055 
   13056 	  r_type = ELF64_R_TYPE (rel->r_info);
   13057 	  if (r_type != R_PPC64_REL24
   13058 	      && r_type != R_PPC64_REL24_NOTOC
   13059 	      && r_type != R_PPC64_REL24_P9NOTOC
   13060 	      && r_type != R_PPC64_REL14
   13061 	      && r_type != R_PPC64_REL14_BRTAKEN
   13062 	      && r_type != R_PPC64_REL14_BRNTAKEN
   13063 	      && r_type != R_PPC64_PLTCALL
   13064 	      && r_type != R_PPC64_PLTCALL_NOTOC)
   13065 	    continue;
   13066 
   13067 	  r_symndx = ELF64_R_SYM (rel->r_info);
   13068 	  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
   13069 			  isec->owner))
   13070 	    {
   13071 	      ret = -1;
   13072 	      break;
   13073 	    }
   13074 
   13075 	  /* Calls to dynamic lib functions go through a plt call stub
   13076 	     that uses r2.  */
   13077 	  eh = ppc_elf_hash_entry (h);
   13078 	  if (eh != NULL
   13079 	      && (eh->elf.plt.plist != NULL
   13080 		  || (eh->oh != NULL
   13081 		      && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
   13082 	    {
   13083 	      ret = 1;
   13084 	      break;
   13085 	    }
   13086 
   13087 	  if (sym_sec == NULL)
   13088 	    /* Ignore other undefined symbols.  */
   13089 	    continue;
   13090 
   13091 	  /* Assume branches to other sections not included in the
   13092 	     link need stubs too, to cover -R and absolute syms.  */
   13093 	  if (sym_sec->output_section == NULL)
   13094 	    {
   13095 	      ret = 1;
   13096 	      break;
   13097 	    }
   13098 
   13099 	  if (h == NULL)
   13100 	    sym_value = sym->st_value;
   13101 	  else
   13102 	    {
   13103 	      if (h->root.type != bfd_link_hash_defined
   13104 		  && h->root.type != bfd_link_hash_defweak)
   13105 		abort ();
   13106 	      sym_value = h->root.u.def.value;
   13107 	    }
   13108 	  sym_value += rel->r_addend;
   13109 
   13110 	  /* If this branch reloc uses an opd sym, find the code section.  */
   13111 	  opd = get_opd_info (sym_sec);
   13112 	  if (opd != NULL)
   13113 	    {
   13114 	      if (h == NULL && opd->adjust != NULL)
   13115 		{
   13116 		  long adjust;
   13117 
   13118 		  adjust = opd->adjust[OPD_NDX (sym_value)];
   13119 		  if (adjust == -1)
   13120 		    /* Assume deleted functions won't ever be called.  */
   13121 		    continue;
   13122 		  sym_value += adjust;
   13123 		}
   13124 
   13125 	      dest = opd_entry_value (sym_sec, sym_value,
   13126 				      &sym_sec, NULL, false);
   13127 	      if (dest == (bfd_vma) -1)
   13128 		continue;
   13129 	    }
   13130 	  else
   13131 	    dest = (sym_value
   13132 		    + sym_sec->output_offset
   13133 		    + sym_sec->output_section->vma);
   13134 
   13135 	  /* Ignore branch to self.  */
   13136 	  if (sym_sec == isec)
   13137 	    continue;
   13138 
   13139 	  /* If the called function uses the toc, we need a stub.  */
   13140 	  if (sym_sec->has_toc_reloc
   13141 	      || sym_sec->makes_toc_func_call)
   13142 	    {
   13143 	      ret = 1;
   13144 	      break;
   13145 	    }
   13146 
   13147 	  /* Assume any branch that needs a long branch stub might in fact
   13148 	     need a plt_branch stub.  A plt_branch stub uses r2.  */
   13149 	  else if (dest - (isec->output_offset
   13150 			   + isec->output_section->vma
   13151 			   + rel->r_offset) + (1 << 25)
   13152 		   >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
   13153 							     ? h->other
   13154 							     : sym->st_other))
   13155 	    {
   13156 	      ret = 1;
   13157 	      break;
   13158 	    }
   13159 
   13160 	  /* If calling back to a section in the process of being
   13161 	     tested, we can't say for sure that no toc adjusting stubs
   13162 	     are needed, so don't return zero.  */
   13163 	  else if (sym_sec->call_check_in_progress)
   13164 	    ret = 2;
   13165 
   13166 	  /* Branches to another section that itself doesn't have any TOC
   13167 	     references are OK.  Recursively call ourselves to check.  */
   13168 	  else if (!sym_sec->call_check_done)
   13169 	    {
   13170 	      int recur;
   13171 
   13172 	      /* Mark current section as indeterminate, so that other
   13173 		 sections that call back to current won't be marked as
   13174 		 known.  */
   13175 	      isec->call_check_in_progress = 1;
   13176 	      recur = toc_adjusting_stub_needed (info, sym_sec);
   13177 	      isec->call_check_in_progress = 0;
   13178 
   13179 	      if (recur != 0)
   13180 		{
   13181 		  ret = recur;
   13182 		  if (recur != 2)
   13183 		    break;
   13184 		}
   13185 	    }
   13186 	}
   13187 
   13188       if (elf_symtab_hdr (isec->owner).contents
   13189 	  != (unsigned char *) local_syms)
   13190 	free (local_syms);
   13191       if (elf_section_data (isec)->relocs != relstart)
   13192 	free (relstart);
   13193     }
   13194 
   13195   if ((ret & 1) == 0
   13196       && isec->map_head.s != NULL
   13197       && (strcmp (isec->output_section->name, ".init") == 0
   13198 	  || strcmp (isec->output_section->name, ".fini") == 0))
   13199     {
   13200       if (isec->map_head.s->has_toc_reloc
   13201 	  || isec->map_head.s->makes_toc_func_call)
   13202 	ret = 1;
   13203       else if (!isec->map_head.s->call_check_done)
   13204 	{
   13205 	  int recur;
   13206 	  isec->call_check_in_progress = 1;
   13207 	  recur = toc_adjusting_stub_needed (info, isec->map_head.s);
   13208 	  isec->call_check_in_progress = 0;
   13209 	  if (recur != 0)
   13210 	    ret = recur;
   13211 	}
   13212     }
   13213 
   13214   if (ret == 1)
   13215     isec->makes_toc_func_call = 1;
   13216 
   13217   return ret;
   13218 }
   13219 
   13220 /* The linker repeatedly calls this function for each input section,
   13221    in the order that input sections are linked into output sections.
   13222    Build lists of input sections to determine groupings between which
   13223    we may insert linker stubs.  */
   13224 
   13225 bool
   13226 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
   13227 {
   13228   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   13229 
   13230   if (htab == NULL)
   13231     return false;
   13232 
   13233   if ((isec->output_section->flags & SEC_CODE) != 0
   13234       && isec->output_section->id < htab->sec_info_arr_size)
   13235     {
   13236       /* This happens to make the list in reverse order,
   13237 	 which is what we want.  */
   13238       htab->sec_info[isec->id].u.list
   13239 	= htab->sec_info[isec->output_section->id].u.list;
   13240       htab->sec_info[isec->output_section->id].u.list = isec;
   13241     }
   13242 
   13243   if (htab->multi_toc_needed)
   13244     {
   13245       /* Analyse sections that aren't already flagged as needing a
   13246 	 valid toc pointer.  Exclude .fixup for the linux kernel.
   13247 	 .fixup contains branches, but only back to the function that
   13248 	 hit an exception.  */
   13249       if (!(isec->has_toc_reloc
   13250 	    || (isec->flags & SEC_CODE) == 0
   13251 	    || strcmp (isec->name, ".fixup") == 0
   13252 	    || isec->call_check_done))
   13253 	{
   13254 	  if (toc_adjusting_stub_needed (info, isec) < 0)
   13255 	    return false;
   13256 	}
   13257       /* Make all sections use the TOC assigned for this object file.
   13258 	 This will be wrong for pasted sections;  We fix that in
   13259 	 check_pasted_section().  */
   13260       if (elf_gp (isec->owner) != 0)
   13261 	htab->toc_curr = elf_gp (isec->owner);
   13262     }
   13263 
   13264   htab->sec_info[isec->id].toc_off = htab->toc_curr;
   13265   return true;
   13266 }
   13267 
   13268 /* Check that all .init and .fini sections use the same toc, if they
   13269    have toc relocs.  */
   13270 
   13271 static bool
   13272 check_pasted_section (struct bfd_link_info *info, const char *name)
   13273 {
   13274   asection *o = bfd_get_section_by_name (info->output_bfd, name);
   13275 
   13276   if (o != NULL)
   13277     {
   13278       struct ppc_link_hash_table *htab = ppc_hash_table (info);
   13279       bfd_vma toc_off = 0;
   13280       asection *i;
   13281 
   13282       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   13283 	if (i->has_toc_reloc)
   13284 	  {
   13285 	    if (toc_off == 0)
   13286 	      toc_off = htab->sec_info[i->id].toc_off;
   13287 	    else if (toc_off != htab->sec_info[i->id].toc_off)
   13288 	      return false;
   13289 	  }
   13290 
   13291       if (toc_off == 0)
   13292 	for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   13293 	  if (i->makes_toc_func_call)
   13294 	    {
   13295 	      toc_off = htab->sec_info[i->id].toc_off;
   13296 	      break;
   13297 	    }
   13298 
   13299       /* Make sure the whole pasted function uses the same toc offset.  */
   13300       if (toc_off != 0)
   13301 	for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   13302 	  htab->sec_info[i->id].toc_off = toc_off;
   13303     }
   13304   return true;
   13305 }
   13306 
   13307 bool
   13308 ppc64_elf_check_init_fini (struct bfd_link_info *info)
   13309 {
   13310   bool ret1 = check_pasted_section (info, ".init");
   13311   bool ret2 = check_pasted_section (info, ".fini");
   13312 
   13313   return ret1 && ret2;
   13314 }
   13315 
   13316 /* See whether we can group stub sections together.  Grouping stub
   13317    sections may result in fewer stubs.  More importantly, we need to
   13318    put all .init* and .fini* stubs at the beginning of the .init or
   13319    .fini output sections respectively, because glibc splits the
   13320    _init and _fini functions into multiple parts.  Putting a stub in
   13321    the middle of a function is not a good idea.  */
   13322 
   13323 static bool
   13324 group_sections (struct bfd_link_info *info,
   13325 		bfd_size_type stub_group_size,
   13326 		bool stubs_always_before_branch)
   13327 {
   13328   struct ppc_link_hash_table *htab;
   13329   asection *osec;
   13330   bool suppress_size_errors;
   13331 
   13332   htab = ppc_hash_table (info);
   13333   if (htab == NULL)
   13334     return false;
   13335 
   13336   suppress_size_errors = false;
   13337   if (stub_group_size == 1)
   13338     {
   13339       /* Default values.  */
   13340       if (stubs_always_before_branch)
   13341 	stub_group_size = 0x1e00000;
   13342       else
   13343 	stub_group_size = 0x1c00000;
   13344       suppress_size_errors = true;
   13345     }
   13346 
   13347   for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
   13348     {
   13349       asection *tail;
   13350 
   13351       if (osec->id >= htab->sec_info_arr_size)
   13352 	continue;
   13353 
   13354       tail = htab->sec_info[osec->id].u.list;
   13355       while (tail != NULL)
   13356 	{
   13357 	  asection *curr;
   13358 	  asection *prev;
   13359 	  bfd_size_type total;
   13360 	  bool big_sec;
   13361 	  bfd_vma curr_toc;
   13362 	  struct map_stub *group;
   13363 	  bfd_size_type group_size;
   13364 
   13365 	  curr = tail;
   13366 	  total = tail->size;
   13367 	  group_size = (ppc64_elf_section_data (tail) != NULL
   13368 			&& ppc64_elf_section_data (tail)->has_14bit_branch
   13369 			? stub_group_size >> 10 : stub_group_size);
   13370 
   13371 	  big_sec = total > group_size;
   13372 	  if (big_sec && !suppress_size_errors)
   13373 	    /* xgettext:c-format */
   13374 	    _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
   13375 				tail->owner, tail);
   13376 	  curr_toc = htab->sec_info[tail->id].toc_off;
   13377 
   13378 	  while ((prev = htab->sec_info[curr->id].u.list) != NULL
   13379 		 && ((total += curr->output_offset - prev->output_offset)
   13380 		     < (ppc64_elf_section_data (prev) != NULL
   13381 			&& ppc64_elf_section_data (prev)->has_14bit_branch
   13382 			? (group_size = stub_group_size >> 10) : group_size))
   13383 		 && htab->sec_info[prev->id].toc_off == curr_toc)
   13384 	    curr = prev;
   13385 
   13386 	  /* OK, the size from the start of CURR to the end is less
   13387 	     than group_size and thus can be handled by one stub
   13388 	     section.  (or the tail section is itself larger than
   13389 	     group_size, in which case we may be toast.)  We should
   13390 	     really be keeping track of the total size of stubs added
   13391 	     here, as stubs contribute to the final output section
   13392 	     size.  That's a little tricky, and this way will only
   13393 	     break if stubs added make the total size more than 2^25,
   13394 	     ie. for the default stub_group_size, if stubs total more
   13395 	     than 2097152 bytes, or nearly 75000 plt call stubs.  */
   13396 	  group = bfd_alloc (curr->owner, sizeof (*group));
   13397 	  if (group == NULL)
   13398 	    return false;
   13399 	  group->link_sec = curr;
   13400 	  group->stub_sec = NULL;
   13401 	  group->needs_save_res = 0;
   13402 	  group->lr_restore = 0;
   13403 	  group->eh_size = 0;
   13404 	  group->eh_base = 0;
   13405 	  group->next = htab->group;
   13406 	  htab->group = group;
   13407 	  do
   13408 	    {
   13409 	      prev = htab->sec_info[tail->id].u.list;
   13410 	      /* Set up this stub group.  */
   13411 	      htab->sec_info[tail->id].u.group = group;
   13412 	    }
   13413 	  while (tail != curr && (tail = prev) != NULL);
   13414 
   13415 	  /* But wait, there's more!  Input sections up to group_size
   13416 	     bytes before the stub section can be handled by it too.
   13417 	     Don't do this if we have a really large section after the
   13418 	     stubs, as adding more stubs increases the chance that
   13419 	     branches may not reach into the stub section.  */
   13420 	  if (!stubs_always_before_branch && !big_sec)
   13421 	    {
   13422 	      total = 0;
   13423 	      while (prev != NULL
   13424 		     && ((total += tail->output_offset - prev->output_offset)
   13425 			 < (ppc64_elf_section_data (prev) != NULL
   13426 			    && ppc64_elf_section_data (prev)->has_14bit_branch
   13427 			    ? (group_size = stub_group_size >> 10)
   13428 			    : group_size))
   13429 		     && htab->sec_info[prev->id].toc_off == curr_toc)
   13430 		{
   13431 		  tail = prev;
   13432 		  prev = htab->sec_info[tail->id].u.list;
   13433 		  htab->sec_info[tail->id].u.group = group;
   13434 		}
   13435 	    }
   13436 	  tail = prev;
   13437 	}
   13438     }
   13439   return true;
   13440 }
   13441 
   13442 static const unsigned char glink_eh_frame_cie[] =
   13443 {
   13444   0, 0, 0, 16,				/* length.  */
   13445   0, 0, 0, 0,				/* id.  */
   13446   1,					/* CIE version.  */
   13447   'z', 'R', 0,				/* Augmentation string.  */
   13448   4,					/* Code alignment.  */
   13449   0x78,					/* Data alignment.  */
   13450   65,					/* RA reg.  */
   13451   1,					/* Augmentation size.  */
   13452   DW_EH_PE_pcrel | DW_EH_PE_sdata4,	/* FDE encoding.  */
   13453   DW_CFA_def_cfa, 1, 0			/* def_cfa: r1 offset 0.  */
   13454 };
   13455 
   13456 /* Stripping output sections is normally done before dynamic section
   13457    symbols have been allocated.  This function is called later, and
   13458    handles cases like htab->brlt which is mapped to its own output
   13459    section.  */
   13460 
   13461 static void
   13462 maybe_strip_output (struct bfd_link_info *info, asection *isec)
   13463 {
   13464   if (isec->size == 0
   13465       && isec->output_section->size == 0
   13466       && !(isec->output_section->flags & SEC_KEEP)
   13467       && !bfd_section_removed_from_list (info->output_bfd,
   13468 					 isec->output_section)
   13469       && elf_section_data (isec->output_section)->dynindx == 0)
   13470     {
   13471       isec->output_section->flags |= SEC_EXCLUDE;
   13472       bfd_section_list_remove (info->output_bfd, isec->output_section);
   13473       info->output_bfd->section_count--;
   13474     }
   13475 }
   13476 
   13477 /* Stash R_PPC64_RELATIVE reloc at input section SEC, r_offset OFF to
   13478    the array of such relocs.  */
   13479 
   13480 static bool
   13481 append_relr_off (struct ppc_link_hash_table *htab, asection *sec, bfd_vma off)
   13482 {
   13483   if (htab->relr_count >= htab->relr_alloc)
   13484     {
   13485       if (htab->relr_alloc == 0)
   13486 	htab->relr_alloc = 4096;
   13487       else
   13488 	htab->relr_alloc *= 2;
   13489       htab->relr = bfd_realloc (htab->relr,
   13490 				htab->relr_alloc * sizeof (*htab->relr));
   13491       if (htab->relr == NULL)
   13492 	return false;
   13493     }
   13494   htab->relr[htab->relr_count].sec = sec;
   13495   htab->relr[htab->relr_count].off = off;
   13496   htab->relr_count++;
   13497   return true;
   13498 }
   13499 
   13500 /* qsort comparator for bfd_vma args.  */
   13501 
   13502 static int
   13503 compare_relr_address (const void *arg1, const void *arg2)
   13504 {
   13505   bfd_vma a = *(bfd_vma *) arg1;
   13506   bfd_vma b = *(bfd_vma *) arg2;
   13507   return a < b ? -1 : a > b ? 1 : 0;
   13508 }
   13509 
   13510 /* Produce a malloc'd sorted array of reloc addresses from the info
   13511    stored by append_relr_off.  */
   13512 
   13513 static bfd_vma *
   13514 sort_relr (struct ppc_link_hash_table *htab)
   13515 {
   13516   bfd_vma *addr = bfd_malloc (htab->relr_count * sizeof (*addr));
   13517   if (addr == NULL)
   13518     return NULL;
   13519 
   13520   for (size_t i = 0; i < htab->relr_count; i++)
   13521     addr[i] = (htab->relr[i].sec->output_section->vma
   13522 	       + htab->relr[i].sec->output_offset
   13523 	       + htab->relr[i].off);
   13524 
   13525   if (htab->relr_count > 1)
   13526     qsort (addr, htab->relr_count, sizeof (*addr), compare_relr_address);
   13527 
   13528   return addr;
   13529 }
   13530 
   13531 /* Look over GOT and PLT entries saved on elf_local_got_ents for all
   13532    input files, stashing info about needed relative relocs.  */
   13533 
   13534 static bool
   13535 got_and_plt_relr_for_local_syms (struct bfd_link_info *info)
   13536 {
   13537   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   13538   bfd *ibfd;
   13539 
   13540   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   13541     {
   13542       struct got_entry **lgot_ents, **lgot, **end_lgot_ents;
   13543       struct plt_entry **local_plt, **lplt, **end_local_plt;
   13544       Elf_Internal_Shdr *symtab_hdr;
   13545       bfd_size_type locsymcount;
   13546       Elf_Internal_Sym *local_syms;
   13547       Elf_Internal_Sym *isym;
   13548       struct plt_entry *pent;
   13549       struct got_entry *gent;
   13550 
   13551       if (!is_ppc64_elf (ibfd))
   13552 	continue;
   13553 
   13554       lgot_ents = elf_local_got_ents (ibfd);
   13555       if (!lgot_ents)
   13556 	continue;
   13557 
   13558       symtab_hdr = &elf_symtab_hdr (ibfd);
   13559       locsymcount = symtab_hdr->sh_info;
   13560       local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
   13561       if (local_syms == NULL && locsymcount != 0)
   13562 	{
   13563 	  local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
   13564 					     0, NULL, NULL, NULL);
   13565 	  if (local_syms == NULL)
   13566 	    return false;
   13567 	}
   13568       end_lgot_ents = lgot_ents + locsymcount;
   13569       local_plt = (struct plt_entry **) end_lgot_ents;
   13570       end_local_plt = local_plt + locsymcount;
   13571       for (lgot = lgot_ents, isym = local_syms;
   13572 	   lgot < end_lgot_ents;
   13573 	   ++lgot, ++isym)
   13574 	for (gent = *lgot; gent != NULL; gent = gent->next)
   13575 	  if (!gent->is_indirect
   13576 	      && gent->tls_type == 0
   13577 	      && gent->got.offset != (bfd_vma) -1
   13578 	      && isym->st_shndx != SHN_ABS)
   13579 	    {
   13580 	      asection *got = ppc64_elf_tdata (gent->owner)->got;
   13581 	      if (!append_relr_off (htab, got, gent->got.offset))
   13582 		{
   13583 		  htab->stub_error = true;
   13584 		  return false;
   13585 		}
   13586 	    }
   13587 
   13588       if (!htab->opd_abi)
   13589 	for (lplt = local_plt, isym = local_syms;
   13590 	     lplt < end_local_plt;
   13591 	     ++lplt, ++isym)
   13592 	  for (pent = *lplt; pent != NULL; pent = pent->next)
   13593 	    if (pent->plt.offset != (bfd_vma) -1
   13594 		&& ELF_ST_TYPE (isym->st_info) != STT_GNU_IFUNC)
   13595 	      {
   13596 		if (!append_relr_off (htab, htab->pltlocal, pent->plt.offset))
   13597 		  {
   13598 		    if (symtab_hdr->contents != (unsigned char *) local_syms)
   13599 		      free (local_syms);
   13600 		    return false;
   13601 		  }
   13602 	      }
   13603 
   13604       if (local_syms != NULL
   13605 	  && symtab_hdr->contents != (unsigned char *) local_syms)
   13606 	{
   13607 	  if (!info->keep_memory)
   13608 	    free (local_syms);
   13609 	  else
   13610 	    symtab_hdr->contents = (unsigned char *) local_syms;
   13611 	}
   13612     }
   13613   return true;
   13614 }
   13615 
   13616 /* Stash info about needed GOT and PLT entry relative relocs for
   13617    global symbol H.  */
   13618 
   13619 static bool
   13620 got_and_plt_relr (struct elf_link_hash_entry *h, void *inf)
   13621 {
   13622   struct bfd_link_info *info;
   13623   struct ppc_link_hash_table *htab;
   13624   struct plt_entry *pent;
   13625   struct got_entry *gent;
   13626 
   13627   if (h->root.type == bfd_link_hash_indirect)
   13628     return true;
   13629 
   13630   info = (struct bfd_link_info *) inf;
   13631   htab = ppc_hash_table (info);
   13632   if (htab == NULL)
   13633     return false;
   13634 
   13635   if (h->type != STT_GNU_IFUNC
   13636       && h->def_regular
   13637       && (h->root.type == bfd_link_hash_defined
   13638 	  || h->root.type == bfd_link_hash_defweak))
   13639     {
   13640       if ((!htab->elf.dynamic_sections_created
   13641 	   || h->dynindx == -1
   13642 	   || SYMBOL_REFERENCES_LOCAL (info, h))
   13643 	  && !bfd_is_abs_symbol (&h->root))
   13644 	for (gent = h->got.glist; gent != NULL; gent = gent->next)
   13645 	  if (!gent->is_indirect
   13646 	      && gent->tls_type == 0
   13647 	      && gent->got.offset != (bfd_vma) -1)
   13648 	    {
   13649 	      asection *got = ppc64_elf_tdata (gent->owner)->got;
   13650 	      if (!append_relr_off (htab, got, gent->got.offset))
   13651 		{
   13652 		  htab->stub_error = true;
   13653 		  return false;
   13654 		}
   13655 	    }
   13656 
   13657       if (!htab->opd_abi
   13658 	  && use_local_plt (info, h))
   13659 	for (pent = h->plt.plist; pent != NULL; pent = pent->next)
   13660 	  if (pent->plt.offset != (bfd_vma) -1)
   13661 	    {
   13662 	      if (!append_relr_off (htab, htab->pltlocal, pent->plt.offset))
   13663 		{
   13664 		  htab->stub_error = true;
   13665 		  return false;
   13666 		}
   13667 	    }
   13668     }
   13669   return true;
   13670 }
   13671 
   13672 /* Determine and set the size of the stub section for a final link.
   13673 
   13674    The basic idea here is to examine all the relocations looking for
   13675    PC-relative calls to a target that is unreachable with a "bl"
   13676    instruction.  */
   13677 
   13678 bool
   13679 ppc64_elf_size_stubs (struct bfd_link_info *info)
   13680 {
   13681   bfd_size_type stub_group_size;
   13682   bool stubs_always_before_branch;
   13683   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   13684 
   13685   if (htab == NULL)
   13686     return false;
   13687 
   13688   if (htab->params->power10_stubs == -1 && !htab->has_power10_relocs)
   13689     htab->params->power10_stubs = 0;
   13690 
   13691   if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
   13692     htab->params->plt_thread_safe = 1;
   13693   if (!htab->opd_abi)
   13694     htab->params->plt_thread_safe = 0;
   13695   else if (htab->params->plt_thread_safe == -1)
   13696     {
   13697       static const char *const thread_starter[] =
   13698 	{
   13699 	  "pthread_create",
   13700 	  /* libstdc++ */
   13701 	  "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
   13702 	  /* librt */
   13703 	  "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
   13704 	  "mq_notify", "create_timer",
   13705 	  /* libanl */
   13706 	  "getaddrinfo_a",
   13707 	  /* libgomp */
   13708 	  "GOMP_parallel",
   13709 	  "GOMP_parallel_start",
   13710 	  "GOMP_parallel_loop_static",
   13711 	  "GOMP_parallel_loop_static_start",
   13712 	  "GOMP_parallel_loop_dynamic",
   13713 	  "GOMP_parallel_loop_dynamic_start",
   13714 	  "GOMP_parallel_loop_guided",
   13715 	  "GOMP_parallel_loop_guided_start",
   13716 	  "GOMP_parallel_loop_runtime",
   13717 	  "GOMP_parallel_loop_runtime_start",
   13718 	  "GOMP_parallel_sections",
   13719 	  "GOMP_parallel_sections_start",
   13720 	  /* libgo */
   13721 	  "__go_go",
   13722 	};
   13723       unsigned i;
   13724 
   13725       for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
   13726 	{
   13727 	  struct elf_link_hash_entry *h;
   13728 	  h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
   13729 				    false, false, true);
   13730 	  htab->params->plt_thread_safe = h != NULL && h->ref_regular;
   13731 	  if (htab->params->plt_thread_safe)
   13732 	    break;
   13733 	}
   13734     }
   13735   stubs_always_before_branch = htab->params->group_size < 0;
   13736   if (htab->params->group_size < 0)
   13737     stub_group_size = -htab->params->group_size;
   13738   else
   13739     stub_group_size = htab->params->group_size;
   13740 
   13741   if (!group_sections (info, stub_group_size, stubs_always_before_branch))
   13742     return false;
   13743 
   13744   htab->tga_group = NULL;
   13745   if (!htab->params->no_tls_get_addr_regsave
   13746       && htab->tga_desc_fd != NULL
   13747       && (htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefined
   13748 	  || htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefweak)
   13749       && htab->tls_get_addr_fd != NULL
   13750       && is_static_defined (&htab->tls_get_addr_fd->elf))
   13751     {
   13752       asection *sym_sec, *code_sec, *stub_sec;
   13753       bfd_vma sym_value;
   13754       struct _opd_sec_data *opd;
   13755 
   13756       sym_sec = htab->tls_get_addr_fd->elf.root.u.def.section;
   13757       sym_value = defined_sym_val (&htab->tls_get_addr_fd->elf);
   13758       code_sec = sym_sec;
   13759       opd = get_opd_info (sym_sec);
   13760       if (opd != NULL)
   13761 	opd_entry_value (sym_sec, sym_value, &code_sec, NULL, false);
   13762       htab->tga_group = htab->sec_info[code_sec->id].u.group;
   13763       stub_sec = (*htab->params->add_stub_section) (".tga_desc.stub",
   13764 						    htab->tga_group->link_sec);
   13765       if (stub_sec == NULL)
   13766 	return false;
   13767       htab->tga_group->stub_sec = stub_sec;
   13768 
   13769       htab->tga_desc_fd->elf.root.type = bfd_link_hash_defined;
   13770       htab->tga_desc_fd->elf.root.u.def.section = stub_sec;
   13771       htab->tga_desc_fd->elf.root.u.def.value = 0;
   13772       htab->tga_desc_fd->elf.type = STT_FUNC;
   13773       htab->tga_desc_fd->elf.def_regular = 1;
   13774       htab->tga_desc_fd->elf.non_elf = 0;
   13775       _bfd_elf_link_hash_hide_symbol (info, &htab->tga_desc_fd->elf, true);
   13776     }
   13777 
   13778   /* Loop until no stubs added.  After iteration 20 of this loop we may
   13779      exit on a stub section shrinking.  */
   13780 
   13781   while (1)
   13782     {
   13783       bfd *input_bfd;
   13784       unsigned int bfd_indx;
   13785       struct map_stub *group;
   13786 
   13787       htab->stub_iteration += 1;
   13788       htab->relr_count = 0;
   13789 
   13790       for (input_bfd = info->input_bfds, bfd_indx = 0;
   13791 	   input_bfd != NULL;
   13792 	   input_bfd = input_bfd->link.next, bfd_indx++)
   13793 	{
   13794 	  Elf_Internal_Shdr *symtab_hdr;
   13795 	  asection *section;
   13796 	  Elf_Internal_Sym *local_syms = NULL;
   13797 
   13798 	  if (!is_ppc64_elf (input_bfd))
   13799 	    continue;
   13800 
   13801 	  /* We'll need the symbol table in a second.  */
   13802 	  symtab_hdr = &elf_symtab_hdr (input_bfd);
   13803 	  if (symtab_hdr->sh_info == 0)
   13804 	    continue;
   13805 
   13806 	  /* Walk over each section attached to the input bfd.  */
   13807 	  for (section = input_bfd->sections;
   13808 	       section != NULL;
   13809 	       section = section->next)
   13810 	    {
   13811 	      Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
   13812 	      bool is_opd;
   13813 
   13814 	      /* If there aren't any relocs, then there's nothing more
   13815 		 to do.  */
   13816 	      if ((section->flags & SEC_RELOC) == 0
   13817 		  || (section->flags & SEC_ALLOC) == 0
   13818 		  || (section->flags & SEC_LOAD) == 0
   13819 		  || section->reloc_count == 0)
   13820 		continue;
   13821 
   13822 	      if (!info->enable_dt_relr
   13823 		  && (section->flags & SEC_CODE) == 0)
   13824 		continue;
   13825 
   13826 	      /* If this section is a link-once section that will be
   13827 		 discarded, then don't create any stubs.  */
   13828 	      if (section->output_section == NULL
   13829 		  || section->output_section->owner != info->output_bfd)
   13830 		continue;
   13831 
   13832 	      /* Get the relocs.  */
   13833 	      internal_relocs
   13834 		= _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
   13835 					     info->keep_memory);
   13836 	      if (internal_relocs == NULL)
   13837 		goto error_ret_free_local;
   13838 
   13839 	      is_opd = ppc64_elf_section_data (section)->sec_type == sec_opd;
   13840 
   13841 	      /* Now examine each relocation.  */
   13842 	      irela = internal_relocs;
   13843 	      irelaend = irela + section->reloc_count;
   13844 	      for (; irela < irelaend; irela++)
   13845 		{
   13846 		  enum elf_ppc64_reloc_type r_type;
   13847 		  unsigned int r_indx;
   13848 		  struct ppc_stub_type stub_type;
   13849 		  struct ppc_stub_hash_entry *stub_entry;
   13850 		  asection *sym_sec, *code_sec;
   13851 		  bfd_vma sym_value, code_value;
   13852 		  bfd_vma destination;
   13853 		  unsigned long local_off;
   13854 		  bool ok_dest;
   13855 		  struct ppc_link_hash_entry *hash;
   13856 		  struct ppc_link_hash_entry *fdh;
   13857 		  struct elf_link_hash_entry *h;
   13858 		  Elf_Internal_Sym *sym;
   13859 		  char *stub_name;
   13860 		  const asection *id_sec;
   13861 		  struct _opd_sec_data *opd;
   13862 		  struct plt_entry *plt_ent;
   13863 
   13864 		  r_type = ELF64_R_TYPE (irela->r_info);
   13865 		  r_indx = ELF64_R_SYM (irela->r_info);
   13866 
   13867 		  if (r_type >= R_PPC64_max)
   13868 		    {
   13869 		      bfd_set_error (bfd_error_bad_value);
   13870 		      goto error_ret_free_internal;
   13871 		    }
   13872 
   13873 		  /* Only look for stubs on branch instructions.  */
   13874 		  switch (r_type)
   13875 		    {
   13876 		    default:
   13877 		      continue;
   13878 
   13879 		    case R_PPC64_REL24:
   13880 		    case R_PPC64_REL24_NOTOC:
   13881 		    case R_PPC64_REL24_P9NOTOC:
   13882 		    case R_PPC64_REL14:
   13883 		    case R_PPC64_REL14_BRTAKEN:
   13884 		    case R_PPC64_REL14_BRNTAKEN:
   13885 		      if ((section->flags & SEC_CODE) != 0)
   13886 			break;
   13887 		      continue;
   13888 
   13889 		    case R_PPC64_ADDR64:
   13890 		    case R_PPC64_TOC:
   13891 		      if (info->enable_dt_relr
   13892 			  && irela->r_offset % 2 == 0
   13893 			  && section->alignment_power != 0)
   13894 			break;
   13895 		      continue;
   13896 		    }
   13897 
   13898 		  /* Now determine the call target, its name, value,
   13899 		     section.  */
   13900 		  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   13901 				  r_indx, input_bfd))
   13902 		    goto error_ret_free_internal;
   13903 
   13904 		  if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
   13905 		    {
   13906 		      /* Only locally defined symbols can possibly use
   13907 			 relative relocations.  */
   13908 		      bfd_vma r_offset;
   13909 		      if ((sym_sec == NULL
   13910 			   || sym_sec->output_section == NULL)
   13911 			  /* No symbol is OK too.  */
   13912 			  && !(sym != NULL && sym->st_shndx == 0)
   13913 			  /* Hack for __ehdr_start, which is undefined
   13914 			     at this point.  */
   13915 			  && !(h != NULL && h->root.linker_def))
   13916 			continue;
   13917 		      if (NO_OPD_RELOCS && is_opd)
   13918 			continue;
   13919 		      if (!is_opd
   13920 			  && r_type == R_PPC64_ADDR64)
   13921 			{
   13922 			  if (h != NULL
   13923 			      ? h->type == STT_GNU_IFUNC
   13924 			      : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
   13925 			    continue;
   13926 			  if (h != NULL
   13927 			      ? bfd_is_abs_symbol (&h->root)
   13928 			      : sym->st_shndx == SHN_ABS)
   13929 			    continue;
   13930 			  if (h != NULL
   13931 			      && !SYMBOL_REFERENCES_LOCAL (info, h))
   13932 			    continue;
   13933 			}
   13934 		      r_offset = _bfd_elf_section_offset (info->output_bfd,
   13935 							  info,
   13936 							  section,
   13937 							  irela->r_offset);
   13938 		      if (r_offset >= (bfd_vma) -2)
   13939 			continue;
   13940 		      if (!append_relr_off (htab, section, r_offset))
   13941 			goto error_ret_free_internal;
   13942 		      continue;
   13943 		    }
   13944 
   13945 		  hash = ppc_elf_hash_entry (h);
   13946 		  ok_dest = false;
   13947 		  fdh = NULL;
   13948 		  sym_value = 0;
   13949 		  if (hash == NULL)
   13950 		    {
   13951 		      sym_value = sym->st_value;
   13952 		      if (sym_sec != NULL
   13953 			  && sym_sec->output_section != NULL)
   13954 			ok_dest = true;
   13955 		    }
   13956 		  else if (hash->elf.root.type == bfd_link_hash_defined
   13957 			   || hash->elf.root.type == bfd_link_hash_defweak)
   13958 		    {
   13959 		      sym_value = hash->elf.root.u.def.value;
   13960 		      if (sym_sec->output_section != NULL)
   13961 			ok_dest = true;
   13962 		    }
   13963 		  else if (hash->elf.root.type == bfd_link_hash_undefweak
   13964 			   || hash->elf.root.type == bfd_link_hash_undefined)
   13965 		    {
   13966 		      /* Recognise an old ABI func code entry sym, and
   13967 			 use the func descriptor sym instead if it is
   13968 			 defined.  */
   13969 		      if (hash->elf.root.root.string[0] == '.'
   13970 			  && hash->oh != NULL)
   13971 			{
   13972 			  fdh = ppc_follow_link (hash->oh);
   13973 			  if (fdh->elf.root.type == bfd_link_hash_defined
   13974 			      || fdh->elf.root.type == bfd_link_hash_defweak)
   13975 			    {
   13976 			      sym_sec = fdh->elf.root.u.def.section;
   13977 			      sym_value = fdh->elf.root.u.def.value;
   13978 			      if (sym_sec->output_section != NULL)
   13979 				ok_dest = true;
   13980 			    }
   13981 			  else
   13982 			    fdh = NULL;
   13983 			}
   13984 		    }
   13985 		  else
   13986 		    {
   13987 		      bfd_set_error (bfd_error_bad_value);
   13988 		      goto error_ret_free_internal;
   13989 		    }
   13990 
   13991 		  destination = 0;
   13992 		  local_off = 0;
   13993 		  if (ok_dest)
   13994 		    {
   13995 		      sym_value += irela->r_addend;
   13996 		      destination = (sym_value
   13997 				     + sym_sec->output_offset
   13998 				     + sym_sec->output_section->vma);
   13999 		      local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
   14000 							    ? hash->elf.other
   14001 							    : sym->st_other);
   14002 		    }
   14003 
   14004 		  code_sec = sym_sec;
   14005 		  code_value = sym_value;
   14006 		  opd = get_opd_info (sym_sec);
   14007 		  if (opd != NULL)
   14008 		    {
   14009 		      bfd_vma dest;
   14010 
   14011 		      if (hash == NULL && opd->adjust != NULL)
   14012 			{
   14013 			  long adjust = opd->adjust[OPD_NDX (sym_value)];
   14014 			  if (adjust == -1)
   14015 			    continue;
   14016 			  code_value += adjust;
   14017 			  sym_value += adjust;
   14018 			}
   14019 		      dest = opd_entry_value (sym_sec, sym_value,
   14020 					      &code_sec, &code_value, false);
   14021 		      if (dest != (bfd_vma) -1)
   14022 			{
   14023 			  destination = dest;
   14024 			  if (fdh != NULL)
   14025 			    {
   14026 			      /* Fixup old ABI sym to point at code
   14027 				 entry.  */
   14028 			      hash->elf.root.type = bfd_link_hash_defweak;
   14029 			      hash->elf.root.u.def.section = code_sec;
   14030 			      hash->elf.root.u.def.value = code_value;
   14031 			    }
   14032 			}
   14033 		    }
   14034 
   14035 		  /* Determine what (if any) linker stub is needed.  */
   14036 		  plt_ent = NULL;
   14037 		  stub_type.main = ppc_type_of_stub (section, irela, &hash,
   14038 						     &plt_ent, destination,
   14039 						     local_off);
   14040 		  stub_type.sub = ppc_stub_toc;
   14041 		  stub_type.r2save = 0;
   14042 
   14043 		  if (r_type == R_PPC64_REL24_NOTOC
   14044 		      || r_type == R_PPC64_REL24_P9NOTOC)
   14045 		    {
   14046 		      enum ppc_stub_sub_type notoc = ppc_stub_notoc;
   14047 		      if (htab->params->power10_stubs == 0
   14048 			  || (r_type == R_PPC64_REL24_P9NOTOC
   14049 			      && htab->params->power10_stubs != 1))
   14050 			notoc = ppc_stub_p9notoc;
   14051 		      if (stub_type.main == ppc_stub_plt_call)
   14052 			stub_type.sub = notoc;
   14053 		      else if (stub_type.main == ppc_stub_long_branch
   14054 			       || (code_sec != NULL
   14055 				   && code_sec->output_section != NULL
   14056 				   && (((hash ? hash->elf.other : sym->st_other)
   14057 					& STO_PPC64_LOCAL_MASK)
   14058 				       > 1 << STO_PPC64_LOCAL_BIT)))
   14059 			{
   14060 			  stub_type.main = ppc_stub_long_branch;
   14061 			  stub_type.sub = notoc;
   14062 			  stub_type.r2save = 0;
   14063 			}
   14064 		    }
   14065 		  else if (stub_type.main != ppc_stub_plt_call)
   14066 		    {
   14067 		      /* Check whether we need a TOC adjusting stub.
   14068 			 Since the linker pastes together pieces from
   14069 			 different object files when creating the
   14070 			 _init and _fini functions, it may be that a
   14071 			 call to what looks like a local sym is in
   14072 			 fact a call needing a TOC adjustment.  */
   14073 		      if ((code_sec != NULL
   14074 			   && code_sec->output_section != NULL
   14075 			   && (code_sec->has_toc_reloc
   14076 			       || code_sec->makes_toc_func_call)
   14077 			   && (htab->sec_info[code_sec->id].toc_off
   14078 			       != htab->sec_info[section->id].toc_off))
   14079 			  || (((hash ? hash->elf.other : sym->st_other)
   14080 			       & STO_PPC64_LOCAL_MASK)
   14081 			      == 1 << STO_PPC64_LOCAL_BIT))
   14082 			{
   14083 			  stub_type.main = ppc_stub_long_branch;
   14084 			  stub_type.sub = ppc_stub_toc;
   14085 			  stub_type.r2save = 1;
   14086 			}
   14087 		    }
   14088 
   14089 		  if (stub_type.main == ppc_stub_none)
   14090 		    continue;
   14091 
   14092 		  /* __tls_get_addr calls might be eliminated.  */
   14093 		  if (stub_type.main != ppc_stub_plt_call
   14094 		      && hash != NULL
   14095 		      && is_tls_get_addr (&hash->elf, htab)
   14096 		      && section->has_tls_reloc
   14097 		      && irela != internal_relocs)
   14098 		    {
   14099 		      /* Get tls info.  */
   14100 		      unsigned char *tls_mask;
   14101 
   14102 		      if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
   14103 					 irela - 1, input_bfd))
   14104 			goto error_ret_free_internal;
   14105 		      if ((*tls_mask & TLS_TLS) != 0
   14106 			  && (*tls_mask & (TLS_GD | TLS_LD)) == 0)
   14107 			continue;
   14108 		    }
   14109 
   14110 		  if (stub_type.main == ppc_stub_plt_call
   14111 		      && stub_type.sub == ppc_stub_toc)
   14112 		    {
   14113 		      if (!htab->opd_abi
   14114 			  && htab->params->plt_localentry0 != 0
   14115 			  && is_elfv2_localentry0 (&hash->elf))
   14116 			htab->has_plt_localentry0 = 1;
   14117 		      else if (irela + 1 < irelaend
   14118 			       && irela[1].r_offset == irela->r_offset + 4
   14119 			       && (ELF64_R_TYPE (irela[1].r_info)
   14120 				   == R_PPC64_TOCSAVE))
   14121 			{
   14122 			  if (!tocsave_find (htab, INSERT,
   14123 					     &local_syms, irela + 1, input_bfd))
   14124 			    goto error_ret_free_internal;
   14125 			}
   14126 		      else
   14127 			stub_type.r2save = 1;
   14128 		    }
   14129 
   14130 		  /* Support for grouping stub sections.  */
   14131 		  id_sec = htab->sec_info[section->id].u.group->link_sec;
   14132 
   14133 		  /* Get the name of this stub.  */
   14134 		  stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
   14135 		  if (!stub_name)
   14136 		    goto error_ret_free_internal;
   14137 
   14138 		  stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
   14139 						     stub_name, false, false);
   14140 		  if (stub_entry != NULL)
   14141 		    {
   14142 		      free (stub_name);
   14143 		      if (!ppc_merge_stub (htab, stub_entry, stub_type, r_type))
   14144 			{
   14145 			  /* xgettext:c-format */
   14146 			  _bfd_error_handler
   14147 			    (_("%pB: cannot create stub entry %s"),
   14148 			     section->owner, stub_entry->root.string);
   14149 			  goto error_ret_free_internal;
   14150 			}
   14151 		      continue;
   14152 		    }
   14153 
   14154 		  stub_entry = ppc_add_stub (stub_name, section, info);
   14155 		  if (stub_entry == NULL)
   14156 		    {
   14157 		      free (stub_name);
   14158 		    error_ret_free_internal:
   14159 		      if (elf_section_data (section)->relocs == NULL)
   14160 			free (internal_relocs);
   14161 		    error_ret_free_local:
   14162 		      if (symtab_hdr->contents
   14163 			  != (unsigned char *) local_syms)
   14164 			free (local_syms);
   14165 		      return false;
   14166 		    }
   14167 
   14168 		  stub_entry->type = stub_type;
   14169 		  if (stub_type.main == ppc_stub_plt_call)
   14170 		    {
   14171 		      stub_entry->target_value = sym_value;
   14172 		      stub_entry->target_section = sym_sec;
   14173 		    }
   14174 		  else
   14175 		    {
   14176 		      stub_entry->target_value = code_value;
   14177 		      stub_entry->target_section = code_sec;
   14178 		    }
   14179 		  stub_entry->h = hash;
   14180 		  stub_entry->plt_ent = plt_ent;
   14181 		  stub_entry->symtype
   14182 		    = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
   14183 		  stub_entry->other = hash ? hash->elf.other : sym->st_other;
   14184 
   14185 		  if (hash != NULL
   14186 		      && (hash->elf.root.type == bfd_link_hash_defined
   14187 			  || hash->elf.root.type == bfd_link_hash_defweak))
   14188 		    htab->stub_globals += 1;
   14189 		}
   14190 
   14191 	      /* We're done with the internal relocs, free them.  */
   14192 	      if (elf_section_data (section)->relocs != internal_relocs)
   14193 		free (internal_relocs);
   14194 	    }
   14195 
   14196 	  if (local_syms != NULL
   14197 	      && symtab_hdr->contents != (unsigned char *) local_syms)
   14198 	    {
   14199 	      if (!info->keep_memory)
   14200 		free (local_syms);
   14201 	      else
   14202 		symtab_hdr->contents = (unsigned char *) local_syms;
   14203 	    }
   14204 	}
   14205 
   14206       /* We may have added some stubs.  Find out the new size of the
   14207 	 stub sections.  */
   14208       for (group = htab->group; group != NULL; group = group->next)
   14209 	{
   14210 	  group->lr_restore = 0;
   14211 	  group->eh_size = 0;
   14212 	  if (group->stub_sec != NULL)
   14213 	    {
   14214 	      asection *stub_sec = group->stub_sec;
   14215 
   14216 	      stub_sec->rawsize = stub_sec->size;
   14217 	      stub_sec->size = 0;
   14218 	      stub_sec->reloc_count = 0;
   14219 	      stub_sec->flags &= ~SEC_RELOC;
   14220 	    }
   14221 	}
   14222       if (htab->tga_group != NULL)
   14223 	{
   14224 	  /* See emit_tga_desc and emit_tga_desc_eh_frame.  */
   14225 	  htab->tga_group->eh_size
   14226 	    = 1 + 2 + (htab->opd_abi != 0) + 3 + 8 * 2 + 3 + 8 + 3;
   14227 	  htab->tga_group->lr_restore = 23 * 4;
   14228 	  htab->tga_group->stub_sec->size = 24 * 4;
   14229 	}
   14230 
   14231       htab->brlt->rawsize = htab->brlt->size;
   14232       htab->brlt->size = 0;
   14233       htab->brlt->reloc_count = 0;
   14234       htab->brlt->flags &= ~SEC_RELOC;
   14235       if (htab->relbrlt != NULL)
   14236 	htab->relbrlt->size = 0;
   14237 
   14238       if (htab->elf.srelrdyn != NULL)
   14239 	{
   14240 	  htab->elf.srelrdyn->rawsize = htab->elf.srelrdyn->size;
   14241 	  htab->elf.srelrdyn->size = 0;
   14242 	}
   14243 
   14244       htab->stub_changed = false;
   14245       htab->stub_id = 0;
   14246       bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
   14247 
   14248       for (group = htab->group; group != NULL; group = group->next)
   14249 	if (group->needs_save_res)
   14250 	  group->stub_sec->size += htab->sfpr->size;
   14251 
   14252       if (info->emitrelocations
   14253 	  && htab->glink != NULL && htab->glink->size != 0)
   14254 	{
   14255 	  htab->glink->reloc_count = 1;
   14256 	  htab->glink->flags |= SEC_RELOC;
   14257 	}
   14258 
   14259       if (htab->glink_eh_frame != NULL
   14260 	  && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
   14261 	  && htab->glink_eh_frame->output_section->size > 8)
   14262 	{
   14263 	  size_t size = 0, align = 4;
   14264 
   14265 	  for (group = htab->group; group != NULL; group = group->next)
   14266 	    if (group->eh_size != 0)
   14267 	      size += (group->eh_size + 17 + align - 1) & -align;
   14268 	  if (htab->glink != NULL && htab->glink->size != 0)
   14269 	    size += (24 + align - 1) & -align;
   14270 	  if (size != 0)
   14271 	    size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
   14272 	  align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
   14273 	  size = (size + align - 1) & -align;
   14274 	  htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
   14275 	  htab->glink_eh_frame->size = size;
   14276 	}
   14277 
   14278       if (htab->params->plt_stub_align != 0)
   14279 	for (group = htab->group; group != NULL; group = group->next)
   14280 	  if (group->stub_sec != NULL)
   14281 	    {
   14282 	      int align = abs (htab->params->plt_stub_align);
   14283 	      group->stub_sec->size
   14284 		= (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
   14285 	    }
   14286 
   14287       if (htab->elf.srelrdyn != NULL)
   14288 	{
   14289 	  bfd_vma r_offset;
   14290 
   14291 	  for (r_offset = 0; r_offset < htab->brlt->size; r_offset += 8)
   14292 	    if (!append_relr_off (htab, htab->brlt, r_offset))
   14293 	      return false;
   14294 
   14295 	  if (!got_and_plt_relr_for_local_syms (info))
   14296 	    return false;
   14297 	  elf_link_hash_traverse (&htab->elf, got_and_plt_relr, info);
   14298 	  if (htab->stub_error)
   14299 	    return false;
   14300 
   14301 	  bfd_vma *relr_addr = sort_relr (htab);
   14302 	  if (htab->relr_count != 0 && relr_addr == NULL)
   14303 	    return false;
   14304 
   14305 	  size_t i = 0;
   14306 	  while (i < htab->relr_count)
   14307 	    {
   14308 	      bfd_vma base = relr_addr[i];
   14309 	      htab->elf.srelrdyn->size += 8;
   14310 	      i++;
   14311 	      /* Handle possible duplicate address.  This can happen
   14312 		 as sections increase in size when adding stubs.  */
   14313 	      while (i < htab->relr_count
   14314 		     && relr_addr[i] == base)
   14315 		i++;
   14316 	      base += 8;
   14317 	      while (1)
   14318 		{
   14319 		  size_t start_i = i;
   14320 		  while (i < htab->relr_count
   14321 			 && relr_addr[i] - base < 63 * 8
   14322 			 && (relr_addr[i] - base) % 8 == 0)
   14323 		    i++;
   14324 		  if (i == start_i)
   14325 		    break;
   14326 		  htab->elf.srelrdyn->size += 8;
   14327 		  base += 63 * 8;
   14328 		}
   14329 	    }
   14330 	  free (relr_addr);
   14331 	}
   14332 
   14333       for (group = htab->group; group != NULL; group = group->next)
   14334 	if (group->stub_sec != NULL
   14335 	    && group->stub_sec->rawsize != group->stub_sec->size
   14336 	    && (htab->stub_iteration <= STUB_SHRINK_ITER
   14337 		|| group->stub_sec->rawsize < group->stub_sec->size))
   14338 	  break;
   14339 
   14340       if (group == NULL
   14341 	  && (!htab->stub_changed
   14342 	      || htab->stub_iteration > STUB_SHRINK_ITER)
   14343 	  && (htab->brlt->rawsize == htab->brlt->size
   14344 	      || (htab->stub_iteration > STUB_SHRINK_ITER
   14345 		  && htab->brlt->rawsize > htab->brlt->size))
   14346 	  && (htab->elf.srelrdyn == NULL
   14347 	      || htab->elf.srelrdyn->rawsize == htab->elf.srelrdyn->size
   14348 	      || (htab->stub_iteration > STUB_SHRINK_ITER
   14349 		  && htab->elf.srelrdyn->rawsize > htab->elf.srelrdyn->size))
   14350 	  && (htab->glink_eh_frame == NULL
   14351 	      || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size)
   14352 	  && (htab->tga_group == NULL
   14353 	      || htab->stub_iteration > 1))
   14354 	break;
   14355 
   14356       if (htab->stub_iteration > STUB_SHRINK_ITER)
   14357 	{
   14358 	  for (group = htab->group; group != NULL; group = group->next)
   14359 	    if (group->stub_sec != NULL
   14360 		&& group->stub_sec->size < group->stub_sec->rawsize)
   14361 	      group->stub_sec->size = group->stub_sec->rawsize;
   14362 
   14363 	  if (htab->brlt->size < htab->brlt->rawsize)
   14364 	    htab->brlt->size = htab->brlt->rawsize;
   14365 
   14366 	  if (htab->elf.srelrdyn != NULL
   14367 	      && htab->elf.srelrdyn->size < htab->elf.srelrdyn->rawsize)
   14368 	    htab->elf.srelrdyn->size = htab->elf.srelrdyn->rawsize;
   14369 	}
   14370 
   14371       /* Ask the linker to do its stuff.  */
   14372       (*htab->params->layout_sections_again) ();
   14373     }
   14374 
   14375   if (htab->glink_eh_frame != NULL
   14376       && htab->glink_eh_frame->size != 0)
   14377     {
   14378       bfd_vma val;
   14379       bfd_byte *p, *last_fde;
   14380       size_t last_fde_len, size, align, pad;
   14381       struct map_stub *group;
   14382 
   14383       /* It is necessary to at least have a rough outline of the
   14384 	 linker generated CIEs and FDEs written before
   14385 	 bfd_elf_discard_info is run, in order for these FDEs to be
   14386 	 indexed in .eh_frame_hdr.  */
   14387       p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
   14388       if (p == NULL)
   14389 	return false;
   14390       htab->glink_eh_frame->contents = p;
   14391       last_fde = p;
   14392       align = 4;
   14393 
   14394       memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
   14395       /* CIE length (rewrite in case little-endian).  */
   14396       last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
   14397       bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
   14398       p += last_fde_len + 4;
   14399 
   14400       for (group = htab->group; group != NULL; group = group->next)
   14401 	if (group->eh_size != 0)
   14402 	  {
   14403 	    group->eh_base = p - htab->glink_eh_frame->contents;
   14404 	    last_fde = p;
   14405 	    last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
   14406 	    /* FDE length.  */
   14407 	    bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
   14408 	    p += 4;
   14409 	    /* CIE pointer.  */
   14410 	    val = p - htab->glink_eh_frame->contents;
   14411 	    bfd_put_32 (htab->elf.dynobj, val, p);
   14412 	    p += 4;
   14413 	    /* Offset to stub section, written later.  */
   14414 	    p += 4;
   14415 	    /* stub section size.  */
   14416 	    bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
   14417 	    p += 4;
   14418 	    /* Augmentation.  */
   14419 	    p += 1;
   14420 	    /* Make sure we don't have all nops.  This is enough for
   14421 	       elf-eh-frame.c to detect the last non-nop opcode.  */
   14422 	    p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
   14423 	    p = last_fde + last_fde_len + 4;
   14424 	  }
   14425       if (htab->glink != NULL && htab->glink->size != 0)
   14426 	{
   14427 	  last_fde = p;
   14428 	  last_fde_len = ((24 + align - 1) & -align) - 4;
   14429 	  /* FDE length.  */
   14430 	  bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
   14431 	  p += 4;
   14432 	  /* CIE pointer.  */
   14433 	  val = p - htab->glink_eh_frame->contents;
   14434 	  bfd_put_32 (htab->elf.dynobj, val, p);
   14435 	  p += 4;
   14436 	  /* Offset to .glink, written later.  */
   14437 	  p += 4;
   14438 	  /* .glink size.  */
   14439 	  bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
   14440 	  p += 4;
   14441 	  /* Augmentation.  */
   14442 	  p += 1;
   14443 
   14444 	  *p++ = DW_CFA_advance_loc + (htab->has_plt_localentry0 ? 3 : 2);
   14445 	  *p++ = DW_CFA_register;
   14446 	  *p++ = 65;
   14447 	  *p++ = htab->opd_abi ? 12 : 0;
   14448 	  *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 4 : 2);
   14449 	  *p++ = DW_CFA_restore_extended;
   14450 	  *p++ = 65;
   14451 	  p += ((24 + align - 1) & -align) - 24;
   14452 	}
   14453       /* Subsume any padding into the last FDE if user .eh_frame
   14454 	 sections are aligned more than glink_eh_frame.  Otherwise any
   14455 	 zero padding will be seen as a terminator.  */
   14456       align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
   14457       size = p - htab->glink_eh_frame->contents;
   14458       pad = ((size + align - 1) & -align) - size;
   14459       htab->glink_eh_frame->size = size + pad;
   14460       bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
   14461     }
   14462 
   14463   maybe_strip_output (info, htab->brlt);
   14464   if (htab->relbrlt != NULL)
   14465     maybe_strip_output (info, htab->relbrlt);
   14466   if (htab->glink_eh_frame != NULL)
   14467     maybe_strip_output (info, htab->glink_eh_frame);
   14468   if (htab->elf.srelrdyn != NULL)
   14469     maybe_strip_output (info, htab->elf.srelrdyn);
   14470 
   14471   return true;
   14472 }
   14473 
   14474 /* Called after we have determined section placement.  If sections
   14475    move, we'll be called again.  Provide a value for TOCstart.  */
   14476 
   14477 bfd_vma
   14478 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
   14479 {
   14480   asection *s;
   14481   bfd_vma TOCstart, adjust;
   14482 
   14483   if (info != NULL)
   14484     {
   14485       struct elf_link_hash_entry *h;
   14486       struct elf_link_hash_table *htab = elf_hash_table (info);
   14487 
   14488       if (is_elf_hash_table (&htab->root)
   14489 	  && htab->hgot != NULL)
   14490 	h = htab->hgot;
   14491       else
   14492 	{
   14493 	  h = (struct elf_link_hash_entry *)
   14494 	    bfd_link_hash_lookup (&htab->root, ".TOC.", false, false, true);
   14495 	  if (is_elf_hash_table (&htab->root))
   14496 	    htab->hgot = h;
   14497 	}
   14498       if (h != NULL
   14499 	  && h->root.type == bfd_link_hash_defined
   14500 	  && !h->root.linker_def
   14501 	  && (!is_elf_hash_table (&htab->root)
   14502 	      || h->def_regular))
   14503 	{
   14504 	  TOCstart = defined_sym_val (h) - TOC_BASE_OFF;
   14505 	  _bfd_set_gp_value (obfd, TOCstart);
   14506 	  return TOCstart;
   14507 	}
   14508     }
   14509 
   14510   /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
   14511      order.  The TOC starts where the first of these sections starts.  */
   14512   s = bfd_get_section_by_name (obfd, ".got");
   14513   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
   14514     s = bfd_get_section_by_name (obfd, ".toc");
   14515   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
   14516     s = bfd_get_section_by_name (obfd, ".tocbss");
   14517   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
   14518     s = bfd_get_section_by_name (obfd, ".plt");
   14519   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
   14520     {
   14521       /* This may happen for
   14522 	 o  references to TOC base (SYM@toc / TOC[tc0]) without a
   14523 	 .toc directive
   14524 	 o  bad linker script
   14525 	 o --gc-sections and empty TOC sections
   14526 
   14527 	 FIXME: Warn user?  */
   14528 
   14529       /* Look for a likely section.  We probably won't even be
   14530 	 using TOCstart.  */
   14531       for (s = obfd->sections; s != NULL; s = s->next)
   14532 	if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
   14533 			 | SEC_EXCLUDE))
   14534 	    == (SEC_ALLOC | SEC_SMALL_DATA))
   14535 	  break;
   14536       if (s == NULL)
   14537 	for (s = obfd->sections; s != NULL; s = s->next)
   14538 	  if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
   14539 	      == (SEC_ALLOC | SEC_SMALL_DATA))
   14540 	    break;
   14541       if (s == NULL)
   14542 	for (s = obfd->sections; s != NULL; s = s->next)
   14543 	  if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
   14544 	      == SEC_ALLOC)
   14545 	    break;
   14546       if (s == NULL)
   14547 	for (s = obfd->sections; s != NULL; s = s->next)
   14548 	  if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
   14549 	    break;
   14550     }
   14551 
   14552   TOCstart = 0;
   14553   if (s != NULL)
   14554     TOCstart = s->output_section->vma + s->output_offset;
   14555 
   14556   /* Force alignment.  */
   14557   adjust = TOCstart & (TOC_BASE_ALIGN - 1);
   14558   TOCstart -= adjust;
   14559   _bfd_set_gp_value (obfd, TOCstart);
   14560 
   14561   if (info != NULL && s != NULL)
   14562     {
   14563       struct ppc_link_hash_table *htab = ppc_hash_table (info);
   14564 
   14565       if (htab != NULL)
   14566 	{
   14567 	  if (htab->elf.hgot != NULL)
   14568 	    {
   14569 	      htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
   14570 	      htab->elf.hgot->root.u.def.section = s;
   14571 	    }
   14572 	}
   14573       else
   14574 	{
   14575 	  struct bfd_link_hash_entry *bh = NULL;
   14576 	  _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
   14577 					    s, TOC_BASE_OFF - adjust,
   14578 					    NULL, false, false, &bh);
   14579 	}
   14580     }
   14581   return TOCstart;
   14582 }
   14583 
   14584 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
   14585    write out any global entry stubs, and PLT relocations.  */
   14586 
   14587 static bool
   14588 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
   14589 {
   14590   struct bfd_link_info *info;
   14591   struct ppc_link_hash_table *htab;
   14592   struct plt_entry *ent;
   14593   asection *s;
   14594 
   14595   if (h->root.type == bfd_link_hash_indirect)
   14596     return true;
   14597 
   14598   info = inf;
   14599   htab = ppc_hash_table (info);
   14600   if (htab == NULL)
   14601     return false;
   14602 
   14603   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
   14604     if (ent->plt.offset != (bfd_vma) -1)
   14605       {
   14606 	/* This symbol has an entry in the procedure linkage
   14607 	   table.  Set it up.  */
   14608 	Elf_Internal_Rela rela;
   14609 	asection *plt, *relplt;
   14610 	bfd_byte *loc;
   14611 
   14612 	if (use_local_plt (info, h))
   14613 	  {
   14614 	    if (!(h->def_regular
   14615 		  && (h->root.type == bfd_link_hash_defined
   14616 		      || h->root.type == bfd_link_hash_defweak)))
   14617 	      continue;
   14618 	    if (h->type == STT_GNU_IFUNC)
   14619 	      {
   14620 		plt = htab->elf.iplt;
   14621 		relplt = htab->elf.irelplt;
   14622 		htab->elf.ifunc_resolvers = true;
   14623 		if (htab->opd_abi)
   14624 		  rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
   14625 		else
   14626 		  rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
   14627 	      }
   14628 	    else
   14629 	      {
   14630 		plt = htab->pltlocal;
   14631 		relplt = NULL;
   14632 		if (bfd_link_pic (info)
   14633 		    && !(info->enable_dt_relr && !htab->opd_abi))
   14634 		  {
   14635 		    relplt = htab->relpltlocal;
   14636 		    if (htab->opd_abi)
   14637 		      rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
   14638 		    else
   14639 		      rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   14640 		  }
   14641 	      }
   14642 	    rela.r_addend = defined_sym_val (h) + ent->addend;
   14643 
   14644 	    if (relplt == NULL)
   14645 	      {
   14646 		loc = plt->contents + ent->plt.offset;
   14647 		bfd_put_64 (info->output_bfd, rela.r_addend, loc);
   14648 		if (htab->opd_abi)
   14649 		  {
   14650 		    bfd_vma toc = elf_gp (info->output_bfd);
   14651 		    toc += htab->sec_info[h->root.u.def.section->id].toc_off;
   14652 		    bfd_put_64 (info->output_bfd, toc, loc + 8);
   14653 		  }
   14654 	      }
   14655 	    else
   14656 	      {
   14657 		rela.r_offset = (plt->output_section->vma
   14658 				 + plt->output_offset
   14659 				 + ent->plt.offset);
   14660 		loc = relplt->contents + (relplt->reloc_count++
   14661 					  * sizeof (Elf64_External_Rela));
   14662 		bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
   14663 	      }
   14664 	  }
   14665 	else
   14666 	  {
   14667 	    rela.r_offset = (htab->elf.splt->output_section->vma
   14668 			     + htab->elf.splt->output_offset
   14669 			     + ent->plt.offset);
   14670 	    rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
   14671 	    rela.r_addend = ent->addend;
   14672 	    loc = (htab->elf.srelplt->contents
   14673 		   + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
   14674 		      / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
   14675 	    if (h->type == STT_GNU_IFUNC && is_static_defined (h))
   14676 	      htab->elf.ifunc_resolvers = true;
   14677 	    bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
   14678 	  }
   14679       }
   14680 
   14681   if (!h->pointer_equality_needed)
   14682     return true;
   14683 
   14684   if (h->def_regular)
   14685     return true;
   14686 
   14687   s = htab->global_entry;
   14688   if (s == NULL || s->size == 0)
   14689     return true;
   14690 
   14691   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
   14692     if (ent->plt.offset != (bfd_vma) -1
   14693 	&& ent->addend == 0)
   14694       {
   14695 	bfd_byte *p;
   14696 	asection *plt;
   14697 	bfd_vma off;
   14698 
   14699 	p = s->contents + h->root.u.def.value;
   14700 	plt = htab->elf.splt;
   14701 	if (use_local_plt (info, h))
   14702 	  {
   14703 	    if (h->type == STT_GNU_IFUNC)
   14704 	      plt = htab->elf.iplt;
   14705 	    else
   14706 	      plt = htab->pltlocal;
   14707 	  }
   14708 	off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
   14709 	off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
   14710 
   14711 	if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
   14712 	  {
   14713 	    info->callbacks->einfo
   14714 	      (_("%P: linkage table error against `%pT'\n"),
   14715 	       h->root.root.string);
   14716 	    bfd_set_error (bfd_error_bad_value);
   14717 	    htab->stub_error = true;
   14718 	  }
   14719 
   14720 	htab->stub_count[ppc_stub_global_entry - 1] += 1;
   14721 	if (htab->params->emit_stub_syms)
   14722 	  {
   14723 	    size_t len = strlen (h->root.root.string);
   14724 	    char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
   14725 
   14726 	    if (name == NULL)
   14727 	      return false;
   14728 
   14729 	    sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
   14730 	    h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
   14731 	    if (h == NULL)
   14732 	      return false;
   14733 	    if (h->root.type == bfd_link_hash_new)
   14734 	      {
   14735 		h->root.type = bfd_link_hash_defined;
   14736 		h->root.u.def.section = s;
   14737 		h->root.u.def.value = p - s->contents;
   14738 		h->ref_regular = 1;
   14739 		h->def_regular = 1;
   14740 		h->ref_regular_nonweak = 1;
   14741 		h->forced_local = 1;
   14742 		h->non_elf = 0;
   14743 		h->root.linker_def = 1;
   14744 	      }
   14745 	  }
   14746 
   14747 	if (PPC_HA (off) != 0)
   14748 	  {
   14749 	    bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
   14750 	    p += 4;
   14751 	  }
   14752 	bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
   14753 	p += 4;
   14754 	bfd_put_32 (s->owner, MTCTR_R12, p);
   14755 	p += 4;
   14756 	bfd_put_32 (s->owner, BCTR, p);
   14757 	break;
   14758       }
   14759   return true;
   14760 }
   14761 
   14762 /* Write PLT relocs for locals.  */
   14763 
   14764 static bool
   14765 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
   14766 {
   14767   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   14768   bfd *ibfd;
   14769 
   14770   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   14771     {
   14772       struct got_entry **lgot_ents, **end_lgot_ents;
   14773       struct plt_entry **local_plt, **lplt, **end_local_plt;
   14774       Elf_Internal_Shdr *symtab_hdr;
   14775       bfd_size_type locsymcount;
   14776       Elf_Internal_Sym *local_syms = NULL;
   14777       struct plt_entry *ent;
   14778 
   14779       if (!is_ppc64_elf (ibfd))
   14780 	continue;
   14781 
   14782       lgot_ents = elf_local_got_ents (ibfd);
   14783       if (!lgot_ents)
   14784 	continue;
   14785 
   14786       symtab_hdr = &elf_symtab_hdr (ibfd);
   14787       locsymcount = symtab_hdr->sh_info;
   14788       end_lgot_ents = lgot_ents + locsymcount;
   14789       local_plt = (struct plt_entry **) end_lgot_ents;
   14790       end_local_plt = local_plt + locsymcount;
   14791       for (lplt = local_plt; lplt < end_local_plt; ++lplt)
   14792 	for (ent = *lplt; ent != NULL; ent = ent->next)
   14793 	  if (ent->plt.offset != (bfd_vma) -1)
   14794 	    {
   14795 	      Elf_Internal_Sym *sym;
   14796 	      asection *sym_sec;
   14797 	      asection *plt, *relplt;
   14798 	      bfd_byte *loc;
   14799 	      bfd_vma val;
   14800 
   14801 	      if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
   14802 			      lplt - local_plt, ibfd))
   14803 		{
   14804 		  if (symtab_hdr->contents != (unsigned char *) local_syms)
   14805 		    free (local_syms);
   14806 		  return false;
   14807 		}
   14808 
   14809 	      val = sym->st_value + ent->addend;
   14810 	      if (sym_sec != NULL && sym_sec->output_section != NULL)
   14811 		val += sym_sec->output_offset + sym_sec->output_section->vma;
   14812 
   14813 	      if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
   14814 		{
   14815 		  htab->elf.ifunc_resolvers = true;
   14816 		  plt = htab->elf.iplt;
   14817 		  relplt = htab->elf.irelplt;
   14818 		}
   14819 	      else
   14820 		{
   14821 		  plt = htab->pltlocal;
   14822 		  relplt = NULL;
   14823 		  if (bfd_link_pic (info)
   14824 		      && !(info->enable_dt_relr && !htab->opd_abi))
   14825 		    relplt = htab->relpltlocal;
   14826 		}
   14827 
   14828 	      if (relplt == NULL)
   14829 		{
   14830 		  loc = plt->contents + ent->plt.offset;
   14831 		  bfd_put_64 (info->output_bfd, val, loc);
   14832 		  if (htab->opd_abi)
   14833 		    {
   14834 		      bfd_vma toc = elf_gp (ibfd);
   14835 		      bfd_put_64 (info->output_bfd, toc, loc + 8);
   14836 		    }
   14837 		}
   14838 	      else
   14839 		{
   14840 		  Elf_Internal_Rela rela;
   14841 		  rela.r_offset = (ent->plt.offset
   14842 				   + plt->output_offset
   14843 				   + plt->output_section->vma);
   14844 		  if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
   14845 		    {
   14846 		      if (htab->opd_abi)
   14847 			rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
   14848 		      else
   14849 			rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
   14850 		    }
   14851 		  else
   14852 		    {
   14853 		      if (htab->opd_abi)
   14854 			rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
   14855 		      else
   14856 			rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   14857 		    }
   14858 		  rela.r_addend = val;
   14859 		  loc = relplt->contents + (relplt->reloc_count++
   14860 					    * sizeof (Elf64_External_Rela));
   14861 		  bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
   14862 		}
   14863 	    }
   14864 
   14865       if (local_syms != NULL
   14866 	  && symtab_hdr->contents != (unsigned char *) local_syms)
   14867 	{
   14868 	  if (!info->keep_memory)
   14869 	    free (local_syms);
   14870 	  else
   14871 	    symtab_hdr->contents = (unsigned char *) local_syms;
   14872 	}
   14873     }
   14874   return true;
   14875 }
   14876 
   14877 /* Emit the static wrapper function preserving registers around a
   14878    __tls_get_addr_opt call.  */
   14879 
   14880 static bool
   14881 emit_tga_desc (struct ppc_link_hash_table *htab)
   14882 {
   14883   asection *stub_sec = htab->tga_group->stub_sec;
   14884   unsigned int cfa_updt = 11 * 4;
   14885   bfd_byte *p;
   14886   bfd_vma to, from, delta;
   14887 
   14888   BFD_ASSERT (htab->tga_desc_fd->elf.root.type == bfd_link_hash_defined
   14889 	      && htab->tga_desc_fd->elf.root.u.def.section == stub_sec
   14890 	      && htab->tga_desc_fd->elf.root.u.def.value == 0);
   14891   to = defined_sym_val (&htab->tls_get_addr_fd->elf);
   14892   from = defined_sym_val (&htab->tga_desc_fd->elf) + cfa_updt;
   14893   delta = to - from;
   14894   if (delta + (1 << 25) >= 1 << 26)
   14895     {
   14896       _bfd_error_handler (_("__tls_get_addr call offset overflow"));
   14897       htab->stub_error = true;
   14898       return false;
   14899     }
   14900 
   14901   p = stub_sec->contents;
   14902   p = tls_get_addr_prologue (htab->elf.dynobj, p, htab);
   14903   bfd_put_32 (stub_sec->owner, B_DOT | 1 | (delta & 0x3fffffc), p);
   14904   p += 4;
   14905   p = tls_get_addr_epilogue (htab->elf.dynobj, p, htab);
   14906   return stub_sec->size == (bfd_size_type) (p - stub_sec->contents);
   14907 }
   14908 
   14909 /* Emit eh_frame describing the static wrapper function.  */
   14910 
   14911 static bfd_byte *
   14912 emit_tga_desc_eh_frame (struct ppc_link_hash_table *htab, bfd_byte *p)
   14913 {
   14914   unsigned int cfa_updt = 11 * 4;
   14915   unsigned int i;
   14916 
   14917   *p++ = DW_CFA_advance_loc + cfa_updt / 4;
   14918   *p++ = DW_CFA_def_cfa_offset;
   14919   if (htab->opd_abi)
   14920     {
   14921       *p++ = 128;
   14922       *p++ = 1;
   14923     }
   14924   else
   14925     *p++ = 96;
   14926   *p++ = DW_CFA_offset_extended_sf;
   14927   *p++ = 65;
   14928   *p++ = (-16 / 8) & 0x7f;
   14929   for (i = 4; i < 12; i++)
   14930     {
   14931       *p++ = DW_CFA_offset + i;
   14932       *p++ = (htab->opd_abi ? 13 : 12) - i;
   14933     }
   14934   *p++ = DW_CFA_advance_loc + 10;
   14935   *p++ = DW_CFA_def_cfa_offset;
   14936   *p++ = 0;
   14937   for (i = 4; i < 12; i++)
   14938     *p++ = DW_CFA_restore + i;
   14939   *p++ = DW_CFA_advance_loc + 2;
   14940   *p++ = DW_CFA_restore_extended;
   14941   *p++ = 65;
   14942   return p;
   14943 }
   14944 
   14945 /* Build all the stubs associated with the current output file.
   14946    The stubs are kept in a hash table attached to the main linker
   14947    hash table.  This function is called via gldelf64ppc_finish.  */
   14948 
   14949 bool
   14950 ppc64_elf_build_stubs (struct bfd_link_info *info,
   14951 		       char **stats)
   14952 {
   14953   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   14954   struct map_stub *group;
   14955   asection *stub_sec;
   14956   bfd_byte *p;
   14957   int stub_sec_count = 0;
   14958 
   14959   if (htab == NULL)
   14960     return false;
   14961 
   14962   /* Allocate memory to hold the linker stubs.  */
   14963   for (group = htab->group; group != NULL; group = group->next)
   14964     {
   14965       group->eh_size = 0;
   14966       group->lr_restore = 0;
   14967       if ((stub_sec = group->stub_sec) != NULL
   14968 	  && stub_sec->size != 0)
   14969 	{
   14970 	  stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
   14971 					   stub_sec->size);
   14972 	  if (stub_sec->contents == NULL)
   14973 	    return false;
   14974 	  stub_sec->size = 0;
   14975 	}
   14976     }
   14977 
   14978   if (htab->glink != NULL && htab->glink->size != 0)
   14979     {
   14980       unsigned int indx;
   14981       bfd_vma plt0;
   14982 
   14983       /* Build the .glink plt call stub.  */
   14984       if (htab->params->emit_stub_syms)
   14985 	{
   14986 	  struct elf_link_hash_entry *h;
   14987 	  h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
   14988 				    true, false, false);
   14989 	  if (h == NULL)
   14990 	    return false;
   14991 	  if (h->root.type == bfd_link_hash_new)
   14992 	    {
   14993 	      h->root.type = bfd_link_hash_defined;
   14994 	      h->root.u.def.section = htab->glink;
   14995 	      h->root.u.def.value = 8;
   14996 	      h->ref_regular = 1;
   14997 	      h->def_regular = 1;
   14998 	      h->ref_regular_nonweak = 1;
   14999 	      h->forced_local = 1;
   15000 	      h->non_elf = 0;
   15001 	      h->root.linker_def = 1;
   15002 	    }
   15003 	}
   15004       plt0 = (htab->elf.splt->output_section->vma
   15005 	      + htab->elf.splt->output_offset
   15006 	      - 16);
   15007       if (info->emitrelocations)
   15008 	{
   15009 	  Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
   15010 	  if (r == NULL)
   15011 	    return false;
   15012 	  r->r_offset = (htab->glink->output_offset
   15013 			 + htab->glink->output_section->vma);
   15014 	  r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
   15015 	  r->r_addend = plt0;
   15016 	}
   15017       p = htab->glink->contents;
   15018       plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
   15019       bfd_put_64 (htab->glink->owner, plt0, p);
   15020       p += 8;
   15021       if (htab->opd_abi)
   15022 	{
   15023 	  bfd_put_32 (htab->glink->owner, MFLR_R12, p);
   15024 	  p += 4;
   15025 	  bfd_put_32 (htab->glink->owner, BCL_20_31, p);
   15026 	  p += 4;
   15027 	  bfd_put_32 (htab->glink->owner, MFLR_R11, p);
   15028 	  p += 4;
   15029 	  bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
   15030 	  p += 4;
   15031 	  bfd_put_32 (htab->glink->owner, MTLR_R12, p);
   15032 	  p += 4;
   15033 	  bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
   15034 	  p += 4;
   15035 	  bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
   15036 	  p += 4;
   15037 	  bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
   15038 	  p += 4;
   15039 	  bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
   15040 	  p += 4;
   15041 	  bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
   15042 	  p += 4;
   15043 	}
   15044       else
   15045 	{
   15046 	  unsigned int insn;
   15047 
   15048 	  /* 0:
   15049 	     .	.quad plt0-1f		# plt0 entry relative to 1:
   15050 	     #
   15051 	     # We get here with r12 initially @ a glink branch
   15052 	     # Load the address of _dl_runtime_resolve from plt0 and
   15053 	     # jump to it, with r0 set to the index of the PLT entry
   15054 	     # to be resolved and r11 the link map.
   15055 	     __glink_PLTresolve:
   15056 	     .	std %r2,24(%r1)		# optional
   15057 	     .	mflr %r0
   15058 	     .	bcl 20,31,1f
   15059 	     1:
   15060 	     .	mflr %r11
   15061 	     .	mtlr %r0
   15062 	     .	ld %r0,(0b-1b)(%r11)
   15063 	     .	sub %r12,%r12,%r11
   15064 	     .	add %r11,%r0,%r11
   15065 	     .	addi %r0,%r12,1b-2f
   15066 	     .	ld %r12,0(%r11)
   15067 	     .	srdi %r0,%r0,2
   15068 	     .	mtctr %r12
   15069 	     .	ld %r11,8(%r11)
   15070 	     .	bctr
   15071 	     2:
   15072 	     .	b __glink_PLTresolve
   15073 	     .	...
   15074 	     .	b __glink_PLTresolve  */
   15075 
   15076 	  if (htab->has_plt_localentry0)
   15077 	    {
   15078 	      bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
   15079 	      p += 4;
   15080 	    }
   15081 	  bfd_put_32 (htab->glink->owner, MFLR_R0, p);
   15082 	  p += 4;
   15083 	  bfd_put_32 (htab->glink->owner, BCL_20_31, p);
   15084 	  p += 4;
   15085 	  bfd_put_32 (htab->glink->owner, MFLR_R11, p);
   15086 	  p += 4;
   15087 	  bfd_put_32 (htab->glink->owner, MTLR_R0, p);
   15088 	  p += 4;
   15089 	  if (htab->has_plt_localentry0)
   15090 	    insn = LD_R0_0R11 | (-20 & 0xfffc);
   15091 	  else
   15092 	    insn = LD_R0_0R11 | (-16 & 0xfffc);
   15093 	  bfd_put_32 (htab->glink->owner, insn, p);
   15094 	  p += 4;
   15095 	  bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
   15096 	  p += 4;
   15097 	  bfd_put_32 (htab->glink->owner, ADD_R11_R0_R11, p);
   15098 	  p += 4;
   15099 	  bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-44 & 0xffff), p);
   15100 	  p += 4;
   15101 	  bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
   15102 	  p += 4;
   15103 	  bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
   15104 	  p += 4;
   15105 	  bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
   15106 	  p += 4;
   15107 	  bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
   15108 	  p += 4;
   15109 	}
   15110       bfd_put_32 (htab->glink->owner, BCTR, p);
   15111       p += 4;
   15112       BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
   15113 
   15114       /* Build the .glink lazy link call stubs.  */
   15115       indx = 0;
   15116       while (p < htab->glink->contents + htab->glink->size)
   15117 	{
   15118 	  if (htab->opd_abi)
   15119 	    {
   15120 	      if (indx < 0x8000)
   15121 		{
   15122 		  bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
   15123 		  p += 4;
   15124 		}
   15125 	      else
   15126 		{
   15127 		  bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
   15128 		  p += 4;
   15129 		  bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
   15130 			      p);
   15131 		  p += 4;
   15132 		}
   15133 	    }
   15134 	  bfd_put_32 (htab->glink->owner,
   15135 		      B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
   15136 	  indx++;
   15137 	  p += 4;
   15138 	}
   15139     }
   15140 
   15141   if (htab->tga_group != NULL)
   15142     {
   15143       htab->tga_group->lr_restore = 23 * 4;
   15144       htab->tga_group->stub_sec->size = 24 * 4;
   15145       if (!emit_tga_desc (htab))
   15146 	return false;
   15147       if (htab->glink_eh_frame != NULL
   15148 	  && htab->glink_eh_frame->size != 0)
   15149 	{
   15150 	  size_t align = 4;
   15151 
   15152 	  p = htab->glink_eh_frame->contents;
   15153 	  p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
   15154 	  p += 17;
   15155 	  htab->tga_group->eh_size = emit_tga_desc_eh_frame (htab, p) - p;
   15156 	}
   15157     }
   15158 
   15159   /* Build .glink global entry stubs, and PLT relocs for globals.  */
   15160   elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
   15161 
   15162   if (!write_plt_relocs_for_local_syms (info))
   15163     return false;
   15164 
   15165   if (htab->brlt != NULL && htab->brlt->size != 0)
   15166     {
   15167       htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
   15168 					 htab->brlt->size);
   15169       if (htab->brlt->contents == NULL)
   15170 	return false;
   15171     }
   15172   if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
   15173     {
   15174       htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
   15175 					    htab->relbrlt->size);
   15176       if (htab->relbrlt->contents == NULL)
   15177 	return false;
   15178     }
   15179 
   15180   /* Build the stubs as directed by the stub hash table.  */
   15181   htab->stub_id = 0;
   15182   bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
   15183 
   15184   for (group = htab->group; group != NULL; group = group->next)
   15185     if (group->needs_save_res)
   15186       group->stub_sec->size += htab->sfpr->size;
   15187 
   15188   if (htab->relbrlt != NULL)
   15189     htab->relbrlt->reloc_count = 0;
   15190 
   15191   if (htab->params->plt_stub_align != 0)
   15192     for (group = htab->group; group != NULL; group = group->next)
   15193       if ((stub_sec = group->stub_sec) != NULL)
   15194 	{
   15195 	  int align = abs (htab->params->plt_stub_align);
   15196 	  stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
   15197 	}
   15198 
   15199   for (group = htab->group; group != NULL; group = group->next)
   15200     if (group->needs_save_res)
   15201       {
   15202 	stub_sec = group->stub_sec;
   15203 	memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
   15204 		htab->sfpr->contents, htab->sfpr->size);
   15205 	if (htab->params->emit_stub_syms)
   15206 	  {
   15207 	    unsigned int i;
   15208 
   15209 	    for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
   15210 	      if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
   15211 		return false;
   15212 	  }
   15213       }
   15214 
   15215   if (htab->glink_eh_frame != NULL
   15216       && htab->glink_eh_frame->size != 0)
   15217     {
   15218       bfd_vma val;
   15219       size_t align = 4;
   15220 
   15221       p = htab->glink_eh_frame->contents;
   15222       p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
   15223 
   15224       for (group = htab->group; group != NULL; group = group->next)
   15225 	if (group->eh_size != 0)
   15226 	  {
   15227 	    /* Offset to stub section.  */
   15228 	    val = (group->stub_sec->output_section->vma
   15229 		   + group->stub_sec->output_offset);
   15230 	    val -= (htab->glink_eh_frame->output_section->vma
   15231 		    + htab->glink_eh_frame->output_offset
   15232 		    + (p + 8 - htab->glink_eh_frame->contents));
   15233 	    if (val + 0x80000000 > 0xffffffff)
   15234 	      {
   15235 		_bfd_error_handler
   15236 		  (_("%s offset too large for .eh_frame sdata4 encoding"),
   15237 		   group->stub_sec->name);
   15238 		return false;
   15239 	      }
   15240 	    bfd_put_32 (htab->elf.dynobj, val, p + 8);
   15241 	    p += (group->eh_size + 17 + 3) & -4;
   15242 	  }
   15243       if (htab->glink != NULL && htab->glink->size != 0)
   15244 	{
   15245 	  /* Offset to .glink.  */
   15246 	  val = (htab->glink->output_section->vma
   15247 		 + htab->glink->output_offset
   15248 		 + 8);
   15249 	  val -= (htab->glink_eh_frame->output_section->vma
   15250 		  + htab->glink_eh_frame->output_offset
   15251 		  + (p + 8 - htab->glink_eh_frame->contents));
   15252 	  if (val + 0x80000000 > 0xffffffff)
   15253 	    {
   15254 	      _bfd_error_handler
   15255 		(_("%s offset too large for .eh_frame sdata4 encoding"),
   15256 		 htab->glink->name);
   15257 	      return false;
   15258 	    }
   15259 	  bfd_put_32 (htab->elf.dynobj, val, p + 8);
   15260 	  p += (24 + align - 1) & -align;
   15261 	}
   15262     }
   15263 
   15264   if (htab->elf.srelrdyn != NULL && htab->elf.srelrdyn->size != 0)
   15265     {
   15266       htab->elf.srelrdyn->contents
   15267 	= bfd_alloc (htab->elf.dynobj, htab->elf.srelrdyn->size);
   15268       if (htab->elf.srelrdyn->contents == NULL)
   15269 	return false;
   15270 
   15271       bfd_vma *relr_addr = sort_relr (htab);
   15272       if (htab->relr_count != 0 && relr_addr == NULL)
   15273 	return false;
   15274 
   15275       size_t i = 0;
   15276       bfd_byte *loc = htab->elf.srelrdyn->contents;
   15277       while (i < htab->relr_count)
   15278 	{
   15279 	  bfd_vma base = relr_addr[i];
   15280 	  BFD_ASSERT (base % 2 == 0);
   15281 	  bfd_put_64 (htab->elf.dynobj, base, loc);
   15282 	  loc += 8;
   15283 	  i++;
   15284 	  while (i < htab->relr_count
   15285 		 && relr_addr[i] == base)
   15286 	    {
   15287 	      htab->stub_error = true;
   15288 	      i++;
   15289 	    }
   15290 	  base += 8;
   15291 	  while (1)
   15292 	    {
   15293 	      bfd_vma bits = 0;
   15294 	      while (i < htab->relr_count
   15295 		     && relr_addr[i] - base < 63 * 8
   15296 		     && (relr_addr[i] - base) % 8 == 0)
   15297 		{
   15298 		  bits |= (bfd_vma) 1 << ((relr_addr[i] - base) / 8);
   15299 		  i++;
   15300 		}
   15301 	      if (bits == 0)
   15302 		break;
   15303 	      bfd_put_64 (htab->elf.dynobj, (bits << 1) | 1, loc);
   15304 	      loc += 8;
   15305 	      base += 63 * 8;
   15306 	    }
   15307 	}
   15308       free (relr_addr);
   15309       /* Pad any excess with 1's, a do-nothing encoding.  */
   15310       while ((size_t) (loc - htab->elf.srelrdyn->contents)
   15311 	     < htab->elf.srelrdyn->size)
   15312 	{
   15313 	  bfd_put_64 (htab->elf.dynobj, 1, loc);
   15314 	  loc += 8;
   15315 	}
   15316     }
   15317 
   15318   for (group = htab->group; group != NULL; group = group->next)
   15319     if ((stub_sec = group->stub_sec) != NULL)
   15320       {
   15321 	stub_sec_count += 1;
   15322 	if (stub_sec->rawsize != stub_sec->size
   15323 	    && (htab->stub_iteration <= STUB_SHRINK_ITER
   15324 		|| stub_sec->rawsize < stub_sec->size))
   15325 	  break;
   15326       }
   15327 
   15328   if (group != NULL)
   15329     htab->stub_error = true;
   15330 
   15331   if (htab->stub_error)
   15332     {
   15333       _bfd_error_handler (_("stubs don't match calculated size"));
   15334       return false;
   15335     }
   15336 
   15337   if (stats != NULL)
   15338     {
   15339       char *groupmsg;
   15340       if (asprintf (&groupmsg,
   15341 		    ngettext ("linker stubs in %u group\n",
   15342 			      "linker stubs in %u groups\n",
   15343 			      stub_sec_count),
   15344 		    stub_sec_count) < 0)
   15345 	*stats = NULL;
   15346       else
   15347 	{
   15348 	  if (asprintf (stats, _("%s"
   15349 				 "  branch         %lu\n"
   15350 				 "  long branch    %lu\n"
   15351 				 "  plt call       %lu\n"
   15352 				 "  global entry   %lu"),
   15353 			groupmsg,
   15354 			htab->stub_count[ppc_stub_long_branch - 1],
   15355 			htab->stub_count[ppc_stub_plt_branch - 1],
   15356 			htab->stub_count[ppc_stub_plt_call - 1],
   15357 			htab->stub_count[ppc_stub_global_entry - 1]) < 0)
   15358 	    *stats = NULL;
   15359 	  free (groupmsg);
   15360 	}
   15361     }
   15362   return true;
   15363 }
   15364 
   15365 /* What to do when ld finds relocations against symbols defined in
   15366    discarded sections.  */
   15367 
   15368 static unsigned int
   15369 ppc64_elf_action_discarded (asection *sec)
   15370 {
   15371   if (strcmp (".opd", sec->name) == 0)
   15372     return 0;
   15373 
   15374   if (strcmp (".toc", sec->name) == 0)
   15375     return 0;
   15376 
   15377   if (strcmp (".toc1", sec->name) == 0)
   15378     return 0;
   15379 
   15380   return _bfd_elf_default_action_discarded (sec);
   15381 }
   15382 
   15383 /* These are the dynamic relocations supported by glibc.  */
   15384 
   15385 static bool
   15386 ppc64_glibc_dynamic_reloc (enum elf_ppc64_reloc_type r_type)
   15387 {
   15388   switch (r_type)
   15389     {
   15390     case R_PPC64_RELATIVE:
   15391     case R_PPC64_NONE:
   15392     case R_PPC64_ADDR64:
   15393     case R_PPC64_GLOB_DAT:
   15394     case R_PPC64_IRELATIVE:
   15395     case R_PPC64_JMP_IREL:
   15396     case R_PPC64_JMP_SLOT:
   15397     case R_PPC64_DTPMOD64:
   15398     case R_PPC64_DTPREL64:
   15399     case R_PPC64_TPREL64:
   15400     case R_PPC64_TPREL16_LO_DS:
   15401     case R_PPC64_TPREL16_DS:
   15402     case R_PPC64_TPREL16:
   15403     case R_PPC64_TPREL16_LO:
   15404     case R_PPC64_TPREL16_HI:
   15405     case R_PPC64_TPREL16_HIGH:
   15406     case R_PPC64_TPREL16_HA:
   15407     case R_PPC64_TPREL16_HIGHA:
   15408     case R_PPC64_TPREL16_HIGHER:
   15409     case R_PPC64_TPREL16_HIGHEST:
   15410     case R_PPC64_TPREL16_HIGHERA:
   15411     case R_PPC64_TPREL16_HIGHESTA:
   15412     case R_PPC64_ADDR16_LO_DS:
   15413     case R_PPC64_ADDR16_LO:
   15414     case R_PPC64_ADDR16_HI:
   15415     case R_PPC64_ADDR16_HIGH:
   15416     case R_PPC64_ADDR16_HA:
   15417     case R_PPC64_ADDR16_HIGHA:
   15418     case R_PPC64_REL30:
   15419     case R_PPC64_COPY:
   15420     case R_PPC64_UADDR64:
   15421     case R_PPC64_UADDR32:
   15422     case R_PPC64_ADDR32:
   15423     case R_PPC64_ADDR24:
   15424     case R_PPC64_ADDR16:
   15425     case R_PPC64_UADDR16:
   15426     case R_PPC64_ADDR16_DS:
   15427     case R_PPC64_ADDR16_HIGHER:
   15428     case R_PPC64_ADDR16_HIGHEST:
   15429     case R_PPC64_ADDR16_HIGHERA:
   15430     case R_PPC64_ADDR16_HIGHESTA:
   15431     case R_PPC64_ADDR14:
   15432     case R_PPC64_ADDR14_BRTAKEN:
   15433     case R_PPC64_ADDR14_BRNTAKEN:
   15434     case R_PPC64_REL32:
   15435     case R_PPC64_REL64:
   15436       return true;
   15437 
   15438     default:
   15439       return false;
   15440     }
   15441 }
   15442 
   15443 /* The RELOCATE_SECTION function is called by the ELF backend linker
   15444    to handle the relocations for a section.
   15445 
   15446    The relocs are always passed as Rela structures; if the section
   15447    actually uses Rel structures, the r_addend field will always be
   15448    zero.
   15449 
   15450    This function is responsible for adjust the section contents as
   15451    necessary, and (if using Rela relocs and generating a
   15452    relocatable output file) adjusting the reloc addend as
   15453    necessary.
   15454 
   15455    This function does not have to worry about setting the reloc
   15456    address or the reloc symbol index.
   15457 
   15458    LOCAL_SYMS is a pointer to the swapped in local symbols.
   15459 
   15460    LOCAL_SECTIONS is an array giving the section in the input file
   15461    corresponding to the st_shndx field of each local symbol.
   15462 
   15463    The global hash table entry for the global symbols can be found
   15464    via elf_sym_hashes (input_bfd).
   15465 
   15466    When generating relocatable output, this function must handle
   15467    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
   15468    going to be the section symbol corresponding to the output
   15469    section, which means that the addend must be adjusted
   15470    accordingly.  */
   15471 
   15472 static int
   15473 ppc64_elf_relocate_section (bfd *output_bfd,
   15474 			    struct bfd_link_info *info,
   15475 			    bfd *input_bfd,
   15476 			    asection *input_section,
   15477 			    bfd_byte *contents,
   15478 			    Elf_Internal_Rela *relocs,
   15479 			    Elf_Internal_Sym *local_syms,
   15480 			    asection **local_sections)
   15481 {
   15482   struct ppc_link_hash_table *htab;
   15483   Elf_Internal_Shdr *symtab_hdr;
   15484   struct elf_link_hash_entry **sym_hashes;
   15485   Elf_Internal_Rela *rel;
   15486   Elf_Internal_Rela *wrel;
   15487   Elf_Internal_Rela *relend;
   15488   Elf_Internal_Rela outrel;
   15489   bfd_byte *loc;
   15490   struct got_entry **local_got_ents;
   15491   bfd_vma TOCstart;
   15492   bool ret = true;
   15493   bool is_opd;
   15494   /* Assume 'at' branch hints.  */
   15495   bool is_isa_v2 = true;
   15496   bool warned_dynamic = false;
   15497   bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
   15498 
   15499   /* Initialize howto table if needed.  */
   15500   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
   15501     ppc_howto_init ();
   15502 
   15503   htab = ppc_hash_table (info);
   15504   if (htab == NULL)
   15505     return false;
   15506 
   15507   /* Don't relocate stub sections.  */
   15508   if (input_section->owner == htab->params->stub_bfd)
   15509     return true;
   15510 
   15511   if (!is_ppc64_elf (input_bfd))
   15512     {
   15513       bfd_set_error (bfd_error_wrong_format);
   15514       return false;
   15515     }
   15516 
   15517   local_got_ents = elf_local_got_ents (input_bfd);
   15518   TOCstart = elf_gp (output_bfd);
   15519   symtab_hdr = &elf_symtab_hdr (input_bfd);
   15520   sym_hashes = elf_sym_hashes (input_bfd);
   15521   is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
   15522 
   15523   rel = wrel = relocs;
   15524   relend = relocs + input_section->reloc_count;
   15525   for (; rel < relend; wrel++, rel++)
   15526     {
   15527       enum elf_ppc64_reloc_type r_type;
   15528       bfd_vma addend;
   15529       bfd_reloc_status_type r;
   15530       Elf_Internal_Sym *sym;
   15531       asection *sec;
   15532       struct elf_link_hash_entry *h_elf;
   15533       struct ppc_link_hash_entry *h;
   15534       struct ppc_link_hash_entry *fdh;
   15535       const char *sym_name;
   15536       unsigned long r_symndx, toc_symndx;
   15537       bfd_vma toc_addend;
   15538       unsigned char tls_mask, tls_gd, tls_type;
   15539       unsigned char sym_type;
   15540       bfd_vma relocation;
   15541       bool unresolved_reloc, save_unresolved_reloc;
   15542       bool warned;
   15543       enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
   15544       unsigned int insn;
   15545       unsigned int mask;
   15546       struct ppc_stub_hash_entry *stub_entry;
   15547       bfd_vma max_br_offset;
   15548       bfd_vma from;
   15549       Elf_Internal_Rela orig_rel;
   15550       reloc_howto_type *howto;
   15551       struct reloc_howto_struct alt_howto;
   15552       uint64_t pinsn;
   15553       bfd_vma offset;
   15554 
   15555     again:
   15556       orig_rel = *rel;
   15557 
   15558       r_type = ELF64_R_TYPE (rel->r_info);
   15559       r_symndx = ELF64_R_SYM (rel->r_info);
   15560 
   15561       /* For old style R_PPC64_TOC relocs with a zero symbol, use the
   15562 	 symbol of the previous ADDR64 reloc.  The symbol gives us the
   15563 	 proper TOC base to use.  */
   15564       if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
   15565 	  && wrel != relocs
   15566 	  && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
   15567 	  && is_opd)
   15568 	r_symndx = ELF64_R_SYM (wrel[-1].r_info);
   15569 
   15570       sym = NULL;
   15571       sec = NULL;
   15572       h_elf = NULL;
   15573       sym_name = NULL;
   15574       unresolved_reloc = false;
   15575       warned = false;
   15576 
   15577       if (r_symndx < symtab_hdr->sh_info)
   15578 	{
   15579 	  /* It's a local symbol.  */
   15580 	  struct _opd_sec_data *opd;
   15581 
   15582 	  sym = local_syms + r_symndx;
   15583 	  sec = local_sections[r_symndx];
   15584 	  sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
   15585 	  sym_type = ELF64_ST_TYPE (sym->st_info);
   15586 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
   15587 	  opd = get_opd_info (sec);
   15588 	  if (opd != NULL && opd->adjust != NULL)
   15589 	    {
   15590 	      long adjust = opd->adjust[OPD_NDX (sym->st_value
   15591 						 + rel->r_addend)];
   15592 	      if (adjust == -1)
   15593 		relocation = 0;
   15594 	      else
   15595 		{
   15596 		  /* If this is a relocation against the opd section sym
   15597 		     and we have edited .opd, adjust the reloc addend so
   15598 		     that ld -r and ld --emit-relocs output is correct.
   15599 		     If it is a reloc against some other .opd symbol,
   15600 		     then the symbol value will be adjusted later.  */
   15601 		  if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
   15602 		    rel->r_addend += adjust;
   15603 		  else
   15604 		    relocation += adjust;
   15605 		}
   15606 	    }
   15607 	}
   15608       else
   15609 	{
   15610 	  bool ignored;
   15611 
   15612 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
   15613 				   r_symndx, symtab_hdr, sym_hashes,
   15614 				   h_elf, sec, relocation,
   15615 				   unresolved_reloc, warned, ignored);
   15616 	  sym_name = h_elf->root.root.string;
   15617 	  sym_type = h_elf->type;
   15618 	  if (sec != NULL
   15619 	      && sec->owner == output_bfd
   15620 	      && strcmp (sec->name, ".opd") == 0)
   15621 	    {
   15622 	      /* This is a symbol defined in a linker script.  All
   15623 		 such are defined in output sections, even those
   15624 		 defined by simple assignment from a symbol defined in
   15625 		 an input section.  Transfer the symbol to an
   15626 		 appropriate input .opd section, so that a branch to
   15627 		 this symbol will be mapped to the location specified
   15628 		 by the opd entry.  */
   15629 	      struct bfd_link_order *lo;
   15630 	      for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
   15631 		if (lo->type == bfd_indirect_link_order)
   15632 		  {
   15633 		    asection *isec = lo->u.indirect.section;
   15634 		    if (h_elf->root.u.def.value >= isec->output_offset
   15635 			&& h_elf->root.u.def.value < (isec->output_offset
   15636 						      + isec->size))
   15637 		      {
   15638 			h_elf->root.u.def.value -= isec->output_offset;
   15639 			h_elf->root.u.def.section = isec;
   15640 			sec = isec;
   15641 			break;
   15642 		      }
   15643 		  }
   15644 	    }
   15645 	}
   15646       h = ppc_elf_hash_entry (h_elf);
   15647 
   15648       if (sec != NULL && discarded_section (sec))
   15649 	{
   15650 	  _bfd_clear_contents (ppc64_elf_howto_table[r_type],
   15651 			       input_bfd, input_section,
   15652 			       contents, rel->r_offset);
   15653 	  wrel->r_offset = rel->r_offset;
   15654 	  wrel->r_info = 0;
   15655 	  wrel->r_addend = 0;
   15656 
   15657 	  /* For ld -r, remove relocations in debug sections against
   15658 	     symbols defined in discarded sections.  Not done for
   15659 	     non-debug to preserve relocs in .eh_frame which the
   15660 	     eh_frame editing code expects to be present.  */
   15661 	  if (bfd_link_relocatable (info)
   15662 	      && (input_section->flags & SEC_DEBUGGING))
   15663 	    wrel--;
   15664 
   15665 	  continue;
   15666 	}
   15667 
   15668       if (bfd_link_relocatable (info))
   15669 	goto copy_reloc;
   15670 
   15671       if (h != NULL && &h->elf == htab->elf.hgot)
   15672 	{
   15673 	  relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
   15674 	  sec = bfd_abs_section_ptr;
   15675 	  unresolved_reloc = false;
   15676 	}
   15677 
   15678       /* TLS optimizations.  Replace instruction sequences and relocs
   15679 	 based on information we collected in tls_optimize.  We edit
   15680 	 RELOCS so that --emit-relocs will output something sensible
   15681 	 for the final instruction stream.  */
   15682       tls_mask = 0;
   15683       tls_gd = 0;
   15684       toc_symndx = 0;
   15685       if (h != NULL)
   15686 	tls_mask = h->tls_mask;
   15687       else if (local_got_ents != NULL)
   15688 	{
   15689 	  struct plt_entry **local_plt = (struct plt_entry **)
   15690 	    (local_got_ents + symtab_hdr->sh_info);
   15691 	  unsigned char *lgot_masks = (unsigned char *)
   15692 	    (local_plt + symtab_hdr->sh_info);
   15693 	  tls_mask = lgot_masks[r_symndx];
   15694 	}
   15695       if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
   15696 	  && (r_type == R_PPC64_TLS
   15697 	      || r_type == R_PPC64_TLSGD
   15698 	      || r_type == R_PPC64_TLSLD))
   15699 	{
   15700 	  /* Check for toc tls entries.  */
   15701 	  unsigned char *toc_tls;
   15702 
   15703 	  if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
   15704 			     &local_syms, rel, input_bfd))
   15705 	    return false;
   15706 
   15707 	  if (toc_tls)
   15708 	    tls_mask = *toc_tls;
   15709 	}
   15710 
   15711       /* Check that tls relocs are used with tls syms, and non-tls
   15712 	 relocs are used with non-tls syms.  */
   15713       if (r_symndx != STN_UNDEF
   15714 	  && r_type != R_PPC64_NONE
   15715 	  && (h == NULL
   15716 	      || h->elf.root.type == bfd_link_hash_defined
   15717 	      || h->elf.root.type == bfd_link_hash_defweak)
   15718 	  && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
   15719 	{
   15720 	  if ((tls_mask & TLS_TLS) != 0
   15721 	      && (r_type == R_PPC64_TLS
   15722 		  || r_type == R_PPC64_TLSGD
   15723 		  || r_type == R_PPC64_TLSLD))
   15724 	    /* R_PPC64_TLS is OK against a symbol in the TOC.  */
   15725 	    ;
   15726 	  else
   15727 	    info->callbacks->einfo
   15728 	      (!IS_PPC64_TLS_RELOC (r_type)
   15729 	       /* xgettext:c-format */
   15730 	       ? _("%H: %s used with TLS symbol `%pT'\n")
   15731 	       /* xgettext:c-format */
   15732 	       : _("%H: %s used with non-TLS symbol `%pT'\n"),
   15733 	       input_bfd, input_section, rel->r_offset,
   15734 	       ppc64_elf_howto_table[r_type]->name,
   15735 	       sym_name);
   15736 	}
   15737 
   15738       /* Ensure reloc mapping code below stays sane.  */
   15739       if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
   15740 	  || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
   15741 	  || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TLSGD16 & 3)
   15742 	  || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
   15743 	  || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
   15744 	  || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
   15745 	  || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TPREL16_DS & 3)
   15746 	  || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
   15747 	  || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
   15748 	  || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
   15749 	abort ();
   15750 
   15751       switch (r_type)
   15752 	{
   15753 	default:
   15754 	  break;
   15755 
   15756 	case R_PPC64_LO_DS_OPT:
   15757 	  if (offset_in_range (input_section, rel->r_offset - d_offset, 4))
   15758 	    {
   15759 	      insn = bfd_get_32 (input_bfd,
   15760 				 contents + rel->r_offset - d_offset);
   15761 	      if ((insn & (0x3fu << 26)) != 58u << 26)
   15762 		abort ();
   15763 	      insn += (14u << 26) - (58u << 26);
   15764 	      bfd_put_32 (input_bfd, insn,
   15765 			  contents + rel->r_offset - d_offset);
   15766 	      r_type = R_PPC64_TOC16_LO;
   15767 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   15768 	    }
   15769 	  break;
   15770 
   15771 	case R_PPC64_TOC16:
   15772 	case R_PPC64_TOC16_LO:
   15773 	case R_PPC64_TOC16_DS:
   15774 	case R_PPC64_TOC16_LO_DS:
   15775 	  {
   15776 	    /* Check for toc tls entries.  */
   15777 	    unsigned char *toc_tls;
   15778 	    int retval;
   15779 
   15780 	    retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
   15781 				   &local_syms, rel, input_bfd);
   15782 	    if (retval == 0)
   15783 	      return false;
   15784 
   15785 	    if (toc_tls)
   15786 	      {
   15787 		tls_mask = *toc_tls;
   15788 		if (r_type == R_PPC64_TOC16_DS
   15789 		    || r_type == R_PPC64_TOC16_LO_DS)
   15790 		  {
   15791 		    if ((tls_mask & TLS_TLS) != 0
   15792 			&& (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
   15793 		      goto toctprel;
   15794 		  }
   15795 		else
   15796 		  {
   15797 		    /* If we found a GD reloc pair, then we might be
   15798 		       doing a GD->IE transition.  */
   15799 		    if (retval == 2)
   15800 		      {
   15801 			tls_gd = TLS_GDIE;
   15802 			if ((tls_mask & TLS_TLS) != 0
   15803 			    && (tls_mask & TLS_GD) == 0)
   15804 			  goto tls_ldgd_opt;
   15805 		      }
   15806 		    else if (retval == 3)
   15807 		      {
   15808 			if ((tls_mask & TLS_TLS) != 0
   15809 			    && (tls_mask & TLS_LD) == 0)
   15810 			  goto tls_ldgd_opt;
   15811 		      }
   15812 		  }
   15813 	      }
   15814 	  }
   15815 	  break;
   15816 
   15817 	case R_PPC64_GOT_TPREL16_HI:
   15818 	case R_PPC64_GOT_TPREL16_HA:
   15819 	  if ((tls_mask & TLS_TLS) != 0
   15820 	      && (tls_mask & TLS_TPREL) == 0
   15821 	      && offset_in_range (input_section, rel->r_offset - d_offset, 4))
   15822 	    {
   15823 	      rel->r_offset -= d_offset;
   15824 	      bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
   15825 	      r_type = R_PPC64_NONE;
   15826 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   15827 	    }
   15828 	  break;
   15829 
   15830 	case R_PPC64_GOT_TPREL16_DS:
   15831 	case R_PPC64_GOT_TPREL16_LO_DS:
   15832 	  if ((tls_mask & TLS_TLS) != 0
   15833 	      && (tls_mask & TLS_TPREL) == 0
   15834 	      && offset_in_range (input_section, rel->r_offset - d_offset, 4))
   15835 	    {
   15836 	    toctprel:
   15837 	      insn = bfd_get_32 (input_bfd,
   15838 				 contents + rel->r_offset - d_offset);
   15839 	      insn &= 31 << 21;
   15840 	      insn |= 0x3c0d0000;	/* addis 0,13,0 */
   15841 	      bfd_put_32 (input_bfd, insn,
   15842 			  contents + rel->r_offset - d_offset);
   15843 	      r_type = R_PPC64_TPREL16_HA;
   15844 	      if (toc_symndx != 0)
   15845 		{
   15846 		  rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
   15847 		  rel->r_addend = toc_addend;
   15848 		  /* We changed the symbol.  Start over in order to
   15849 		     get h, sym, sec etc. right.  */
   15850 		  goto again;
   15851 		}
   15852 	      else
   15853 		rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   15854 	    }
   15855 	  break;
   15856 
   15857 	case R_PPC64_GOT_TPREL_PCREL34:
   15858 	  if ((tls_mask & TLS_TLS) != 0
   15859 	      && (tls_mask & TLS_TPREL) == 0
   15860 	      && offset_in_range (input_section, rel->r_offset, 8))
   15861 	    {
   15862 	      /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel  */
   15863 	      pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   15864 	      pinsn <<= 32;
   15865 	      pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
   15866 	      pinsn += ((2ULL << 56) + (-1ULL << 52)
   15867 			+ (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
   15868 	      bfd_put_32 (input_bfd, pinsn >> 32,
   15869 			  contents + rel->r_offset);
   15870 	      bfd_put_32 (input_bfd, pinsn & 0xffffffff,
   15871 			  contents + rel->r_offset + 4);
   15872 	      r_type = R_PPC64_TPREL34;
   15873 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   15874 	    }
   15875 	  break;
   15876 
   15877 	case R_PPC64_TLS:
   15878 	  if ((tls_mask & TLS_TLS) != 0
   15879 	      && (tls_mask & TLS_TPREL) == 0
   15880 	      && offset_in_range (input_section, rel->r_offset & ~3, 4))
   15881 	    {
   15882 	      insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
   15883 	      insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
   15884 	      if (insn == 0)
   15885 		break;
   15886 	      if ((rel->r_offset & 3) == 0)
   15887 		{
   15888 		  bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   15889 		  /* Was PPC64_TLS which sits on insn boundary, now
   15890 		     PPC64_TPREL16_LO which is at low-order half-word.  */
   15891 		  rel->r_offset += d_offset;
   15892 		  r_type = R_PPC64_TPREL16_LO;
   15893 		  if (toc_symndx != 0)
   15894 		    {
   15895 		      rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
   15896 		      rel->r_addend = toc_addend;
   15897 		      /* We changed the symbol.  Start over in order to
   15898 			 get h, sym, sec etc. right.  */
   15899 		      goto again;
   15900 		    }
   15901 		  else
   15902 		    rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   15903 		}
   15904 	      else if ((rel->r_offset & 3) == 1)
   15905 		{
   15906 		  /* For pcrel IE to LE we already have the full
   15907 		     offset and thus don't need an addi here.  A nop
   15908 		     or mr will do.  */
   15909 		  if ((insn & (0x3fu << 26)) == 14 << 26)
   15910 		    {
   15911 		      /* Extract regs from addi rt,ra,si.  */
   15912 		      unsigned int rt = (insn >> 21) & 0x1f;
   15913 		      unsigned int ra = (insn >> 16) & 0x1f;
   15914 		      if (rt == ra)
   15915 			insn = NOP;
   15916 		      else
   15917 			{
   15918 			  /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs.  */
   15919 			  insn = (rt << 16) | (ra << 21) | (ra << 11);
   15920 			  insn |= (31u << 26) | (444u << 1);
   15921 			}
   15922 		    }
   15923 		  bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
   15924 		}
   15925 	    }
   15926 	  break;
   15927 
   15928 	case R_PPC64_GOT_TLSGD16_HI:
   15929 	case R_PPC64_GOT_TLSGD16_HA:
   15930 	  tls_gd = TLS_GDIE;
   15931 	  if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
   15932 	      && offset_in_range (input_section, rel->r_offset & ~3, 4))
   15933 	    goto tls_gdld_hi;
   15934 	  break;
   15935 
   15936 	case R_PPC64_GOT_TLSLD16_HI:
   15937 	case R_PPC64_GOT_TLSLD16_HA:
   15938 	  if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
   15939 	      && offset_in_range (input_section, rel->r_offset & ~3, 4))
   15940 	    {
   15941 	    tls_gdld_hi:
   15942 	      if ((tls_mask & tls_gd) != 0)
   15943 		r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
   15944 			  + R_PPC64_GOT_TPREL16_DS);
   15945 	      else
   15946 		{
   15947 		  rel->r_offset -= d_offset;
   15948 		  bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
   15949 		  r_type = R_PPC64_NONE;
   15950 		}
   15951 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   15952 	    }
   15953 	  break;
   15954 
   15955 	case R_PPC64_GOT_TLSGD16:
   15956 	case R_PPC64_GOT_TLSGD16_LO:
   15957 	  tls_gd = TLS_GDIE;
   15958 	  if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
   15959 	      && offset_in_range (input_section, rel->r_offset & ~3, 4))
   15960 	    goto tls_ldgd_opt;
   15961 	  break;
   15962 
   15963 	case R_PPC64_GOT_TLSLD16:
   15964 	case R_PPC64_GOT_TLSLD16_LO:
   15965 	  if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
   15966 	      && offset_in_range (input_section, rel->r_offset & ~3, 4))
   15967 	    {
   15968 	      unsigned int insn1, insn2;
   15969 
   15970 	    tls_ldgd_opt:
   15971 	      offset = (bfd_vma) -1;
   15972 	      /* If not using the newer R_PPC64_TLSGD/LD to mark
   15973 		 __tls_get_addr calls, we must trust that the call
   15974 		 stays with its arg setup insns, ie. that the next
   15975 		 reloc is the __tls_get_addr call associated with
   15976 		 the current reloc.  Edit both insns.  */
   15977 	      if (input_section->nomark_tls_get_addr
   15978 		  && rel + 1 < relend
   15979 		  && branch_reloc_hash_match (input_bfd, rel + 1,
   15980 					      htab->tls_get_addr_fd,
   15981 					      htab->tga_desc_fd,
   15982 					      htab->tls_get_addr,
   15983 					      htab->tga_desc))
   15984 		offset = rel[1].r_offset;
   15985 	      /* We read the low GOT_TLS (or TOC16) insn because we
   15986 		 need to keep the destination reg.  It may be
   15987 		 something other than the usual r3, and moved to r3
   15988 		 before the call by intervening code.  */
   15989 	      insn1 = bfd_get_32 (input_bfd,
   15990 				  contents + rel->r_offset - d_offset);
   15991 	      if ((tls_mask & tls_gd) != 0)
   15992 		{
   15993 		  /* IE */
   15994 		  insn1 &= (0x1f << 21) | (0x1f << 16);
   15995 		  insn1 |= 58u << 26;	/* ld */
   15996 		  insn2 = 0x7c636a14;	/* add 3,3,13 */
   15997 		  if (offset != (bfd_vma) -1)
   15998 		    rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
   15999 		  if (r_type == R_PPC64_TOC16
   16000 		      || r_type == R_PPC64_TOC16_LO)
   16001 		    r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
   16002 		  else
   16003 		    r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
   16004 			      + R_PPC64_GOT_TPREL16_DS);
   16005 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   16006 		}
   16007 	      else
   16008 		{
   16009 		  /* LE */
   16010 		  insn1 &= 0x1f << 21;
   16011 		  insn1 |= 0x3c0d0000;	/* addis r,13,0 */
   16012 		  insn2 = 0x38630000;	/* addi 3,3,0 */
   16013 		  if (tls_gd == 0)
   16014 		    {
   16015 		      /* Was an LD reloc.  */
   16016 		      r_symndx = STN_UNDEF;
   16017 		      rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
   16018 		    }
   16019 		  else if (toc_symndx != 0)
   16020 		    {
   16021 		      r_symndx = toc_symndx;
   16022 		      rel->r_addend = toc_addend;
   16023 		    }
   16024 		  r_type = R_PPC64_TPREL16_HA;
   16025 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   16026 		  if (offset != (bfd_vma) -1)
   16027 		    {
   16028 		      rel[1].r_info = ELF64_R_INFO (r_symndx,
   16029 						    R_PPC64_TPREL16_LO);
   16030 		      rel[1].r_offset = offset + d_offset;
   16031 		      rel[1].r_addend = rel->r_addend;
   16032 		    }
   16033 		}
   16034 	      bfd_put_32 (input_bfd, insn1,
   16035 			  contents + rel->r_offset - d_offset);
   16036 	      if (offset != (bfd_vma) -1
   16037 		  && offset_in_range (input_section, offset, 4))
   16038 		{
   16039 		  bfd_put_32 (input_bfd, insn2, contents + offset);
   16040 		  if (offset_in_range (input_section, offset + 4, 4))
   16041 		    {
   16042 		      insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
   16043 		      if (insn2 == LD_R2_0R1 + STK_TOC (htab))
   16044 			bfd_put_32 (input_bfd, NOP, contents + offset + 4);
   16045 		    }
   16046 		}
   16047 	      if ((tls_mask & tls_gd) == 0
   16048 		  && (tls_gd == 0 || toc_symndx != 0))
   16049 		{
   16050 		  /* We changed the symbol.  Start over in order
   16051 		     to get h, sym, sec etc. right.  */
   16052 		  goto again;
   16053 		}
   16054 	    }
   16055 	  break;
   16056 
   16057 	case R_PPC64_GOT_TLSGD_PCREL34:
   16058 	  if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
   16059 	      && offset_in_range (input_section, rel->r_offset, 8))
   16060 	    {
   16061 	      pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   16062 	      pinsn <<= 32;
   16063 	      pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
   16064 	      if ((tls_mask & TLS_GDIE) != 0)
   16065 		{
   16066 		  /* IE, pla -> pld  */
   16067 		  pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
   16068 		  r_type = R_PPC64_GOT_TPREL_PCREL34;
   16069 		}
   16070 	      else
   16071 		{
   16072 		  /* LE, pla pcrel -> paddi r13  */
   16073 		  pinsn += (-1ULL << 52) + (13ULL << 16);
   16074 		  r_type = R_PPC64_TPREL34;
   16075 		}
   16076 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   16077 	      bfd_put_32 (input_bfd, pinsn >> 32,
   16078 			  contents + rel->r_offset);
   16079 	      bfd_put_32 (input_bfd, pinsn & 0xffffffff,
   16080 			  contents + rel->r_offset + 4);
   16081 	    }
   16082 	  break;
   16083 
   16084 	case R_PPC64_GOT_TLSLD_PCREL34:
   16085 	  if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
   16086 	      && offset_in_range (input_section, rel->r_offset, 8))
   16087 	    {
   16088 	      pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   16089 	      pinsn <<= 32;
   16090 	      pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
   16091 	      pinsn += (-1ULL << 52) + (13ULL << 16);
   16092 	      bfd_put_32 (input_bfd, pinsn >> 32,
   16093 			  contents + rel->r_offset);
   16094 	      bfd_put_32 (input_bfd, pinsn & 0xffffffff,
   16095 			  contents + rel->r_offset + 4);
   16096 	      rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
   16097 	      r_symndx = STN_UNDEF;
   16098 	      r_type = R_PPC64_TPREL34;
   16099 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   16100 	      goto again;
   16101 	    }
   16102 	  break;
   16103 
   16104 	case R_PPC64_TLSGD:
   16105 	  if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
   16106 	      && rel + 1 < relend
   16107 	      && offset_in_range (input_section, rel->r_offset,
   16108 				  is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
   16109 				  ? 8 : 4))
   16110 	    {
   16111 	      unsigned int insn2;
   16112 	      enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
   16113 
   16114 	      offset = rel->r_offset;
   16115 	      if (is_plt_seq_reloc (r_type1))
   16116 		{
   16117 		  bfd_put_32 (output_bfd, NOP, contents + offset);
   16118 		  if (r_type1 == R_PPC64_PLT_PCREL34
   16119 		      || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
   16120 		    bfd_put_32 (output_bfd, NOP, contents + offset + 4);
   16121 		  rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
   16122 		  break;
   16123 		}
   16124 
   16125 	      if (r_type1 == R_PPC64_PLTCALL)
   16126 		bfd_put_32 (output_bfd, NOP, contents + offset + 4);
   16127 
   16128 	      if ((tls_mask & TLS_GDIE) != 0)
   16129 		{
   16130 		  /* IE */
   16131 		  r_type = R_PPC64_NONE;
   16132 		  insn2 = 0x7c636a14;	/* add 3,3,13 */
   16133 		}
   16134 	      else
   16135 		{
   16136 		  /* LE */
   16137 		  if (toc_symndx != 0)
   16138 		    {
   16139 		      r_symndx = toc_symndx;
   16140 		      rel->r_addend = toc_addend;
   16141 		    }
   16142 		  if (r_type1 == R_PPC64_REL24_NOTOC
   16143 		      || r_type1 == R_PPC64_REL24_P9NOTOC
   16144 		      || r_type1 == R_PPC64_PLTCALL_NOTOC)
   16145 		    {
   16146 		      r_type = R_PPC64_NONE;
   16147 		      insn2 = NOP;
   16148 		    }
   16149 		  else
   16150 		    {
   16151 		      rel->r_offset = offset + d_offset;
   16152 		      r_type = R_PPC64_TPREL16_LO;
   16153 		      insn2 = 0x38630000;	/* addi 3,3,0 */
   16154 		    }
   16155 		}
   16156 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   16157 	      /* Zap the reloc on the _tls_get_addr call too.  */
   16158 	      BFD_ASSERT (offset == rel[1].r_offset);
   16159 	      rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
   16160 	      bfd_put_32 (input_bfd, insn2, contents + offset);
   16161 	      if ((tls_mask & TLS_GDIE) == 0
   16162 		  && toc_symndx != 0
   16163 		  && r_type != R_PPC64_NONE)
   16164 		goto again;
   16165 	    }
   16166 	  break;
   16167 
   16168 	case R_PPC64_TLSLD:
   16169 	  if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
   16170 	      && rel + 1 < relend
   16171 	      && offset_in_range (input_section, rel->r_offset,
   16172 				  is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
   16173 				  ? 8 : 4))
   16174 	    {
   16175 	      unsigned int insn2;
   16176 	      enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
   16177 
   16178 	      offset = rel->r_offset;
   16179 	      if (is_plt_seq_reloc (r_type1))
   16180 		{
   16181 		  bfd_put_32 (output_bfd, NOP, contents + offset);
   16182 		  if (r_type1 == R_PPC64_PLT_PCREL34
   16183 		      || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
   16184 		    bfd_put_32 (output_bfd, NOP, contents + offset + 4);
   16185 		  rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
   16186 		  break;
   16187 		}
   16188 
   16189 	      if (r_type1 == R_PPC64_PLTCALL)
   16190 		bfd_put_32 (output_bfd, NOP, contents + offset + 4);
   16191 
   16192 	      if (r_type1 == R_PPC64_REL24_NOTOC
   16193 		  || r_type1 == R_PPC64_REL24_P9NOTOC
   16194 		  || r_type1 == R_PPC64_PLTCALL_NOTOC)
   16195 		{
   16196 		  r_type = R_PPC64_NONE;
   16197 		  insn2 = NOP;
   16198 		}
   16199 	      else
   16200 		{
   16201 		  rel->r_offset = offset + d_offset;
   16202 		  r_symndx = STN_UNDEF;
   16203 		  r_type = R_PPC64_TPREL16_LO;
   16204 		  rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
   16205 		  insn2 = 0x38630000;	/* addi 3,3,0 */
   16206 		}
   16207 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   16208 	      /* Zap the reloc on the _tls_get_addr call too.  */
   16209 	      BFD_ASSERT (offset == rel[1].r_offset);
   16210 	      rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
   16211 	      bfd_put_32 (input_bfd, insn2, contents + offset);
   16212 	      if (r_type != R_PPC64_NONE)
   16213 		goto again;
   16214 	    }
   16215 	  break;
   16216 
   16217 	case R_PPC64_DTPMOD64:
   16218 	  if (rel + 1 < relend
   16219 	      && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
   16220 	      && rel[1].r_offset == rel->r_offset + 8)
   16221 	    {
   16222 	      if ((tls_mask & TLS_GD) == 0
   16223 		  && offset_in_range (input_section, rel->r_offset, 8))
   16224 		{
   16225 		  rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
   16226 		  if ((tls_mask & TLS_GDIE) != 0)
   16227 		    r_type = R_PPC64_TPREL64;
   16228 		  else
   16229 		    {
   16230 		      bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
   16231 		      r_type = R_PPC64_NONE;
   16232 		    }
   16233 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   16234 		}
   16235 	    }
   16236 	  else
   16237 	    {
   16238 	      if ((tls_mask & TLS_LD) == 0
   16239 		  && offset_in_range (input_section, rel->r_offset, 8))
   16240 		{
   16241 		  bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
   16242 		  r_type = R_PPC64_NONE;
   16243 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   16244 		}
   16245 	    }
   16246 	  break;
   16247 
   16248 	case R_PPC64_TPREL64:
   16249 	  if ((tls_mask & TLS_TPREL) == 0)
   16250 	    {
   16251 	      r_type = R_PPC64_NONE;
   16252 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   16253 	    }
   16254 	  break;
   16255 
   16256 	case R_PPC64_ENTRY:
   16257 	  relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
   16258 	  if (!bfd_link_pic (info)
   16259 	      && !info->traditional_format
   16260 	      && relocation + 0x80008000 <= 0xffffffff
   16261 	      && offset_in_range (input_section, rel->r_offset, 8))
   16262 	    {
   16263 	      unsigned int insn1, insn2;
   16264 
   16265 	      insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
   16266 	      insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
   16267 	      if ((insn1 & ~0xfffc) == LD_R2_0R12
   16268 		  && insn2 == ADD_R2_R2_R12)
   16269 		{
   16270 		  bfd_put_32 (input_bfd,
   16271 			      LIS_R2 + PPC_HA (relocation),
   16272 			      contents + rel->r_offset);
   16273 		  bfd_put_32 (input_bfd,
   16274 			      ADDI_R2_R2 + PPC_LO (relocation),
   16275 			      contents + rel->r_offset + 4);
   16276 		}
   16277 	    }
   16278 	  else
   16279 	    {
   16280 	      relocation -= (rel->r_offset
   16281 			     + input_section->output_offset
   16282 			     + input_section->output_section->vma);
   16283 	      if (relocation + 0x80008000 <= 0xffffffff
   16284 		  && offset_in_range (input_section, rel->r_offset, 8))
   16285 		{
   16286 		  unsigned int insn1, insn2;
   16287 
   16288 		  insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
   16289 		  insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
   16290 		  if ((insn1 & ~0xfffc) == LD_R2_0R12
   16291 		      && insn2 == ADD_R2_R2_R12)
   16292 		    {
   16293 		      bfd_put_32 (input_bfd,
   16294 				  ADDIS_R2_R12 + PPC_HA (relocation),
   16295 				  contents + rel->r_offset);
   16296 		      bfd_put_32 (input_bfd,
   16297 				  ADDI_R2_R2 + PPC_LO (relocation),
   16298 				  contents + rel->r_offset + 4);
   16299 		    }
   16300 		}
   16301 	    }
   16302 	  break;
   16303 
   16304 	case R_PPC64_REL16_HA:
   16305 	  /* If we are generating a non-PIC executable, edit
   16306 	     .	0:	addis 2,12,.TOC.-0b@ha
   16307 	     .		addi 2,2,.TOC.-0b@l
   16308 	     used by ELFv2 global entry points to set up r2, to
   16309 	     .		lis 2,.TOC.@ha
   16310 	     .		addi 2,2,.TOC.@l
   16311 	     if .TOC. is in range.  */
   16312 	  if (!bfd_link_pic (info)
   16313 	      && !info->traditional_format
   16314 	      && !htab->opd_abi
   16315 	      && rel->r_addend == d_offset
   16316 	      && h != NULL && &h->elf == htab->elf.hgot
   16317 	      && rel + 1 < relend
   16318 	      && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
   16319 	      && rel[1].r_offset == rel->r_offset + 4
   16320 	      && rel[1].r_addend == rel->r_addend + 4
   16321 	      && relocation + 0x80008000 <= 0xffffffff
   16322 	      && offset_in_range (input_section, rel->r_offset - d_offset, 8))
   16323 	    {
   16324 	      unsigned int insn1, insn2;
   16325 	      offset = rel->r_offset - d_offset;
   16326 	      insn1 = bfd_get_32 (input_bfd, contents + offset);
   16327 	      insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
   16328 	      if ((insn1 & 0xffff0000) == ADDIS_R2_R12
   16329 		  && (insn2 & 0xffff0000) == ADDI_R2_R2)
   16330 		{
   16331 		  r_type = R_PPC64_ADDR16_HA;
   16332 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   16333 		  rel->r_addend -= d_offset;
   16334 		  rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
   16335 		  rel[1].r_addend -= d_offset + 4;
   16336 		  bfd_put_32 (input_bfd, LIS_R2, contents + offset);
   16337 		}
   16338 	    }
   16339 	  break;
   16340 	}
   16341 
   16342       /* Handle other relocations that tweak non-addend part of insn.  */
   16343       insn = 0;
   16344       max_br_offset = 1 << 25;
   16345       addend = rel->r_addend;
   16346       reloc_dest = DEST_NORMAL;
   16347       switch (r_type)
   16348 	{
   16349 	default:
   16350 	  break;
   16351 
   16352 	case R_PPC64_TOCSAVE:
   16353 	  if (relocation + addend == (rel->r_offset
   16354 				      + input_section->output_offset
   16355 				      + input_section->output_section->vma)
   16356 	      && tocsave_find (htab, NO_INSERT,
   16357 			       &local_syms, rel, input_bfd)
   16358 	      && offset_in_range (input_section, rel->r_offset, 4))
   16359 	    {
   16360 	      insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   16361 	      if (insn == NOP
   16362 		  || insn == CROR_151515 || insn == CROR_313131)
   16363 		bfd_put_32 (input_bfd,
   16364 			    STD_R2_0R1 + STK_TOC (htab),
   16365 			    contents + rel->r_offset);
   16366 	    }
   16367 	  break;
   16368 
   16369 	  /* Branch taken prediction relocations.  */
   16370 	case R_PPC64_ADDR14_BRTAKEN:
   16371 	case R_PPC64_REL14_BRTAKEN:
   16372 	  insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
   16373 	  /* Fall through.  */
   16374 
   16375 	  /* Branch not taken prediction relocations.  */
   16376 	case R_PPC64_ADDR14_BRNTAKEN:
   16377 	case R_PPC64_REL14_BRNTAKEN:
   16378 	  if (!offset_in_range (input_section, rel->r_offset, 4))
   16379 	    break;
   16380 	  insn |= bfd_get_32 (input_bfd,
   16381 			      contents + rel->r_offset) & ~(0x01 << 21);
   16382 	  /* Fall through.  */
   16383 
   16384 	case R_PPC64_REL14:
   16385 	  max_br_offset = 1 << 15;
   16386 	  /* Fall through.  */
   16387 
   16388 	case R_PPC64_REL24:
   16389 	case R_PPC64_REL24_NOTOC:
   16390 	case R_PPC64_REL24_P9NOTOC:
   16391 	case R_PPC64_PLTCALL:
   16392 	case R_PPC64_PLTCALL_NOTOC:
   16393 	  /* Calls to functions with a different TOC, such as calls to
   16394 	     shared objects, need to alter the TOC pointer.  This is
   16395 	     done using a linkage stub.  A REL24 branching to these
   16396 	     linkage stubs needs to be followed by a nop, as the nop
   16397 	     will be replaced with an instruction to restore the TOC
   16398 	     base pointer.  */
   16399 	  fdh = h;
   16400 	  if (h != NULL
   16401 	      && h->oh != NULL
   16402 	      && h->oh->is_func_descriptor)
   16403 	    fdh = ppc_follow_link (h->oh);
   16404 	  stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
   16405 					   htab);
   16406 	  if ((r_type == R_PPC64_PLTCALL
   16407 	       || r_type == R_PPC64_PLTCALL_NOTOC)
   16408 	      && stub_entry != NULL
   16409 	      && stub_entry->type.main == ppc_stub_plt_call)
   16410 	    stub_entry = NULL;
   16411 
   16412 	  if (stub_entry != NULL
   16413 	      && (stub_entry->type.main == ppc_stub_plt_call
   16414 		  || stub_entry->type.r2save))
   16415 	    {
   16416 	      bool can_plt_call = false;
   16417 
   16418 	      if (r_type == R_PPC64_REL24_NOTOC
   16419 		  || r_type == R_PPC64_REL24_P9NOTOC)
   16420 		{
   16421 		  /* NOTOC calls don't need to restore r2.  */
   16422 		  can_plt_call = true;
   16423 		}
   16424 	      else if (stub_entry->type.main == ppc_stub_plt_call
   16425 		       && !htab->opd_abi
   16426 		       && htab->params->plt_localentry0 != 0
   16427 		       && h != NULL
   16428 		       && is_elfv2_localentry0 (&h->elf))
   16429 		{
   16430 		  /* The function doesn't use or change r2.  */
   16431 		  can_plt_call = true;
   16432 		}
   16433 
   16434 	      /* All of these stubs may modify r2, so there must be a
   16435 		 branch and link followed by a nop.  The nop is
   16436 		 replaced by an insn to restore r2.  */
   16437 	      else if (offset_in_range (input_section, rel->r_offset, 8))
   16438 		{
   16439 		  unsigned long br;
   16440 
   16441 		  br = bfd_get_32 (input_bfd,
   16442 				   contents + rel->r_offset);
   16443 		  if ((br & 1) != 0)
   16444 		    {
   16445 		      unsigned long nop;
   16446 
   16447 		      nop = bfd_get_32 (input_bfd,
   16448 					contents + rel->r_offset + 4);
   16449 		      if (nop == LD_R2_0R1 + STK_TOC (htab))
   16450 			can_plt_call = true;
   16451 		      else if (nop == NOP
   16452 			       || nop == CROR_151515
   16453 			       || nop == CROR_313131)
   16454 			{
   16455 			  if (h != NULL
   16456 			      && is_tls_get_addr (&h->elf, htab)
   16457 			      && htab->params->tls_get_addr_opt)
   16458 			    {
   16459 			      /* Special stub used, leave nop alone.  */
   16460 			    }
   16461 			  else
   16462 			    bfd_put_32 (input_bfd,
   16463 					LD_R2_0R1 + STK_TOC (htab),
   16464 					contents + rel->r_offset + 4);
   16465 			  can_plt_call = true;
   16466 			}
   16467 		    }
   16468 		}
   16469 
   16470 	      if (!can_plt_call && h != NULL)
   16471 		{
   16472 		  const char *name = h->elf.root.root.string;
   16473 
   16474 		  if (*name == '.')
   16475 		    ++name;
   16476 
   16477 		  if (startswith (name, "__libc_start_main")
   16478 		      && (name[17] == 0 || name[17] == '@'))
   16479 		    {
   16480 		      /* Allow crt1 branch to go via a toc adjusting
   16481 			 stub.  Other calls that never return could do
   16482 			 the same, if we could detect such.  */
   16483 		      can_plt_call = true;
   16484 		    }
   16485 		}
   16486 
   16487 	      if (!can_plt_call)
   16488 		{
   16489 		  /* g++ as of 20130507 emits self-calls without a
   16490 		     following nop.  This is arguably wrong since we
   16491 		     have conflicting information.  On the one hand a
   16492 		     global symbol and on the other a local call
   16493 		     sequence, but don't error for this special case.
   16494 		     It isn't possible to cheaply verify we have
   16495 		     exactly such a call.  Allow all calls to the same
   16496 		     section.  */
   16497 		  asection *code_sec = sec;
   16498 
   16499 		  if (get_opd_info (sec) != NULL)
   16500 		    {
   16501 		      bfd_vma off = (relocation + addend
   16502 				     - sec->output_section->vma
   16503 				     - sec->output_offset);
   16504 
   16505 		      opd_entry_value (sec, off, &code_sec, NULL, false);
   16506 		    }
   16507 		  if (code_sec == input_section)
   16508 		    can_plt_call = true;
   16509 		}
   16510 
   16511 	      if (!can_plt_call)
   16512 		{
   16513 		  if (stub_entry->type.main == ppc_stub_plt_call)
   16514 		    info->callbacks->einfo
   16515 		      /* xgettext:c-format */
   16516 		      (_("%H: call to `%pT' lacks nop, can't restore toc; "
   16517 			 "(plt call stub)\n"),
   16518 		       input_bfd, input_section, rel->r_offset, sym_name);
   16519 		  else
   16520 		    info->callbacks->einfo
   16521 		      /* xgettext:c-format */
   16522 		      (_("%H: call to `%pT' lacks nop, can't restore toc; "
   16523 			 "(toc save/adjust stub)\n"),
   16524 		       input_bfd, input_section, rel->r_offset, sym_name);
   16525 
   16526 		  bfd_set_error (bfd_error_bad_value);
   16527 		  ret = false;
   16528 		}
   16529 
   16530 	      if (can_plt_call
   16531 		  && stub_entry->type.main == ppc_stub_plt_call)
   16532 		unresolved_reloc = false;
   16533 	    }
   16534 
   16535 	  if ((stub_entry == NULL
   16536 	       || stub_entry->type.main == ppc_stub_long_branch
   16537 	       || stub_entry->type.main == ppc_stub_plt_branch)
   16538 	      && get_opd_info (sec) != NULL)
   16539 	    {
   16540 	      /* The branch destination is the value of the opd entry. */
   16541 	      bfd_vma off = (relocation + addend
   16542 			     - sec->output_section->vma
   16543 			     - sec->output_offset);
   16544 	      bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, false);
   16545 	      if (dest != (bfd_vma) -1)
   16546 		{
   16547 		  relocation = dest;
   16548 		  addend = 0;
   16549 		  reloc_dest = DEST_OPD;
   16550 		}
   16551 	    }
   16552 
   16553 	  /* If the branch is out of reach we ought to have a long
   16554 	     branch stub.  */
   16555 	  from = (rel->r_offset
   16556 		  + input_section->output_offset
   16557 		  + input_section->output_section->vma);
   16558 
   16559 	  relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
   16560 						  ? fdh->elf.other
   16561 						  : sym->st_other);
   16562 
   16563 	  if (stub_entry != NULL
   16564 	      && (stub_entry->type.main == ppc_stub_long_branch
   16565 		  || stub_entry->type.main == ppc_stub_plt_branch))
   16566 	    {
   16567 	      if (stub_entry->type.sub == ppc_stub_toc
   16568 		  && !stub_entry->type.r2save
   16569 		  && (r_type == R_PPC64_ADDR14_BRTAKEN
   16570 		      || r_type == R_PPC64_ADDR14_BRNTAKEN
   16571 		      || (relocation + addend - from + max_br_offset
   16572 			  < 2 * max_br_offset)))
   16573 		/* Don't use the stub if this branch is in range.  */
   16574 		stub_entry = NULL;
   16575 
   16576 	      if (stub_entry != NULL
   16577 		  && stub_entry->type.sub >= ppc_stub_notoc
   16578 		  && ((r_type != R_PPC64_REL24_NOTOC
   16579 		       && r_type != R_PPC64_REL24_P9NOTOC)
   16580 		      || ((fdh ? fdh->elf.other : sym->st_other)
   16581 			  & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
   16582 		  && (relocation + addend - from + max_br_offset
   16583 		      < 2 * max_br_offset))
   16584 		stub_entry = NULL;
   16585 
   16586 	      if (stub_entry != NULL
   16587 		  && stub_entry->type.r2save
   16588 		  && (r_type == R_PPC64_REL24_NOTOC
   16589 		      || r_type == R_PPC64_REL24_P9NOTOC)
   16590 		  && (relocation + addend - from + max_br_offset
   16591 		      < 2 * max_br_offset))
   16592 		stub_entry = NULL;
   16593 	    }
   16594 
   16595 	  if (stub_entry != NULL)
   16596 	    {
   16597 	      /* Munge up the value and addend so that we call the stub
   16598 		 rather than the procedure directly.  */
   16599 	      asection *stub_sec = stub_entry->group->stub_sec;
   16600 
   16601 	      if (stub_entry->type.main == ppc_stub_save_res)
   16602 		relocation += (stub_sec->output_offset
   16603 			       + stub_sec->output_section->vma
   16604 			       + stub_sec->size - htab->sfpr->size
   16605 			       - htab->sfpr->output_offset
   16606 			       - htab->sfpr->output_section->vma);
   16607 	      else
   16608 		relocation = (stub_entry->stub_offset
   16609 			      + stub_sec->output_offset
   16610 			      + stub_sec->output_section->vma);
   16611 	      addend = 0;
   16612 	      reloc_dest = DEST_STUB;
   16613 
   16614 	      if (((stub_entry->type.r2save
   16615 		    && (r_type == R_PPC64_REL24_NOTOC
   16616 			|| r_type == R_PPC64_REL24_P9NOTOC))
   16617 		   || ((stub_entry->type.main == ppc_stub_plt_call
   16618 			&& (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save))
   16619 		       && rel + 1 < relend
   16620 		       && rel[1].r_offset == rel->r_offset + 4
   16621 		       && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE))
   16622 		  && !(stub_entry->type.main == ppc_stub_plt_call
   16623 		       && htab->params->tls_get_addr_opt
   16624 		       && h != NULL
   16625 		       && is_tls_get_addr (&h->elf, htab)))
   16626 		{
   16627 		  /* Skip over the r2 store at the start of the stub.  */
   16628 		  relocation += 4;
   16629 		}
   16630 
   16631 	      if ((r_type == R_PPC64_REL24_NOTOC
   16632 		   || r_type == R_PPC64_REL24_P9NOTOC)
   16633 		  && stub_entry->type.main == ppc_stub_plt_call
   16634 		  && stub_entry->type.sub >= ppc_stub_notoc)
   16635 		htab->notoc_plt = 1;
   16636 	    }
   16637 
   16638 	  if (insn != 0)
   16639 	    {
   16640 	      if (is_isa_v2)
   16641 		{
   16642 		  /* Set 'a' bit.  This is 0b00010 in BO field for branch
   16643 		     on CR(BI) insns (BO == 001at or 011at), and 0b01000
   16644 		     for branch on CTR insns (BO == 1a00t or 1a01t).  */
   16645 		  if ((insn & (0x14 << 21)) == (0x04 << 21))
   16646 		    insn |= 0x02 << 21;
   16647 		  else if ((insn & (0x14 << 21)) == (0x10 << 21))
   16648 		    insn |= 0x08 << 21;
   16649 		  else
   16650 		    break;
   16651 		}
   16652 	      else
   16653 		{
   16654 		  /* Invert 'y' bit if not the default.  */
   16655 		  if ((bfd_signed_vma) (relocation + addend - from) < 0)
   16656 		    insn ^= 0x01 << 21;
   16657 		}
   16658 
   16659 	      bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   16660 	    }
   16661 
   16662 	  /* NOP out calls to undefined weak functions.
   16663 	     We can thus call a weak function without first
   16664 	     checking whether the function is defined.  */
   16665 	  else if (h != NULL
   16666 		   && h->elf.root.type == bfd_link_hash_undefweak
   16667 		   && h->elf.dynindx == -1
   16668 		   && (r_type == R_PPC64_REL24
   16669 		       || r_type == R_PPC64_REL24_NOTOC
   16670 		       || r_type == R_PPC64_REL24_P9NOTOC)
   16671 		   && relocation == 0
   16672 		   && addend == 0
   16673 		   && offset_in_range (input_section, rel->r_offset, 4))
   16674 	    {
   16675 	      bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
   16676 	      goto copy_reloc;
   16677 	    }
   16678 	  break;
   16679 
   16680 	case R_PPC64_GOT16_DS:
   16681 	  if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
   16682 	      || (bfd_link_pic (info)
   16683 		  && sec == bfd_abs_section_ptr)
   16684 	      || !htab->do_toc_opt)
   16685 	    break;
   16686 	  from = TOCstart + htab->sec_info[input_section->id].toc_off;
   16687 	  if (relocation + addend - from + 0x8000 < 0x10000
   16688 	      && sec != NULL
   16689 	      && sec->output_section != NULL
   16690 	      && !discarded_section (sec)
   16691 	      && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
   16692 	      && offset_in_range (input_section, rel->r_offset & ~3, 4))
   16693 	    {
   16694 	      insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
   16695 	      if ((insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
   16696 		{
   16697 		  insn += (14u << 26) - (58u << 26);
   16698 		  bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
   16699 		  r_type = R_PPC64_TOC16;
   16700 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   16701 		}
   16702 	    }
   16703 	  break;
   16704 
   16705 	case R_PPC64_GOT16_LO_DS:
   16706 	case R_PPC64_GOT16_HA:
   16707 	  if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
   16708 	      || (bfd_link_pic (info)
   16709 		  && sec == bfd_abs_section_ptr)
   16710 	      || !htab->do_toc_opt)
   16711 	    break;
   16712 	  from = TOCstart + htab->sec_info[input_section->id].toc_off;
   16713 	  if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
   16714 	      && sec != NULL
   16715 	      && sec->output_section != NULL
   16716 	      && !discarded_section (sec)
   16717 	      && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
   16718 	      && offset_in_range (input_section, rel->r_offset & ~3, 4))
   16719 	    {
   16720 	      insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
   16721 	      if (r_type == R_PPC64_GOT16_LO_DS
   16722 		  && (insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
   16723 		{
   16724 		  insn += (14u << 26) - (58u << 26);
   16725 		  bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
   16726 		  r_type = R_PPC64_TOC16_LO;
   16727 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   16728 		}
   16729 	      else if (r_type == R_PPC64_GOT16_HA
   16730 		       && (insn & (0x3fu << 26)) == 15u << 26 /* addis */)
   16731 		{
   16732 		  r_type = R_PPC64_TOC16_HA;
   16733 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   16734 		}
   16735 	    }
   16736 	  break;
   16737 
   16738 	case R_PPC64_GOT_PCREL34:
   16739 	  if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
   16740 	      || (bfd_link_pic (info)
   16741 		  && sec == bfd_abs_section_ptr)
   16742 	      || !htab->do_toc_opt)
   16743 	    break;
   16744 	  from = (rel->r_offset
   16745 		  + input_section->output_section->vma
   16746 		  + input_section->output_offset);
   16747 	  if (!(relocation - from + (1ULL << 33) < 1ULL << 34
   16748 		&& sec != NULL
   16749 		&& sec->output_section != NULL
   16750 		&& !discarded_section (sec)
   16751 		&& (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
   16752 		&& offset_in_range (input_section, rel->r_offset, 8)))
   16753 	    break;
   16754 
   16755 	  offset = rel->r_offset;
   16756 	  pinsn = bfd_get_32 (input_bfd, contents + offset);
   16757 	  pinsn <<= 32;
   16758 	  pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
   16759 	  if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
   16760 	      != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
   16761 	    break;
   16762 
   16763 	  /* Replace with paddi.  */
   16764 	  pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
   16765 	  r_type = R_PPC64_PCREL34;
   16766 	  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   16767 	  bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
   16768 	  bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
   16769 	  /* Fall through.  */
   16770 
   16771 	case R_PPC64_PCREL34:
   16772 	  if (!htab->params->no_pcrel_opt
   16773 	      && rel + 1 < relend
   16774 	      && rel[1].r_offset == rel->r_offset
   16775 	      && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT)
   16776 	      && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
   16777 	      && offset_in_range (input_section, rel->r_offset, 8))
   16778 	    {
   16779 	      offset = rel->r_offset;
   16780 	      pinsn = bfd_get_32 (input_bfd, contents + offset);
   16781 	      pinsn <<= 32;
   16782 	      pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
   16783 	      if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
   16784 		   == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
   16785 		       | (14ULL << 26) /* paddi */))
   16786 		{
   16787 		  bfd_vma off2 = rel[1].r_addend;
   16788 		  if (off2 == 0)
   16789 		    /* zero means next insn.  */
   16790 		    off2 = 8;
   16791 		  off2 += offset;
   16792 		  if (offset_in_range (input_section, off2, 4))
   16793 		    {
   16794 		      uint64_t pinsn2;
   16795 		      bfd_signed_vma addend_off;
   16796 		      pinsn2 = bfd_get_32 (input_bfd, contents + off2);
   16797 		      pinsn2 <<= 32;
   16798 		      if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
   16799 			{
   16800 			  if (!offset_in_range (input_section, off2, 8))
   16801 			    break;
   16802 			  pinsn2 |= bfd_get_32 (input_bfd,
   16803 						contents + off2 + 4);
   16804 			}
   16805 		      if (xlate_pcrel_opt (&pinsn, &pinsn2, &addend_off))
   16806 			{
   16807 			  addend += addend_off;
   16808 			  rel->r_addend = addend;
   16809 			  bfd_put_32 (input_bfd, pinsn >> 32,
   16810 				      contents + offset);
   16811 			  bfd_put_32 (input_bfd, pinsn,
   16812 				      contents + offset + 4);
   16813 			  bfd_put_32 (input_bfd, pinsn2 >> 32,
   16814 				      contents + off2);
   16815 			  if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
   16816 			    bfd_put_32 (input_bfd, pinsn2,
   16817 					contents + off2 + 4);
   16818 			}
   16819 		    }
   16820 		}
   16821 	    }
   16822 	  break;
   16823 	}
   16824 
   16825       tls_type = 0;
   16826       save_unresolved_reloc = unresolved_reloc;
   16827       switch (r_type)
   16828 	{
   16829 	default:
   16830 	  /* xgettext:c-format */
   16831 	  _bfd_error_handler (_("%pB: %s unsupported"),
   16832 			      input_bfd, ppc64_elf_howto_table[r_type]->name);
   16833 
   16834 	  bfd_set_error (bfd_error_bad_value);
   16835 	  ret = false;
   16836 	  goto copy_reloc;
   16837 
   16838 	case R_PPC64_NONE:
   16839 	case R_PPC64_TLS:
   16840 	case R_PPC64_TLSGD:
   16841 	case R_PPC64_TLSLD:
   16842 	case R_PPC64_TOCSAVE:
   16843 	case R_PPC64_GNU_VTINHERIT:
   16844 	case R_PPC64_GNU_VTENTRY:
   16845 	case R_PPC64_ENTRY:
   16846 	case R_PPC64_PCREL_OPT:
   16847 	  goto copy_reloc;
   16848 
   16849 	  /* GOT16 relocations.  Like an ADDR16 using the symbol's
   16850 	     address in the GOT as relocation value instead of the
   16851 	     symbol's value itself.  Also, create a GOT entry for the
   16852 	     symbol and put the symbol value there.  */
   16853 	case R_PPC64_GOT_TLSGD16:
   16854 	case R_PPC64_GOT_TLSGD16_LO:
   16855 	case R_PPC64_GOT_TLSGD16_HI:
   16856 	case R_PPC64_GOT_TLSGD16_HA:
   16857 	case R_PPC64_GOT_TLSGD_PCREL34:
   16858 	  tls_type = TLS_TLS | TLS_GD;
   16859 	  goto dogot;
   16860 
   16861 	case R_PPC64_GOT_TLSLD16:
   16862 	case R_PPC64_GOT_TLSLD16_LO:
   16863 	case R_PPC64_GOT_TLSLD16_HI:
   16864 	case R_PPC64_GOT_TLSLD16_HA:
   16865 	case R_PPC64_GOT_TLSLD_PCREL34:
   16866 	  tls_type = TLS_TLS | TLS_LD;
   16867 	  goto dogot;
   16868 
   16869 	case R_PPC64_GOT_TPREL16_DS:
   16870 	case R_PPC64_GOT_TPREL16_LO_DS:
   16871 	case R_PPC64_GOT_TPREL16_HI:
   16872 	case R_PPC64_GOT_TPREL16_HA:
   16873 	case R_PPC64_GOT_TPREL_PCREL34:
   16874 	  tls_type = TLS_TLS | TLS_TPREL;
   16875 	  goto dogot;
   16876 
   16877 	case R_PPC64_GOT_DTPREL16_DS:
   16878 	case R_PPC64_GOT_DTPREL16_LO_DS:
   16879 	case R_PPC64_GOT_DTPREL16_HI:
   16880 	case R_PPC64_GOT_DTPREL16_HA:
   16881 	case R_PPC64_GOT_DTPREL_PCREL34:
   16882 	  tls_type = TLS_TLS | TLS_DTPREL;
   16883 	  goto dogot;
   16884 
   16885 	case R_PPC64_GOT16:
   16886 	case R_PPC64_GOT16_LO:
   16887 	case R_PPC64_GOT16_HI:
   16888 	case R_PPC64_GOT16_HA:
   16889 	case R_PPC64_GOT16_DS:
   16890 	case R_PPC64_GOT16_LO_DS:
   16891 	case R_PPC64_GOT_PCREL34:
   16892 	dogot:
   16893 	  {
   16894 	    /* Relocation is to the entry for this symbol in the global
   16895 	       offset table.  */
   16896 	    asection *got;
   16897 	    bfd_vma *offp;
   16898 	    bfd_vma off;
   16899 	    unsigned long indx = 0;
   16900 	    struct got_entry *ent;
   16901 
   16902 	    if (tls_type == (TLS_TLS | TLS_LD)
   16903 		&& (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
   16904 	      ent = ppc64_tlsld_got (input_bfd);
   16905 	    else
   16906 	      {
   16907 		if (h != NULL)
   16908 		  {
   16909 		    if (!htab->elf.dynamic_sections_created
   16910 			|| h->elf.dynindx == -1
   16911 			|| SYMBOL_REFERENCES_LOCAL (info, &h->elf)
   16912 			|| UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
   16913 		      /* This is actually a static link, or it is a
   16914 			 -Bsymbolic link and the symbol is defined
   16915 			 locally, or the symbol was forced to be local
   16916 			 because of a version file.  */
   16917 		      ;
   16918 		    else
   16919 		      {
   16920 			indx = h->elf.dynindx;
   16921 			unresolved_reloc = false;
   16922 		      }
   16923 		    ent = h->elf.got.glist;
   16924 		  }
   16925 		else
   16926 		  {
   16927 		    if (local_got_ents == NULL)
   16928 		      abort ();
   16929 		    ent = local_got_ents[r_symndx];
   16930 		  }
   16931 
   16932 		for (; ent != NULL; ent = ent->next)
   16933 		  if (ent->addend == orig_rel.r_addend
   16934 		      && ent->owner == input_bfd
   16935 		      && ent->tls_type == tls_type)
   16936 		    break;
   16937 	      }
   16938 
   16939 	    if (ent == NULL)
   16940 	      abort ();
   16941 	    if (ent->is_indirect)
   16942 	      ent = ent->got.ent;
   16943 	    offp = &ent->got.offset;
   16944 	    got = ppc64_elf_tdata (ent->owner)->got;
   16945 	    if (got == NULL)
   16946 	      abort ();
   16947 
   16948 	    /* The offset must always be a multiple of 8.  We use the
   16949 	       least significant bit to record whether we have already
   16950 	       processed this entry.  */
   16951 	    off = *offp;
   16952 	    if ((off & 1) != 0)
   16953 	      off &= ~1;
   16954 	    else
   16955 	      {
   16956 		/* Generate relocs for the dynamic linker, except in
   16957 		   the case of TLSLD where we'll use one entry per
   16958 		   module.  */
   16959 		asection *relgot;
   16960 		bool ifunc;
   16961 
   16962 		*offp = off | 1;
   16963 		relgot = NULL;
   16964 		ifunc = (h != NULL
   16965 			 ? h->elf.type == STT_GNU_IFUNC
   16966 			 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
   16967 		if (ifunc)
   16968 		  {
   16969 		    relgot = htab->elf.irelplt;
   16970 		    if (indx == 0 || is_static_defined (&h->elf))
   16971 		      htab->elf.ifunc_resolvers = true;
   16972 		  }
   16973 		else if (indx != 0
   16974 			 || (bfd_link_pic (info)
   16975 			     && (h == NULL
   16976 				 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
   16977 			     && !(tls_type != 0
   16978 				  && bfd_link_executable (info)
   16979 				  && (h == NULL
   16980 				      || SYMBOL_REFERENCES_LOCAL (info,
   16981 								  &h->elf)))
   16982 			     && (h != NULL
   16983 				 ? !bfd_is_abs_symbol (&h->elf.root)
   16984 				 : sym->st_shndx != SHN_ABS)))
   16985 
   16986 		  relgot = ppc64_elf_tdata (ent->owner)->relgot;
   16987 		if (relgot != NULL)
   16988 		  {
   16989 		    outrel.r_offset = (got->output_section->vma
   16990 				       + got->output_offset
   16991 				       + off);
   16992 		    outrel.r_addend = orig_rel.r_addend;
   16993 		    if (tls_type & (TLS_LD | TLS_GD))
   16994 		      {
   16995 			outrel.r_addend = 0;
   16996 			outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
   16997 			if (tls_type == (TLS_TLS | TLS_GD))
   16998 			  {
   16999 			    loc = relgot->contents;
   17000 			    loc += (relgot->reloc_count++
   17001 				    * sizeof (Elf64_External_Rela));
   17002 			    bfd_elf64_swap_reloca_out (output_bfd,
   17003 						       &outrel, loc);
   17004 			    outrel.r_offset += 8;
   17005 			    outrel.r_addend = orig_rel.r_addend;
   17006 			    outrel.r_info
   17007 			      = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
   17008 			  }
   17009 		      }
   17010 		    else if (tls_type == (TLS_TLS | TLS_DTPREL))
   17011 		      outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
   17012 		    else if (tls_type == (TLS_TLS | TLS_TPREL))
   17013 		      outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
   17014 		    else if (indx != 0)
   17015 		      outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
   17016 		    else
   17017 		      {
   17018 			if (ifunc)
   17019 			  outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
   17020 			else
   17021 			  outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   17022 
   17023 			/* Write the .got section contents for the sake
   17024 			   of prelink.  */
   17025 			loc = got->contents + off;
   17026 			bfd_put_64 (output_bfd, outrel.r_addend + relocation,
   17027 				    loc);
   17028 		      }
   17029 
   17030 		    if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
   17031 		      {
   17032 			outrel.r_addend += relocation;
   17033 			if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
   17034 			  {
   17035 			    if (htab->elf.tls_sec == NULL)
   17036 			      outrel.r_addend = 0;
   17037 			    else
   17038 			      outrel.r_addend -= htab->elf.tls_sec->vma;
   17039 			  }
   17040 		      }
   17041 		    if (!(info->enable_dt_relr
   17042 			  && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE))
   17043 		      {
   17044 			loc = relgot->contents;
   17045 			loc += (relgot->reloc_count++
   17046 				* sizeof (Elf64_External_Rela));
   17047 			bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
   17048 		      }
   17049 		  }
   17050 
   17051 		/* Init the .got section contents here if we're not
   17052 		   emitting a reloc.  */
   17053 		else
   17054 		  {
   17055 		    relocation += orig_rel.r_addend;
   17056 		    if (tls_type != 0)
   17057 		      {
   17058 			if (htab->elf.tls_sec == NULL)
   17059 			  relocation = 0;
   17060 			else
   17061 			  {
   17062 			    if (tls_type & TLS_LD)
   17063 			      relocation = 0;
   17064 			    else
   17065 			      relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
   17066 			    if (tls_type & TLS_TPREL)
   17067 			      relocation += DTP_OFFSET - TP_OFFSET;
   17068 			  }
   17069 
   17070 			if (tls_type & (TLS_GD | TLS_LD))
   17071 			  {
   17072 			    bfd_put_64 (output_bfd, relocation,
   17073 					got->contents + off + 8);
   17074 			    relocation = 1;
   17075 			  }
   17076 		      }
   17077 		    bfd_put_64 (output_bfd, relocation,
   17078 				got->contents + off);
   17079 		  }
   17080 	      }
   17081 
   17082 	    if (off >= (bfd_vma) -2)
   17083 	      abort ();
   17084 
   17085 	    relocation = got->output_section->vma + got->output_offset + off;
   17086 	    addend = 0;
   17087 	    if (!(r_type == R_PPC64_GOT_PCREL34
   17088 		  || r_type == R_PPC64_GOT_TLSGD_PCREL34
   17089 		  || r_type == R_PPC64_GOT_TLSLD_PCREL34
   17090 		  || r_type == R_PPC64_GOT_TPREL_PCREL34
   17091 		  || r_type == R_PPC64_GOT_DTPREL_PCREL34))
   17092 	      addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
   17093 	  }
   17094 	  break;
   17095 
   17096 	case R_PPC64_PLT16_HA:
   17097 	case R_PPC64_PLT16_HI:
   17098 	case R_PPC64_PLT16_LO:
   17099 	case R_PPC64_PLT16_LO_DS:
   17100 	case R_PPC64_PLT_PCREL34:
   17101 	case R_PPC64_PLT_PCREL34_NOTOC:
   17102 	case R_PPC64_PLT32:
   17103 	case R_PPC64_PLT64:
   17104 	case R_PPC64_PLTSEQ:
   17105 	case R_PPC64_PLTSEQ_NOTOC:
   17106 	case R_PPC64_PLTCALL:
   17107 	case R_PPC64_PLTCALL_NOTOC:
   17108 	  /* Relocation is to the entry for this symbol in the
   17109 	     procedure linkage table.  */
   17110 	  unresolved_reloc = true;
   17111 	  {
   17112 	    struct plt_entry **plt_list = NULL;
   17113 	    if (h != NULL)
   17114 	      plt_list = &h->elf.plt.plist;
   17115 	    else if (local_got_ents != NULL)
   17116 	      {
   17117 		struct plt_entry **local_plt = (struct plt_entry **)
   17118 		  (local_got_ents + symtab_hdr->sh_info);
   17119 		plt_list = local_plt + r_symndx;
   17120 	      }
   17121 	    if (plt_list)
   17122 	      {
   17123 		struct plt_entry *ent;
   17124 
   17125 		for (ent = *plt_list; ent != NULL; ent = ent->next)
   17126 		  if (ent->plt.offset != (bfd_vma) -1
   17127 		      && ent->addend == orig_rel.r_addend)
   17128 		    {
   17129 		      asection *plt;
   17130 		      bfd_vma got;
   17131 
   17132 		      plt = htab->elf.splt;
   17133 		      if (use_local_plt (info, elf_hash_entry (h)))
   17134 			{
   17135 			  if (h != NULL
   17136 			      ? h->elf.type == STT_GNU_IFUNC
   17137 			      : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
   17138 			    plt = htab->elf.iplt;
   17139 			  else
   17140 			    plt = htab->pltlocal;
   17141 			}
   17142 		      relocation = (plt->output_section->vma
   17143 				    + plt->output_offset
   17144 				    + ent->plt.offset);
   17145 		      if (r_type == R_PPC64_PLT16_HA
   17146 			  || r_type == R_PPC64_PLT16_HI
   17147 			  || r_type == R_PPC64_PLT16_LO
   17148 			  || r_type == R_PPC64_PLT16_LO_DS)
   17149 			{
   17150 			  got = (elf_gp (output_bfd)
   17151 				 + htab->sec_info[input_section->id].toc_off);
   17152 			  relocation -= got;
   17153 			}
   17154 		      addend = 0;
   17155 		      unresolved_reloc = false;
   17156 		      break;
   17157 		    }
   17158 	      }
   17159 	  }
   17160 	  break;
   17161 
   17162 	case R_PPC64_TOC:
   17163 	  /* Relocation value is TOC base.  */
   17164 	  relocation = TOCstart;
   17165 	  if (r_symndx == STN_UNDEF)
   17166 	    relocation += htab->sec_info[input_section->id].toc_off;
   17167 	  else if (unresolved_reloc)
   17168 	    ;
   17169 	  else if (sec != NULL && sec->id < htab->sec_info_arr_size)
   17170 	    relocation += htab->sec_info[sec->id].toc_off;
   17171 	  else
   17172 	    unresolved_reloc = true;
   17173 	  if (unresolved_reloc
   17174 	      || (!is_opd
   17175 		  && h != NULL
   17176 		  && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
   17177 	    info->callbacks->einfo
   17178 	      /* xgettext:c-format */
   17179 	      (_("%H: %s against %pT is not supported\n"),
   17180 	       input_bfd, input_section, rel->r_offset,
   17181 	       ppc64_elf_howto_table[r_type]->name, sym_name);
   17182 	  goto dodyn;
   17183 
   17184 	  /* TOC16 relocs.  We want the offset relative to the TOC base,
   17185 	     which is the address of the start of the TOC plus 0x8000.
   17186 	     The TOC consists of sections .got, .toc, .tocbss, and .plt,
   17187 	     in this order.  */
   17188 	case R_PPC64_TOC16:
   17189 	case R_PPC64_TOC16_LO:
   17190 	case R_PPC64_TOC16_HI:
   17191 	case R_PPC64_TOC16_DS:
   17192 	case R_PPC64_TOC16_LO_DS:
   17193 	case R_PPC64_TOC16_HA:
   17194 	  addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
   17195 	  if (h != NULL)
   17196 	    goto dodyn;
   17197 	  break;
   17198 
   17199 	  /* Relocate against the beginning of the section.  */
   17200 	case R_PPC64_SECTOFF:
   17201 	case R_PPC64_SECTOFF_LO:
   17202 	case R_PPC64_SECTOFF_HI:
   17203 	case R_PPC64_SECTOFF_DS:
   17204 	case R_PPC64_SECTOFF_LO_DS:
   17205 	case R_PPC64_SECTOFF_HA:
   17206 	  if (sec != NULL)
   17207 	    addend -= sec->output_section->vma;
   17208 	  break;
   17209 
   17210 	case R_PPC64_REL16:
   17211 	case R_PPC64_REL16_LO:
   17212 	case R_PPC64_REL16_HI:
   17213 	case R_PPC64_REL16_HA:
   17214 	case R_PPC64_REL16_HIGH:
   17215 	case R_PPC64_REL16_HIGHA:
   17216 	case R_PPC64_REL16_HIGHER:
   17217 	case R_PPC64_REL16_HIGHERA:
   17218 	case R_PPC64_REL16_HIGHEST:
   17219 	case R_PPC64_REL16_HIGHESTA:
   17220 	case R_PPC64_REL16_HIGHER34:
   17221 	case R_PPC64_REL16_HIGHERA34:
   17222 	case R_PPC64_REL16_HIGHEST34:
   17223 	case R_PPC64_REL16_HIGHESTA34:
   17224 	case R_PPC64_REL16DX_HA:
   17225 	case R_PPC64_REL14:
   17226 	case R_PPC64_REL14_BRNTAKEN:
   17227 	case R_PPC64_REL14_BRTAKEN:
   17228 	case R_PPC64_REL24:
   17229 	case R_PPC64_REL24_NOTOC:
   17230 	case R_PPC64_REL24_P9NOTOC:
   17231 	case R_PPC64_PCREL34:
   17232 	case R_PPC64_PCREL28:
   17233 	  break;
   17234 
   17235 	case R_PPC64_TPREL16:
   17236 	case R_PPC64_TPREL16_LO:
   17237 	case R_PPC64_TPREL16_HI:
   17238 	case R_PPC64_TPREL16_HA:
   17239 	case R_PPC64_TPREL16_DS:
   17240 	case R_PPC64_TPREL16_LO_DS:
   17241 	case R_PPC64_TPREL16_HIGH:
   17242 	case R_PPC64_TPREL16_HIGHA:
   17243 	case R_PPC64_TPREL16_HIGHER:
   17244 	case R_PPC64_TPREL16_HIGHERA:
   17245 	case R_PPC64_TPREL16_HIGHEST:
   17246 	case R_PPC64_TPREL16_HIGHESTA:
   17247 	  if (h != NULL
   17248 	      && h->elf.root.type == bfd_link_hash_undefweak
   17249 	      && h->elf.dynindx == -1
   17250 	      && offset_in_range (input_section, rel->r_offset - d_offset, 4))
   17251 	    {
   17252 	      /* Make this relocation against an undefined weak symbol
   17253 		 resolve to zero.  This is really just a tweak, since
   17254 		 code using weak externs ought to check that they are
   17255 		 defined before using them.  */
   17256 	      bfd_byte *p = contents + rel->r_offset - d_offset;
   17257 
   17258 	      insn = bfd_get_32 (input_bfd, p);
   17259 	      insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
   17260 	      if (insn != 0)
   17261 		bfd_put_32 (input_bfd, insn, p);
   17262 	      break;
   17263 	    }
   17264 	  /* Fall through.  */
   17265 
   17266 	case R_PPC64_TPREL34:
   17267 	  if (htab->elf.tls_sec != NULL)
   17268 	    addend -= htab->elf.tls_sec->vma + TP_OFFSET;
   17269 	  /* The TPREL16 relocs shouldn't really be used in shared
   17270 	     libs or with non-local symbols as that will result in
   17271 	     DT_TEXTREL being set, but support them anyway.  */
   17272 	  goto dodyn;
   17273 
   17274 	case R_PPC64_DTPREL16:
   17275 	case R_PPC64_DTPREL16_LO:
   17276 	case R_PPC64_DTPREL16_HI:
   17277 	case R_PPC64_DTPREL16_HA:
   17278 	case R_PPC64_DTPREL16_DS:
   17279 	case R_PPC64_DTPREL16_LO_DS:
   17280 	case R_PPC64_DTPREL16_HIGH:
   17281 	case R_PPC64_DTPREL16_HIGHA:
   17282 	case R_PPC64_DTPREL16_HIGHER:
   17283 	case R_PPC64_DTPREL16_HIGHERA:
   17284 	case R_PPC64_DTPREL16_HIGHEST:
   17285 	case R_PPC64_DTPREL16_HIGHESTA:
   17286 	case R_PPC64_DTPREL34:
   17287 	  if (htab->elf.tls_sec != NULL)
   17288 	    addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
   17289 	  break;
   17290 
   17291 	case R_PPC64_ADDR64_LOCAL:
   17292 	  addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
   17293 					      ? h->elf.other
   17294 					      : sym->st_other);
   17295 	  break;
   17296 
   17297 	case R_PPC64_DTPMOD64:
   17298 	  relocation = 1;
   17299 	  addend = 0;
   17300 	  goto dodyn;
   17301 
   17302 	case R_PPC64_TPREL64:
   17303 	  if (htab->elf.tls_sec != NULL)
   17304 	    addend -= htab->elf.tls_sec->vma + TP_OFFSET;
   17305 	  goto dodyn;
   17306 
   17307 	case R_PPC64_DTPREL64:
   17308 	  if (htab->elf.tls_sec != NULL)
   17309 	    addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
   17310 	  /* Fall through.  */
   17311 
   17312 	  /* Relocations that may need to be propagated if this is a
   17313 	     dynamic object.  */
   17314 	case R_PPC64_REL30:
   17315 	case R_PPC64_REL32:
   17316 	case R_PPC64_REL64:
   17317 	case R_PPC64_ADDR14:
   17318 	case R_PPC64_ADDR14_BRNTAKEN:
   17319 	case R_PPC64_ADDR14_BRTAKEN:
   17320 	case R_PPC64_ADDR16:
   17321 	case R_PPC64_ADDR16_DS:
   17322 	case R_PPC64_ADDR16_HA:
   17323 	case R_PPC64_ADDR16_HI:
   17324 	case R_PPC64_ADDR16_HIGH:
   17325 	case R_PPC64_ADDR16_HIGHA:
   17326 	case R_PPC64_ADDR16_HIGHER:
   17327 	case R_PPC64_ADDR16_HIGHERA:
   17328 	case R_PPC64_ADDR16_HIGHEST:
   17329 	case R_PPC64_ADDR16_HIGHESTA:
   17330 	case R_PPC64_ADDR16_LO:
   17331 	case R_PPC64_ADDR16_LO_DS:
   17332 	case R_PPC64_ADDR16_HIGHER34:
   17333 	case R_PPC64_ADDR16_HIGHERA34:
   17334 	case R_PPC64_ADDR16_HIGHEST34:
   17335 	case R_PPC64_ADDR16_HIGHESTA34:
   17336 	case R_PPC64_ADDR24:
   17337 	case R_PPC64_ADDR32:
   17338 	case R_PPC64_ADDR64:
   17339 	case R_PPC64_UADDR16:
   17340 	case R_PPC64_UADDR32:
   17341 	case R_PPC64_UADDR64:
   17342 	case R_PPC64_D34:
   17343 	case R_PPC64_D34_LO:
   17344 	case R_PPC64_D34_HI30:
   17345 	case R_PPC64_D34_HA30:
   17346 	case R_PPC64_D28:
   17347 	dodyn:
   17348 	  if ((input_section->flags & SEC_ALLOC) == 0)
   17349 	    break;
   17350 
   17351 	  if (NO_OPD_RELOCS && is_opd)
   17352 	    break;
   17353 
   17354 	  if (bfd_link_pic (info)
   17355 	      ? ((h == NULL
   17356 		  || h->elf.dyn_relocs != NULL)
   17357 		 && ((h != NULL && pc_dynrelocs (h))
   17358 		     || must_be_dyn_reloc (info, r_type)))
   17359 	      : (h != NULL
   17360 		 ? h->elf.dyn_relocs != NULL
   17361 		 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
   17362 	    {
   17363 	      bool skip, relocate;
   17364 	      asection *sreloc;
   17365 	      bfd_vma out_off;
   17366 	      long indx = 0;
   17367 
   17368 	      /* When generating a dynamic object, these relocations
   17369 		 are copied into the output file to be resolved at run
   17370 		 time.  */
   17371 
   17372 	      skip = false;
   17373 	      relocate = false;
   17374 
   17375 	      out_off = _bfd_elf_section_offset (output_bfd, info,
   17376 						 input_section, rel->r_offset);
   17377 	      if (out_off == (bfd_vma) -1)
   17378 		skip = true;
   17379 	      else if (out_off == (bfd_vma) -2)
   17380 		skip = true, relocate = true;
   17381 	      out_off += (input_section->output_section->vma
   17382 			  + input_section->output_offset);
   17383 	      outrel.r_offset = out_off;
   17384 	      outrel.r_addend = rel->r_addend;
   17385 
   17386 	      /* Optimize unaligned reloc use.  */
   17387 	      if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
   17388 		  || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
   17389 		r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
   17390 	      else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
   17391 		       || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
   17392 		r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
   17393 	      else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
   17394 		       || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
   17395 		r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
   17396 
   17397 	      if (skip)
   17398 		memset (&outrel, 0, sizeof outrel);
   17399 	      else if (h != NULL
   17400 		       && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)
   17401 		       && !is_opd
   17402 		       && r_type != R_PPC64_TOC)
   17403 		{
   17404 		  indx = h->elf.dynindx;
   17405 		  BFD_ASSERT (indx != -1);
   17406 		  outrel.r_info = ELF64_R_INFO (indx, r_type);
   17407 		}
   17408 	      else
   17409 		{
   17410 		  /* This symbol is local, or marked to become local,
   17411 		     or this is an opd section reloc which must point
   17412 		     at a local function.  */
   17413 		  outrel.r_addend += relocation;
   17414 		  if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
   17415 		    {
   17416 		      if (is_opd && h != NULL)
   17417 			{
   17418 			  /* Lie about opd entries.  This case occurs
   17419 			     when building shared libraries and we
   17420 			     reference a function in another shared
   17421 			     lib.  The same thing happens for a weak
   17422 			     definition in an application that's
   17423 			     overridden by a strong definition in a
   17424 			     shared lib.  (I believe this is a generic
   17425 			     bug in binutils handling of weak syms.)
   17426 			     In these cases we won't use the opd
   17427 			     entry in this lib.  */
   17428 			  unresolved_reloc = false;
   17429 			}
   17430 		      if (!is_opd
   17431 			  && r_type == R_PPC64_ADDR64
   17432 			  && (h != NULL
   17433 			      ? h->elf.type == STT_GNU_IFUNC
   17434 			      : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
   17435 			outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
   17436 		      else
   17437 			{
   17438 			  outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   17439 
   17440 			  /* We need to relocate .opd contents for ld.so.
   17441 			     Prelink also wants simple and consistent rules
   17442 			     for relocs.  This make all RELATIVE relocs have
   17443 			     *r_offset equal to r_addend.  */
   17444 			  relocate = true;
   17445 			}
   17446 		    }
   17447 		  else
   17448 		    {
   17449 		      if (h != NULL
   17450 			  ? h->elf.type == STT_GNU_IFUNC
   17451 			  : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
   17452 			{
   17453 			  info->callbacks->einfo
   17454 			    /* xgettext:c-format */
   17455 			    (_("%H: %s for indirect "
   17456 			       "function `%pT' unsupported\n"),
   17457 			     input_bfd, input_section, rel->r_offset,
   17458 			     ppc64_elf_howto_table[r_type]->name,
   17459 			     sym_name);
   17460 			  ret = false;
   17461 			}
   17462 		      else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
   17463 			;
   17464 		      else if (sec == NULL || sec->owner == NULL)
   17465 			{
   17466 			  bfd_set_error (bfd_error_bad_value);
   17467 			  return false;
   17468 			}
   17469 		      else
   17470 			{
   17471 			  asection *osec = sec->output_section;
   17472 
   17473 			  if ((osec->flags & SEC_THREAD_LOCAL) != 0)
   17474 			    {
   17475 			      /* TLS symbol values are relative to the
   17476 				 TLS segment.  Dynamic relocations for
   17477 				 local TLS symbols therefore can't be
   17478 				 reduced to a relocation against their
   17479 				 section symbol because it holds the
   17480 				 address of the section, not a value
   17481 				 relative to the TLS segment.  We could
   17482 				 change the .tdata dynamic section symbol
   17483 				 to be zero value but STN_UNDEF works
   17484 				 and is used elsewhere, eg. for TPREL64
   17485 				 GOT relocs against local TLS symbols.  */
   17486 			      osec = htab->elf.tls_sec;
   17487 			      indx = 0;
   17488 			    }
   17489 			  else
   17490 			    {
   17491 			      indx = elf_section_data (osec)->dynindx;
   17492 			      if (indx == 0)
   17493 				{
   17494 				  if ((osec->flags & SEC_READONLY) == 0
   17495 				      && htab->elf.data_index_section != NULL)
   17496 				    osec = htab->elf.data_index_section;
   17497 				  else
   17498 				    osec = htab->elf.text_index_section;
   17499 				  indx = elf_section_data (osec)->dynindx;
   17500 				}
   17501 			      BFD_ASSERT (indx != 0);
   17502 			    }
   17503 
   17504 			  /* We are turning this relocation into one
   17505 			     against a section symbol, so subtract out
   17506 			     the output section's address but not the
   17507 			     offset of the input section in the output
   17508 			     section.  */
   17509 			  outrel.r_addend -= osec->vma;
   17510 			}
   17511 
   17512 		      outrel.r_info = ELF64_R_INFO (indx, r_type);
   17513 		    }
   17514 		}
   17515 
   17516 	      if (!(info->enable_dt_relr
   17517 		    && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE
   17518 		    && rel->r_offset % 2 == 0
   17519 		    && input_section->alignment_power != 0
   17520 		    && ELF64_R_TYPE (orig_rel.r_info) != R_PPC64_UADDR64))
   17521 		{
   17522 		  sreloc = elf_section_data (input_section)->sreloc;
   17523 		  if (h != NULL
   17524 		      ? h->elf.type == STT_GNU_IFUNC
   17525 		      : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
   17526 		    {
   17527 		      sreloc = htab->elf.irelplt;
   17528 		      if (indx == 0 || is_static_defined (&h->elf))
   17529 			htab->elf.ifunc_resolvers = true;
   17530 		    }
   17531 		  if (sreloc == NULL)
   17532 		    abort ();
   17533 
   17534 		  if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
   17535 		      >= sreloc->size)
   17536 		    abort ();
   17537 		  loc = sreloc->contents;
   17538 		  loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
   17539 		  bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
   17540 		}
   17541 
   17542 	      if (!warned_dynamic
   17543 		  && !ppc64_glibc_dynamic_reloc (ELF64_R_TYPE (outrel.r_info)))
   17544 		{
   17545 		  info->callbacks->einfo
   17546 		    /* xgettext:c-format */
   17547 		    (_("%X%P: %pB: %s against %pT "
   17548 		       "is not supported by glibc as a dynamic relocation\n"),
   17549 		     input_bfd,
   17550 		     ppc64_elf_howto_table[ELF64_R_TYPE (outrel.r_info)]->name,
   17551 		     sym_name);
   17552 		  warned_dynamic = true;
   17553 		}
   17554 
   17555 	      /* If this reloc is against an external symbol, it will
   17556 		 be computed at runtime, so there's no need to do
   17557 		 anything now.  However, for the sake of prelink ensure
   17558 		 that the section contents are a known value.  */
   17559 	      if (!relocate)
   17560 		{
   17561 		  unresolved_reloc = false;
   17562 		  /* The value chosen here is quite arbitrary as ld.so
   17563 		     ignores section contents except for the special
   17564 		     case of .opd where the contents might be accessed
   17565 		     before relocation.  Choose zero, as that won't
   17566 		     cause reloc overflow.  */
   17567 		  relocation = 0;
   17568 		  addend = 0;
   17569 		  /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
   17570 		     to improve backward compatibility with older
   17571 		     versions of ld.  */
   17572 		  if (r_type == R_PPC64_ADDR64)
   17573 		    addend = outrel.r_addend;
   17574 		  /* Adjust pc_relative relocs to have zero in *r_offset.  */
   17575 		  else if (ppc64_elf_howto_table[r_type]->pc_relative)
   17576 		    addend = outrel.r_offset;
   17577 		}
   17578 	    }
   17579 	  break;
   17580 
   17581 	case R_PPC64_COPY:
   17582 	case R_PPC64_GLOB_DAT:
   17583 	case R_PPC64_JMP_SLOT:
   17584 	case R_PPC64_JMP_IREL:
   17585 	case R_PPC64_RELATIVE:
   17586 	  /* We shouldn't ever see these dynamic relocs in relocatable
   17587 	     files.  */
   17588 	  /* Fall through.  */
   17589 
   17590 	case R_PPC64_PLTGOT16:
   17591 	case R_PPC64_PLTGOT16_DS:
   17592 	case R_PPC64_PLTGOT16_HA:
   17593 	case R_PPC64_PLTGOT16_HI:
   17594 	case R_PPC64_PLTGOT16_LO:
   17595 	case R_PPC64_PLTGOT16_LO_DS:
   17596 	case R_PPC64_PLTREL32:
   17597 	case R_PPC64_PLTREL64:
   17598 	  /* These ones haven't been implemented yet.  */
   17599 
   17600 	  info->callbacks->einfo
   17601 	    /* xgettext:c-format */
   17602 	    (_("%P: %pB: %s is not supported for `%pT'\n"),
   17603 	     input_bfd,
   17604 	     ppc64_elf_howto_table[r_type]->name, sym_name);
   17605 
   17606 	  bfd_set_error (bfd_error_invalid_operation);
   17607 	  ret = false;
   17608 	  goto copy_reloc;
   17609 	}
   17610 
   17611       /* Multi-instruction sequences that access the TOC can be
   17612 	 optimized, eg. addis ra,r2,0; addi rb,ra,x;
   17613 	 to		nop;	       addi rb,r2,x;  */
   17614       switch (r_type)
   17615 	{
   17616 	default:
   17617 	  break;
   17618 
   17619 	case R_PPC64_GOT_TLSLD16_HI:
   17620 	case R_PPC64_GOT_TLSGD16_HI:
   17621 	case R_PPC64_GOT_TPREL16_HI:
   17622 	case R_PPC64_GOT_DTPREL16_HI:
   17623 	case R_PPC64_GOT16_HI:
   17624 	case R_PPC64_TOC16_HI:
   17625 	  /* These relocs would only be useful if building up an
   17626 	     offset to later add to r2, perhaps in an indexed
   17627 	     addressing mode instruction.  Don't try to optimize.
   17628 	     Unfortunately, the possibility of someone building up an
   17629 	     offset like this or even with the HA relocs, means that
   17630 	     we need to check the high insn when optimizing the low
   17631 	     insn.  */
   17632 	  break;
   17633 
   17634 	case R_PPC64_PLTCALL_NOTOC:
   17635 	  if (!unresolved_reloc)
   17636 	    htab->notoc_plt = 1;
   17637 	  /* Fall through.  */
   17638 	case R_PPC64_PLTCALL:
   17639 	  if (unresolved_reloc
   17640 	      && offset_in_range (input_section, rel->r_offset,
   17641 				  r_type == R_PPC64_PLTCALL ? 8 : 4))
   17642 	    {
   17643 	      /* No plt entry.  Make this into a direct call.  */
   17644 	      bfd_byte *p = contents + rel->r_offset;
   17645 	      insn = bfd_get_32 (input_bfd, p);
   17646 	      insn &= 1;
   17647 	      bfd_put_32 (input_bfd, B_DOT | insn, p);
   17648 	      if (r_type == R_PPC64_PLTCALL)
   17649 		bfd_put_32 (input_bfd, NOP, p + 4);
   17650 	      unresolved_reloc = save_unresolved_reloc;
   17651 	      r_type = R_PPC64_REL24;
   17652 	    }
   17653 	  break;
   17654 
   17655 	case R_PPC64_PLTSEQ_NOTOC:
   17656 	case R_PPC64_PLTSEQ:
   17657 	  if (unresolved_reloc)
   17658 	    {
   17659 	      unresolved_reloc = false;
   17660 	      goto nop_it;
   17661 	    }
   17662 	  break;
   17663 
   17664 	case R_PPC64_PLT_PCREL34_NOTOC:
   17665 	  if (!unresolved_reloc)
   17666 	    htab->notoc_plt = 1;
   17667 	  /* Fall through.  */
   17668 	case R_PPC64_PLT_PCREL34:
   17669 	  if (unresolved_reloc
   17670 	      && offset_in_range (input_section, rel->r_offset, 8))
   17671 	    {
   17672 	      bfd_byte *p = contents + rel->r_offset;
   17673 	      bfd_put_32 (input_bfd, PNOP >> 32, p);
   17674 	      bfd_put_32 (input_bfd, PNOP, p + 4);
   17675 	      unresolved_reloc = false;
   17676 	      goto copy_reloc;
   17677 	    }
   17678 	  break;
   17679 
   17680 	case R_PPC64_PLT16_HA:
   17681 	  if (unresolved_reloc)
   17682 	    {
   17683 	      unresolved_reloc = false;
   17684 	      goto nop_it;
   17685 	    }
   17686 	  /* Fall through.  */
   17687 	case R_PPC64_GOT_TLSLD16_HA:
   17688 	case R_PPC64_GOT_TLSGD16_HA:
   17689 	case R_PPC64_GOT_TPREL16_HA:
   17690 	case R_PPC64_GOT_DTPREL16_HA:
   17691 	case R_PPC64_GOT16_HA:
   17692 	case R_PPC64_TOC16_HA:
   17693 	  if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
   17694 	      && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn
   17695 	      && !(bfd_link_pic (info)
   17696 		   && (h != NULL
   17697 		       ? bfd_is_abs_symbol (&h->elf.root)
   17698 		       : sec == bfd_abs_section_ptr)))
   17699 	    {
   17700 	      bfd_byte *p;
   17701 	    nop_it:
   17702 	      if (offset_in_range (input_section, rel->r_offset & ~3, 4))
   17703 		{
   17704 		  p = contents + (rel->r_offset & ~3);
   17705 		  bfd_put_32 (input_bfd, NOP, p);
   17706 		  goto copy_reloc;
   17707 		}
   17708 	    }
   17709 	  break;
   17710 
   17711 	case R_PPC64_PLT16_LO:
   17712 	case R_PPC64_PLT16_LO_DS:
   17713 	  if (unresolved_reloc)
   17714 	    {
   17715 	      unresolved_reloc = false;
   17716 	      goto nop_it;
   17717 	    }
   17718 	  /* Fall through.  */
   17719 	case R_PPC64_GOT_TLSLD16_LO:
   17720 	case R_PPC64_GOT_TLSGD16_LO:
   17721 	case R_PPC64_GOT_TPREL16_LO_DS:
   17722 	case R_PPC64_GOT_DTPREL16_LO_DS:
   17723 	case R_PPC64_GOT16_LO:
   17724 	case R_PPC64_GOT16_LO_DS:
   17725 	case R_PPC64_TOC16_LO:
   17726 	case R_PPC64_TOC16_LO_DS:
   17727 	  if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
   17728 	      && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn
   17729 	      && !(bfd_link_pic (info)
   17730 		   && (h != NULL
   17731 		       ? bfd_is_abs_symbol (&h->elf.root)
   17732 		       : sec == bfd_abs_section_ptr))
   17733 	      && offset_in_range (input_section, rel->r_offset & ~3, 4))
   17734 	    {
   17735 	      bfd_byte *p = contents + (rel->r_offset & ~3);
   17736 	      insn = bfd_get_32 (input_bfd, p);
   17737 	      if ((insn & (0x3fu << 26)) == 12u << 26 /* addic */)
   17738 		{
   17739 		  /* Transform addic to addi when we change reg.  */
   17740 		  insn &= ~((0x3fu << 26) | (0x1f << 16));
   17741 		  insn |= (14u << 26) | (2 << 16);
   17742 		}
   17743 	      else
   17744 		{
   17745 		  insn &= ~(0x1f << 16);
   17746 		  insn |= 2 << 16;
   17747 		}
   17748 	      bfd_put_32 (input_bfd, insn, p);
   17749 	    }
   17750 	  break;
   17751 
   17752 	case R_PPC64_TPREL16_HA:
   17753 	  if (htab->do_tls_opt
   17754 	      && relocation + addend + 0x8000 < 0x10000
   17755 	      && offset_in_range (input_section, rel->r_offset & ~3, 4))
   17756 	    {
   17757 	      bfd_byte *p = contents + (rel->r_offset & ~3);
   17758 	      bfd_put_32 (input_bfd, NOP, p);
   17759 	      goto copy_reloc;
   17760 	    }
   17761 	  break;
   17762 
   17763 	case R_PPC64_TPREL16_LO:
   17764 	case R_PPC64_TPREL16_LO_DS:
   17765 	  if (htab->do_tls_opt
   17766 	      && relocation + addend + 0x8000 < 0x10000
   17767 	      && offset_in_range (input_section, rel->r_offset & ~3, 4))
   17768 	    {
   17769 	      bfd_byte *p = contents + (rel->r_offset & ~3);
   17770 	      insn = bfd_get_32 (input_bfd, p);
   17771 	      insn &= ~(0x1f << 16);
   17772 	      insn |= 13 << 16;
   17773 	      bfd_put_32 (input_bfd, insn, p);
   17774 	    }
   17775 	  break;
   17776 	}
   17777 
   17778       /* Do any further special processing.  */
   17779       switch (r_type)
   17780 	{
   17781 	default:
   17782 	  break;
   17783 
   17784 	case R_PPC64_REL16_HA:
   17785 	case R_PPC64_REL16_HIGHA:
   17786 	case R_PPC64_REL16_HIGHERA:
   17787 	case R_PPC64_REL16_HIGHESTA:
   17788 	case R_PPC64_REL16DX_HA:
   17789 	case R_PPC64_ADDR16_HA:
   17790 	case R_PPC64_ADDR16_HIGHA:
   17791 	case R_PPC64_ADDR16_HIGHERA:
   17792 	case R_PPC64_ADDR16_HIGHESTA:
   17793 	case R_PPC64_TOC16_HA:
   17794 	case R_PPC64_SECTOFF_HA:
   17795 	case R_PPC64_TPREL16_HA:
   17796 	case R_PPC64_TPREL16_HIGHA:
   17797 	case R_PPC64_TPREL16_HIGHERA:
   17798 	case R_PPC64_TPREL16_HIGHESTA:
   17799 	case R_PPC64_DTPREL16_HA:
   17800 	case R_PPC64_DTPREL16_HIGHA:
   17801 	case R_PPC64_DTPREL16_HIGHERA:
   17802 	case R_PPC64_DTPREL16_HIGHESTA:
   17803 	  /* It's just possible that this symbol is a weak symbol
   17804 	     that's not actually defined anywhere. In that case,
   17805 	     'sec' would be NULL, and we should leave the symbol
   17806 	     alone (it will be set to zero elsewhere in the link).  */
   17807 	  if (sec == NULL)
   17808 	    break;
   17809 	  /* Fall through.  */
   17810 
   17811 	case R_PPC64_GOT16_HA:
   17812 	case R_PPC64_PLTGOT16_HA:
   17813 	case R_PPC64_PLT16_HA:
   17814 	case R_PPC64_GOT_TLSGD16_HA:
   17815 	case R_PPC64_GOT_TLSLD16_HA:
   17816 	case R_PPC64_GOT_TPREL16_HA:
   17817 	case R_PPC64_GOT_DTPREL16_HA:
   17818 	  /* Add 0x10000 if sign bit in 0:15 is set.
   17819 	     Bits 0:15 are not used.  */
   17820 	  addend += 0x8000;
   17821 	  break;
   17822 
   17823 	case R_PPC64_D34_HA30:
   17824 	case R_PPC64_ADDR16_HIGHERA34:
   17825 	case R_PPC64_ADDR16_HIGHESTA34:
   17826 	case R_PPC64_REL16_HIGHERA34:
   17827 	case R_PPC64_REL16_HIGHESTA34:
   17828 	  if (sec != NULL)
   17829 	    addend += 1ULL << 33;
   17830 	  break;
   17831 
   17832 	case R_PPC64_ADDR16_DS:
   17833 	case R_PPC64_ADDR16_LO_DS:
   17834 	case R_PPC64_GOT16_DS:
   17835 	case R_PPC64_GOT16_LO_DS:
   17836 	case R_PPC64_PLT16_LO_DS:
   17837 	case R_PPC64_SECTOFF_DS:
   17838 	case R_PPC64_SECTOFF_LO_DS:
   17839 	case R_PPC64_TOC16_DS:
   17840 	case R_PPC64_TOC16_LO_DS:
   17841 	case R_PPC64_PLTGOT16_DS:
   17842 	case R_PPC64_PLTGOT16_LO_DS:
   17843 	case R_PPC64_GOT_TPREL16_DS:
   17844 	case R_PPC64_GOT_TPREL16_LO_DS:
   17845 	case R_PPC64_GOT_DTPREL16_DS:
   17846 	case R_PPC64_GOT_DTPREL16_LO_DS:
   17847 	case R_PPC64_TPREL16_DS:
   17848 	case R_PPC64_TPREL16_LO_DS:
   17849 	case R_PPC64_DTPREL16_DS:
   17850 	case R_PPC64_DTPREL16_LO_DS:
   17851 	  if (!offset_in_range (input_section, rel->r_offset & ~3, 4))
   17852 	    break;
   17853 	  insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
   17854 	  mask = 3;
   17855 	  /* If this reloc is against an lq, lxv, or stxv insn, then
   17856 	     the value must be a multiple of 16.  This is somewhat of
   17857 	     a hack, but the "correct" way to do this by defining _DQ
   17858 	     forms of all the _DS relocs bloats all reloc switches in
   17859 	     this file.  It doesn't make much sense to use these
   17860 	     relocs in data, so testing the insn should be safe.  */
   17861 	  if ((insn & (0x3fu << 26)) == (56u << 26)
   17862 	      || ((insn & (0x3fu << 26)) == (61u << 26) && (insn & 3) == 1))
   17863 	    mask = 15;
   17864 	  relocation += addend;
   17865 	  addend = insn & (mask ^ 3);
   17866 	  if ((relocation & mask) != 0)
   17867 	    {
   17868 	      relocation ^= relocation & mask;
   17869 	      info->callbacks->einfo
   17870 		/* xgettext:c-format */
   17871 		(_("%H: error: %s not a multiple of %u\n"),
   17872 		 input_bfd, input_section, rel->r_offset,
   17873 		 ppc64_elf_howto_table[r_type]->name,
   17874 		 mask + 1);
   17875 	      bfd_set_error (bfd_error_bad_value);
   17876 	      ret = false;
   17877 	      goto copy_reloc;
   17878 	    }
   17879 	  break;
   17880 	}
   17881 
   17882       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
   17883 	 because such sections are not SEC_ALLOC and thus ld.so will
   17884 	 not process them.  */
   17885       howto = ppc64_elf_howto_table[(int) r_type];
   17886       if (unresolved_reloc
   17887 	  && !((input_section->flags & SEC_DEBUGGING) != 0
   17888 	       && h->elf.def_dynamic)
   17889 	  && _bfd_elf_section_offset (output_bfd, info, input_section,
   17890 				      rel->r_offset) != (bfd_vma) -1)
   17891 	{
   17892 	  info->callbacks->einfo
   17893 	    /* xgettext:c-format */
   17894 	    (_("%H: unresolvable %s against `%pT'\n"),
   17895 	     input_bfd, input_section, rel->r_offset,
   17896 	     howto->name,
   17897 	     h->elf.root.root.string);
   17898 	  ret = false;
   17899 	}
   17900 
   17901       /* 16-bit fields in insns mostly have signed values, but a
   17902 	 few insns have 16-bit unsigned values.  Really, we should
   17903 	 have different reloc types.  */
   17904       if (howto->complain_on_overflow != complain_overflow_dont
   17905 	  && howto->dst_mask == 0xffff
   17906 	  && (input_section->flags & SEC_CODE) != 0
   17907 	  && offset_in_range (input_section, rel->r_offset & ~3, 4))
   17908 	{
   17909 	  enum complain_overflow complain = complain_overflow_signed;
   17910 
   17911 	  insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
   17912 	  if ((insn & (0x3fu << 26)) == 10u << 26 /* cmpli */)
   17913 	    complain = complain_overflow_bitfield;
   17914 	  else if (howto->rightshift == 0
   17915 		   ? ((insn & (0x3fu << 26)) == 28u << 26 /* andi */
   17916 		      || (insn & (0x3fu << 26)) == 24u << 26 /* ori */
   17917 		      || (insn & (0x3fu << 26)) == 26u << 26 /* xori */)
   17918 		   : ((insn & (0x3fu << 26)) == 29u << 26 /* andis */
   17919 		      || (insn & (0x3fu << 26)) == 25u << 26 /* oris */
   17920 		      || (insn & (0x3fu << 26)) == 27u << 26 /* xoris */))
   17921 	    complain = complain_overflow_unsigned;
   17922 	  if (howto->complain_on_overflow != complain)
   17923 	    {
   17924 	      alt_howto = *howto;
   17925 	      alt_howto.complain_on_overflow = complain;
   17926 	      howto = &alt_howto;
   17927 	    }
   17928 	}
   17929 
   17930       switch (r_type)
   17931 	{
   17932 	  /* Split field relocs aren't handled by _bfd_final_link_relocate.  */
   17933 	case R_PPC64_D34:
   17934 	case R_PPC64_D34_LO:
   17935 	case R_PPC64_D34_HI30:
   17936 	case R_PPC64_D34_HA30:
   17937 	case R_PPC64_PCREL34:
   17938 	case R_PPC64_GOT_PCREL34:
   17939 	case R_PPC64_TPREL34:
   17940 	case R_PPC64_DTPREL34:
   17941 	case R_PPC64_GOT_TLSGD_PCREL34:
   17942 	case R_PPC64_GOT_TLSLD_PCREL34:
   17943 	case R_PPC64_GOT_TPREL_PCREL34:
   17944 	case R_PPC64_GOT_DTPREL_PCREL34:
   17945 	case R_PPC64_PLT_PCREL34:
   17946 	case R_PPC64_PLT_PCREL34_NOTOC:
   17947 	case R_PPC64_D28:
   17948 	case R_PPC64_PCREL28:
   17949 	  if (!offset_in_range (input_section, rel->r_offset, 8))
   17950 	    r = bfd_reloc_outofrange;
   17951 	  else
   17952 	    {
   17953 	      relocation += addend;
   17954 	      if (howto->pc_relative)
   17955 		relocation -= (rel->r_offset
   17956 			       + input_section->output_offset
   17957 			       + input_section->output_section->vma);
   17958 	      relocation >>= howto->rightshift;
   17959 
   17960 	      pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   17961 	      pinsn <<= 32;
   17962 	      pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
   17963 
   17964 	      pinsn &= ~howto->dst_mask;
   17965 	      pinsn |= (((relocation << 16) | (relocation & 0xffff))
   17966 			& howto->dst_mask);
   17967 	      bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
   17968 	      bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
   17969 	      r = bfd_reloc_ok;
   17970 	      if (howto->complain_on_overflow == complain_overflow_signed
   17971 		  && (relocation + (1ULL << (howto->bitsize - 1))
   17972 		      >= 1ULL << howto->bitsize))
   17973 		r = bfd_reloc_overflow;
   17974 	    }
   17975 	  break;
   17976 
   17977 	case R_PPC64_REL16DX_HA:
   17978 	  if (!offset_in_range (input_section, rel->r_offset, 4))
   17979 	    r = bfd_reloc_outofrange;
   17980 	  else
   17981 	    {
   17982 	      relocation += addend;
   17983 	      relocation -= (rel->r_offset
   17984 			     + input_section->output_offset
   17985 			     + input_section->output_section->vma);
   17986 	      relocation = (bfd_signed_vma) relocation >> 16;
   17987 	      insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   17988 	      insn &= ~0x1fffc1;
   17989 	      insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
   17990 	      bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   17991 	      r = bfd_reloc_ok;
   17992 	      if (relocation + 0x8000 > 0xffff)
   17993 		r = bfd_reloc_overflow;
   17994 	    }
   17995 	  break;
   17996 
   17997 	default:
   17998 	  r = _bfd_final_link_relocate (howto, input_bfd, input_section,
   17999 					contents, rel->r_offset,
   18000 					relocation, addend);
   18001 	}
   18002 
   18003       if (r != bfd_reloc_ok)
   18004 	{
   18005 	  char *more_info = NULL;
   18006 	  const char *reloc_name = howto->name;
   18007 
   18008 	  if (reloc_dest != DEST_NORMAL)
   18009 	    {
   18010 	      more_info = bfd_malloc (strlen (reloc_name) + 8);
   18011 	      if (more_info != NULL)
   18012 		{
   18013 		  strcpy (more_info, reloc_name);
   18014 		  strcat (more_info, (reloc_dest == DEST_OPD
   18015 				      ? " (OPD)" : " (stub)"));
   18016 		  reloc_name = more_info;
   18017 		}
   18018 	    }
   18019 
   18020 	  if (r == bfd_reloc_overflow)
   18021 	    {
   18022 	      /* On code like "if (foo) foo();" don't report overflow
   18023 		 on a branch to zero when foo is undefined.  */
   18024 	      if (!warned
   18025 		  && (reloc_dest == DEST_STUB
   18026 		      || !(h != NULL
   18027 			   && (h->elf.root.type == bfd_link_hash_undefweak
   18028 			       || h->elf.root.type == bfd_link_hash_undefined)
   18029 			   && is_branch_reloc (r_type))))
   18030 		info->callbacks->reloc_overflow
   18031 		  (info, (struct bfd_link_hash_entry *) h, sym_name,
   18032 		   reloc_name, orig_rel.r_addend, input_bfd, input_section,
   18033 		   rel->r_offset);
   18034 	    }
   18035 	  else
   18036 	    {
   18037 	      info->callbacks->einfo
   18038 		/* xgettext:c-format */
   18039 		(_("%H: %s against `%pT': error %d\n"),
   18040 		 input_bfd, input_section, rel->r_offset,
   18041 		 reloc_name, sym_name, (int) r);
   18042 	      ret = false;
   18043 	    }
   18044 	  free (more_info);
   18045 	}
   18046     copy_reloc:
   18047       if (wrel != rel)
   18048 	*wrel = *rel;
   18049     }
   18050 
   18051   if (wrel != rel)
   18052     {
   18053       Elf_Internal_Shdr *rel_hdr;
   18054       size_t deleted = rel - wrel;
   18055 
   18056       rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
   18057       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
   18058       if (rel_hdr->sh_size == 0)
   18059 	{
   18060 	  /* It is too late to remove an empty reloc section.  Leave
   18061 	     one NONE reloc.
   18062 	     ??? What is wrong with an empty section???  */
   18063 	  rel_hdr->sh_size = rel_hdr->sh_entsize;
   18064 	  deleted -= 1;
   18065 	}
   18066       rel_hdr = _bfd_elf_single_rel_hdr (input_section);
   18067       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
   18068       input_section->reloc_count -= deleted;
   18069     }
   18070 
   18071   /* If we're emitting relocations, then shortly after this function
   18072      returns, reloc offsets and addends for this section will be
   18073      adjusted.  Worse, reloc symbol indices will be for the output
   18074      file rather than the input.  Save a copy of the relocs for
   18075      opd_entry_value.  */
   18076   if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
   18077     {
   18078       bfd_size_type amt;
   18079       amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
   18080       rel = bfd_alloc (input_bfd, amt);
   18081       BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
   18082       ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
   18083       if (rel == NULL)
   18084 	return false;
   18085       memcpy (rel, relocs, amt);
   18086     }
   18087   return ret;
   18088 }
   18089 
   18090 /* Adjust the value of any local symbols in opd sections.  */
   18091 
   18092 static int
   18093 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
   18094 			      const char *name ATTRIBUTE_UNUSED,
   18095 			      Elf_Internal_Sym *elfsym,
   18096 			      asection *input_sec,
   18097 			      struct elf_link_hash_entry *h)
   18098 {
   18099   struct _opd_sec_data *opd;
   18100   long adjust;
   18101   bfd_vma value;
   18102 
   18103   if (h != NULL)
   18104     return 1;
   18105 
   18106   opd = get_opd_info (input_sec);
   18107   if (opd == NULL || opd->adjust == NULL)
   18108     return 1;
   18109 
   18110   value = elfsym->st_value - input_sec->output_offset;
   18111   if (!bfd_link_relocatable (info))
   18112     value -= input_sec->output_section->vma;
   18113 
   18114   adjust = opd->adjust[OPD_NDX (value)];
   18115   if (adjust == -1)
   18116     return 2;
   18117 
   18118   elfsym->st_value += adjust;
   18119   return 1;
   18120 }
   18121 
   18122 /* Finish up dynamic symbol handling.  We set the contents of various
   18123    dynamic sections here.  */
   18124 
   18125 static bool
   18126 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
   18127 				 struct bfd_link_info *info,
   18128 				 struct elf_link_hash_entry *h,
   18129 				 Elf_Internal_Sym *sym)
   18130 {
   18131   struct ppc_link_hash_table *htab;
   18132   struct plt_entry *ent;
   18133 
   18134   htab = ppc_hash_table (info);
   18135   if (htab == NULL)
   18136     return false;
   18137 
   18138   if (!htab->opd_abi && !h->def_regular)
   18139     for (ent = h->plt.plist; ent != NULL; ent = ent->next)
   18140       if (ent->plt.offset != (bfd_vma) -1)
   18141 	{
   18142 	  /* Mark the symbol as undefined, rather than as
   18143 	     defined in glink.  Leave the value if there were
   18144 	     any relocations where pointer equality matters
   18145 	     (this is a clue for the dynamic linker, to make
   18146 	     function pointer comparisons work between an
   18147 	     application and shared library), otherwise set it
   18148 	     to zero.  */
   18149 	  sym->st_shndx = SHN_UNDEF;
   18150 	  if (!h->pointer_equality_needed)
   18151 	    sym->st_value = 0;
   18152 	  else if (!h->ref_regular_nonweak)
   18153 	    {
   18154 	      /* This breaks function pointer comparisons, but
   18155 		 that is better than breaking tests for a NULL
   18156 		 function pointer.  */
   18157 	      sym->st_value = 0;
   18158 	    }
   18159 	  break;
   18160 	}
   18161 
   18162   if (h->needs_copy
   18163       && (h->root.type == bfd_link_hash_defined
   18164 	  || h->root.type == bfd_link_hash_defweak)
   18165       && (h->root.u.def.section == htab->elf.sdynbss
   18166 	  || h->root.u.def.section == htab->elf.sdynrelro))
   18167     {
   18168       /* This symbol needs a copy reloc.  Set it up.  */
   18169       Elf_Internal_Rela rela;
   18170       asection *srel;
   18171       bfd_byte *loc;
   18172 
   18173       if (h->dynindx == -1)
   18174 	abort ();
   18175 
   18176       rela.r_offset = defined_sym_val (h);
   18177       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
   18178       rela.r_addend = 0;
   18179       if (h->root.u.def.section == htab->elf.sdynrelro)
   18180 	srel = htab->elf.sreldynrelro;
   18181       else
   18182 	srel = htab->elf.srelbss;
   18183       loc = srel->contents;
   18184       loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
   18185       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
   18186     }
   18187 
   18188   return true;
   18189 }
   18190 
   18191 /* Used to decide how to sort relocs in an optimal manner for the
   18192    dynamic linker, before writing them out.  */
   18193 
   18194 static enum elf_reloc_type_class
   18195 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
   18196 			    const asection *rel_sec,
   18197 			    const Elf_Internal_Rela *rela)
   18198 {
   18199   enum elf_ppc64_reloc_type r_type;
   18200   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   18201 
   18202   if (rel_sec == htab->elf.irelplt)
   18203     return reloc_class_ifunc;
   18204 
   18205   r_type = ELF64_R_TYPE (rela->r_info);
   18206   switch (r_type)
   18207     {
   18208     case R_PPC64_RELATIVE:
   18209       return reloc_class_relative;
   18210     case R_PPC64_JMP_SLOT:
   18211       return reloc_class_plt;
   18212     case R_PPC64_COPY:
   18213       return reloc_class_copy;
   18214     default:
   18215       return reloc_class_normal;
   18216     }
   18217 }
   18218 
   18219 /* Finish up the dynamic sections.  */
   18220 
   18221 static bool
   18222 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
   18223 				   struct bfd_link_info *info)
   18224 {
   18225   struct ppc_link_hash_table *htab;
   18226   bfd *dynobj;
   18227   asection *sdyn;
   18228 
   18229   htab = ppc_hash_table (info);
   18230   if (htab == NULL)
   18231     return false;
   18232 
   18233   dynobj = htab->elf.dynobj;
   18234   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
   18235 
   18236   if (htab->elf.dynamic_sections_created)
   18237     {
   18238       Elf64_External_Dyn *dyncon, *dynconend;
   18239 
   18240       if (sdyn == NULL || htab->elf.sgot == NULL)
   18241 	abort ();
   18242 
   18243       dyncon = (Elf64_External_Dyn *) sdyn->contents;
   18244       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
   18245       for (; dyncon < dynconend; dyncon++)
   18246 	{
   18247 	  Elf_Internal_Dyn dyn;
   18248 	  asection *s;
   18249 
   18250 	  bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
   18251 
   18252 	  switch (dyn.d_tag)
   18253 	    {
   18254 	    default:
   18255 	      continue;
   18256 
   18257 	    case DT_PPC64_GLINK:
   18258 	      s = htab->glink;
   18259 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   18260 	      /* We stupidly defined DT_PPC64_GLINK to be the start
   18261 		 of glink rather than the first entry point, which is
   18262 		 what ld.so needs, and now have a bigger stub to
   18263 		 support automatic multiple TOCs.  */
   18264 	      dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
   18265 	      break;
   18266 
   18267 	    case DT_PPC64_OPD:
   18268 	      s = bfd_get_section_by_name (output_bfd, ".opd");
   18269 	      if (s == NULL)
   18270 		continue;
   18271 	      dyn.d_un.d_ptr = s->vma;
   18272 	      break;
   18273 
   18274 	    case DT_PPC64_OPT:
   18275 	      if ((htab->do_multi_toc && htab->multi_toc_needed)
   18276 		  || htab->notoc_plt)
   18277 		dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
   18278 	      if (htab->has_plt_localentry0)
   18279 		dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
   18280 	      break;
   18281 
   18282 	    case DT_PPC64_OPDSZ:
   18283 	      s = bfd_get_section_by_name (output_bfd, ".opd");
   18284 	      if (s == NULL)
   18285 		continue;
   18286 	      dyn.d_un.d_val = s->size;
   18287 	      break;
   18288 
   18289 	    case DT_PLTGOT:
   18290 	      s = htab->elf.splt;
   18291 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   18292 	      break;
   18293 
   18294 	    case DT_JMPREL:
   18295 	      s = htab->elf.srelplt;
   18296 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   18297 	      break;
   18298 
   18299 	    case DT_PLTRELSZ:
   18300 	      dyn.d_un.d_val = htab->elf.srelplt->size;
   18301 	      break;
   18302 
   18303 	    case DT_TEXTREL:
   18304 	      if (htab->elf.ifunc_resolvers)
   18305 		info->callbacks->einfo
   18306 		  (_("%P: warning: text relocations and GNU indirect "
   18307 		     "functions may result in a segfault at runtime\n"));
   18308 	      continue;
   18309 	    }
   18310 
   18311 	  bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
   18312 	}
   18313     }
   18314 
   18315   if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
   18316       && htab->elf.sgot->output_section != bfd_abs_section_ptr)
   18317     {
   18318       /* Fill in the first entry in the global offset table.
   18319 	 We use it to hold the link-time TOCbase.  */
   18320       bfd_put_64 (output_bfd,
   18321 		  elf_gp (output_bfd) + TOC_BASE_OFF,
   18322 		  htab->elf.sgot->contents);
   18323 
   18324       /* Set .got entry size.  */
   18325       elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
   18326 	= 8;
   18327     }
   18328 
   18329   if (htab->elf.splt != NULL && htab->elf.splt->size != 0
   18330       && htab->elf.splt->output_section != bfd_abs_section_ptr)
   18331     {
   18332       /* Set .plt entry size.  */
   18333       elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
   18334 	= PLT_ENTRY_SIZE (htab);
   18335     }
   18336 
   18337   /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
   18338      brlt ourselves if emitrelocations.  */
   18339   if (htab->brlt != NULL
   18340       && htab->brlt->reloc_count != 0
   18341       && !_bfd_elf_link_output_relocs (output_bfd,
   18342 				       htab->brlt,
   18343 				       elf_section_data (htab->brlt)->rela.hdr,
   18344 				       elf_section_data (htab->brlt)->relocs,
   18345 				       NULL))
   18346     return false;
   18347 
   18348   if (htab->glink != NULL
   18349       && htab->glink->reloc_count != 0
   18350       && !_bfd_elf_link_output_relocs (output_bfd,
   18351 				       htab->glink,
   18352 				       elf_section_data (htab->glink)->rela.hdr,
   18353 				       elf_section_data (htab->glink)->relocs,
   18354 				       NULL))
   18355     return false;
   18356 
   18357 
   18358   if (htab->glink_eh_frame != NULL
   18359       && htab->glink_eh_frame->size != 0
   18360       && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
   18361       && !_bfd_elf_write_section_eh_frame (output_bfd, info,
   18362 					   htab->glink_eh_frame,
   18363 					   htab->glink_eh_frame->contents))
   18364     return false;
   18365 
   18366   /* We need to handle writing out multiple GOT sections ourselves,
   18367      since we didn't add them to DYNOBJ.  We know dynobj is the first
   18368      bfd.  */
   18369   while ((dynobj = dynobj->link.next) != NULL)
   18370     {
   18371       asection *s;
   18372 
   18373       if (!is_ppc64_elf (dynobj))
   18374 	continue;
   18375 
   18376       s = ppc64_elf_tdata (dynobj)->got;
   18377       if (s != NULL
   18378 	  && s->size != 0
   18379 	  && s->output_section != bfd_abs_section_ptr
   18380 	  && !bfd_set_section_contents (output_bfd, s->output_section,
   18381 					s->contents, s->output_offset,
   18382 					s->size))
   18383 	return false;
   18384       s = ppc64_elf_tdata (dynobj)->relgot;
   18385       if (s != NULL
   18386 	  && s->size != 0
   18387 	  && s->output_section != bfd_abs_section_ptr
   18388 	  && !bfd_set_section_contents (output_bfd, s->output_section,
   18389 					s->contents, s->output_offset,
   18390 					s->size))
   18391 	return false;
   18392     }
   18393 
   18394   return true;
   18395 }
   18396 
   18397 #include "elf64-target.h"
   18398 
   18399 /* FreeBSD support */
   18400 
   18401 #undef  TARGET_LITTLE_SYM
   18402 #define TARGET_LITTLE_SYM powerpc_elf64_fbsd_le_vec
   18403 #undef  TARGET_LITTLE_NAME
   18404 #define TARGET_LITTLE_NAME "elf64-powerpcle-freebsd"
   18405 
   18406 #undef  TARGET_BIG_SYM
   18407 #define TARGET_BIG_SYM	powerpc_elf64_fbsd_vec
   18408 #undef  TARGET_BIG_NAME
   18409 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
   18410 
   18411 #undef  ELF_OSABI
   18412 #define	ELF_OSABI       ELFOSABI_FREEBSD
   18413 
   18414 #undef  elf64_bed
   18415 #define elf64_bed	elf64_powerpc_fbsd_bed
   18416 
   18417 #include "elf64-target.h"
   18418