Home | History | Annotate | Download | only in bfd
      1 /* PowerPC64-specific support for 64-bit ELF.
      2    Copyright (C) 1999-2014 Free Software Foundation, Inc.
      3    Written by Linus Nordberg, Swox AB <info (at) swox.com>,
      4    based on elf32-ppc.c by Ian Lance Taylor.
      5    Largely rewritten by Alan Modra.
      6 
      7    This file is part of BFD, the Binary File Descriptor library.
      8 
      9    This program is free software; you can redistribute it and/or modify
     10    it under the terms of the GNU General Public License as published by
     11    the Free Software Foundation; either version 3 of the License, or
     12    (at your option) any later version.
     13 
     14    This program is distributed in the hope that it will be useful,
     15    but WITHOUT ANY WARRANTY; without even the implied warranty of
     16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17    GNU General Public License for more details.
     18 
     19    You should have received a copy of the GNU General Public License along
     20    with this program; if not, write to the Free Software Foundation, Inc.,
     21    51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
     22 
     23 
     24 /* The 64-bit PowerPC ELF ABI may be found at
     25    http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
     26    http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html  */
     27 
     28 #include "sysdep.h"
     29 #include <stdarg.h>
     30 #include "bfd.h"
     31 #include "bfdlink.h"
     32 #include "libbfd.h"
     33 #include "elf-bfd.h"
     34 #include "elf/ppc64.h"
     35 #include "elf64-ppc.h"
     36 #include "dwarf2.h"
     37 
     38 static bfd_reloc_status_type ppc64_elf_ha_reloc
     39   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     40 static bfd_reloc_status_type ppc64_elf_branch_reloc
     41   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     42 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
     43   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     44 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
     45   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     46 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
     47   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     48 static bfd_reloc_status_type ppc64_elf_toc_reloc
     49   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     50 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
     51   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     52 static bfd_reloc_status_type ppc64_elf_toc64_reloc
     53   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     54 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
     55   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     56 static bfd_vma opd_entry_value
     57   (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
     58 
     59 #define TARGET_LITTLE_SYM	powerpc_elf64_le_vec
     60 #define TARGET_LITTLE_NAME	"elf64-powerpcle"
     61 #define TARGET_BIG_SYM		powerpc_elf64_vec
     62 #define TARGET_BIG_NAME		"elf64-powerpc"
     63 #define ELF_ARCH		bfd_arch_powerpc
     64 #define ELF_TARGET_ID		PPC64_ELF_DATA
     65 #define ELF_MACHINE_CODE	EM_PPC64
     66 #define ELF_MAXPAGESIZE		0x10000
     67 #define ELF_COMMONPAGESIZE	0x1000
     68 #define elf_info_to_howto	ppc64_elf_info_to_howto
     69 
     70 #define elf_backend_want_got_sym 0
     71 #define elf_backend_want_plt_sym 0
     72 #define elf_backend_plt_alignment 3
     73 #define elf_backend_plt_not_loaded 1
     74 #define elf_backend_got_header_size 8
     75 #define elf_backend_can_gc_sections 1
     76 #define elf_backend_can_refcount 1
     77 #define elf_backend_rela_normal 1
     78 #define elf_backend_default_execstack 0
     79 
     80 #define bfd_elf64_mkobject		      ppc64_elf_mkobject
     81 #define bfd_elf64_bfd_reloc_type_lookup	      ppc64_elf_reloc_type_lookup
     82 #define bfd_elf64_bfd_reloc_name_lookup	      ppc64_elf_reloc_name_lookup
     83 #define bfd_elf64_bfd_merge_private_bfd_data  ppc64_elf_merge_private_bfd_data
     84 #define bfd_elf64_bfd_print_private_bfd_data  ppc64_elf_print_private_bfd_data
     85 #define bfd_elf64_new_section_hook	      ppc64_elf_new_section_hook
     86 #define bfd_elf64_bfd_link_hash_table_create  ppc64_elf_link_hash_table_create
     87 #define bfd_elf64_get_synthetic_symtab	      ppc64_elf_get_synthetic_symtab
     88 #define bfd_elf64_bfd_link_just_syms	      ppc64_elf_link_just_syms
     89 
     90 #define elf_backend_object_p		      ppc64_elf_object_p
     91 #define elf_backend_grok_prstatus	      ppc64_elf_grok_prstatus
     92 #define elf_backend_grok_psinfo		      ppc64_elf_grok_psinfo
     93 #define elf_backend_write_core_note	      ppc64_elf_write_core_note
     94 #define elf_backend_create_dynamic_sections   ppc64_elf_create_dynamic_sections
     95 #define elf_backend_copy_indirect_symbol      ppc64_elf_copy_indirect_symbol
     96 #define elf_backend_add_symbol_hook	      ppc64_elf_add_symbol_hook
     97 #define elf_backend_check_directives	      ppc64_elf_before_check_relocs
     98 #define elf_backend_notice_as_needed	      ppc64_elf_notice_as_needed
     99 #define elf_backend_archive_symbol_lookup     ppc64_elf_archive_symbol_lookup
    100 #define elf_backend_check_relocs	      ppc64_elf_check_relocs
    101 #define elf_backend_gc_keep		      ppc64_elf_gc_keep
    102 #define elf_backend_gc_mark_dynamic_ref       ppc64_elf_gc_mark_dynamic_ref
    103 #define elf_backend_gc_mark_hook	      ppc64_elf_gc_mark_hook
    104 #define elf_backend_gc_sweep_hook	      ppc64_elf_gc_sweep_hook
    105 #define elf_backend_adjust_dynamic_symbol     ppc64_elf_adjust_dynamic_symbol
    106 #define elf_backend_hide_symbol		      ppc64_elf_hide_symbol
    107 #define elf_backend_maybe_function_sym	      ppc64_elf_maybe_function_sym
    108 #define elf_backend_always_size_sections      ppc64_elf_func_desc_adjust
    109 #define elf_backend_size_dynamic_sections     ppc64_elf_size_dynamic_sections
    110 #define elf_backend_hash_symbol		      ppc64_elf_hash_symbol
    111 #define elf_backend_init_index_section	      _bfd_elf_init_2_index_sections
    112 #define elf_backend_action_discarded	      ppc64_elf_action_discarded
    113 #define elf_backend_relocate_section	      ppc64_elf_relocate_section
    114 #define elf_backend_finish_dynamic_symbol     ppc64_elf_finish_dynamic_symbol
    115 #define elf_backend_reloc_type_class	      ppc64_elf_reloc_type_class
    116 #define elf_backend_finish_dynamic_sections   ppc64_elf_finish_dynamic_sections
    117 #define elf_backend_link_output_symbol_hook   ppc64_elf_output_symbol_hook
    118 #define elf_backend_special_sections	      ppc64_elf_special_sections
    119 #define elf_backend_merge_symbol_attribute    ppc64_elf_merge_symbol_attribute
    120 
    121 /* The name of the dynamic interpreter.  This is put in the .interp
    122    section.  */
    123 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
    124 
    125 /* The size in bytes of an entry in the procedure linkage table.  */
    126 #define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
    127 
    128 /* The initial size of the plt reserved for the dynamic linker.  */
    129 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
    130 
    131 /* Offsets to some stack save slots.  */
    132 #define STK_LR 16
    133 #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
    134 /* This one is dodgy.  ELFv2 does not have a linker word, so use the
    135    CR save slot.  Used only by optimised __tls_get_addr call stub,
    136    relying on __tls_get_addr_opt not saving CR..  */
    137 #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
    138 
    139 /* TOC base pointers offset from start of TOC.  */
    140 #define TOC_BASE_OFF	0x8000
    141 
    142 /* Offset of tp and dtp pointers from start of TLS block.  */
    143 #define TP_OFFSET	0x7000
    144 #define DTP_OFFSET	0x8000
    145 
    146 /* .plt call stub instructions.  The normal stub is like this, but
    147    sometimes the .plt entry crosses a 64k boundary and we need to
    148    insert an addi to adjust r11.  */
    149 #define STD_R2_0R1	0xf8410000	/* std	 %r2,0+40(%r1)	     */
    150 #define ADDIS_R11_R2	0x3d620000	/* addis %r11,%r2,xxx@ha     */
    151 #define LD_R12_0R11	0xe98b0000	/* ld	 %r12,xxx+0@l(%r11)  */
    152 #define MTCTR_R12	0x7d8903a6	/* mtctr %r12		     */
    153 #define LD_R2_0R11	0xe84b0000	/* ld	 %r2,xxx+8@l(%r11)   */
    154 #define LD_R11_0R11	0xe96b0000	/* ld	 %r11,xxx+16@l(%r11) */
    155 #define BCTR		0x4e800420	/* bctr			     */
    156 
    157 #define ADDI_R11_R11	0x396b0000	/* addi %r11,%r11,off@l  */
    158 #define ADDIS_R2_R2	0x3c420000	/* addis %r2,%r2,off@ha  */
    159 #define ADDI_R2_R2	0x38420000	/* addi  %r2,%r2,off@l   */
    160 
    161 #define XOR_R2_R12_R12	0x7d826278	/* xor   %r2,%r12,%r12   */
    162 #define ADD_R11_R11_R2	0x7d6b1214	/* add   %r11,%r11,%r2   */
    163 #define XOR_R11_R12_R12	0x7d8b6278	/* xor   %r11,%r12,%r12  */
    164 #define ADD_R2_R2_R11	0x7c425a14	/* add   %r2,%r2,%r11    */
    165 #define CMPLDI_R2_0	0x28220000	/* cmpldi %r2,0          */
    166 #define BNECTR		0x4ca20420	/* bnectr+               */
    167 #define BNECTR_P4	0x4ce20420	/* bnectr+               */
    168 
    169 #define LD_R12_0R2	0xe9820000	/* ld	 %r12,xxx+0(%r2) */
    170 #define LD_R11_0R2	0xe9620000	/* ld	 %r11,xxx+0(%r2) */
    171 #define LD_R2_0R2	0xe8420000	/* ld	 %r2,xxx+0(%r2)  */
    172 
    173 #define LD_R2_0R1	0xe8410000	/* ld    %r2,0(%r1)      */
    174 
    175 #define ADDIS_R12_R2	0x3d820000	/* addis %r12,%r2,xxx@ha     */
    176 #define ADDIS_R12_R12	0x3d8c0000	/* addis %r12,%r12,xxx@ha */
    177 #define LD_R12_0R12	0xe98c0000	/* ld    %r12,xxx@l(%r12) */
    178 
    179 /* glink call stub instructions.  We enter with the index in R0.  */
    180 #define GLINK_CALL_STUB_SIZE (16*4)
    181 					/* 0:				*/
    182 					/*  .quad plt0-1f		*/
    183 					/* __glink:			*/
    184 #define MFLR_R12	0x7d8802a6	/*  mflr %12			*/
    185 #define BCL_20_31	0x429f0005	/*  bcl 20,31,1f		*/
    186 					/* 1:				*/
    187 #define MFLR_R11	0x7d6802a6	/*  mflr %11			*/
    188 					/*  ld %2,(0b-1b)(%11)		*/
    189 #define MTLR_R12	0x7d8803a6	/*  mtlr %12			*/
    190 #define ADD_R11_R2_R11	0x7d625a14	/*  add %11,%2,%11		*/
    191 					/*  ld %12,0(%11)		*/
    192 					/*  ld %2,8(%11)		*/
    193 					/*  mtctr %12			*/
    194 					/*  ld %11,16(%11)		*/
    195 					/*  bctr			*/
    196 #define MFLR_R0		0x7c0802a6	/*  mflr %r0			*/
    197 #define MTLR_R0		0x7c0803a6	/*  mtlr %r0			*/
    198 #define SUB_R12_R12_R11	0x7d8b6050	/*  subf %r12,%r11,%r12		*/
    199 #define ADDI_R0_R12	0x380c0000	/*  addi %r0,%r12,0		*/
    200 #define SRDI_R0_R0_2	0x7800f082	/*  rldicl %r0,%r0,62,2		*/
    201 
    202 /* Pad with this.  */
    203 #define NOP		0x60000000
    204 
    205 /* Some other nops.  */
    206 #define CROR_151515	0x4def7b82
    207 #define CROR_313131	0x4ffffb82
    208 
    209 /* .glink entries for the first 32k functions are two instructions.  */
    210 #define LI_R0_0		0x38000000	/* li    %r0,0		*/
    211 #define B_DOT		0x48000000	/* b     .		*/
    212 
    213 /* After that, we need two instructions to load the index, followed by
    214    a branch.  */
    215 #define LIS_R0_0	0x3c000000	/* lis   %r0,0		*/
    216 #define ORI_R0_R0_0	0x60000000	/* ori	 %r0,%r0,0	*/
    217 
    218 /* Instructions used by the save and restore reg functions.  */
    219 #define STD_R0_0R1	0xf8010000	/* std   %r0,0(%r1)	*/
    220 #define STD_R0_0R12	0xf80c0000	/* std   %r0,0(%r12)	*/
    221 #define LD_R0_0R1	0xe8010000	/* ld    %r0,0(%r1)	*/
    222 #define LD_R0_0R12	0xe80c0000	/* ld    %r0,0(%r12)	*/
    223 #define STFD_FR0_0R1	0xd8010000	/* stfd  %fr0,0(%r1)	*/
    224 #define LFD_FR0_0R1	0xc8010000	/* lfd   %fr0,0(%r1)	*/
    225 #define LI_R12_0	0x39800000	/* li    %r12,0		*/
    226 #define STVX_VR0_R12_R0	0x7c0c01ce	/* stvx  %v0,%r12,%r0	*/
    227 #define LVX_VR0_R12_R0	0x7c0c00ce	/* lvx   %v0,%r12,%r0	*/
    228 #define MTLR_R0		0x7c0803a6	/* mtlr  %r0		*/
    229 #define BLR		0x4e800020	/* blr			*/
    230 
    231 /* Since .opd is an array of descriptors and each entry will end up
    232    with identical R_PPC64_RELATIVE relocs, there is really no need to
    233    propagate .opd relocs;  The dynamic linker should be taught to
    234    relocate .opd without reloc entries.  */
    235 #ifndef NO_OPD_RELOCS
    236 #define NO_OPD_RELOCS 0
    237 #endif
    238 
    239 static inline int
    240 abiversion (bfd *abfd)
    241 {
    242   return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
    243 }
    244 
    245 static inline void
    246 set_abiversion (bfd *abfd, int ver)
    247 {
    248   elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
    249   elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
    250 }
    251 
    252 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
    254 
    255 /* Relocation HOWTO's.  */
    256 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
    257 
    258 static reloc_howto_type ppc64_elf_howto_raw[] = {
    259   /* This reloc does nothing.  */
    260   HOWTO (R_PPC64_NONE,		/* type */
    261 	 0,			/* rightshift */
    262 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    263 	 32,			/* bitsize */
    264 	 FALSE,			/* pc_relative */
    265 	 0,			/* bitpos */
    266 	 complain_overflow_dont, /* complain_on_overflow */
    267 	 bfd_elf_generic_reloc,	/* special_function */
    268 	 "R_PPC64_NONE",	/* name */
    269 	 FALSE,			/* partial_inplace */
    270 	 0,			/* src_mask */
    271 	 0,			/* dst_mask */
    272 	 FALSE),		/* pcrel_offset */
    273 
    274   /* A standard 32 bit relocation.  */
    275   HOWTO (R_PPC64_ADDR32,	/* type */
    276 	 0,			/* rightshift */
    277 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    278 	 32,			/* bitsize */
    279 	 FALSE,			/* pc_relative */
    280 	 0,			/* bitpos */
    281 	 complain_overflow_bitfield, /* complain_on_overflow */
    282 	 bfd_elf_generic_reloc,	/* special_function */
    283 	 "R_PPC64_ADDR32",	/* name */
    284 	 FALSE,			/* partial_inplace */
    285 	 0,			/* src_mask */
    286 	 0xffffffff,		/* dst_mask */
    287 	 FALSE),		/* pcrel_offset */
    288 
    289   /* An absolute 26 bit branch; the lower two bits must be zero.
    290      FIXME: we don't check that, we just clear them.  */
    291   HOWTO (R_PPC64_ADDR24,	/* type */
    292 	 0,			/* rightshift */
    293 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    294 	 26,			/* bitsize */
    295 	 FALSE,			/* pc_relative */
    296 	 0,			/* bitpos */
    297 	 complain_overflow_bitfield, /* complain_on_overflow */
    298 	 bfd_elf_generic_reloc,	/* special_function */
    299 	 "R_PPC64_ADDR24",	/* name */
    300 	 FALSE,			/* partial_inplace */
    301 	 0,			/* src_mask */
    302 	 0x03fffffc,		/* dst_mask */
    303 	 FALSE),		/* pcrel_offset */
    304 
    305   /* A standard 16 bit relocation.  */
    306   HOWTO (R_PPC64_ADDR16,	/* type */
    307 	 0,			/* rightshift */
    308 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    309 	 16,			/* bitsize */
    310 	 FALSE,			/* pc_relative */
    311 	 0,			/* bitpos */
    312 	 complain_overflow_bitfield, /* complain_on_overflow */
    313 	 bfd_elf_generic_reloc,	/* special_function */
    314 	 "R_PPC64_ADDR16",	/* name */
    315 	 FALSE,			/* partial_inplace */
    316 	 0,			/* src_mask */
    317 	 0xffff,		/* dst_mask */
    318 	 FALSE),		/* pcrel_offset */
    319 
    320   /* A 16 bit relocation without overflow.  */
    321   HOWTO (R_PPC64_ADDR16_LO,	/* type */
    322 	 0,			/* rightshift */
    323 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    324 	 16,			/* bitsize */
    325 	 FALSE,			/* pc_relative */
    326 	 0,			/* bitpos */
    327 	 complain_overflow_dont,/* complain_on_overflow */
    328 	 bfd_elf_generic_reloc,	/* special_function */
    329 	 "R_PPC64_ADDR16_LO",	/* name */
    330 	 FALSE,			/* partial_inplace */
    331 	 0,			/* src_mask */
    332 	 0xffff,		/* dst_mask */
    333 	 FALSE),		/* pcrel_offset */
    334 
    335   /* Bits 16-31 of an address.  */
    336   HOWTO (R_PPC64_ADDR16_HI,	/* type */
    337 	 16,			/* rightshift */
    338 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    339 	 16,			/* bitsize */
    340 	 FALSE,			/* pc_relative */
    341 	 0,			/* bitpos */
    342 	 complain_overflow_signed, /* complain_on_overflow */
    343 	 bfd_elf_generic_reloc,	/* special_function */
    344 	 "R_PPC64_ADDR16_HI",	/* name */
    345 	 FALSE,			/* partial_inplace */
    346 	 0,			/* src_mask */
    347 	 0xffff,		/* dst_mask */
    348 	 FALSE),		/* pcrel_offset */
    349 
    350   /* Bits 16-31 of an address, plus 1 if the contents of the low 16
    351      bits, treated as a signed number, is negative.  */
    352   HOWTO (R_PPC64_ADDR16_HA,	/* type */
    353 	 16,			/* rightshift */
    354 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    355 	 16,			/* bitsize */
    356 	 FALSE,			/* pc_relative */
    357 	 0,			/* bitpos */
    358 	 complain_overflow_signed, /* complain_on_overflow */
    359 	 ppc64_elf_ha_reloc,	/* special_function */
    360 	 "R_PPC64_ADDR16_HA",	/* name */
    361 	 FALSE,			/* partial_inplace */
    362 	 0,			/* src_mask */
    363 	 0xffff,		/* dst_mask */
    364 	 FALSE),		/* pcrel_offset */
    365 
    366   /* An absolute 16 bit branch; the lower two bits must be zero.
    367      FIXME: we don't check that, we just clear them.  */
    368   HOWTO (R_PPC64_ADDR14,	/* type */
    369 	 0,			/* rightshift */
    370 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    371 	 16,			/* bitsize */
    372 	 FALSE,			/* pc_relative */
    373 	 0,			/* bitpos */
    374 	 complain_overflow_signed, /* complain_on_overflow */
    375 	 ppc64_elf_branch_reloc, /* special_function */
    376 	 "R_PPC64_ADDR14",	/* name */
    377 	 FALSE,			/* partial_inplace */
    378 	 0,			/* src_mask */
    379 	 0x0000fffc,		/* dst_mask */
    380 	 FALSE),		/* pcrel_offset */
    381 
    382   /* An absolute 16 bit branch, for which bit 10 should be set to
    383      indicate that the branch is expected to be taken.  The lower two
    384      bits must be zero.  */
    385   HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
    386 	 0,			/* rightshift */
    387 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    388 	 16,			/* bitsize */
    389 	 FALSE,			/* pc_relative */
    390 	 0,			/* bitpos */
    391 	 complain_overflow_signed, /* complain_on_overflow */
    392 	 ppc64_elf_brtaken_reloc, /* special_function */
    393 	 "R_PPC64_ADDR14_BRTAKEN",/* name */
    394 	 FALSE,			/* partial_inplace */
    395 	 0,			/* src_mask */
    396 	 0x0000fffc,		/* dst_mask */
    397 	 FALSE),		/* pcrel_offset */
    398 
    399   /* An absolute 16 bit branch, for which bit 10 should be set to
    400      indicate that the branch is not expected to be taken.  The lower
    401      two bits must be zero.  */
    402   HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
    403 	 0,			/* rightshift */
    404 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    405 	 16,			/* bitsize */
    406 	 FALSE,			/* pc_relative */
    407 	 0,			/* bitpos */
    408 	 complain_overflow_signed, /* complain_on_overflow */
    409 	 ppc64_elf_brtaken_reloc, /* special_function */
    410 	 "R_PPC64_ADDR14_BRNTAKEN",/* name */
    411 	 FALSE,			/* partial_inplace */
    412 	 0,			/* src_mask */
    413 	 0x0000fffc,		/* dst_mask */
    414 	 FALSE),		/* pcrel_offset */
    415 
    416   /* A relative 26 bit branch; the lower two bits must be zero.  */
    417   HOWTO (R_PPC64_REL24,		/* type */
    418 	 0,			/* rightshift */
    419 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    420 	 26,			/* bitsize */
    421 	 TRUE,			/* pc_relative */
    422 	 0,			/* bitpos */
    423 	 complain_overflow_signed, /* complain_on_overflow */
    424 	 ppc64_elf_branch_reloc, /* special_function */
    425 	 "R_PPC64_REL24",	/* name */
    426 	 FALSE,			/* partial_inplace */
    427 	 0,			/* src_mask */
    428 	 0x03fffffc,		/* dst_mask */
    429 	 TRUE),			/* pcrel_offset */
    430 
    431   /* A relative 16 bit branch; the lower two bits must be zero.  */
    432   HOWTO (R_PPC64_REL14,		/* type */
    433 	 0,			/* rightshift */
    434 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    435 	 16,			/* bitsize */
    436 	 TRUE,			/* pc_relative */
    437 	 0,			/* bitpos */
    438 	 complain_overflow_signed, /* complain_on_overflow */
    439 	 ppc64_elf_branch_reloc, /* special_function */
    440 	 "R_PPC64_REL14",	/* name */
    441 	 FALSE,			/* partial_inplace */
    442 	 0,			/* src_mask */
    443 	 0x0000fffc,		/* dst_mask */
    444 	 TRUE),			/* pcrel_offset */
    445 
    446   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
    447      the branch is expected to be taken.  The lower two bits must be
    448      zero.  */
    449   HOWTO (R_PPC64_REL14_BRTAKEN,	/* type */
    450 	 0,			/* rightshift */
    451 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    452 	 16,			/* bitsize */
    453 	 TRUE,			/* pc_relative */
    454 	 0,			/* bitpos */
    455 	 complain_overflow_signed, /* complain_on_overflow */
    456 	 ppc64_elf_brtaken_reloc, /* special_function */
    457 	 "R_PPC64_REL14_BRTAKEN", /* name */
    458 	 FALSE,			/* partial_inplace */
    459 	 0,			/* src_mask */
    460 	 0x0000fffc,		/* dst_mask */
    461 	 TRUE),			/* pcrel_offset */
    462 
    463   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
    464      the branch is not expected to be taken.  The lower two bits must
    465      be zero.  */
    466   HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
    467 	 0,			/* rightshift */
    468 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    469 	 16,			/* bitsize */
    470 	 TRUE,			/* pc_relative */
    471 	 0,			/* bitpos */
    472 	 complain_overflow_signed, /* complain_on_overflow */
    473 	 ppc64_elf_brtaken_reloc, /* special_function */
    474 	 "R_PPC64_REL14_BRNTAKEN",/* name */
    475 	 FALSE,			/* partial_inplace */
    476 	 0,			/* src_mask */
    477 	 0x0000fffc,		/* dst_mask */
    478 	 TRUE),			/* pcrel_offset */
    479 
    480   /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
    481      symbol.  */
    482   HOWTO (R_PPC64_GOT16,		/* type */
    483 	 0,			/* rightshift */
    484 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    485 	 16,			/* bitsize */
    486 	 FALSE,			/* pc_relative */
    487 	 0,			/* bitpos */
    488 	 complain_overflow_signed, /* complain_on_overflow */
    489 	 ppc64_elf_unhandled_reloc, /* special_function */
    490 	 "R_PPC64_GOT16",	/* name */
    491 	 FALSE,			/* partial_inplace */
    492 	 0,			/* src_mask */
    493 	 0xffff,		/* dst_mask */
    494 	 FALSE),		/* pcrel_offset */
    495 
    496   /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
    497      the symbol.  */
    498   HOWTO (R_PPC64_GOT16_LO,	/* type */
    499 	 0,			/* rightshift */
    500 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    501 	 16,			/* bitsize */
    502 	 FALSE,			/* pc_relative */
    503 	 0,			/* bitpos */
    504 	 complain_overflow_dont, /* complain_on_overflow */
    505 	 ppc64_elf_unhandled_reloc, /* special_function */
    506 	 "R_PPC64_GOT16_LO",	/* name */
    507 	 FALSE,			/* partial_inplace */
    508 	 0,			/* src_mask */
    509 	 0xffff,		/* dst_mask */
    510 	 FALSE),		/* pcrel_offset */
    511 
    512   /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
    513      the symbol.  */
    514   HOWTO (R_PPC64_GOT16_HI,	/* type */
    515 	 16,			/* rightshift */
    516 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    517 	 16,			/* bitsize */
    518 	 FALSE,			/* pc_relative */
    519 	 0,			/* bitpos */
    520 	 complain_overflow_signed,/* complain_on_overflow */
    521 	 ppc64_elf_unhandled_reloc, /* special_function */
    522 	 "R_PPC64_GOT16_HI",	/* name */
    523 	 FALSE,			/* partial_inplace */
    524 	 0,			/* src_mask */
    525 	 0xffff,		/* dst_mask */
    526 	 FALSE),		/* pcrel_offset */
    527 
    528   /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
    529      the symbol.  */
    530   HOWTO (R_PPC64_GOT16_HA,	/* type */
    531 	 16,			/* rightshift */
    532 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    533 	 16,			/* bitsize */
    534 	 FALSE,			/* pc_relative */
    535 	 0,			/* bitpos */
    536 	 complain_overflow_signed,/* complain_on_overflow */
    537 	 ppc64_elf_unhandled_reloc, /* special_function */
    538 	 "R_PPC64_GOT16_HA",	/* name */
    539 	 FALSE,			/* partial_inplace */
    540 	 0,			/* src_mask */
    541 	 0xffff,		/* dst_mask */
    542 	 FALSE),		/* pcrel_offset */
    543 
    544   /* This is used only by the dynamic linker.  The symbol should exist
    545      both in the object being run and in some shared library.  The
    546      dynamic linker copies the data addressed by the symbol from the
    547      shared library into the object, because the object being
    548      run has to have the data at some particular address.  */
    549   HOWTO (R_PPC64_COPY,		/* type */
    550 	 0,			/* rightshift */
    551 	 0,			/* this one is variable size */
    552 	 0,			/* bitsize */
    553 	 FALSE,			/* pc_relative */
    554 	 0,			/* bitpos */
    555 	 complain_overflow_dont, /* complain_on_overflow */
    556 	 ppc64_elf_unhandled_reloc, /* special_function */
    557 	 "R_PPC64_COPY",	/* name */
    558 	 FALSE,			/* partial_inplace */
    559 	 0,			/* src_mask */
    560 	 0,			/* dst_mask */
    561 	 FALSE),		/* pcrel_offset */
    562 
    563   /* Like R_PPC64_ADDR64, but used when setting global offset table
    564      entries.  */
    565   HOWTO (R_PPC64_GLOB_DAT,	/* type */
    566 	 0,			/* rightshift */
    567 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    568 	 64,			/* bitsize */
    569 	 FALSE,			/* pc_relative */
    570 	 0,			/* bitpos */
    571 	 complain_overflow_dont, /* complain_on_overflow */
    572 	 ppc64_elf_unhandled_reloc,  /* special_function */
    573 	 "R_PPC64_GLOB_DAT",	/* name */
    574 	 FALSE,			/* partial_inplace */
    575 	 0,			/* src_mask */
    576 	 ONES (64),		/* dst_mask */
    577 	 FALSE),		/* pcrel_offset */
    578 
    579   /* Created by the link editor.  Marks a procedure linkage table
    580      entry for a symbol.  */
    581   HOWTO (R_PPC64_JMP_SLOT,	/* type */
    582 	 0,			/* rightshift */
    583 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
    584 	 0,			/* bitsize */
    585 	 FALSE,			/* pc_relative */
    586 	 0,			/* bitpos */
    587 	 complain_overflow_dont, /* complain_on_overflow */
    588 	 ppc64_elf_unhandled_reloc, /* special_function */
    589 	 "R_PPC64_JMP_SLOT",	/* name */
    590 	 FALSE,			/* partial_inplace */
    591 	 0,			/* src_mask */
    592 	 0,			/* dst_mask */
    593 	 FALSE),		/* pcrel_offset */
    594 
    595   /* Used only by the dynamic linker.  When the object is run, this
    596      doubleword64 is set to the load address of the object, plus the
    597      addend.  */
    598   HOWTO (R_PPC64_RELATIVE,	/* type */
    599 	 0,			/* rightshift */
    600 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    601 	 64,			/* bitsize */
    602 	 FALSE,			/* pc_relative */
    603 	 0,			/* bitpos */
    604 	 complain_overflow_dont, /* complain_on_overflow */
    605 	 bfd_elf_generic_reloc,	/* special_function */
    606 	 "R_PPC64_RELATIVE",	/* name */
    607 	 FALSE,			/* partial_inplace */
    608 	 0,			/* src_mask */
    609 	 ONES (64),		/* dst_mask */
    610 	 FALSE),		/* pcrel_offset */
    611 
    612   /* Like R_PPC64_ADDR32, but may be unaligned.  */
    613   HOWTO (R_PPC64_UADDR32,	/* type */
    614 	 0,			/* rightshift */
    615 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    616 	 32,			/* bitsize */
    617 	 FALSE,			/* pc_relative */
    618 	 0,			/* bitpos */
    619 	 complain_overflow_bitfield, /* complain_on_overflow */
    620 	 bfd_elf_generic_reloc,	/* special_function */
    621 	 "R_PPC64_UADDR32",	/* name */
    622 	 FALSE,			/* partial_inplace */
    623 	 0,			/* src_mask */
    624 	 0xffffffff,		/* dst_mask */
    625 	 FALSE),		/* pcrel_offset */
    626 
    627   /* Like R_PPC64_ADDR16, but may be unaligned.  */
    628   HOWTO (R_PPC64_UADDR16,	/* type */
    629 	 0,			/* rightshift */
    630 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    631 	 16,			/* bitsize */
    632 	 FALSE,			/* pc_relative */
    633 	 0,			/* bitpos */
    634 	 complain_overflow_bitfield, /* complain_on_overflow */
    635 	 bfd_elf_generic_reloc,	/* special_function */
    636 	 "R_PPC64_UADDR16",	/* name */
    637 	 FALSE,			/* partial_inplace */
    638 	 0,			/* src_mask */
    639 	 0xffff,		/* dst_mask */
    640 	 FALSE),		/* pcrel_offset */
    641 
    642   /* 32-bit PC relative.  */
    643   HOWTO (R_PPC64_REL32,		/* type */
    644 	 0,			/* rightshift */
    645 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    646 	 32,			/* bitsize */
    647 	 TRUE,			/* pc_relative */
    648 	 0,			/* bitpos */
    649 	 complain_overflow_signed, /* complain_on_overflow */
    650 	 bfd_elf_generic_reloc,	/* special_function */
    651 	 "R_PPC64_REL32",	/* name */
    652 	 FALSE,			/* partial_inplace */
    653 	 0,			/* src_mask */
    654 	 0xffffffff,		/* dst_mask */
    655 	 TRUE),			/* pcrel_offset */
    656 
    657   /* 32-bit relocation to the symbol's procedure linkage table.  */
    658   HOWTO (R_PPC64_PLT32,		/* type */
    659 	 0,			/* rightshift */
    660 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    661 	 32,			/* bitsize */
    662 	 FALSE,			/* pc_relative */
    663 	 0,			/* bitpos */
    664 	 complain_overflow_bitfield, /* complain_on_overflow */
    665 	 ppc64_elf_unhandled_reloc, /* special_function */
    666 	 "R_PPC64_PLT32",	/* name */
    667 	 FALSE,			/* partial_inplace */
    668 	 0,			/* src_mask */
    669 	 0xffffffff,		/* dst_mask */
    670 	 FALSE),		/* pcrel_offset */
    671 
    672   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
    673      FIXME: R_PPC64_PLTREL32 not supported.  */
    674   HOWTO (R_PPC64_PLTREL32,	/* type */
    675 	 0,			/* rightshift */
    676 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    677 	 32,			/* bitsize */
    678 	 TRUE,			/* pc_relative */
    679 	 0,			/* bitpos */
    680 	 complain_overflow_signed, /* complain_on_overflow */
    681 	 bfd_elf_generic_reloc,	/* special_function */
    682 	 "R_PPC64_PLTREL32",	/* name */
    683 	 FALSE,			/* partial_inplace */
    684 	 0,			/* src_mask */
    685 	 0xffffffff,		/* dst_mask */
    686 	 TRUE),			/* pcrel_offset */
    687 
    688   /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
    689      the symbol.  */
    690   HOWTO (R_PPC64_PLT16_LO,	/* type */
    691 	 0,			/* rightshift */
    692 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    693 	 16,			/* bitsize */
    694 	 FALSE,			/* pc_relative */
    695 	 0,			/* bitpos */
    696 	 complain_overflow_dont, /* complain_on_overflow */
    697 	 ppc64_elf_unhandled_reloc, /* special_function */
    698 	 "R_PPC64_PLT16_LO",	/* name */
    699 	 FALSE,			/* partial_inplace */
    700 	 0,			/* src_mask */
    701 	 0xffff,		/* dst_mask */
    702 	 FALSE),		/* pcrel_offset */
    703 
    704   /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
    705      the symbol.  */
    706   HOWTO (R_PPC64_PLT16_HI,	/* type */
    707 	 16,			/* rightshift */
    708 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    709 	 16,			/* bitsize */
    710 	 FALSE,			/* pc_relative */
    711 	 0,			/* bitpos */
    712 	 complain_overflow_signed, /* complain_on_overflow */
    713 	 ppc64_elf_unhandled_reloc, /* special_function */
    714 	 "R_PPC64_PLT16_HI",	/* name */
    715 	 FALSE,			/* partial_inplace */
    716 	 0,			/* src_mask */
    717 	 0xffff,		/* dst_mask */
    718 	 FALSE),		/* pcrel_offset */
    719 
    720   /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
    721      the symbol.  */
    722   HOWTO (R_PPC64_PLT16_HA,	/* type */
    723 	 16,			/* rightshift */
    724 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    725 	 16,			/* bitsize */
    726 	 FALSE,			/* pc_relative */
    727 	 0,			/* bitpos */
    728 	 complain_overflow_signed, /* complain_on_overflow */
    729 	 ppc64_elf_unhandled_reloc, /* special_function */
    730 	 "R_PPC64_PLT16_HA",	/* name */
    731 	 FALSE,			/* partial_inplace */
    732 	 0,			/* src_mask */
    733 	 0xffff,		/* dst_mask */
    734 	 FALSE),		/* pcrel_offset */
    735 
    736   /* 16-bit section relative relocation.  */
    737   HOWTO (R_PPC64_SECTOFF,	/* type */
    738 	 0,			/* rightshift */
    739 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    740 	 16,			/* bitsize */
    741 	 FALSE,			/* pc_relative */
    742 	 0,			/* bitpos */
    743 	 complain_overflow_signed, /* complain_on_overflow */
    744 	 ppc64_elf_sectoff_reloc, /* special_function */
    745 	 "R_PPC64_SECTOFF",	/* name */
    746 	 FALSE,			/* partial_inplace */
    747 	 0,			/* src_mask */
    748 	 0xffff,		/* dst_mask */
    749 	 FALSE),		/* pcrel_offset */
    750 
    751   /* Like R_PPC64_SECTOFF, but no overflow warning.  */
    752   HOWTO (R_PPC64_SECTOFF_LO,	/* type */
    753 	 0,			/* rightshift */
    754 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    755 	 16,			/* bitsize */
    756 	 FALSE,			/* pc_relative */
    757 	 0,			/* bitpos */
    758 	 complain_overflow_dont, /* complain_on_overflow */
    759 	 ppc64_elf_sectoff_reloc, /* special_function */
    760 	 "R_PPC64_SECTOFF_LO",	/* name */
    761 	 FALSE,			/* partial_inplace */
    762 	 0,			/* src_mask */
    763 	 0xffff,		/* dst_mask */
    764 	 FALSE),		/* pcrel_offset */
    765 
    766   /* 16-bit upper half section relative relocation.  */
    767   HOWTO (R_PPC64_SECTOFF_HI,	/* type */
    768 	 16,			/* rightshift */
    769 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    770 	 16,			/* bitsize */
    771 	 FALSE,			/* pc_relative */
    772 	 0,			/* bitpos */
    773 	 complain_overflow_signed, /* complain_on_overflow */
    774 	 ppc64_elf_sectoff_reloc, /* special_function */
    775 	 "R_PPC64_SECTOFF_HI",	/* name */
    776 	 FALSE,			/* partial_inplace */
    777 	 0,			/* src_mask */
    778 	 0xffff,		/* dst_mask */
    779 	 FALSE),		/* pcrel_offset */
    780 
    781   /* 16-bit upper half adjusted section relative relocation.  */
    782   HOWTO (R_PPC64_SECTOFF_HA,	/* type */
    783 	 16,			/* rightshift */
    784 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    785 	 16,			/* bitsize */
    786 	 FALSE,			/* pc_relative */
    787 	 0,			/* bitpos */
    788 	 complain_overflow_signed, /* complain_on_overflow */
    789 	 ppc64_elf_sectoff_ha_reloc, /* special_function */
    790 	 "R_PPC64_SECTOFF_HA",	/* name */
    791 	 FALSE,			/* partial_inplace */
    792 	 0,			/* src_mask */
    793 	 0xffff,		/* dst_mask */
    794 	 FALSE),		/* pcrel_offset */
    795 
    796   /* Like R_PPC64_REL24 without touching the two least significant bits.  */
    797   HOWTO (R_PPC64_REL30,		/* type */
    798 	 2,			/* rightshift */
    799 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    800 	 30,			/* bitsize */
    801 	 TRUE,			/* pc_relative */
    802 	 0,			/* bitpos */
    803 	 complain_overflow_dont, /* complain_on_overflow */
    804 	 bfd_elf_generic_reloc, /* special_function */
    805 	 "R_PPC64_REL30",	/* name */
    806 	 FALSE,			/* partial_inplace */
    807 	 0,			/* src_mask */
    808 	 0xfffffffc,		/* dst_mask */
    809 	 TRUE),			/* pcrel_offset */
    810 
    811   /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
    812 
    813   /* A standard 64-bit relocation.  */
    814   HOWTO (R_PPC64_ADDR64,	/* type */
    815 	 0,			/* rightshift */
    816 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    817 	 64,			/* bitsize */
    818 	 FALSE,			/* pc_relative */
    819 	 0,			/* bitpos */
    820 	 complain_overflow_dont, /* complain_on_overflow */
    821 	 bfd_elf_generic_reloc,	/* special_function */
    822 	 "R_PPC64_ADDR64",	/* name */
    823 	 FALSE,			/* partial_inplace */
    824 	 0,			/* src_mask */
    825 	 ONES (64),		/* dst_mask */
    826 	 FALSE),		/* pcrel_offset */
    827 
    828   /* The bits 32-47 of an address.  */
    829   HOWTO (R_PPC64_ADDR16_HIGHER,	/* type */
    830 	 32,			/* rightshift */
    831 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    832 	 16,			/* bitsize */
    833 	 FALSE,			/* pc_relative */
    834 	 0,			/* bitpos */
    835 	 complain_overflow_dont, /* complain_on_overflow */
    836 	 bfd_elf_generic_reloc,	/* special_function */
    837 	 "R_PPC64_ADDR16_HIGHER", /* name */
    838 	 FALSE,			/* partial_inplace */
    839 	 0,			/* src_mask */
    840 	 0xffff,		/* dst_mask */
    841 	 FALSE),		/* pcrel_offset */
    842 
    843   /* The bits 32-47 of an address, plus 1 if the contents of the low
    844      16 bits, treated as a signed number, is negative.  */
    845   HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
    846 	 32,			/* rightshift */
    847 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    848 	 16,			/* bitsize */
    849 	 FALSE,			/* pc_relative */
    850 	 0,			/* bitpos */
    851 	 complain_overflow_dont, /* complain_on_overflow */
    852 	 ppc64_elf_ha_reloc,	/* special_function */
    853 	 "R_PPC64_ADDR16_HIGHERA", /* name */
    854 	 FALSE,			/* partial_inplace */
    855 	 0,			/* src_mask */
    856 	 0xffff,		/* dst_mask */
    857 	 FALSE),		/* pcrel_offset */
    858 
    859   /* The bits 48-63 of an address.  */
    860   HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
    861 	 48,			/* rightshift */
    862 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    863 	 16,			/* bitsize */
    864 	 FALSE,			/* pc_relative */
    865 	 0,			/* bitpos */
    866 	 complain_overflow_dont, /* complain_on_overflow */
    867 	 bfd_elf_generic_reloc,	/* special_function */
    868 	 "R_PPC64_ADDR16_HIGHEST", /* name */
    869 	 FALSE,			/* partial_inplace */
    870 	 0,			/* src_mask */
    871 	 0xffff,		/* dst_mask */
    872 	 FALSE),		/* pcrel_offset */
    873 
    874   /* The bits 48-63 of an address, plus 1 if the contents of the low
    875      16 bits, treated as a signed number, is negative.  */
    876   HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
    877 	 48,			/* rightshift */
    878 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    879 	 16,			/* bitsize */
    880 	 FALSE,			/* pc_relative */
    881 	 0,			/* bitpos */
    882 	 complain_overflow_dont, /* complain_on_overflow */
    883 	 ppc64_elf_ha_reloc,	/* special_function */
    884 	 "R_PPC64_ADDR16_HIGHESTA", /* name */
    885 	 FALSE,			/* partial_inplace */
    886 	 0,			/* src_mask */
    887 	 0xffff,		/* dst_mask */
    888 	 FALSE),		/* pcrel_offset */
    889 
    890   /* Like ADDR64, but may be unaligned.  */
    891   HOWTO (R_PPC64_UADDR64,	/* type */
    892 	 0,			/* rightshift */
    893 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    894 	 64,			/* bitsize */
    895 	 FALSE,			/* pc_relative */
    896 	 0,			/* bitpos */
    897 	 complain_overflow_dont, /* complain_on_overflow */
    898 	 bfd_elf_generic_reloc,	/* special_function */
    899 	 "R_PPC64_UADDR64",	/* name */
    900 	 FALSE,			/* partial_inplace */
    901 	 0,			/* src_mask */
    902 	 ONES (64),		/* dst_mask */
    903 	 FALSE),		/* pcrel_offset */
    904 
    905   /* 64-bit relative relocation.  */
    906   HOWTO (R_PPC64_REL64,		/* type */
    907 	 0,			/* rightshift */
    908 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    909 	 64,			/* bitsize */
    910 	 TRUE,			/* pc_relative */
    911 	 0,			/* bitpos */
    912 	 complain_overflow_dont, /* complain_on_overflow */
    913 	 bfd_elf_generic_reloc,	/* special_function */
    914 	 "R_PPC64_REL64",	/* name */
    915 	 FALSE,			/* partial_inplace */
    916 	 0,			/* src_mask */
    917 	 ONES (64),		/* dst_mask */
    918 	 TRUE),			/* pcrel_offset */
    919 
    920   /* 64-bit relocation to the symbol's procedure linkage table.  */
    921   HOWTO (R_PPC64_PLT64,		/* type */
    922 	 0,			/* rightshift */
    923 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    924 	 64,			/* bitsize */
    925 	 FALSE,			/* pc_relative */
    926 	 0,			/* bitpos */
    927 	 complain_overflow_dont, /* complain_on_overflow */
    928 	 ppc64_elf_unhandled_reloc, /* special_function */
    929 	 "R_PPC64_PLT64",	/* name */
    930 	 FALSE,			/* partial_inplace */
    931 	 0,			/* src_mask */
    932 	 ONES (64),		/* dst_mask */
    933 	 FALSE),		/* pcrel_offset */
    934 
    935   /* 64-bit PC relative relocation to the symbol's procedure linkage
    936      table.  */
    937   /* FIXME: R_PPC64_PLTREL64 not supported.  */
    938   HOWTO (R_PPC64_PLTREL64,	/* type */
    939 	 0,			/* rightshift */
    940 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    941 	 64,			/* bitsize */
    942 	 TRUE,			/* pc_relative */
    943 	 0,			/* bitpos */
    944 	 complain_overflow_dont, /* complain_on_overflow */
    945 	 ppc64_elf_unhandled_reloc, /* special_function */
    946 	 "R_PPC64_PLTREL64",	/* name */
    947 	 FALSE,			/* partial_inplace */
    948 	 0,			/* src_mask */
    949 	 ONES (64),		/* dst_mask */
    950 	 TRUE),			/* pcrel_offset */
    951 
    952   /* 16 bit TOC-relative relocation.  */
    953 
    954   /* R_PPC64_TOC16	  47	   half16*	S + A - .TOC.  */
    955   HOWTO (R_PPC64_TOC16,		/* type */
    956 	 0,			/* rightshift */
    957 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    958 	 16,			/* bitsize */
    959 	 FALSE,			/* pc_relative */
    960 	 0,			/* bitpos */
    961 	 complain_overflow_signed, /* complain_on_overflow */
    962 	 ppc64_elf_toc_reloc,	/* special_function */
    963 	 "R_PPC64_TOC16",	/* name */
    964 	 FALSE,			/* partial_inplace */
    965 	 0,			/* src_mask */
    966 	 0xffff,		/* dst_mask */
    967 	 FALSE),		/* pcrel_offset */
    968 
    969   /* 16 bit TOC-relative relocation without overflow.  */
    970 
    971   /* R_PPC64_TOC16_LO	  48	   half16	 #lo (S + A - .TOC.)  */
    972   HOWTO (R_PPC64_TOC16_LO,	/* type */
    973 	 0,			/* rightshift */
    974 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    975 	 16,			/* bitsize */
    976 	 FALSE,			/* pc_relative */
    977 	 0,			/* bitpos */
    978 	 complain_overflow_dont, /* complain_on_overflow */
    979 	 ppc64_elf_toc_reloc,	/* special_function */
    980 	 "R_PPC64_TOC16_LO",	/* name */
    981 	 FALSE,			/* partial_inplace */
    982 	 0,			/* src_mask */
    983 	 0xffff,		/* dst_mask */
    984 	 FALSE),		/* pcrel_offset */
    985 
    986   /* 16 bit TOC-relative relocation, high 16 bits.  */
    987 
    988   /* R_PPC64_TOC16_HI	  49	   half16	 #hi (S + A - .TOC.)  */
    989   HOWTO (R_PPC64_TOC16_HI,	/* type */
    990 	 16,			/* rightshift */
    991 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    992 	 16,			/* bitsize */
    993 	 FALSE,			/* pc_relative */
    994 	 0,			/* bitpos */
    995 	 complain_overflow_signed, /* complain_on_overflow */
    996 	 ppc64_elf_toc_reloc,	/* special_function */
    997 	 "R_PPC64_TOC16_HI",	/* name */
    998 	 FALSE,			/* partial_inplace */
    999 	 0,			/* src_mask */
   1000 	 0xffff,		/* dst_mask */
   1001 	 FALSE),		/* pcrel_offset */
   1002 
   1003   /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
   1004      contents of the low 16 bits, treated as a signed number, is
   1005      negative.  */
   1006 
   1007   /* R_PPC64_TOC16_HA	  50	   half16	 #ha (S + A - .TOC.)  */
   1008   HOWTO (R_PPC64_TOC16_HA,	/* type */
   1009 	 16,			/* rightshift */
   1010 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1011 	 16,			/* bitsize */
   1012 	 FALSE,			/* pc_relative */
   1013 	 0,			/* bitpos */
   1014 	 complain_overflow_signed, /* complain_on_overflow */
   1015 	 ppc64_elf_toc_ha_reloc, /* special_function */
   1016 	 "R_PPC64_TOC16_HA",	/* name */
   1017 	 FALSE,			/* partial_inplace */
   1018 	 0,			/* src_mask */
   1019 	 0xffff,		/* dst_mask */
   1020 	 FALSE),		/* pcrel_offset */
   1021 
   1022   /* 64-bit relocation; insert value of TOC base (.TOC.).  */
   1023 
   1024   /* R_PPC64_TOC		  51	   doubleword64	 .TOC.  */
   1025   HOWTO (R_PPC64_TOC,		/* type */
   1026 	 0,			/* rightshift */
   1027 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
   1028 	 64,			/* bitsize */
   1029 	 FALSE,			/* pc_relative */
   1030 	 0,			/* bitpos */
   1031 	 complain_overflow_dont, /* complain_on_overflow */
   1032 	 ppc64_elf_toc64_reloc,	/* special_function */
   1033 	 "R_PPC64_TOC",		/* name */
   1034 	 FALSE,			/* partial_inplace */
   1035 	 0,			/* src_mask */
   1036 	 ONES (64),		/* dst_mask */
   1037 	 FALSE),		/* pcrel_offset */
   1038 
   1039   /* Like R_PPC64_GOT16, but also informs the link editor that the
   1040      value to relocate may (!) refer to a PLT entry which the link
   1041      editor (a) may replace with the symbol value.  If the link editor
   1042      is unable to fully resolve the symbol, it may (b) create a PLT
   1043      entry and store the address to the new PLT entry in the GOT.
   1044      This permits lazy resolution of function symbols at run time.
   1045      The link editor may also skip all of this and just (c) emit a
   1046      R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
   1047   /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
   1048     HOWTO (R_PPC64_PLTGOT16,	/* type */
   1049 	 0,			/* rightshift */
   1050 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1051 	 16,			/* bitsize */
   1052 	 FALSE,			/* pc_relative */
   1053 	 0,			/* bitpos */
   1054 	 complain_overflow_signed, /* complain_on_overflow */
   1055 	 ppc64_elf_unhandled_reloc, /* special_function */
   1056 	 "R_PPC64_PLTGOT16",	/* name */
   1057 	 FALSE,			/* partial_inplace */
   1058 	 0,			/* src_mask */
   1059 	 0xffff,		/* dst_mask */
   1060 	 FALSE),		/* pcrel_offset */
   1061 
   1062   /* Like R_PPC64_PLTGOT16, but without overflow.  */
   1063   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
   1064   HOWTO (R_PPC64_PLTGOT16_LO,	/* type */
   1065 	 0,			/* rightshift */
   1066 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1067 	 16,			/* bitsize */
   1068 	 FALSE,			/* pc_relative */
   1069 	 0,			/* bitpos */
   1070 	 complain_overflow_dont, /* complain_on_overflow */
   1071 	 ppc64_elf_unhandled_reloc, /* special_function */
   1072 	 "R_PPC64_PLTGOT16_LO",	/* name */
   1073 	 FALSE,			/* partial_inplace */
   1074 	 0,			/* src_mask */
   1075 	 0xffff,		/* dst_mask */
   1076 	 FALSE),		/* pcrel_offset */
   1077 
   1078   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
   1079   /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
   1080   HOWTO (R_PPC64_PLTGOT16_HI,	/* type */
   1081 	 16,			/* rightshift */
   1082 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1083 	 16,			/* bitsize */
   1084 	 FALSE,			/* pc_relative */
   1085 	 0,			/* bitpos */
   1086 	 complain_overflow_signed, /* complain_on_overflow */
   1087 	 ppc64_elf_unhandled_reloc, /* special_function */
   1088 	 "R_PPC64_PLTGOT16_HI",	/* name */
   1089 	 FALSE,			/* partial_inplace */
   1090 	 0,			/* src_mask */
   1091 	 0xffff,		/* dst_mask */
   1092 	 FALSE),		/* pcrel_offset */
   1093 
   1094   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
   1095      1 if the contents of the low 16 bits, treated as a signed number,
   1096      is negative.  */
   1097   /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
   1098   HOWTO (R_PPC64_PLTGOT16_HA,	/* type */
   1099 	 16,			/* rightshift */
   1100 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1101 	 16,			/* bitsize */
   1102 	 FALSE,			/* pc_relative */
   1103 	 0,			/* bitpos */
   1104 	 complain_overflow_signed, /* complain_on_overflow */
   1105 	 ppc64_elf_unhandled_reloc, /* special_function */
   1106 	 "R_PPC64_PLTGOT16_HA",	/* name */
   1107 	 FALSE,			/* partial_inplace */
   1108 	 0,			/* src_mask */
   1109 	 0xffff,		/* dst_mask */
   1110 	 FALSE),		/* pcrel_offset */
   1111 
   1112   /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
   1113   HOWTO (R_PPC64_ADDR16_DS,	/* type */
   1114 	 0,			/* rightshift */
   1115 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1116 	 16,			/* bitsize */
   1117 	 FALSE,			/* pc_relative */
   1118 	 0,			/* bitpos */
   1119 	 complain_overflow_signed, /* complain_on_overflow */
   1120 	 bfd_elf_generic_reloc,	/* special_function */
   1121 	 "R_PPC64_ADDR16_DS",	/* name */
   1122 	 FALSE,			/* partial_inplace */
   1123 	 0,			/* src_mask */
   1124 	 0xfffc,		/* dst_mask */
   1125 	 FALSE),		/* pcrel_offset */
   1126 
   1127   /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
   1128   HOWTO (R_PPC64_ADDR16_LO_DS,	/* type */
   1129 	 0,			/* rightshift */
   1130 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1131 	 16,			/* bitsize */
   1132 	 FALSE,			/* pc_relative */
   1133 	 0,			/* bitpos */
   1134 	 complain_overflow_dont,/* complain_on_overflow */
   1135 	 bfd_elf_generic_reloc,	/* special_function */
   1136 	 "R_PPC64_ADDR16_LO_DS",/* name */
   1137 	 FALSE,			/* partial_inplace */
   1138 	 0,			/* src_mask */
   1139 	 0xfffc,		/* dst_mask */
   1140 	 FALSE),		/* pcrel_offset */
   1141 
   1142   /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
   1143   HOWTO (R_PPC64_GOT16_DS,	/* type */
   1144 	 0,			/* rightshift */
   1145 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1146 	 16,			/* bitsize */
   1147 	 FALSE,			/* pc_relative */
   1148 	 0,			/* bitpos */
   1149 	 complain_overflow_signed, /* complain_on_overflow */
   1150 	 ppc64_elf_unhandled_reloc, /* special_function */
   1151 	 "R_PPC64_GOT16_DS",	/* name */
   1152 	 FALSE,			/* partial_inplace */
   1153 	 0,			/* src_mask */
   1154 	 0xfffc,		/* dst_mask */
   1155 	 FALSE),		/* pcrel_offset */
   1156 
   1157   /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
   1158   HOWTO (R_PPC64_GOT16_LO_DS,	/* type */
   1159 	 0,			/* rightshift */
   1160 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1161 	 16,			/* bitsize */
   1162 	 FALSE,			/* pc_relative */
   1163 	 0,			/* bitpos */
   1164 	 complain_overflow_dont, /* complain_on_overflow */
   1165 	 ppc64_elf_unhandled_reloc, /* special_function */
   1166 	 "R_PPC64_GOT16_LO_DS",	/* name */
   1167 	 FALSE,			/* partial_inplace */
   1168 	 0,			/* src_mask */
   1169 	 0xfffc,		/* dst_mask */
   1170 	 FALSE),		/* pcrel_offset */
   1171 
   1172   /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
   1173   HOWTO (R_PPC64_PLT16_LO_DS,	/* type */
   1174 	 0,			/* rightshift */
   1175 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1176 	 16,			/* bitsize */
   1177 	 FALSE,			/* pc_relative */
   1178 	 0,			/* bitpos */
   1179 	 complain_overflow_dont, /* complain_on_overflow */
   1180 	 ppc64_elf_unhandled_reloc, /* special_function */
   1181 	 "R_PPC64_PLT16_LO_DS",	/* name */
   1182 	 FALSE,			/* partial_inplace */
   1183 	 0,			/* src_mask */
   1184 	 0xfffc,		/* dst_mask */
   1185 	 FALSE),		/* pcrel_offset */
   1186 
   1187   /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
   1188   HOWTO (R_PPC64_SECTOFF_DS,	/* type */
   1189 	 0,			/* rightshift */
   1190 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1191 	 16,			/* bitsize */
   1192 	 FALSE,			/* pc_relative */
   1193 	 0,			/* bitpos */
   1194 	 complain_overflow_signed, /* complain_on_overflow */
   1195 	 ppc64_elf_sectoff_reloc, /* special_function */
   1196 	 "R_PPC64_SECTOFF_DS",	/* name */
   1197 	 FALSE,			/* partial_inplace */
   1198 	 0,			/* src_mask */
   1199 	 0xfffc,		/* dst_mask */
   1200 	 FALSE),		/* pcrel_offset */
   1201 
   1202   /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
   1203   HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
   1204 	 0,			/* rightshift */
   1205 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1206 	 16,			/* bitsize */
   1207 	 FALSE,			/* pc_relative */
   1208 	 0,			/* bitpos */
   1209 	 complain_overflow_dont, /* complain_on_overflow */
   1210 	 ppc64_elf_sectoff_reloc, /* special_function */
   1211 	 "R_PPC64_SECTOFF_LO_DS",/* name */
   1212 	 FALSE,			/* partial_inplace */
   1213 	 0,			/* src_mask */
   1214 	 0xfffc,		/* dst_mask */
   1215 	 FALSE),		/* pcrel_offset */
   1216 
   1217   /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
   1218   HOWTO (R_PPC64_TOC16_DS,	/* type */
   1219 	 0,			/* rightshift */
   1220 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1221 	 16,			/* bitsize */
   1222 	 FALSE,			/* pc_relative */
   1223 	 0,			/* bitpos */
   1224 	 complain_overflow_signed, /* complain_on_overflow */
   1225 	 ppc64_elf_toc_reloc,	/* special_function */
   1226 	 "R_PPC64_TOC16_DS",	/* name */
   1227 	 FALSE,			/* partial_inplace */
   1228 	 0,			/* src_mask */
   1229 	 0xfffc,		/* dst_mask */
   1230 	 FALSE),		/* pcrel_offset */
   1231 
   1232   /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
   1233   HOWTO (R_PPC64_TOC16_LO_DS,	/* type */
   1234 	 0,			/* rightshift */
   1235 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1236 	 16,			/* bitsize */
   1237 	 FALSE,			/* pc_relative */
   1238 	 0,			/* bitpos */
   1239 	 complain_overflow_dont, /* complain_on_overflow */
   1240 	 ppc64_elf_toc_reloc,	/* special_function */
   1241 	 "R_PPC64_TOC16_LO_DS",	/* name */
   1242 	 FALSE,			/* partial_inplace */
   1243 	 0,			/* src_mask */
   1244 	 0xfffc,		/* dst_mask */
   1245 	 FALSE),		/* pcrel_offset */
   1246 
   1247   /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
   1248   /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
   1249   HOWTO (R_PPC64_PLTGOT16_DS,	/* type */
   1250 	 0,			/* rightshift */
   1251 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1252 	 16,			/* bitsize */
   1253 	 FALSE,			/* pc_relative */
   1254 	 0,			/* bitpos */
   1255 	 complain_overflow_signed, /* complain_on_overflow */
   1256 	 ppc64_elf_unhandled_reloc, /* special_function */
   1257 	 "R_PPC64_PLTGOT16_DS",	/* name */
   1258 	 FALSE,			/* partial_inplace */
   1259 	 0,			/* src_mask */
   1260 	 0xfffc,		/* dst_mask */
   1261 	 FALSE),		/* pcrel_offset */
   1262 
   1263   /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
   1264   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
   1265   HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
   1266 	 0,			/* rightshift */
   1267 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1268 	 16,			/* bitsize */
   1269 	 FALSE,			/* pc_relative */
   1270 	 0,			/* bitpos */
   1271 	 complain_overflow_dont, /* complain_on_overflow */
   1272 	 ppc64_elf_unhandled_reloc, /* special_function */
   1273 	 "R_PPC64_PLTGOT16_LO_DS",/* name */
   1274 	 FALSE,			/* partial_inplace */
   1275 	 0,			/* src_mask */
   1276 	 0xfffc,		/* dst_mask */
   1277 	 FALSE),		/* pcrel_offset */
   1278 
   1279   /* Marker relocs for TLS.  */
   1280   HOWTO (R_PPC64_TLS,
   1281 	 0,			/* rightshift */
   1282 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1283 	 32,			/* bitsize */
   1284 	 FALSE,			/* pc_relative */
   1285 	 0,			/* bitpos */
   1286 	 complain_overflow_dont, /* complain_on_overflow */
   1287 	 bfd_elf_generic_reloc,	/* special_function */
   1288 	 "R_PPC64_TLS",		/* name */
   1289 	 FALSE,			/* partial_inplace */
   1290 	 0,			/* src_mask */
   1291 	 0,			/* dst_mask */
   1292 	 FALSE),		/* pcrel_offset */
   1293 
   1294   HOWTO (R_PPC64_TLSGD,
   1295 	 0,			/* rightshift */
   1296 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1297 	 32,			/* bitsize */
   1298 	 FALSE,			/* pc_relative */
   1299 	 0,			/* bitpos */
   1300 	 complain_overflow_dont, /* complain_on_overflow */
   1301 	 bfd_elf_generic_reloc,	/* special_function */
   1302 	 "R_PPC64_TLSGD",	/* name */
   1303 	 FALSE,			/* partial_inplace */
   1304 	 0,			/* src_mask */
   1305 	 0,			/* dst_mask */
   1306 	 FALSE),		/* pcrel_offset */
   1307 
   1308   HOWTO (R_PPC64_TLSLD,
   1309 	 0,			/* rightshift */
   1310 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1311 	 32,			/* bitsize */
   1312 	 FALSE,			/* pc_relative */
   1313 	 0,			/* bitpos */
   1314 	 complain_overflow_dont, /* complain_on_overflow */
   1315 	 bfd_elf_generic_reloc,	/* special_function */
   1316 	 "R_PPC64_TLSLD",	/* name */
   1317 	 FALSE,			/* partial_inplace */
   1318 	 0,			/* src_mask */
   1319 	 0,			/* dst_mask */
   1320 	 FALSE),		/* pcrel_offset */
   1321 
   1322   HOWTO (R_PPC64_TOCSAVE,
   1323 	 0,			/* rightshift */
   1324 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1325 	 32,			/* bitsize */
   1326 	 FALSE,			/* pc_relative */
   1327 	 0,			/* bitpos */
   1328 	 complain_overflow_dont, /* complain_on_overflow */
   1329 	 bfd_elf_generic_reloc,	/* special_function */
   1330 	 "R_PPC64_TOCSAVE",	/* name */
   1331 	 FALSE,			/* partial_inplace */
   1332 	 0,			/* src_mask */
   1333 	 0,			/* dst_mask */
   1334 	 FALSE),		/* pcrel_offset */
   1335 
   1336   /* Computes the load module index of the load module that contains the
   1337      definition of its TLS sym.  */
   1338   HOWTO (R_PPC64_DTPMOD64,
   1339 	 0,			/* rightshift */
   1340 	 4,			/* size (0 = byte, 1 = short, 2 = long) */
   1341 	 64,			/* bitsize */
   1342 	 FALSE,			/* pc_relative */
   1343 	 0,			/* bitpos */
   1344 	 complain_overflow_dont, /* complain_on_overflow */
   1345 	 ppc64_elf_unhandled_reloc, /* special_function */
   1346 	 "R_PPC64_DTPMOD64",	/* name */
   1347 	 FALSE,			/* partial_inplace */
   1348 	 0,			/* src_mask */
   1349 	 ONES (64),		/* dst_mask */
   1350 	 FALSE),		/* pcrel_offset */
   1351 
   1352   /* Computes a dtv-relative displacement, the difference between the value
   1353      of sym+add and the base address of the thread-local storage block that
   1354      contains the definition of sym, minus 0x8000.  */
   1355   HOWTO (R_PPC64_DTPREL64,
   1356 	 0,			/* rightshift */
   1357 	 4,			/* size (0 = byte, 1 = short, 2 = long) */
   1358 	 64,			/* bitsize */
   1359 	 FALSE,			/* pc_relative */
   1360 	 0,			/* bitpos */
   1361 	 complain_overflow_dont, /* complain_on_overflow */
   1362 	 ppc64_elf_unhandled_reloc, /* special_function */
   1363 	 "R_PPC64_DTPREL64",	/* name */
   1364 	 FALSE,			/* partial_inplace */
   1365 	 0,			/* src_mask */
   1366 	 ONES (64),		/* dst_mask */
   1367 	 FALSE),		/* pcrel_offset */
   1368 
   1369   /* A 16 bit dtprel reloc.  */
   1370   HOWTO (R_PPC64_DTPREL16,
   1371 	 0,			/* rightshift */
   1372 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1373 	 16,			/* bitsize */
   1374 	 FALSE,			/* pc_relative */
   1375 	 0,			/* bitpos */
   1376 	 complain_overflow_signed, /* complain_on_overflow */
   1377 	 ppc64_elf_unhandled_reloc, /* special_function */
   1378 	 "R_PPC64_DTPREL16",	/* name */
   1379 	 FALSE,			/* partial_inplace */
   1380 	 0,			/* src_mask */
   1381 	 0xffff,		/* dst_mask */
   1382 	 FALSE),		/* pcrel_offset */
   1383 
   1384   /* Like DTPREL16, but no overflow.  */
   1385   HOWTO (R_PPC64_DTPREL16_LO,
   1386 	 0,			/* rightshift */
   1387 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1388 	 16,			/* bitsize */
   1389 	 FALSE,			/* pc_relative */
   1390 	 0,			/* bitpos */
   1391 	 complain_overflow_dont, /* complain_on_overflow */
   1392 	 ppc64_elf_unhandled_reloc, /* special_function */
   1393 	 "R_PPC64_DTPREL16_LO",	/* name */
   1394 	 FALSE,			/* partial_inplace */
   1395 	 0,			/* src_mask */
   1396 	 0xffff,		/* dst_mask */
   1397 	 FALSE),		/* pcrel_offset */
   1398 
   1399   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
   1400   HOWTO (R_PPC64_DTPREL16_HI,
   1401 	 16,			/* rightshift */
   1402 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1403 	 16,			/* bitsize */
   1404 	 FALSE,			/* pc_relative */
   1405 	 0,			/* bitpos */
   1406 	 complain_overflow_signed, /* complain_on_overflow */
   1407 	 ppc64_elf_unhandled_reloc, /* special_function */
   1408 	 "R_PPC64_DTPREL16_HI",	/* name */
   1409 	 FALSE,			/* partial_inplace */
   1410 	 0,			/* src_mask */
   1411 	 0xffff,		/* dst_mask */
   1412 	 FALSE),		/* pcrel_offset */
   1413 
   1414   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
   1415   HOWTO (R_PPC64_DTPREL16_HA,
   1416 	 16,			/* rightshift */
   1417 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1418 	 16,			/* bitsize */
   1419 	 FALSE,			/* pc_relative */
   1420 	 0,			/* bitpos */
   1421 	 complain_overflow_signed, /* complain_on_overflow */
   1422 	 ppc64_elf_unhandled_reloc, /* special_function */
   1423 	 "R_PPC64_DTPREL16_HA",	/* name */
   1424 	 FALSE,			/* partial_inplace */
   1425 	 0,			/* src_mask */
   1426 	 0xffff,		/* dst_mask */
   1427 	 FALSE),		/* pcrel_offset */
   1428 
   1429   /* Like DTPREL16_HI, but next higher group of 16 bits.  */
   1430   HOWTO (R_PPC64_DTPREL16_HIGHER,
   1431 	 32,			/* rightshift */
   1432 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1433 	 16,			/* bitsize */
   1434 	 FALSE,			/* pc_relative */
   1435 	 0,			/* bitpos */
   1436 	 complain_overflow_dont, /* complain_on_overflow */
   1437 	 ppc64_elf_unhandled_reloc, /* special_function */
   1438 	 "R_PPC64_DTPREL16_HIGHER", /* name */
   1439 	 FALSE,			/* partial_inplace */
   1440 	 0,			/* src_mask */
   1441 	 0xffff,		/* dst_mask */
   1442 	 FALSE),		/* pcrel_offset */
   1443 
   1444   /* Like DTPREL16_HIGHER, but adjust for low 16 bits.  */
   1445   HOWTO (R_PPC64_DTPREL16_HIGHERA,
   1446 	 32,			/* rightshift */
   1447 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1448 	 16,			/* bitsize */
   1449 	 FALSE,			/* pc_relative */
   1450 	 0,			/* bitpos */
   1451 	 complain_overflow_dont, /* complain_on_overflow */
   1452 	 ppc64_elf_unhandled_reloc, /* special_function */
   1453 	 "R_PPC64_DTPREL16_HIGHERA", /* name */
   1454 	 FALSE,			/* partial_inplace */
   1455 	 0,			/* src_mask */
   1456 	 0xffff,		/* dst_mask */
   1457 	 FALSE),		/* pcrel_offset */
   1458 
   1459   /* Like DTPREL16_HIGHER, but next higher group of 16 bits.  */
   1460   HOWTO (R_PPC64_DTPREL16_HIGHEST,
   1461 	 48,			/* rightshift */
   1462 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1463 	 16,			/* bitsize */
   1464 	 FALSE,			/* pc_relative */
   1465 	 0,			/* bitpos */
   1466 	 complain_overflow_dont, /* complain_on_overflow */
   1467 	 ppc64_elf_unhandled_reloc, /* special_function */
   1468 	 "R_PPC64_DTPREL16_HIGHEST", /* name */
   1469 	 FALSE,			/* partial_inplace */
   1470 	 0,			/* src_mask */
   1471 	 0xffff,		/* dst_mask */
   1472 	 FALSE),		/* pcrel_offset */
   1473 
   1474   /* Like DTPREL16_HIGHEST, but adjust for low 16 bits.  */
   1475   HOWTO (R_PPC64_DTPREL16_HIGHESTA,
   1476 	 48,			/* rightshift */
   1477 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1478 	 16,			/* bitsize */
   1479 	 FALSE,			/* pc_relative */
   1480 	 0,			/* bitpos */
   1481 	 complain_overflow_dont, /* complain_on_overflow */
   1482 	 ppc64_elf_unhandled_reloc, /* special_function */
   1483 	 "R_PPC64_DTPREL16_HIGHESTA", /* name */
   1484 	 FALSE,			/* partial_inplace */
   1485 	 0,			/* src_mask */
   1486 	 0xffff,		/* dst_mask */
   1487 	 FALSE),		/* pcrel_offset */
   1488 
   1489   /* Like DTPREL16, but for insns with a DS field.  */
   1490   HOWTO (R_PPC64_DTPREL16_DS,
   1491 	 0,			/* rightshift */
   1492 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1493 	 16,			/* bitsize */
   1494 	 FALSE,			/* pc_relative */
   1495 	 0,			/* bitpos */
   1496 	 complain_overflow_signed, /* complain_on_overflow */
   1497 	 ppc64_elf_unhandled_reloc, /* special_function */
   1498 	 "R_PPC64_DTPREL16_DS",	/* name */
   1499 	 FALSE,			/* partial_inplace */
   1500 	 0,			/* src_mask */
   1501 	 0xfffc,		/* dst_mask */
   1502 	 FALSE),		/* pcrel_offset */
   1503 
   1504   /* Like DTPREL16_DS, but no overflow.  */
   1505   HOWTO (R_PPC64_DTPREL16_LO_DS,
   1506 	 0,			/* rightshift */
   1507 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1508 	 16,			/* bitsize */
   1509 	 FALSE,			/* pc_relative */
   1510 	 0,			/* bitpos */
   1511 	 complain_overflow_dont, /* complain_on_overflow */
   1512 	 ppc64_elf_unhandled_reloc, /* special_function */
   1513 	 "R_PPC64_DTPREL16_LO_DS", /* name */
   1514 	 FALSE,			/* partial_inplace */
   1515 	 0,			/* src_mask */
   1516 	 0xfffc,		/* dst_mask */
   1517 	 FALSE),		/* pcrel_offset */
   1518 
   1519   /* Computes a tp-relative displacement, the difference between the value of
   1520      sym+add and the value of the thread pointer (r13).  */
   1521   HOWTO (R_PPC64_TPREL64,
   1522 	 0,			/* rightshift */
   1523 	 4,			/* size (0 = byte, 1 = short, 2 = long) */
   1524 	 64,			/* bitsize */
   1525 	 FALSE,			/* pc_relative */
   1526 	 0,			/* bitpos */
   1527 	 complain_overflow_dont, /* complain_on_overflow */
   1528 	 ppc64_elf_unhandled_reloc, /* special_function */
   1529 	 "R_PPC64_TPREL64",	/* name */
   1530 	 FALSE,			/* partial_inplace */
   1531 	 0,			/* src_mask */
   1532 	 ONES (64),		/* dst_mask */
   1533 	 FALSE),		/* pcrel_offset */
   1534 
   1535   /* A 16 bit tprel reloc.  */
   1536   HOWTO (R_PPC64_TPREL16,
   1537 	 0,			/* rightshift */
   1538 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1539 	 16,			/* bitsize */
   1540 	 FALSE,			/* pc_relative */
   1541 	 0,			/* bitpos */
   1542 	 complain_overflow_signed, /* complain_on_overflow */
   1543 	 ppc64_elf_unhandled_reloc, /* special_function */
   1544 	 "R_PPC64_TPREL16",	/* name */
   1545 	 FALSE,			/* partial_inplace */
   1546 	 0,			/* src_mask */
   1547 	 0xffff,		/* dst_mask */
   1548 	 FALSE),		/* pcrel_offset */
   1549 
   1550   /* Like TPREL16, but no overflow.  */
   1551   HOWTO (R_PPC64_TPREL16_LO,
   1552 	 0,			/* rightshift */
   1553 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1554 	 16,			/* bitsize */
   1555 	 FALSE,			/* pc_relative */
   1556 	 0,			/* bitpos */
   1557 	 complain_overflow_dont, /* complain_on_overflow */
   1558 	 ppc64_elf_unhandled_reloc, /* special_function */
   1559 	 "R_PPC64_TPREL16_LO",	/* name */
   1560 	 FALSE,			/* partial_inplace */
   1561 	 0,			/* src_mask */
   1562 	 0xffff,		/* dst_mask */
   1563 	 FALSE),		/* pcrel_offset */
   1564 
   1565   /* Like TPREL16_LO, but next higher group of 16 bits.  */
   1566   HOWTO (R_PPC64_TPREL16_HI,
   1567 	 16,			/* rightshift */
   1568 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1569 	 16,			/* bitsize */
   1570 	 FALSE,			/* pc_relative */
   1571 	 0,			/* bitpos */
   1572 	 complain_overflow_signed, /* complain_on_overflow */
   1573 	 ppc64_elf_unhandled_reloc, /* special_function */
   1574 	 "R_PPC64_TPREL16_HI",	/* name */
   1575 	 FALSE,			/* partial_inplace */
   1576 	 0,			/* src_mask */
   1577 	 0xffff,		/* dst_mask */
   1578 	 FALSE),		/* pcrel_offset */
   1579 
   1580   /* Like TPREL16_HI, but adjust for low 16 bits.  */
   1581   HOWTO (R_PPC64_TPREL16_HA,
   1582 	 16,			/* rightshift */
   1583 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1584 	 16,			/* bitsize */
   1585 	 FALSE,			/* pc_relative */
   1586 	 0,			/* bitpos */
   1587 	 complain_overflow_signed, /* complain_on_overflow */
   1588 	 ppc64_elf_unhandled_reloc, /* special_function */
   1589 	 "R_PPC64_TPREL16_HA",	/* name */
   1590 	 FALSE,			/* partial_inplace */
   1591 	 0,			/* src_mask */
   1592 	 0xffff,		/* dst_mask */
   1593 	 FALSE),		/* pcrel_offset */
   1594 
   1595   /* Like TPREL16_HI, but next higher group of 16 bits.  */
   1596   HOWTO (R_PPC64_TPREL16_HIGHER,
   1597 	 32,			/* rightshift */
   1598 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1599 	 16,			/* bitsize */
   1600 	 FALSE,			/* pc_relative */
   1601 	 0,			/* bitpos */
   1602 	 complain_overflow_dont, /* complain_on_overflow */
   1603 	 ppc64_elf_unhandled_reloc, /* special_function */
   1604 	 "R_PPC64_TPREL16_HIGHER",	/* name */
   1605 	 FALSE,			/* partial_inplace */
   1606 	 0,			/* src_mask */
   1607 	 0xffff,		/* dst_mask */
   1608 	 FALSE),		/* pcrel_offset */
   1609 
   1610   /* Like TPREL16_HIGHER, but adjust for low 16 bits.  */
   1611   HOWTO (R_PPC64_TPREL16_HIGHERA,
   1612 	 32,			/* rightshift */
   1613 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1614 	 16,			/* bitsize */
   1615 	 FALSE,			/* pc_relative */
   1616 	 0,			/* bitpos */
   1617 	 complain_overflow_dont, /* complain_on_overflow */
   1618 	 ppc64_elf_unhandled_reloc, /* special_function */
   1619 	 "R_PPC64_TPREL16_HIGHERA", /* name */
   1620 	 FALSE,			/* partial_inplace */
   1621 	 0,			/* src_mask */
   1622 	 0xffff,		/* dst_mask */
   1623 	 FALSE),		/* pcrel_offset */
   1624 
   1625   /* Like TPREL16_HIGHER, but next higher group of 16 bits.  */
   1626   HOWTO (R_PPC64_TPREL16_HIGHEST,
   1627 	 48,			/* rightshift */
   1628 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1629 	 16,			/* bitsize */
   1630 	 FALSE,			/* pc_relative */
   1631 	 0,			/* bitpos */
   1632 	 complain_overflow_dont, /* complain_on_overflow */
   1633 	 ppc64_elf_unhandled_reloc, /* special_function */
   1634 	 "R_PPC64_TPREL16_HIGHEST", /* name */
   1635 	 FALSE,			/* partial_inplace */
   1636 	 0,			/* src_mask */
   1637 	 0xffff,		/* dst_mask */
   1638 	 FALSE),		/* pcrel_offset */
   1639 
   1640   /* Like TPREL16_HIGHEST, but adjust for low 16 bits.  */
   1641   HOWTO (R_PPC64_TPREL16_HIGHESTA,
   1642 	 48,			/* rightshift */
   1643 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1644 	 16,			/* bitsize */
   1645 	 FALSE,			/* pc_relative */
   1646 	 0,			/* bitpos */
   1647 	 complain_overflow_dont, /* complain_on_overflow */
   1648 	 ppc64_elf_unhandled_reloc, /* special_function */
   1649 	 "R_PPC64_TPREL16_HIGHESTA", /* name */
   1650 	 FALSE,			/* partial_inplace */
   1651 	 0,			/* src_mask */
   1652 	 0xffff,		/* dst_mask */
   1653 	 FALSE),		/* pcrel_offset */
   1654 
   1655   /* Like TPREL16, but for insns with a DS field.  */
   1656   HOWTO (R_PPC64_TPREL16_DS,
   1657 	 0,			/* rightshift */
   1658 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1659 	 16,			/* bitsize */
   1660 	 FALSE,			/* pc_relative */
   1661 	 0,			/* bitpos */
   1662 	 complain_overflow_signed, /* complain_on_overflow */
   1663 	 ppc64_elf_unhandled_reloc, /* special_function */
   1664 	 "R_PPC64_TPREL16_DS",	/* name */
   1665 	 FALSE,			/* partial_inplace */
   1666 	 0,			/* src_mask */
   1667 	 0xfffc,		/* dst_mask */
   1668 	 FALSE),		/* pcrel_offset */
   1669 
   1670   /* Like TPREL16_DS, but no overflow.  */
   1671   HOWTO (R_PPC64_TPREL16_LO_DS,
   1672 	 0,			/* rightshift */
   1673 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1674 	 16,			/* bitsize */
   1675 	 FALSE,			/* pc_relative */
   1676 	 0,			/* bitpos */
   1677 	 complain_overflow_dont, /* complain_on_overflow */
   1678 	 ppc64_elf_unhandled_reloc, /* special_function */
   1679 	 "R_PPC64_TPREL16_LO_DS", /* name */
   1680 	 FALSE,			/* partial_inplace */
   1681 	 0,			/* src_mask */
   1682 	 0xfffc,		/* dst_mask */
   1683 	 FALSE),		/* pcrel_offset */
   1684 
   1685   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
   1686      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
   1687      to the first entry relative to the TOC base (r2).  */
   1688   HOWTO (R_PPC64_GOT_TLSGD16,
   1689 	 0,			/* rightshift */
   1690 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1691 	 16,			/* bitsize */
   1692 	 FALSE,			/* pc_relative */
   1693 	 0,			/* bitpos */
   1694 	 complain_overflow_signed, /* complain_on_overflow */
   1695 	 ppc64_elf_unhandled_reloc, /* special_function */
   1696 	 "R_PPC64_GOT_TLSGD16",	/* name */
   1697 	 FALSE,			/* partial_inplace */
   1698 	 0,			/* src_mask */
   1699 	 0xffff,		/* dst_mask */
   1700 	 FALSE),		/* pcrel_offset */
   1701 
   1702   /* Like GOT_TLSGD16, but no overflow.  */
   1703   HOWTO (R_PPC64_GOT_TLSGD16_LO,
   1704 	 0,			/* rightshift */
   1705 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1706 	 16,			/* bitsize */
   1707 	 FALSE,			/* pc_relative */
   1708 	 0,			/* bitpos */
   1709 	 complain_overflow_dont, /* complain_on_overflow */
   1710 	 ppc64_elf_unhandled_reloc, /* special_function */
   1711 	 "R_PPC64_GOT_TLSGD16_LO", /* name */
   1712 	 FALSE,			/* partial_inplace */
   1713 	 0,			/* src_mask */
   1714 	 0xffff,		/* dst_mask */
   1715 	 FALSE),		/* pcrel_offset */
   1716 
   1717   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
   1718   HOWTO (R_PPC64_GOT_TLSGD16_HI,
   1719 	 16,			/* rightshift */
   1720 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1721 	 16,			/* bitsize */
   1722 	 FALSE,			/* pc_relative */
   1723 	 0,			/* bitpos */
   1724 	 complain_overflow_signed, /* complain_on_overflow */
   1725 	 ppc64_elf_unhandled_reloc, /* special_function */
   1726 	 "R_PPC64_GOT_TLSGD16_HI", /* name */
   1727 	 FALSE,			/* partial_inplace */
   1728 	 0,			/* src_mask */
   1729 	 0xffff,		/* dst_mask */
   1730 	 FALSE),		/* pcrel_offset */
   1731 
   1732   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
   1733   HOWTO (R_PPC64_GOT_TLSGD16_HA,
   1734 	 16,			/* rightshift */
   1735 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1736 	 16,			/* bitsize */
   1737 	 FALSE,			/* pc_relative */
   1738 	 0,			/* bitpos */
   1739 	 complain_overflow_signed, /* complain_on_overflow */
   1740 	 ppc64_elf_unhandled_reloc, /* special_function */
   1741 	 "R_PPC64_GOT_TLSGD16_HA", /* name */
   1742 	 FALSE,			/* partial_inplace */
   1743 	 0,			/* src_mask */
   1744 	 0xffff,		/* dst_mask */
   1745 	 FALSE),		/* pcrel_offset */
   1746 
   1747   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
   1748      with values (sym+add)@dtpmod and zero, and computes the offset to the
   1749      first entry relative to the TOC base (r2).  */
   1750   HOWTO (R_PPC64_GOT_TLSLD16,
   1751 	 0,			/* rightshift */
   1752 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1753 	 16,			/* bitsize */
   1754 	 FALSE,			/* pc_relative */
   1755 	 0,			/* bitpos */
   1756 	 complain_overflow_signed, /* complain_on_overflow */
   1757 	 ppc64_elf_unhandled_reloc, /* special_function */
   1758 	 "R_PPC64_GOT_TLSLD16",	/* name */
   1759 	 FALSE,			/* partial_inplace */
   1760 	 0,			/* src_mask */
   1761 	 0xffff,		/* dst_mask */
   1762 	 FALSE),		/* pcrel_offset */
   1763 
   1764   /* Like GOT_TLSLD16, but no overflow.  */
   1765   HOWTO (R_PPC64_GOT_TLSLD16_LO,
   1766 	 0,			/* rightshift */
   1767 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1768 	 16,			/* bitsize */
   1769 	 FALSE,			/* pc_relative */
   1770 	 0,			/* bitpos */
   1771 	 complain_overflow_dont, /* complain_on_overflow */
   1772 	 ppc64_elf_unhandled_reloc, /* special_function */
   1773 	 "R_PPC64_GOT_TLSLD16_LO", /* name */
   1774 	 FALSE,			/* partial_inplace */
   1775 	 0,			/* src_mask */
   1776 	 0xffff,		/* dst_mask */
   1777 	 FALSE),		/* pcrel_offset */
   1778 
   1779   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
   1780   HOWTO (R_PPC64_GOT_TLSLD16_HI,
   1781 	 16,			/* rightshift */
   1782 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1783 	 16,			/* bitsize */
   1784 	 FALSE,			/* pc_relative */
   1785 	 0,			/* bitpos */
   1786 	 complain_overflow_signed, /* complain_on_overflow */
   1787 	 ppc64_elf_unhandled_reloc, /* special_function */
   1788 	 "R_PPC64_GOT_TLSLD16_HI", /* name */
   1789 	 FALSE,			/* partial_inplace */
   1790 	 0,			/* src_mask */
   1791 	 0xffff,		/* dst_mask */
   1792 	 FALSE),		/* pcrel_offset */
   1793 
   1794   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
   1795   HOWTO (R_PPC64_GOT_TLSLD16_HA,
   1796 	 16,			/* rightshift */
   1797 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1798 	 16,			/* bitsize */
   1799 	 FALSE,			/* pc_relative */
   1800 	 0,			/* bitpos */
   1801 	 complain_overflow_signed, /* complain_on_overflow */
   1802 	 ppc64_elf_unhandled_reloc, /* special_function */
   1803 	 "R_PPC64_GOT_TLSLD16_HA", /* name */
   1804 	 FALSE,			/* partial_inplace */
   1805 	 0,			/* src_mask */
   1806 	 0xffff,		/* dst_mask */
   1807 	 FALSE),		/* pcrel_offset */
   1808 
   1809   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
   1810      the offset to the entry relative to the TOC base (r2).  */
   1811   HOWTO (R_PPC64_GOT_DTPREL16_DS,
   1812 	 0,			/* rightshift */
   1813 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1814 	 16,			/* bitsize */
   1815 	 FALSE,			/* pc_relative */
   1816 	 0,			/* bitpos */
   1817 	 complain_overflow_signed, /* complain_on_overflow */
   1818 	 ppc64_elf_unhandled_reloc, /* special_function */
   1819 	 "R_PPC64_GOT_DTPREL16_DS", /* name */
   1820 	 FALSE,			/* partial_inplace */
   1821 	 0,			/* src_mask */
   1822 	 0xfffc,		/* dst_mask */
   1823 	 FALSE),		/* pcrel_offset */
   1824 
   1825   /* Like GOT_DTPREL16_DS, but no overflow.  */
   1826   HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
   1827 	 0,			/* rightshift */
   1828 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1829 	 16,			/* bitsize */
   1830 	 FALSE,			/* pc_relative */
   1831 	 0,			/* bitpos */
   1832 	 complain_overflow_dont, /* complain_on_overflow */
   1833 	 ppc64_elf_unhandled_reloc, /* special_function */
   1834 	 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
   1835 	 FALSE,			/* partial_inplace */
   1836 	 0,			/* src_mask */
   1837 	 0xfffc,		/* dst_mask */
   1838 	 FALSE),		/* pcrel_offset */
   1839 
   1840   /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits.  */
   1841   HOWTO (R_PPC64_GOT_DTPREL16_HI,
   1842 	 16,			/* rightshift */
   1843 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1844 	 16,			/* bitsize */
   1845 	 FALSE,			/* pc_relative */
   1846 	 0,			/* bitpos */
   1847 	 complain_overflow_signed, /* complain_on_overflow */
   1848 	 ppc64_elf_unhandled_reloc, /* special_function */
   1849 	 "R_PPC64_GOT_DTPREL16_HI", /* name */
   1850 	 FALSE,			/* partial_inplace */
   1851 	 0,			/* src_mask */
   1852 	 0xffff,		/* dst_mask */
   1853 	 FALSE),		/* pcrel_offset */
   1854 
   1855   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
   1856   HOWTO (R_PPC64_GOT_DTPREL16_HA,
   1857 	 16,			/* rightshift */
   1858 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1859 	 16,			/* bitsize */
   1860 	 FALSE,			/* pc_relative */
   1861 	 0,			/* bitpos */
   1862 	 complain_overflow_signed, /* complain_on_overflow */
   1863 	 ppc64_elf_unhandled_reloc, /* special_function */
   1864 	 "R_PPC64_GOT_DTPREL16_HA", /* name */
   1865 	 FALSE,			/* partial_inplace */
   1866 	 0,			/* src_mask */
   1867 	 0xffff,		/* dst_mask */
   1868 	 FALSE),		/* pcrel_offset */
   1869 
   1870   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
   1871      offset to the entry relative to the TOC base (r2).  */
   1872   HOWTO (R_PPC64_GOT_TPREL16_DS,
   1873 	 0,			/* rightshift */
   1874 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1875 	 16,			/* bitsize */
   1876 	 FALSE,			/* pc_relative */
   1877 	 0,			/* bitpos */
   1878 	 complain_overflow_signed, /* complain_on_overflow */
   1879 	 ppc64_elf_unhandled_reloc, /* special_function */
   1880 	 "R_PPC64_GOT_TPREL16_DS", /* name */
   1881 	 FALSE,			/* partial_inplace */
   1882 	 0,			/* src_mask */
   1883 	 0xfffc,		/* dst_mask */
   1884 	 FALSE),		/* pcrel_offset */
   1885 
   1886   /* Like GOT_TPREL16_DS, but no overflow.  */
   1887   HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
   1888 	 0,			/* rightshift */
   1889 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1890 	 16,			/* bitsize */
   1891 	 FALSE,			/* pc_relative */
   1892 	 0,			/* bitpos */
   1893 	 complain_overflow_dont, /* complain_on_overflow */
   1894 	 ppc64_elf_unhandled_reloc, /* special_function */
   1895 	 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
   1896 	 FALSE,			/* partial_inplace */
   1897 	 0,			/* src_mask */
   1898 	 0xfffc,		/* dst_mask */
   1899 	 FALSE),		/* pcrel_offset */
   1900 
   1901   /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits.  */
   1902   HOWTO (R_PPC64_GOT_TPREL16_HI,
   1903 	 16,			/* rightshift */
   1904 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1905 	 16,			/* bitsize */
   1906 	 FALSE,			/* pc_relative */
   1907 	 0,			/* bitpos */
   1908 	 complain_overflow_signed, /* complain_on_overflow */
   1909 	 ppc64_elf_unhandled_reloc, /* special_function */
   1910 	 "R_PPC64_GOT_TPREL16_HI", /* name */
   1911 	 FALSE,			/* partial_inplace */
   1912 	 0,			/* src_mask */
   1913 	 0xffff,		/* dst_mask */
   1914 	 FALSE),		/* pcrel_offset */
   1915 
   1916   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
   1917   HOWTO (R_PPC64_GOT_TPREL16_HA,
   1918 	 16,			/* rightshift */
   1919 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1920 	 16,			/* bitsize */
   1921 	 FALSE,			/* pc_relative */
   1922 	 0,			/* bitpos */
   1923 	 complain_overflow_signed, /* complain_on_overflow */
   1924 	 ppc64_elf_unhandled_reloc, /* special_function */
   1925 	 "R_PPC64_GOT_TPREL16_HA", /* name */
   1926 	 FALSE,			/* partial_inplace */
   1927 	 0,			/* src_mask */
   1928 	 0xffff,		/* dst_mask */
   1929 	 FALSE),		/* pcrel_offset */
   1930 
   1931   HOWTO (R_PPC64_JMP_IREL,	/* type */
   1932 	 0,			/* rightshift */
   1933 	 0,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
   1934 	 0,			/* bitsize */
   1935 	 FALSE,			/* pc_relative */
   1936 	 0,			/* bitpos */
   1937 	 complain_overflow_dont, /* complain_on_overflow */
   1938 	 ppc64_elf_unhandled_reloc, /* special_function */
   1939 	 "R_PPC64_JMP_IREL",	/* name */
   1940 	 FALSE,			/* partial_inplace */
   1941 	 0,			/* src_mask */
   1942 	 0,			/* dst_mask */
   1943 	 FALSE),		/* pcrel_offset */
   1944 
   1945   HOWTO (R_PPC64_IRELATIVE,	/* type */
   1946 	 0,			/* rightshift */
   1947 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
   1948 	 64,			/* bitsize */
   1949 	 FALSE,			/* pc_relative */
   1950 	 0,			/* bitpos */
   1951 	 complain_overflow_dont, /* complain_on_overflow */
   1952 	 bfd_elf_generic_reloc,	/* special_function */
   1953 	 "R_PPC64_IRELATIVE",	/* name */
   1954 	 FALSE,			/* partial_inplace */
   1955 	 0,			/* src_mask */
   1956 	 ONES (64),		/* dst_mask */
   1957 	 FALSE),		/* pcrel_offset */
   1958 
   1959   /* A 16 bit relative relocation.  */
   1960   HOWTO (R_PPC64_REL16,		/* type */
   1961 	 0,			/* rightshift */
   1962 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1963 	 16,			/* bitsize */
   1964 	 TRUE,			/* pc_relative */
   1965 	 0,			/* bitpos */
   1966 	 complain_overflow_signed, /* complain_on_overflow */
   1967 	 bfd_elf_generic_reloc,	/* special_function */
   1968 	 "R_PPC64_REL16",	/* name */
   1969 	 FALSE,			/* partial_inplace */
   1970 	 0,			/* src_mask */
   1971 	 0xffff,		/* dst_mask */
   1972 	 TRUE),			/* pcrel_offset */
   1973 
   1974   /* A 16 bit relative relocation without overflow.  */
   1975   HOWTO (R_PPC64_REL16_LO,	/* type */
   1976 	 0,			/* rightshift */
   1977 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1978 	 16,			/* bitsize */
   1979 	 TRUE,			/* pc_relative */
   1980 	 0,			/* bitpos */
   1981 	 complain_overflow_dont,/* complain_on_overflow */
   1982 	 bfd_elf_generic_reloc,	/* special_function */
   1983 	 "R_PPC64_REL16_LO",	/* name */
   1984 	 FALSE,			/* partial_inplace */
   1985 	 0,			/* src_mask */
   1986 	 0xffff,		/* dst_mask */
   1987 	 TRUE),			/* pcrel_offset */
   1988 
   1989   /* The high order 16 bits of a relative address.  */
   1990   HOWTO (R_PPC64_REL16_HI,	/* type */
   1991 	 16,			/* rightshift */
   1992 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1993 	 16,			/* bitsize */
   1994 	 TRUE,			/* pc_relative */
   1995 	 0,			/* bitpos */
   1996 	 complain_overflow_signed, /* complain_on_overflow */
   1997 	 bfd_elf_generic_reloc,	/* special_function */
   1998 	 "R_PPC64_REL16_HI",	/* name */
   1999 	 FALSE,			/* partial_inplace */
   2000 	 0,			/* src_mask */
   2001 	 0xffff,		/* dst_mask */
   2002 	 TRUE),			/* pcrel_offset */
   2003 
   2004   /* The high order 16 bits of a relative address, plus 1 if the contents of
   2005      the low 16 bits, treated as a signed number, is negative.  */
   2006   HOWTO (R_PPC64_REL16_HA,	/* type */
   2007 	 16,			/* rightshift */
   2008 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   2009 	 16,			/* bitsize */
   2010 	 TRUE,			/* pc_relative */
   2011 	 0,			/* bitpos */
   2012 	 complain_overflow_signed, /* complain_on_overflow */
   2013 	 ppc64_elf_ha_reloc,	/* special_function */
   2014 	 "R_PPC64_REL16_HA",	/* name */
   2015 	 FALSE,			/* partial_inplace */
   2016 	 0,			/* src_mask */
   2017 	 0xffff,		/* dst_mask */
   2018 	 TRUE),			/* pcrel_offset */
   2019 
   2020   /* Like R_PPC64_ADDR16_HI, but no overflow.  */
   2021   HOWTO (R_PPC64_ADDR16_HIGH,	/* type */
   2022 	 16,			/* rightshift */
   2023 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   2024 	 16,			/* bitsize */
   2025 	 FALSE,			/* pc_relative */
   2026 	 0,			/* bitpos */
   2027 	 complain_overflow_dont, /* complain_on_overflow */
   2028 	 bfd_elf_generic_reloc,	/* special_function */
   2029 	 "R_PPC64_ADDR16_HIGH",	/* name */
   2030 	 FALSE,			/* partial_inplace */
   2031 	 0,			/* src_mask */
   2032 	 0xffff,		/* dst_mask */
   2033 	 FALSE),		/* pcrel_offset */
   2034 
   2035   /* Like R_PPC64_ADDR16_HA, but no overflow.  */
   2036   HOWTO (R_PPC64_ADDR16_HIGHA,	/* type */
   2037 	 16,			/* rightshift */
   2038 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   2039 	 16,			/* bitsize */
   2040 	 FALSE,			/* pc_relative */
   2041 	 0,			/* bitpos */
   2042 	 complain_overflow_dont, /* complain_on_overflow */
   2043 	 ppc64_elf_ha_reloc,	/* special_function */
   2044 	 "R_PPC64_ADDR16_HIGHA",	/* name */
   2045 	 FALSE,			/* partial_inplace */
   2046 	 0,			/* src_mask */
   2047 	 0xffff,		/* dst_mask */
   2048 	 FALSE),		/* pcrel_offset */
   2049 
   2050   /* Like R_PPC64_DTPREL16_HI, but no overflow.  */
   2051   HOWTO (R_PPC64_DTPREL16_HIGH,
   2052 	 16,			/* rightshift */
   2053 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   2054 	 16,			/* bitsize */
   2055 	 FALSE,			/* pc_relative */
   2056 	 0,			/* bitpos */
   2057 	 complain_overflow_dont, /* complain_on_overflow */
   2058 	 ppc64_elf_unhandled_reloc, /* special_function */
   2059 	 "R_PPC64_DTPREL16_HIGH", /* name */
   2060 	 FALSE,			/* partial_inplace */
   2061 	 0,			/* src_mask */
   2062 	 0xffff,		/* dst_mask */
   2063 	 FALSE),		/* pcrel_offset */
   2064 
   2065   /* Like R_PPC64_DTPREL16_HA, but no overflow.  */
   2066   HOWTO (R_PPC64_DTPREL16_HIGHA,
   2067 	 16,			/* rightshift */
   2068 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   2069 	 16,			/* bitsize */
   2070 	 FALSE,			/* pc_relative */
   2071 	 0,			/* bitpos */
   2072 	 complain_overflow_dont, /* complain_on_overflow */
   2073 	 ppc64_elf_unhandled_reloc, /* special_function */
   2074 	 "R_PPC64_DTPREL16_HIGHA", /* name */
   2075 	 FALSE,			/* partial_inplace */
   2076 	 0,			/* src_mask */
   2077 	 0xffff,		/* dst_mask */
   2078 	 FALSE),		/* pcrel_offset */
   2079 
   2080   /* Like R_PPC64_TPREL16_HI, but no overflow.  */
   2081   HOWTO (R_PPC64_TPREL16_HIGH,
   2082 	 16,			/* rightshift */
   2083 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   2084 	 16,			/* bitsize */
   2085 	 FALSE,			/* pc_relative */
   2086 	 0,			/* bitpos */
   2087 	 complain_overflow_dont, /* complain_on_overflow */
   2088 	 ppc64_elf_unhandled_reloc, /* special_function */
   2089 	 "R_PPC64_TPREL16_HIGH",	/* name */
   2090 	 FALSE,			/* partial_inplace */
   2091 	 0,			/* src_mask */
   2092 	 0xffff,		/* dst_mask */
   2093 	 FALSE),		/* pcrel_offset */
   2094 
   2095   /* Like R_PPC64_TPREL16_HA, but no overflow.  */
   2096   HOWTO (R_PPC64_TPREL16_HIGHA,
   2097 	 16,			/* rightshift */
   2098 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   2099 	 16,			/* bitsize */
   2100 	 FALSE,			/* pc_relative */
   2101 	 0,			/* bitpos */
   2102 	 complain_overflow_dont, /* complain_on_overflow */
   2103 	 ppc64_elf_unhandled_reloc, /* special_function */
   2104 	 "R_PPC64_TPREL16_HIGHA",	/* name */
   2105 	 FALSE,			/* partial_inplace */
   2106 	 0,			/* src_mask */
   2107 	 0xffff,		/* dst_mask */
   2108 	 FALSE),		/* pcrel_offset */
   2109 
   2110   /* Like ADDR64, but use local entry point of function.  */
   2111   HOWTO (R_PPC64_ADDR64_LOCAL,	/* type */
   2112 	 0,			/* rightshift */
   2113 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
   2114 	 64,			/* bitsize */
   2115 	 FALSE,			/* pc_relative */
   2116 	 0,			/* bitpos */
   2117 	 complain_overflow_dont, /* complain_on_overflow */
   2118 	 bfd_elf_generic_reloc,	/* special_function */
   2119 	 "R_PPC64_ADDR64_LOCAL", /* name */
   2120 	 FALSE,			/* partial_inplace */
   2121 	 0,			/* src_mask */
   2122 	 ONES (64),		/* dst_mask */
   2123 	 FALSE),		/* pcrel_offset */
   2124 
   2125   /* GNU extension to record C++ vtable hierarchy.  */
   2126   HOWTO (R_PPC64_GNU_VTINHERIT,	/* type */
   2127 	 0,			/* rightshift */
   2128 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
   2129 	 0,			/* bitsize */
   2130 	 FALSE,			/* pc_relative */
   2131 	 0,			/* bitpos */
   2132 	 complain_overflow_dont, /* complain_on_overflow */
   2133 	 NULL,			/* special_function */
   2134 	 "R_PPC64_GNU_VTINHERIT", /* name */
   2135 	 FALSE,			/* partial_inplace */
   2136 	 0,			/* src_mask */
   2137 	 0,			/* dst_mask */
   2138 	 FALSE),		/* pcrel_offset */
   2139 
   2140   /* GNU extension to record C++ vtable member usage.  */
   2141   HOWTO (R_PPC64_GNU_VTENTRY,	/* type */
   2142 	 0,			/* rightshift */
   2143 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
   2144 	 0,			/* bitsize */
   2145 	 FALSE,			/* pc_relative */
   2146 	 0,			/* bitpos */
   2147 	 complain_overflow_dont, /* complain_on_overflow */
   2148 	 NULL,			/* special_function */
   2149 	 "R_PPC64_GNU_VTENTRY",	/* name */
   2150 	 FALSE,			/* partial_inplace */
   2151 	 0,			/* src_mask */
   2152 	 0,			/* dst_mask */
   2153 	 FALSE),		/* pcrel_offset */
   2154 };
   2155 
   2156 
   2157 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
   2159    be done.  */
   2160 
   2161 static void
   2162 ppc_howto_init (void)
   2163 {
   2164   unsigned int i, type;
   2165 
   2166   for (i = 0;
   2167        i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
   2168        i++)
   2169     {
   2170       type = ppc64_elf_howto_raw[i].type;
   2171       BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
   2172 			  / sizeof (ppc64_elf_howto_table[0])));
   2173       ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
   2174     }
   2175 }
   2176 
   2177 static reloc_howto_type *
   2178 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
   2179 			     bfd_reloc_code_real_type code)
   2180 {
   2181   enum elf_ppc64_reloc_type r = R_PPC64_NONE;
   2182 
   2183   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
   2184     /* Initialize howto table if needed.  */
   2185     ppc_howto_init ();
   2186 
   2187   switch (code)
   2188     {
   2189     default:
   2190       return NULL;
   2191 
   2192     case BFD_RELOC_NONE:			r = R_PPC64_NONE;
   2193       break;
   2194     case BFD_RELOC_32:				r = R_PPC64_ADDR32;
   2195       break;
   2196     case BFD_RELOC_PPC_BA26:			r = R_PPC64_ADDR24;
   2197       break;
   2198     case BFD_RELOC_16:				r = R_PPC64_ADDR16;
   2199       break;
   2200     case BFD_RELOC_LO16:			r = R_PPC64_ADDR16_LO;
   2201       break;
   2202     case BFD_RELOC_HI16:			r = R_PPC64_ADDR16_HI;
   2203       break;
   2204     case BFD_RELOC_PPC64_ADDR16_HIGH:		r = R_PPC64_ADDR16_HIGH;
   2205       break;
   2206     case BFD_RELOC_HI16_S:			r = R_PPC64_ADDR16_HA;
   2207       break;
   2208     case BFD_RELOC_PPC64_ADDR16_HIGHA:		r = R_PPC64_ADDR16_HIGHA;
   2209       break;
   2210     case BFD_RELOC_PPC_BA16:			r = R_PPC64_ADDR14;
   2211       break;
   2212     case BFD_RELOC_PPC_BA16_BRTAKEN:		r = R_PPC64_ADDR14_BRTAKEN;
   2213       break;
   2214     case BFD_RELOC_PPC_BA16_BRNTAKEN:		r = R_PPC64_ADDR14_BRNTAKEN;
   2215       break;
   2216     case BFD_RELOC_PPC_B26:			r = R_PPC64_REL24;
   2217       break;
   2218     case BFD_RELOC_PPC_B16:			r = R_PPC64_REL14;
   2219       break;
   2220     case BFD_RELOC_PPC_B16_BRTAKEN:		r = R_PPC64_REL14_BRTAKEN;
   2221       break;
   2222     case BFD_RELOC_PPC_B16_BRNTAKEN:		r = R_PPC64_REL14_BRNTAKEN;
   2223       break;
   2224     case BFD_RELOC_16_GOTOFF:			r = R_PPC64_GOT16;
   2225       break;
   2226     case BFD_RELOC_LO16_GOTOFF:			r = R_PPC64_GOT16_LO;
   2227       break;
   2228     case BFD_RELOC_HI16_GOTOFF:			r = R_PPC64_GOT16_HI;
   2229       break;
   2230     case BFD_RELOC_HI16_S_GOTOFF:		r = R_PPC64_GOT16_HA;
   2231       break;
   2232     case BFD_RELOC_PPC_COPY:			r = R_PPC64_COPY;
   2233       break;
   2234     case BFD_RELOC_PPC_GLOB_DAT:		r = R_PPC64_GLOB_DAT;
   2235       break;
   2236     case BFD_RELOC_32_PCREL:			r = R_PPC64_REL32;
   2237       break;
   2238     case BFD_RELOC_32_PLTOFF:			r = R_PPC64_PLT32;
   2239       break;
   2240     case BFD_RELOC_32_PLT_PCREL:		r = R_PPC64_PLTREL32;
   2241       break;
   2242     case BFD_RELOC_LO16_PLTOFF:			r = R_PPC64_PLT16_LO;
   2243       break;
   2244     case BFD_RELOC_HI16_PLTOFF:			r = R_PPC64_PLT16_HI;
   2245       break;
   2246     case BFD_RELOC_HI16_S_PLTOFF:		r = R_PPC64_PLT16_HA;
   2247       break;
   2248     case BFD_RELOC_16_BASEREL:			r = R_PPC64_SECTOFF;
   2249       break;
   2250     case BFD_RELOC_LO16_BASEREL:		r = R_PPC64_SECTOFF_LO;
   2251       break;
   2252     case BFD_RELOC_HI16_BASEREL:		r = R_PPC64_SECTOFF_HI;
   2253       break;
   2254     case BFD_RELOC_HI16_S_BASEREL:		r = R_PPC64_SECTOFF_HA;
   2255       break;
   2256     case BFD_RELOC_CTOR:			r = R_PPC64_ADDR64;
   2257       break;
   2258     case BFD_RELOC_64:				r = R_PPC64_ADDR64;
   2259       break;
   2260     case BFD_RELOC_PPC64_HIGHER:		r = R_PPC64_ADDR16_HIGHER;
   2261       break;
   2262     case BFD_RELOC_PPC64_HIGHER_S:		r = R_PPC64_ADDR16_HIGHERA;
   2263       break;
   2264     case BFD_RELOC_PPC64_HIGHEST:		r = R_PPC64_ADDR16_HIGHEST;
   2265       break;
   2266     case BFD_RELOC_PPC64_HIGHEST_S:		r = R_PPC64_ADDR16_HIGHESTA;
   2267       break;
   2268     case BFD_RELOC_64_PCREL:			r = R_PPC64_REL64;
   2269       break;
   2270     case BFD_RELOC_64_PLTOFF:			r = R_PPC64_PLT64;
   2271       break;
   2272     case BFD_RELOC_64_PLT_PCREL:		r = R_PPC64_PLTREL64;
   2273       break;
   2274     case BFD_RELOC_PPC_TOC16:			r = R_PPC64_TOC16;
   2275       break;
   2276     case BFD_RELOC_PPC64_TOC16_LO:		r = R_PPC64_TOC16_LO;
   2277       break;
   2278     case BFD_RELOC_PPC64_TOC16_HI:		r = R_PPC64_TOC16_HI;
   2279       break;
   2280     case BFD_RELOC_PPC64_TOC16_HA:		r = R_PPC64_TOC16_HA;
   2281       break;
   2282     case BFD_RELOC_PPC64_TOC:			r = R_PPC64_TOC;
   2283       break;
   2284     case BFD_RELOC_PPC64_PLTGOT16:		r = R_PPC64_PLTGOT16;
   2285       break;
   2286     case BFD_RELOC_PPC64_PLTGOT16_LO:		r = R_PPC64_PLTGOT16_LO;
   2287       break;
   2288     case BFD_RELOC_PPC64_PLTGOT16_HI:		r = R_PPC64_PLTGOT16_HI;
   2289       break;
   2290     case BFD_RELOC_PPC64_PLTGOT16_HA:		r = R_PPC64_PLTGOT16_HA;
   2291       break;
   2292     case BFD_RELOC_PPC64_ADDR16_DS:		r = R_PPC64_ADDR16_DS;
   2293       break;
   2294     case BFD_RELOC_PPC64_ADDR16_LO_DS:		r = R_PPC64_ADDR16_LO_DS;
   2295       break;
   2296     case BFD_RELOC_PPC64_GOT16_DS:		r = R_PPC64_GOT16_DS;
   2297       break;
   2298     case BFD_RELOC_PPC64_GOT16_LO_DS:		r = R_PPC64_GOT16_LO_DS;
   2299       break;
   2300     case BFD_RELOC_PPC64_PLT16_LO_DS:		r = R_PPC64_PLT16_LO_DS;
   2301       break;
   2302     case BFD_RELOC_PPC64_SECTOFF_DS:		r = R_PPC64_SECTOFF_DS;
   2303       break;
   2304     case BFD_RELOC_PPC64_SECTOFF_LO_DS:		r = R_PPC64_SECTOFF_LO_DS;
   2305       break;
   2306     case BFD_RELOC_PPC64_TOC16_DS:		r = R_PPC64_TOC16_DS;
   2307       break;
   2308     case BFD_RELOC_PPC64_TOC16_LO_DS:		r = R_PPC64_TOC16_LO_DS;
   2309       break;
   2310     case BFD_RELOC_PPC64_PLTGOT16_DS:		r = R_PPC64_PLTGOT16_DS;
   2311       break;
   2312     case BFD_RELOC_PPC64_PLTGOT16_LO_DS:	r = R_PPC64_PLTGOT16_LO_DS;
   2313       break;
   2314     case BFD_RELOC_PPC_TLS:			r = R_PPC64_TLS;
   2315       break;
   2316     case BFD_RELOC_PPC_TLSGD:			r = R_PPC64_TLSGD;
   2317       break;
   2318     case BFD_RELOC_PPC_TLSLD:			r = R_PPC64_TLSLD;
   2319       break;
   2320     case BFD_RELOC_PPC_DTPMOD:			r = R_PPC64_DTPMOD64;
   2321       break;
   2322     case BFD_RELOC_PPC_TPREL16:			r = R_PPC64_TPREL16;
   2323       break;
   2324     case BFD_RELOC_PPC_TPREL16_LO:		r = R_PPC64_TPREL16_LO;
   2325       break;
   2326     case BFD_RELOC_PPC_TPREL16_HI:		r = R_PPC64_TPREL16_HI;
   2327       break;
   2328     case BFD_RELOC_PPC64_TPREL16_HIGH:		r = R_PPC64_TPREL16_HIGH;
   2329       break;
   2330     case BFD_RELOC_PPC_TPREL16_HA:		r = R_PPC64_TPREL16_HA;
   2331       break;
   2332     case BFD_RELOC_PPC64_TPREL16_HIGHA:		r = R_PPC64_TPREL16_HIGHA;
   2333       break;
   2334     case BFD_RELOC_PPC_TPREL:			r = R_PPC64_TPREL64;
   2335       break;
   2336     case BFD_RELOC_PPC_DTPREL16:		r = R_PPC64_DTPREL16;
   2337       break;
   2338     case BFD_RELOC_PPC_DTPREL16_LO:		r = R_PPC64_DTPREL16_LO;
   2339       break;
   2340     case BFD_RELOC_PPC_DTPREL16_HI:		r = R_PPC64_DTPREL16_HI;
   2341       break;
   2342     case BFD_RELOC_PPC64_DTPREL16_HIGH:		r = R_PPC64_DTPREL16_HIGH;
   2343       break;
   2344     case BFD_RELOC_PPC_DTPREL16_HA:		r = R_PPC64_DTPREL16_HA;
   2345       break;
   2346     case BFD_RELOC_PPC64_DTPREL16_HIGHA:	r = R_PPC64_DTPREL16_HIGHA;
   2347       break;
   2348     case BFD_RELOC_PPC_DTPREL:			r = R_PPC64_DTPREL64;
   2349       break;
   2350     case BFD_RELOC_PPC_GOT_TLSGD16:		r = R_PPC64_GOT_TLSGD16;
   2351       break;
   2352     case BFD_RELOC_PPC_GOT_TLSGD16_LO:		r = R_PPC64_GOT_TLSGD16_LO;
   2353       break;
   2354     case BFD_RELOC_PPC_GOT_TLSGD16_HI:		r = R_PPC64_GOT_TLSGD16_HI;
   2355       break;
   2356     case BFD_RELOC_PPC_GOT_TLSGD16_HA:		r = R_PPC64_GOT_TLSGD16_HA;
   2357       break;
   2358     case BFD_RELOC_PPC_GOT_TLSLD16:		r = R_PPC64_GOT_TLSLD16;
   2359       break;
   2360     case BFD_RELOC_PPC_GOT_TLSLD16_LO:		r = R_PPC64_GOT_TLSLD16_LO;
   2361       break;
   2362     case BFD_RELOC_PPC_GOT_TLSLD16_HI:		r = R_PPC64_GOT_TLSLD16_HI;
   2363       break;
   2364     case BFD_RELOC_PPC_GOT_TLSLD16_HA:		r = R_PPC64_GOT_TLSLD16_HA;
   2365       break;
   2366     case BFD_RELOC_PPC_GOT_TPREL16:		r = R_PPC64_GOT_TPREL16_DS;
   2367       break;
   2368     case BFD_RELOC_PPC_GOT_TPREL16_LO:		r = R_PPC64_GOT_TPREL16_LO_DS;
   2369       break;
   2370     case BFD_RELOC_PPC_GOT_TPREL16_HI:		r = R_PPC64_GOT_TPREL16_HI;
   2371       break;
   2372     case BFD_RELOC_PPC_GOT_TPREL16_HA:		r = R_PPC64_GOT_TPREL16_HA;
   2373       break;
   2374     case BFD_RELOC_PPC_GOT_DTPREL16:		r = R_PPC64_GOT_DTPREL16_DS;
   2375       break;
   2376     case BFD_RELOC_PPC_GOT_DTPREL16_LO:		r = R_PPC64_GOT_DTPREL16_LO_DS;
   2377       break;
   2378     case BFD_RELOC_PPC_GOT_DTPREL16_HI:		r = R_PPC64_GOT_DTPREL16_HI;
   2379       break;
   2380     case BFD_RELOC_PPC_GOT_DTPREL16_HA:		r = R_PPC64_GOT_DTPREL16_HA;
   2381       break;
   2382     case BFD_RELOC_PPC64_TPREL16_DS:		r = R_PPC64_TPREL16_DS;
   2383       break;
   2384     case BFD_RELOC_PPC64_TPREL16_LO_DS:		r = R_PPC64_TPREL16_LO_DS;
   2385       break;
   2386     case BFD_RELOC_PPC64_TPREL16_HIGHER:	r = R_PPC64_TPREL16_HIGHER;
   2387       break;
   2388     case BFD_RELOC_PPC64_TPREL16_HIGHERA:	r = R_PPC64_TPREL16_HIGHERA;
   2389       break;
   2390     case BFD_RELOC_PPC64_TPREL16_HIGHEST:	r = R_PPC64_TPREL16_HIGHEST;
   2391       break;
   2392     case BFD_RELOC_PPC64_TPREL16_HIGHESTA:	r = R_PPC64_TPREL16_HIGHESTA;
   2393       break;
   2394     case BFD_RELOC_PPC64_DTPREL16_DS:		r = R_PPC64_DTPREL16_DS;
   2395       break;
   2396     case BFD_RELOC_PPC64_DTPREL16_LO_DS:	r = R_PPC64_DTPREL16_LO_DS;
   2397       break;
   2398     case BFD_RELOC_PPC64_DTPREL16_HIGHER:	r = R_PPC64_DTPREL16_HIGHER;
   2399       break;
   2400     case BFD_RELOC_PPC64_DTPREL16_HIGHERA:	r = R_PPC64_DTPREL16_HIGHERA;
   2401       break;
   2402     case BFD_RELOC_PPC64_DTPREL16_HIGHEST:	r = R_PPC64_DTPREL16_HIGHEST;
   2403       break;
   2404     case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:	r = R_PPC64_DTPREL16_HIGHESTA;
   2405       break;
   2406     case BFD_RELOC_16_PCREL:			r = R_PPC64_REL16;
   2407       break;
   2408     case BFD_RELOC_LO16_PCREL:			r = R_PPC64_REL16_LO;
   2409       break;
   2410     case BFD_RELOC_HI16_PCREL:			r = R_PPC64_REL16_HI;
   2411       break;
   2412     case BFD_RELOC_HI16_S_PCREL:		r = R_PPC64_REL16_HA;
   2413       break;
   2414     case BFD_RELOC_PPC64_ADDR64_LOCAL:		r = R_PPC64_ADDR64_LOCAL;
   2415       break;
   2416     case BFD_RELOC_VTABLE_INHERIT:		r = R_PPC64_GNU_VTINHERIT;
   2417       break;
   2418     case BFD_RELOC_VTABLE_ENTRY:		r = R_PPC64_GNU_VTENTRY;
   2419       break;
   2420     }
   2421 
   2422   return ppc64_elf_howto_table[r];
   2423 };
   2424 
   2425 static reloc_howto_type *
   2426 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
   2427 			     const char *r_name)
   2428 {
   2429   unsigned int i;
   2430 
   2431   for (i = 0;
   2432        i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
   2433        i++)
   2434     if (ppc64_elf_howto_raw[i].name != NULL
   2435 	&& strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
   2436       return &ppc64_elf_howto_raw[i];
   2437 
   2438   return NULL;
   2439 }
   2440 
   2441 /* Set the howto pointer for a PowerPC ELF reloc.  */
   2442 
   2443 static void
   2444 ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
   2445 			 Elf_Internal_Rela *dst)
   2446 {
   2447   unsigned int type;
   2448 
   2449   /* Initialize howto table if needed.  */
   2450   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
   2451     ppc_howto_init ();
   2452 
   2453   type = ELF64_R_TYPE (dst->r_info);
   2454   if (type >= (sizeof (ppc64_elf_howto_table)
   2455 	       / sizeof (ppc64_elf_howto_table[0])))
   2456     {
   2457       (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
   2458 			     abfd, (int) type);
   2459       type = R_PPC64_NONE;
   2460     }
   2461   cache_ptr->howto = ppc64_elf_howto_table[type];
   2462 }
   2463 
   2464 /* Handle the R_PPC64_ADDR16_HA and similar relocs.  */
   2465 
   2466 static bfd_reloc_status_type
   2467 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2468 		    void *data, asection *input_section,
   2469 		    bfd *output_bfd, char **error_message)
   2470 {
   2471   /* If this is a relocatable link (output_bfd test tells us), just
   2472      call the generic function.  Any adjustment will be done at final
   2473      link time.  */
   2474   if (output_bfd != NULL)
   2475     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2476 				  input_section, output_bfd, error_message);
   2477 
   2478   /* Adjust the addend for sign extension of the low 16 bits.
   2479      We won't actually be using the low 16 bits, so trashing them
   2480      doesn't matter.  */
   2481   reloc_entry->addend += 0x8000;
   2482   return bfd_reloc_continue;
   2483 }
   2484 
   2485 static bfd_reloc_status_type
   2486 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2487 			void *data, asection *input_section,
   2488 			bfd *output_bfd, char **error_message)
   2489 {
   2490   if (output_bfd != NULL)
   2491     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2492 				  input_section, output_bfd, error_message);
   2493 
   2494   if (strcmp (symbol->section->name, ".opd") == 0
   2495       && (symbol->section->owner->flags & DYNAMIC) == 0)
   2496     {
   2497       bfd_vma dest = opd_entry_value (symbol->section,
   2498 				      symbol->value + reloc_entry->addend,
   2499 				      NULL, NULL, FALSE);
   2500       if (dest != (bfd_vma) -1)
   2501 	reloc_entry->addend = dest - (symbol->value
   2502 				      + symbol->section->output_section->vma
   2503 				      + symbol->section->output_offset);
   2504     }
   2505   else
   2506     {
   2507       elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
   2508 
   2509       if (symbol->section->owner != abfd
   2510 	  && abiversion (symbol->section->owner) >= 2)
   2511 	{
   2512 	  unsigned int i;
   2513 
   2514 	  for (i = 0; i < symbol->section->owner->symcount; ++i)
   2515 	    {
   2516 	      asymbol *symdef = symbol->section->owner->outsymbols[i];
   2517 
   2518 	      if (strcmp (symdef->name, symbol->name) == 0)
   2519 		{
   2520 		  elfsym = (elf_symbol_type *) symdef;
   2521 		  break;
   2522 		}
   2523 	    }
   2524 	}
   2525       reloc_entry->addend
   2526 	+= PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
   2527     }
   2528   return bfd_reloc_continue;
   2529 }
   2530 
   2531 static bfd_reloc_status_type
   2532 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2533 			 void *data, asection *input_section,
   2534 			 bfd *output_bfd, char **error_message)
   2535 {
   2536   long insn;
   2537   enum elf_ppc64_reloc_type r_type;
   2538   bfd_size_type octets;
   2539   /* Assume 'at' branch hints.  */
   2540   bfd_boolean is_isa_v2 = TRUE;
   2541 
   2542   /* If this is a relocatable link (output_bfd test tells us), just
   2543      call the generic function.  Any adjustment will be done at final
   2544      link time.  */
   2545   if (output_bfd != NULL)
   2546     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2547 				  input_section, output_bfd, error_message);
   2548 
   2549   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
   2550   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
   2551   insn &= ~(0x01 << 21);
   2552   r_type = reloc_entry->howto->type;
   2553   if (r_type == R_PPC64_ADDR14_BRTAKEN
   2554       || r_type == R_PPC64_REL14_BRTAKEN)
   2555     insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
   2556 
   2557   if (is_isa_v2)
   2558     {
   2559       /* Set 'a' bit.  This is 0b00010 in BO field for branch
   2560 	 on CR(BI) insns (BO == 001at or 011at), and 0b01000
   2561 	 for branch on CTR insns (BO == 1a00t or 1a01t).  */
   2562       if ((insn & (0x14 << 21)) == (0x04 << 21))
   2563 	insn |= 0x02 << 21;
   2564       else if ((insn & (0x14 << 21)) == (0x10 << 21))
   2565 	insn |= 0x08 << 21;
   2566       else
   2567 	goto out;
   2568     }
   2569   else
   2570     {
   2571       bfd_vma target = 0;
   2572       bfd_vma from;
   2573 
   2574       if (!bfd_is_com_section (symbol->section))
   2575 	target = symbol->value;
   2576       target += symbol->section->output_section->vma;
   2577       target += symbol->section->output_offset;
   2578       target += reloc_entry->addend;
   2579 
   2580       from = (reloc_entry->address
   2581 	      + input_section->output_offset
   2582 	      + input_section->output_section->vma);
   2583 
   2584       /* Invert 'y' bit if not the default.  */
   2585       if ((bfd_signed_vma) (target - from) < 0)
   2586 	insn ^= 0x01 << 21;
   2587     }
   2588   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
   2589  out:
   2590   return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
   2591 				 input_section, output_bfd, error_message);
   2592 }
   2593 
   2594 static bfd_reloc_status_type
   2595 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2596 			 void *data, asection *input_section,
   2597 			 bfd *output_bfd, char **error_message)
   2598 {
   2599   /* If this is a relocatable link (output_bfd test tells us), just
   2600      call the generic function.  Any adjustment will be done at final
   2601      link time.  */
   2602   if (output_bfd != NULL)
   2603     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2604 				  input_section, output_bfd, error_message);
   2605 
   2606   /* Subtract the symbol section base address.  */
   2607   reloc_entry->addend -= symbol->section->output_section->vma;
   2608   return bfd_reloc_continue;
   2609 }
   2610 
   2611 static bfd_reloc_status_type
   2612 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2613 			    void *data, asection *input_section,
   2614 			    bfd *output_bfd, char **error_message)
   2615 {
   2616   /* If this is a relocatable link (output_bfd test tells us), just
   2617      call the generic function.  Any adjustment will be done at final
   2618      link time.  */
   2619   if (output_bfd != NULL)
   2620     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2621 				  input_section, output_bfd, error_message);
   2622 
   2623   /* Subtract the symbol section base address.  */
   2624   reloc_entry->addend -= symbol->section->output_section->vma;
   2625 
   2626   /* Adjust the addend for sign extension of the low 16 bits.  */
   2627   reloc_entry->addend += 0x8000;
   2628   return bfd_reloc_continue;
   2629 }
   2630 
   2631 static bfd_reloc_status_type
   2632 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2633 		     void *data, asection *input_section,
   2634 		     bfd *output_bfd, char **error_message)
   2635 {
   2636   bfd_vma TOCstart;
   2637 
   2638   /* If this is a relocatable link (output_bfd test tells us), just
   2639      call the generic function.  Any adjustment will be done at final
   2640      link time.  */
   2641   if (output_bfd != NULL)
   2642     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2643 				  input_section, output_bfd, error_message);
   2644 
   2645   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
   2646   if (TOCstart == 0)
   2647     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
   2648 
   2649   /* Subtract the TOC base address.  */
   2650   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
   2651   return bfd_reloc_continue;
   2652 }
   2653 
   2654 static bfd_reloc_status_type
   2655 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2656 			void *data, asection *input_section,
   2657 			bfd *output_bfd, char **error_message)
   2658 {
   2659   bfd_vma TOCstart;
   2660 
   2661   /* If this is a relocatable link (output_bfd test tells us), just
   2662      call the generic function.  Any adjustment will be done at final
   2663      link time.  */
   2664   if (output_bfd != NULL)
   2665     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2666 				  input_section, output_bfd, error_message);
   2667 
   2668   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
   2669   if (TOCstart == 0)
   2670     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
   2671 
   2672   /* Subtract the TOC base address.  */
   2673   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
   2674 
   2675   /* Adjust the addend for sign extension of the low 16 bits.  */
   2676   reloc_entry->addend += 0x8000;
   2677   return bfd_reloc_continue;
   2678 }
   2679 
   2680 static bfd_reloc_status_type
   2681 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2682 		       void *data, asection *input_section,
   2683 		       bfd *output_bfd, char **error_message)
   2684 {
   2685   bfd_vma TOCstart;
   2686   bfd_size_type octets;
   2687 
   2688   /* If this is a relocatable link (output_bfd test tells us), just
   2689      call the generic function.  Any adjustment will be done at final
   2690      link time.  */
   2691   if (output_bfd != NULL)
   2692     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2693 				  input_section, output_bfd, error_message);
   2694 
   2695   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
   2696   if (TOCstart == 0)
   2697     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
   2698 
   2699   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
   2700   bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
   2701   return bfd_reloc_ok;
   2702 }
   2703 
   2704 static bfd_reloc_status_type
   2705 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2706 			   void *data, asection *input_section,
   2707 			   bfd *output_bfd, char **error_message)
   2708 {
   2709   /* If this is a relocatable link (output_bfd test tells us), just
   2710      call the generic function.  Any adjustment will be done at final
   2711      link time.  */
   2712   if (output_bfd != NULL)
   2713     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2714 				  input_section, output_bfd, error_message);
   2715 
   2716   if (error_message != NULL)
   2717     {
   2718       static char buf[60];
   2719       sprintf (buf, "generic linker can't handle %s",
   2720 	       reloc_entry->howto->name);
   2721       *error_message = buf;
   2722     }
   2723   return bfd_reloc_dangerous;
   2724 }
   2725 
   2726 /* Track GOT entries needed for a given symbol.  We might need more
   2727    than one got entry per symbol.  */
   2728 struct got_entry
   2729 {
   2730   struct got_entry *next;
   2731 
   2732   /* The symbol addend that we'll be placing in the GOT.  */
   2733   bfd_vma addend;
   2734 
   2735   /* Unlike other ELF targets, we use separate GOT entries for the same
   2736      symbol referenced from different input files.  This is to support
   2737      automatic multiple TOC/GOT sections, where the TOC base can vary
   2738      from one input file to another.  After partitioning into TOC groups
   2739      we merge entries within the group.
   2740 
   2741      Point to the BFD owning this GOT entry.  */
   2742   bfd *owner;
   2743 
   2744   /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
   2745      TLS_TPREL or TLS_DTPREL for tls entries.  */
   2746   unsigned char tls_type;
   2747 
   2748   /* Non-zero if got.ent points to real entry.  */
   2749   unsigned char is_indirect;
   2750 
   2751   /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
   2752   union
   2753     {
   2754       bfd_signed_vma refcount;
   2755       bfd_vma offset;
   2756       struct got_entry *ent;
   2757     } got;
   2758 };
   2759 
   2760 /* The same for PLT.  */
   2761 struct plt_entry
   2762 {
   2763   struct plt_entry *next;
   2764 
   2765   bfd_vma addend;
   2766 
   2767   union
   2768     {
   2769       bfd_signed_vma refcount;
   2770       bfd_vma offset;
   2771     } plt;
   2772 };
   2773 
   2774 struct ppc64_elf_obj_tdata
   2775 {
   2776   struct elf_obj_tdata elf;
   2777 
   2778   /* Shortcuts to dynamic linker sections.  */
   2779   asection *got;
   2780   asection *relgot;
   2781 
   2782   /* Used during garbage collection.  We attach global symbols defined
   2783      on removed .opd entries to this section so that the sym is removed.  */
   2784   asection *deleted_section;
   2785 
   2786   /* TLS local dynamic got entry handling.  Support for multiple GOT
   2787      sections means we potentially need one of these for each input bfd.  */
   2788   struct got_entry tlsld_got;
   2789 
   2790   union {
   2791     /* A copy of relocs before they are modified for --emit-relocs.  */
   2792     Elf_Internal_Rela *relocs;
   2793 
   2794     /* Section contents.  */
   2795     bfd_byte *contents;
   2796   } opd;
   2797 
   2798   /* Nonzero if this bfd has small toc/got relocs, ie. that expect
   2799      the reloc to be in the range -32768 to 32767.  */
   2800   unsigned int has_small_toc_reloc : 1;
   2801 
   2802   /* Set if toc/got ha relocs detected not using r2, or lo reloc
   2803      instruction not one we handle.  */
   2804   unsigned int unexpected_toc_insn : 1;
   2805 };
   2806 
   2807 #define ppc64_elf_tdata(bfd) \
   2808   ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
   2809 
   2810 #define ppc64_tlsld_got(bfd) \
   2811   (&ppc64_elf_tdata (bfd)->tlsld_got)
   2812 
   2813 #define is_ppc64_elf(bfd) \
   2814   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
   2815    && elf_object_id (bfd) == PPC64_ELF_DATA)
   2816 
   2817 /* Override the generic function because we store some extras.  */
   2818 
   2819 static bfd_boolean
   2820 ppc64_elf_mkobject (bfd *abfd)
   2821 {
   2822   return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
   2823 				  PPC64_ELF_DATA);
   2824 }
   2825 
   2826 /* Fix bad default arch selected for a 64 bit input bfd when the
   2827    default is 32 bit.  */
   2828 
   2829 static bfd_boolean
   2830 ppc64_elf_object_p (bfd *abfd)
   2831 {
   2832   if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
   2833     {
   2834       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
   2835 
   2836       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
   2837 	{
   2838 	  /* Relies on arch after 32 bit default being 64 bit default.  */
   2839 	  abfd->arch_info = abfd->arch_info->next;
   2840 	  BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
   2841 	}
   2842     }
   2843   return TRUE;
   2844 }
   2845 
   2846 /* Support for core dump NOTE sections.  */
   2847 
   2848 static bfd_boolean
   2849 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
   2850 {
   2851   size_t offset, size;
   2852 
   2853   if (note->descsz != 504)
   2854     return FALSE;
   2855 
   2856   /* pr_cursig */
   2857   elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
   2858 
   2859   /* pr_pid */
   2860   elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
   2861 
   2862   /* pr_reg */
   2863   offset = 112;
   2864   size = 384;
   2865 
   2866   /* Make a ".reg/999" section.  */
   2867   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
   2868 					  size, note->descpos + offset);
   2869 }
   2870 
   2871 static bfd_boolean
   2872 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
   2873 {
   2874   if (note->descsz != 136)
   2875     return FALSE;
   2876 
   2877   elf_tdata (abfd)->core->pid
   2878     = bfd_get_32 (abfd, note->descdata + 24);
   2879   elf_tdata (abfd)->core->program
   2880     = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
   2881   elf_tdata (abfd)->core->command
   2882     = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
   2883 
   2884   return TRUE;
   2885 }
   2886 
   2887 static char *
   2888 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
   2889 			   ...)
   2890 {
   2891   switch (note_type)
   2892     {
   2893     default:
   2894       return NULL;
   2895 
   2896     case NT_PRPSINFO:
   2897       {
   2898 	char data[136];
   2899 	va_list ap;
   2900 
   2901 	va_start (ap, note_type);
   2902 	memset (data, 0, sizeof (data));
   2903 	strncpy (data + 40, va_arg (ap, const char *), 16);
   2904 	strncpy (data + 56, va_arg (ap, const char *), 80);
   2905 	va_end (ap);
   2906 	return elfcore_write_note (abfd, buf, bufsiz,
   2907 				   "CORE", note_type, data, sizeof (data));
   2908       }
   2909 
   2910     case NT_PRSTATUS:
   2911       {
   2912 	char data[504];
   2913 	va_list ap;
   2914 	long pid;
   2915 	int cursig;
   2916 	const void *greg;
   2917 
   2918 	va_start (ap, note_type);
   2919 	memset (data, 0, 112);
   2920 	pid = va_arg (ap, long);
   2921 	bfd_put_32 (abfd, pid, data + 32);
   2922 	cursig = va_arg (ap, int);
   2923 	bfd_put_16 (abfd, cursig, data + 12);
   2924 	greg = va_arg (ap, const void *);
   2925 	memcpy (data + 112, greg, 384);
   2926 	memset (data + 496, 0, 8);
   2927 	va_end (ap);
   2928 	return elfcore_write_note (abfd, buf, bufsiz,
   2929 				   "CORE", note_type, data, sizeof (data));
   2930       }
   2931     }
   2932 }
   2933 
   2934 /* Add extra PPC sections.  */
   2935 
   2936 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
   2937 {
   2938   { STRING_COMMA_LEN (".plt"),    0, SHT_NOBITS,   0 },
   2939   { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
   2940   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   2941   { STRING_COMMA_LEN (".toc"),    0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   2942   { STRING_COMMA_LEN (".toc1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   2943   { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
   2944   { NULL,                     0,  0, 0,            0 }
   2945 };
   2946 
   2947 enum _ppc64_sec_type {
   2948   sec_normal = 0,
   2949   sec_opd = 1,
   2950   sec_toc = 2
   2951 };
   2952 
   2953 struct _ppc64_elf_section_data
   2954 {
   2955   struct bfd_elf_section_data elf;
   2956 
   2957   union
   2958   {
   2959     /* An array with one entry for each opd function descriptor.  */
   2960     struct _opd_sec_data
   2961     {
   2962       /* Points to the function code section for local opd entries.  */
   2963       asection **func_sec;
   2964 
   2965       /* After editing .opd, adjust references to opd local syms.  */
   2966       long *adjust;
   2967     } opd;
   2968 
   2969     /* An array for toc sections, indexed by offset/8.  */
   2970     struct _toc_sec_data
   2971     {
   2972       /* Specifies the relocation symbol index used at a given toc offset.  */
   2973       unsigned *symndx;
   2974 
   2975       /* And the relocation addend.  */
   2976       bfd_vma *add;
   2977     } toc;
   2978   } u;
   2979 
   2980   enum _ppc64_sec_type sec_type:2;
   2981 
   2982   /* Flag set when small branches are detected.  Used to
   2983      select suitable defaults for the stub group size.  */
   2984   unsigned int has_14bit_branch:1;
   2985 };
   2986 
   2987 #define ppc64_elf_section_data(sec) \
   2988   ((struct _ppc64_elf_section_data *) elf_section_data (sec))
   2989 
   2990 static bfd_boolean
   2991 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
   2992 {
   2993   if (!sec->used_by_bfd)
   2994     {
   2995       struct _ppc64_elf_section_data *sdata;
   2996       bfd_size_type amt = sizeof (*sdata);
   2997 
   2998       sdata = bfd_zalloc (abfd, amt);
   2999       if (sdata == NULL)
   3000 	return FALSE;
   3001       sec->used_by_bfd = sdata;
   3002     }
   3003 
   3004   return _bfd_elf_new_section_hook (abfd, sec);
   3005 }
   3006 
   3007 static struct _opd_sec_data *
   3008 get_opd_info (asection * sec)
   3009 {
   3010   if (sec != NULL
   3011       && ppc64_elf_section_data (sec) != NULL
   3012       && ppc64_elf_section_data (sec)->sec_type == sec_opd)
   3013     return &ppc64_elf_section_data (sec)->u.opd;
   3014   return NULL;
   3015 }
   3016 
   3017 /* Parameters for the qsort hook.  */
   3019 static bfd_boolean synthetic_relocatable;
   3020 
   3021 /* qsort comparison function for ppc64_elf_get_synthetic_symtab.  */
   3022 
   3023 static int
   3024 compare_symbols (const void *ap, const void *bp)
   3025 {
   3026   const asymbol *a = * (const asymbol **) ap;
   3027   const asymbol *b = * (const asymbol **) bp;
   3028 
   3029   /* Section symbols first.  */
   3030   if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
   3031     return -1;
   3032   if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
   3033     return 1;
   3034 
   3035   /* then .opd symbols.  */
   3036   if (strcmp (a->section->name, ".opd") == 0
   3037       && strcmp (b->section->name, ".opd") != 0)
   3038     return -1;
   3039   if (strcmp (a->section->name, ".opd") != 0
   3040       && strcmp (b->section->name, ".opd") == 0)
   3041     return 1;
   3042 
   3043   /* then other code symbols.  */
   3044   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   3045       == (SEC_CODE | SEC_ALLOC)
   3046       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   3047 	 != (SEC_CODE | SEC_ALLOC))
   3048     return -1;
   3049 
   3050   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   3051       != (SEC_CODE | SEC_ALLOC)
   3052       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   3053 	 == (SEC_CODE | SEC_ALLOC))
   3054     return 1;
   3055 
   3056   if (synthetic_relocatable)
   3057     {
   3058       if (a->section->id < b->section->id)
   3059 	return -1;
   3060 
   3061       if (a->section->id > b->section->id)
   3062 	return 1;
   3063     }
   3064 
   3065   if (a->value + a->section->vma < b->value + b->section->vma)
   3066     return -1;
   3067 
   3068   if (a->value + a->section->vma > b->value + b->section->vma)
   3069     return 1;
   3070 
   3071   /* For syms with the same value, prefer strong dynamic global function
   3072      syms over other syms.  */
   3073   if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
   3074     return -1;
   3075 
   3076   if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
   3077     return 1;
   3078 
   3079   if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
   3080     return -1;
   3081 
   3082   if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
   3083     return 1;
   3084 
   3085   if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
   3086     return -1;
   3087 
   3088   if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
   3089     return 1;
   3090 
   3091   if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
   3092     return -1;
   3093 
   3094   if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
   3095     return 1;
   3096 
   3097   return 0;
   3098 }
   3099 
   3100 /* Search SYMS for a symbol of the given VALUE.  */
   3101 
   3102 static asymbol *
   3103 sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
   3104 {
   3105   long mid;
   3106 
   3107   if (id == -1)
   3108     {
   3109       while (lo < hi)
   3110 	{
   3111 	  mid = (lo + hi) >> 1;
   3112 	  if (syms[mid]->value + syms[mid]->section->vma < value)
   3113 	    lo = mid + 1;
   3114 	  else if (syms[mid]->value + syms[mid]->section->vma > value)
   3115 	    hi = mid;
   3116 	  else
   3117 	    return syms[mid];
   3118 	}
   3119     }
   3120   else
   3121     {
   3122       while (lo < hi)
   3123 	{
   3124 	  mid = (lo + hi) >> 1;
   3125 	  if (syms[mid]->section->id < id)
   3126 	    lo = mid + 1;
   3127 	  else if (syms[mid]->section->id > id)
   3128 	    hi = mid;
   3129 	  else if (syms[mid]->value < value)
   3130 	    lo = mid + 1;
   3131 	  else if (syms[mid]->value > value)
   3132 	    hi = mid;
   3133 	  else
   3134 	    return syms[mid];
   3135 	}
   3136     }
   3137   return NULL;
   3138 }
   3139 
   3140 static bfd_boolean
   3141 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
   3142 {
   3143   bfd_vma vma = *(bfd_vma *) ptr;
   3144   return ((section->flags & SEC_ALLOC) != 0
   3145 	  && section->vma <= vma
   3146 	  && vma < section->vma + section->size);
   3147 }
   3148 
   3149 /* Create synthetic symbols, effectively restoring "dot-symbol" function
   3150    entry syms.  Also generate @plt symbols for the glink branch table.  */
   3151 
   3152 static long
   3153 ppc64_elf_get_synthetic_symtab (bfd *abfd,
   3154 				long static_count, asymbol **static_syms,
   3155 				long dyn_count, asymbol **dyn_syms,
   3156 				asymbol **ret)
   3157 {
   3158   asymbol *s;
   3159   long i;
   3160   long count;
   3161   char *names;
   3162   long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
   3163   asection *opd = NULL;
   3164   bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
   3165   asymbol **syms;
   3166   int abi = abiversion (abfd);
   3167 
   3168   *ret = NULL;
   3169 
   3170   if (abi < 2)
   3171     {
   3172       opd = bfd_get_section_by_name (abfd, ".opd");
   3173       if (opd == NULL && abi == 1)
   3174 	return 0;
   3175     }
   3176 
   3177   symcount = static_count;
   3178   if (!relocatable)
   3179     symcount += dyn_count;
   3180   if (symcount == 0)
   3181     return 0;
   3182 
   3183   syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
   3184   if (syms == NULL)
   3185     return -1;
   3186 
   3187   if (!relocatable && static_count != 0 && dyn_count != 0)
   3188     {
   3189       /* Use both symbol tables.  */
   3190       memcpy (syms, static_syms, static_count * sizeof (*syms));
   3191       memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
   3192     }
   3193   else if (!relocatable && static_count == 0)
   3194     memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
   3195   else
   3196     memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
   3197 
   3198   synthetic_relocatable = relocatable;
   3199   qsort (syms, symcount, sizeof (*syms), compare_symbols);
   3200 
   3201   if (!relocatable && symcount > 1)
   3202     {
   3203       long j;
   3204       /* Trim duplicate syms, since we may have merged the normal and
   3205 	 dynamic symbols.  Actually, we only care about syms that have
   3206 	 different values, so trim any with the same value.  */
   3207       for (i = 1, j = 1; i < symcount; ++i)
   3208 	if (syms[i - 1]->value + syms[i - 1]->section->vma
   3209 	    != syms[i]->value + syms[i]->section->vma)
   3210 	  syms[j++] = syms[i];
   3211       symcount = j;
   3212     }
   3213 
   3214   i = 0;
   3215   if (strcmp (syms[i]->section->name, ".opd") == 0)
   3216     ++i;
   3217   codesecsym = i;
   3218 
   3219   for (; i < symcount; ++i)
   3220     if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   3221 	 != (SEC_CODE | SEC_ALLOC))
   3222 	|| (syms[i]->flags & BSF_SECTION_SYM) == 0)
   3223       break;
   3224   codesecsymend = i;
   3225 
   3226   for (; i < symcount; ++i)
   3227     if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
   3228       break;
   3229   secsymend = i;
   3230 
   3231   for (; i < symcount; ++i)
   3232     if (strcmp (syms[i]->section->name, ".opd") != 0)
   3233       break;
   3234   opdsymend = i;
   3235 
   3236   for (; i < symcount; ++i)
   3237     if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   3238 	!= (SEC_CODE | SEC_ALLOC))
   3239       break;
   3240   symcount = i;
   3241 
   3242   count = 0;
   3243 
   3244   if (relocatable)
   3245     {
   3246       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
   3247       arelent *r;
   3248       size_t size;
   3249       long relcount;
   3250 
   3251       if (opdsymend == secsymend)
   3252 	goto done;
   3253 
   3254       slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   3255       relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
   3256       if (relcount == 0)
   3257 	goto done;
   3258 
   3259       if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
   3260 	{
   3261 	  count = -1;
   3262 	  goto done;
   3263 	}
   3264 
   3265       size = 0;
   3266       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
   3267 	{
   3268 	  asymbol *sym;
   3269 
   3270 	  while (r < opd->relocation + relcount
   3271 		 && r->address < syms[i]->value + opd->vma)
   3272 	    ++r;
   3273 
   3274 	  if (r == opd->relocation + relcount)
   3275 	    break;
   3276 
   3277 	  if (r->address != syms[i]->value + opd->vma)
   3278 	    continue;
   3279 
   3280 	  if (r->howto->type != R_PPC64_ADDR64)
   3281 	    continue;
   3282 
   3283 	  sym = *r->sym_ptr_ptr;
   3284 	  if (!sym_exists_at (syms, opdsymend, symcount,
   3285 			      sym->section->id, sym->value + r->addend))
   3286 	    {
   3287 	      ++count;
   3288 	      size += sizeof (asymbol);
   3289 	      size += strlen (syms[i]->name) + 2;
   3290 	    }
   3291 	}
   3292 
   3293       s = *ret = bfd_malloc (size);
   3294       if (s == NULL)
   3295 	{
   3296 	  count = -1;
   3297 	  goto done;
   3298 	}
   3299 
   3300       names = (char *) (s + count);
   3301 
   3302       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
   3303 	{
   3304 	  asymbol *sym;
   3305 
   3306 	  while (r < opd->relocation + relcount
   3307 		 && r->address < syms[i]->value + opd->vma)
   3308 	    ++r;
   3309 
   3310 	  if (r == opd->relocation + relcount)
   3311 	    break;
   3312 
   3313 	  if (r->address != syms[i]->value + opd->vma)
   3314 	    continue;
   3315 
   3316 	  if (r->howto->type != R_PPC64_ADDR64)
   3317 	    continue;
   3318 
   3319 	  sym = *r->sym_ptr_ptr;
   3320 	  if (!sym_exists_at (syms, opdsymend, symcount,
   3321 			      sym->section->id, sym->value + r->addend))
   3322 	    {
   3323 	      size_t len;
   3324 
   3325 	      *s = *syms[i];
   3326 	      s->flags |= BSF_SYNTHETIC;
   3327 	      s->section = sym->section;
   3328 	      s->value = sym->value + r->addend;
   3329 	      s->name = names;
   3330 	      *names++ = '.';
   3331 	      len = strlen (syms[i]->name);
   3332 	      memcpy (names, syms[i]->name, len + 1);
   3333 	      names += len + 1;
   3334 	      /* Have udata.p point back to the original symbol this
   3335 		 synthetic symbol was derived from.  */
   3336 	      s->udata.p = syms[i];
   3337 	      s++;
   3338 	    }
   3339 	}
   3340     }
   3341   else
   3342     {
   3343       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
   3344       bfd_byte *contents = NULL;
   3345       size_t size;
   3346       long plt_count = 0;
   3347       bfd_vma glink_vma = 0, resolv_vma = 0;
   3348       asection *dynamic, *glink = NULL, *relplt = NULL;
   3349       arelent *p;
   3350 
   3351       if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
   3352 	{
   3353 	free_contents_and_exit:
   3354 	  if (contents)
   3355 	    free (contents);
   3356 	  count = -1;
   3357 	  goto done;
   3358 	}
   3359 
   3360       size = 0;
   3361       for (i = secsymend; i < opdsymend; ++i)
   3362 	{
   3363 	  bfd_vma ent;
   3364 
   3365 	  /* Ignore bogus symbols.  */
   3366 	  if (syms[i]->value > opd->size - 8)
   3367 	    continue;
   3368 
   3369 	  ent = bfd_get_64 (abfd, contents + syms[i]->value);
   3370 	  if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
   3371 	    {
   3372 	      ++count;
   3373 	      size += sizeof (asymbol);
   3374 	      size += strlen (syms[i]->name) + 2;
   3375 	    }
   3376 	}
   3377 
   3378       /* Get start of .glink stubs from DT_PPC64_GLINK.  */
   3379       if (dyn_count != 0
   3380 	  && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
   3381 	{
   3382 	  bfd_byte *dynbuf, *extdyn, *extdynend;
   3383 	  size_t extdynsize;
   3384 	  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
   3385 
   3386 	  if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
   3387 	    goto free_contents_and_exit;
   3388 
   3389 	  extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
   3390 	  swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
   3391 
   3392 	  extdyn = dynbuf;
   3393 	  extdynend = extdyn + dynamic->size;
   3394 	  for (; extdyn < extdynend; extdyn += extdynsize)
   3395 	    {
   3396 	      Elf_Internal_Dyn dyn;
   3397 	      (*swap_dyn_in) (abfd, extdyn, &dyn);
   3398 
   3399 	      if (dyn.d_tag == DT_NULL)
   3400 		break;
   3401 
   3402 	      if (dyn.d_tag == DT_PPC64_GLINK)
   3403 		{
   3404 		  /* The first glink stub starts at offset 32; see
   3405 		     comment in ppc64_elf_finish_dynamic_sections. */
   3406 		  glink_vma = dyn.d_un.d_val + GLINK_CALL_STUB_SIZE - 8 * 4;
   3407 		  /* The .glink section usually does not survive the final
   3408 		     link; search for the section (usually .text) where the
   3409 		     glink stubs now reside.  */
   3410 		  glink = bfd_sections_find_if (abfd, section_covers_vma,
   3411 						&glink_vma);
   3412 		  break;
   3413 		}
   3414 	    }
   3415 
   3416 	  free (dynbuf);
   3417 	}
   3418 
   3419       if (glink != NULL)
   3420 	{
   3421 	  /* Determine __glink trampoline by reading the relative branch
   3422 	     from the first glink stub.  */
   3423 	  bfd_byte buf[4];
   3424 	  unsigned int off = 0;
   3425 
   3426 	  while (bfd_get_section_contents (abfd, glink, buf,
   3427 					   glink_vma + off - glink->vma, 4))
   3428 	    {
   3429 	      unsigned int insn = bfd_get_32 (abfd, buf);
   3430 	      insn ^= B_DOT;
   3431 	      if ((insn & ~0x3fffffc) == 0)
   3432 		{
   3433 		  resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
   3434 		  break;
   3435 		}
   3436 	      off += 4;
   3437 	      if (off > 4)
   3438 		break;
   3439 	    }
   3440 
   3441 	  if (resolv_vma)
   3442 	    size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
   3443 
   3444 	  relplt = bfd_get_section_by_name (abfd, ".rela.plt");
   3445 	  if (relplt != NULL)
   3446 	    {
   3447 	      slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   3448 	      if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
   3449 		goto free_contents_and_exit;
   3450 
   3451 	      plt_count = relplt->size / sizeof (Elf64_External_Rela);
   3452 	      size += plt_count * sizeof (asymbol);
   3453 
   3454 	      p = relplt->relocation;
   3455 	      for (i = 0; i < plt_count; i++, p++)
   3456 		{
   3457 		  size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
   3458 		  if (p->addend != 0)
   3459 		    size += sizeof ("+0x") - 1 + 16;
   3460 		}
   3461 	    }
   3462 	}
   3463 
   3464       s = *ret = bfd_malloc (size);
   3465       if (s == NULL)
   3466 	goto free_contents_and_exit;
   3467 
   3468       names = (char *) (s + count + plt_count + (resolv_vma != 0));
   3469 
   3470       for (i = secsymend; i < opdsymend; ++i)
   3471 	{
   3472 	  bfd_vma ent;
   3473 
   3474 	  if (syms[i]->value > opd->size - 8)
   3475 	    continue;
   3476 
   3477 	  ent = bfd_get_64 (abfd, contents + syms[i]->value);
   3478 	  if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
   3479 	    {
   3480 	      long lo, hi;
   3481 	      size_t len;
   3482 	      asection *sec = abfd->sections;
   3483 
   3484 	      *s = *syms[i];
   3485 	      lo = codesecsym;
   3486 	      hi = codesecsymend;
   3487 	      while (lo < hi)
   3488 		{
   3489 		  long mid = (lo + hi) >> 1;
   3490 		  if (syms[mid]->section->vma < ent)
   3491 		    lo = mid + 1;
   3492 		  else if (syms[mid]->section->vma > ent)
   3493 		    hi = mid;
   3494 		  else
   3495 		    {
   3496 		      sec = syms[mid]->section;
   3497 		      break;
   3498 		    }
   3499 		}
   3500 
   3501 	      if (lo >= hi && lo > codesecsym)
   3502 		sec = syms[lo - 1]->section;
   3503 
   3504 	      for (; sec != NULL; sec = sec->next)
   3505 		{
   3506 		  if (sec->vma > ent)
   3507 		    break;
   3508 		  /* SEC_LOAD may not be set if SEC is from a separate debug
   3509 		     info file.  */
   3510 		  if ((sec->flags & SEC_ALLOC) == 0)
   3511 		    break;
   3512 		  if ((sec->flags & SEC_CODE) != 0)
   3513 		    s->section = sec;
   3514 		}
   3515 	      s->flags |= BSF_SYNTHETIC;
   3516 	      s->value = ent - s->section->vma;
   3517 	      s->name = names;
   3518 	      *names++ = '.';
   3519 	      len = strlen (syms[i]->name);
   3520 	      memcpy (names, syms[i]->name, len + 1);
   3521 	      names += len + 1;
   3522 	      /* Have udata.p point back to the original symbol this
   3523 		 synthetic symbol was derived from.  */
   3524 	      s->udata.p = syms[i];
   3525 	      s++;
   3526 	    }
   3527 	}
   3528       free (contents);
   3529 
   3530       if (glink != NULL && relplt != NULL)
   3531 	{
   3532 	  if (resolv_vma)
   3533 	    {
   3534 	      /* Add a symbol for the main glink trampoline.  */
   3535 	      memset (s, 0, sizeof *s);
   3536 	      s->the_bfd = abfd;
   3537 	      s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
   3538 	      s->section = glink;
   3539 	      s->value = resolv_vma - glink->vma;
   3540 	      s->name = names;
   3541 	      memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
   3542 	      names += sizeof ("__glink_PLTresolve");
   3543 	      s++;
   3544 	      count++;
   3545 	    }
   3546 
   3547 	  /* FIXME: It would be very much nicer to put sym@plt on the
   3548 	     stub rather than on the glink branch table entry.  The
   3549 	     objdump disassembler would then use a sensible symbol
   3550 	     name on plt calls.  The difficulty in doing so is
   3551 	     a) finding the stubs, and,
   3552 	     b) matching stubs against plt entries, and,
   3553 	     c) there can be multiple stubs for a given plt entry.
   3554 
   3555 	     Solving (a) could be done by code scanning, but older
   3556 	     ppc64 binaries used different stubs to current code.
   3557 	     (b) is the tricky one since you need to known the toc
   3558 	     pointer for at least one function that uses a pic stub to
   3559 	     be able to calculate the plt address referenced.
   3560 	     (c) means gdb would need to set multiple breakpoints (or
   3561 	     find the glink branch itself) when setting breakpoints
   3562 	     for pending shared library loads.  */
   3563 	  p = relplt->relocation;
   3564 	  for (i = 0; i < plt_count; i++, p++)
   3565 	    {
   3566 	      size_t len;
   3567 
   3568 	      *s = **p->sym_ptr_ptr;
   3569 	      /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
   3570 		 we are defining a symbol, ensure one of them is set.  */
   3571 	      if ((s->flags & BSF_LOCAL) == 0)
   3572 		s->flags |= BSF_GLOBAL;
   3573 	      s->flags |= BSF_SYNTHETIC;
   3574 	      s->section = glink;
   3575 	      s->value = glink_vma - glink->vma;
   3576 	      s->name = names;
   3577 	      s->udata.p = NULL;
   3578 	      len = strlen ((*p->sym_ptr_ptr)->name);
   3579 	      memcpy (names, (*p->sym_ptr_ptr)->name, len);
   3580 	      names += len;
   3581 	      if (p->addend != 0)
   3582 		{
   3583 		  memcpy (names, "+0x", sizeof ("+0x") - 1);
   3584 		  names += sizeof ("+0x") - 1;
   3585 		  bfd_sprintf_vma (abfd, names, p->addend);
   3586 		  names += strlen (names);
   3587 		}
   3588 	      memcpy (names, "@plt", sizeof ("@plt"));
   3589 	      names += sizeof ("@plt");
   3590 	      s++;
   3591 	      if (abi < 2)
   3592 		{
   3593 		  glink_vma += 8;
   3594 		  if (i >= 0x8000)
   3595 		    glink_vma += 4;
   3596 		}
   3597 	      else
   3598 		glink_vma += 4;
   3599 	    }
   3600 	  count += plt_count;
   3601 	}
   3602     }
   3603 
   3604  done:
   3605   free (syms);
   3606   return count;
   3607 }
   3608 
   3609 /* The following functions are specific to the ELF linker, while
   3611    functions above are used generally.  Those named ppc64_elf_* are
   3612    called by the main ELF linker code.  They appear in this file more
   3613    or less in the order in which they are called.  eg.
   3614    ppc64_elf_check_relocs is called early in the link process,
   3615    ppc64_elf_finish_dynamic_sections is one of the last functions
   3616    called.
   3617 
   3618    PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
   3619    functions have both a function code symbol and a function descriptor
   3620    symbol.  A call to foo in a relocatable object file looks like:
   3621 
   3622    .		.text
   3623    .	x:
   3624    .		bl	.foo
   3625    .		nop
   3626 
   3627    The function definition in another object file might be:
   3628 
   3629    .		.section .opd
   3630    .	foo:	.quad	.foo
   3631    .		.quad	.TOC.@tocbase
   3632    .		.quad	0
   3633    .
   3634    .		.text
   3635    .	.foo:	blr
   3636 
   3637    When the linker resolves the call during a static link, the branch
   3638    unsurprisingly just goes to .foo and the .opd information is unused.
   3639    If the function definition is in a shared library, things are a little
   3640    different:  The call goes via a plt call stub, the opd information gets
   3641    copied to the plt, and the linker patches the nop.
   3642 
   3643    .	x:
   3644    .		bl	.foo_stub
   3645    .		ld	2,40(1)
   3646    .
   3647    .
   3648    .	.foo_stub:
   3649    .		std	2,40(1)			# in practice, the call stub
   3650    .		addis	11,2,Lfoo@toc@ha	# is slightly optimized, but
   3651    .		addi	11,11,Lfoo@toc@l	# this is the general idea
   3652    .		ld	12,0(11)
   3653    .		ld	2,8(11)
   3654    .		mtctr	12
   3655    .		ld	11,16(11)
   3656    .		bctr
   3657    .
   3658    .		.section .plt
   3659    .	Lfoo:	reloc (R_PPC64_JMP_SLOT, foo)
   3660 
   3661    The "reloc ()" notation is supposed to indicate that the linker emits
   3662    an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
   3663    copying.
   3664 
   3665    What are the difficulties here?  Well, firstly, the relocations
   3666    examined by the linker in check_relocs are against the function code
   3667    sym .foo, while the dynamic relocation in the plt is emitted against
   3668    the function descriptor symbol, foo.  Somewhere along the line, we need
   3669    to carefully copy dynamic link information from one symbol to the other.
   3670    Secondly, the generic part of the elf linker will make .foo a dynamic
   3671    symbol as is normal for most other backends.  We need foo dynamic
   3672    instead, at least for an application final link.  However, when
   3673    creating a shared library containing foo, we need to have both symbols
   3674    dynamic so that references to .foo are satisfied during the early
   3675    stages of linking.  Otherwise the linker might decide to pull in a
   3676    definition from some other object, eg. a static library.
   3677 
   3678    Update: As of August 2004, we support a new convention.  Function
   3679    calls may use the function descriptor symbol, ie. "bl foo".  This
   3680    behaves exactly as "bl .foo".  */
   3681 
   3682 /* Of those relocs that might be copied as dynamic relocs, this function
   3683    selects those that must be copied when linking a shared library,
   3684    even when the symbol is local.  */
   3685 
   3686 static int
   3687 must_be_dyn_reloc (struct bfd_link_info *info,
   3688 		   enum elf_ppc64_reloc_type r_type)
   3689 {
   3690   switch (r_type)
   3691     {
   3692     default:
   3693       return 1;
   3694 
   3695     case R_PPC64_REL32:
   3696     case R_PPC64_REL64:
   3697     case R_PPC64_REL30:
   3698       return 0;
   3699 
   3700     case R_PPC64_TPREL16:
   3701     case R_PPC64_TPREL16_LO:
   3702     case R_PPC64_TPREL16_HI:
   3703     case R_PPC64_TPREL16_HA:
   3704     case R_PPC64_TPREL16_DS:
   3705     case R_PPC64_TPREL16_LO_DS:
   3706     case R_PPC64_TPREL16_HIGH:
   3707     case R_PPC64_TPREL16_HIGHA:
   3708     case R_PPC64_TPREL16_HIGHER:
   3709     case R_PPC64_TPREL16_HIGHERA:
   3710     case R_PPC64_TPREL16_HIGHEST:
   3711     case R_PPC64_TPREL16_HIGHESTA:
   3712     case R_PPC64_TPREL64:
   3713       return !info->executable;
   3714     }
   3715 }
   3716 
   3717 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
   3718    copying dynamic variables from a shared lib into an app's dynbss
   3719    section, and instead use a dynamic relocation to point into the
   3720    shared lib.  With code that gcc generates, it's vital that this be
   3721    enabled;  In the PowerPC64 ABI, the address of a function is actually
   3722    the address of a function descriptor, which resides in the .opd
   3723    section.  gcc uses the descriptor directly rather than going via the
   3724    GOT as some other ABI's do, which means that initialized function
   3725    pointers must reference the descriptor.  Thus, a function pointer
   3726    initialized to the address of a function in a shared library will
   3727    either require a copy reloc, or a dynamic reloc.  Using a copy reloc
   3728    redefines the function descriptor symbol to point to the copy.  This
   3729    presents a problem as a plt entry for that function is also
   3730    initialized from the function descriptor symbol and the copy reloc
   3731    may not be initialized first.  */
   3732 #define ELIMINATE_COPY_RELOCS 1
   3733 
   3734 /* Section name for stubs is the associated section name plus this
   3735    string.  */
   3736 #define STUB_SUFFIX ".stub"
   3737 
   3738 /* Linker stubs.
   3739    ppc_stub_long_branch:
   3740    Used when a 14 bit branch (or even a 24 bit branch) can't reach its
   3741    destination, but a 24 bit branch in a stub section will reach.
   3742    .	b	dest
   3743 
   3744    ppc_stub_plt_branch:
   3745    Similar to the above, but a 24 bit branch in the stub section won't
   3746    reach its destination.
   3747    .	addis	%r11,%r2,xxx@toc@ha
   3748    .	ld	%r12,xxx@toc@l(%r11)
   3749    .	mtctr	%r12
   3750    .	bctr
   3751 
   3752    ppc_stub_plt_call:
   3753    Used to call a function in a shared library.  If it so happens that
   3754    the plt entry referenced crosses a 64k boundary, then an extra
   3755    "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
   3756    .	std	%r2,40(%r1)
   3757    .	addis	%r11,%r2,xxx@toc@ha
   3758    .	ld	%r12,xxx+0@toc@l(%r11)
   3759    .	mtctr	%r12
   3760    .	ld	%r2,xxx+8@toc@l(%r11)
   3761    .	ld	%r11,xxx+16@toc@l(%r11)
   3762    .	bctr
   3763 
   3764    ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
   3765    code to adjust the value and save r2 to support multiple toc sections.
   3766    A ppc_stub_long_branch with an r2 offset looks like:
   3767    .	std	%r2,40(%r1)
   3768    .	addis	%r2,%r2,off@ha
   3769    .	addi	%r2,%r2,off@l
   3770    .	b	dest
   3771 
   3772    A ppc_stub_plt_branch with an r2 offset looks like:
   3773    .	std	%r2,40(%r1)
   3774    .	addis	%r11,%r2,xxx@toc@ha
   3775    .	ld	%r12,xxx@toc@l(%r11)
   3776    .	addis	%r2,%r2,off@ha
   3777    .	addi	%r2,%r2,off@l
   3778    .	mtctr	%r12
   3779    .	bctr
   3780 
   3781    In cases where the "addis" instruction would add zero, the "addis" is
   3782    omitted and following instructions modified slightly in some cases.
   3783 */
   3784 
   3785 enum ppc_stub_type {
   3786   ppc_stub_none,
   3787   ppc_stub_long_branch,
   3788   ppc_stub_long_branch_r2off,
   3789   ppc_stub_plt_branch,
   3790   ppc_stub_plt_branch_r2off,
   3791   ppc_stub_plt_call,
   3792   ppc_stub_plt_call_r2save,
   3793   ppc_stub_global_entry
   3794 };
   3795 
   3796 struct ppc_stub_hash_entry {
   3797 
   3798   /* Base hash table entry structure.  */
   3799   struct bfd_hash_entry root;
   3800 
   3801   enum ppc_stub_type stub_type;
   3802 
   3803   /* The stub section.  */
   3804   asection *stub_sec;
   3805 
   3806   /* Offset within stub_sec of the beginning of this stub.  */
   3807   bfd_vma stub_offset;
   3808 
   3809   /* Given the symbol's value and its section we can determine its final
   3810      value when building the stubs (so the stub knows where to jump.  */
   3811   bfd_vma target_value;
   3812   asection *target_section;
   3813 
   3814   /* The symbol table entry, if any, that this was derived from.  */
   3815   struct ppc_link_hash_entry *h;
   3816   struct plt_entry *plt_ent;
   3817 
   3818   /* Where this stub is being called from, or, in the case of combined
   3819      stub sections, the first input section in the group.  */
   3820   asection *id_sec;
   3821 
   3822   /* Symbol st_other.  */
   3823   unsigned char other;
   3824 };
   3825 
   3826 struct ppc_branch_hash_entry {
   3827 
   3828   /* Base hash table entry structure.  */
   3829   struct bfd_hash_entry root;
   3830 
   3831   /* Offset within branch lookup table.  */
   3832   unsigned int offset;
   3833 
   3834   /* Generation marker.  */
   3835   unsigned int iter;
   3836 };
   3837 
   3838 /* Used to track dynamic relocations for local symbols.  */
   3839 struct ppc_dyn_relocs
   3840 {
   3841   struct ppc_dyn_relocs *next;
   3842 
   3843   /* The input section of the reloc.  */
   3844   asection *sec;
   3845 
   3846   /* Total number of relocs copied for the input section.  */
   3847   unsigned int count : 31;
   3848 
   3849   /* Whether this entry is for STT_GNU_IFUNC symbols.  */
   3850   unsigned int ifunc : 1;
   3851 };
   3852 
   3853 struct ppc_link_hash_entry
   3854 {
   3855   struct elf_link_hash_entry elf;
   3856 
   3857   union {
   3858     /* A pointer to the most recently used stub hash entry against this
   3859        symbol.  */
   3860     struct ppc_stub_hash_entry *stub_cache;
   3861 
   3862     /* A pointer to the next symbol starting with a '.'  */
   3863     struct ppc_link_hash_entry *next_dot_sym;
   3864   } u;
   3865 
   3866   /* Track dynamic relocs copied for this symbol.  */
   3867   struct elf_dyn_relocs *dyn_relocs;
   3868 
   3869   /* Link between function code and descriptor symbols.  */
   3870   struct ppc_link_hash_entry *oh;
   3871 
   3872   /* Flag function code and descriptor symbols.  */
   3873   unsigned int is_func:1;
   3874   unsigned int is_func_descriptor:1;
   3875   unsigned int fake:1;
   3876 
   3877   /* Whether global opd/toc sym has been adjusted or not.
   3878      After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
   3879      should be set for all globals defined in any opd/toc section.  */
   3880   unsigned int adjust_done:1;
   3881 
   3882   /* Set if we twiddled this symbol to weak at some stage.  */
   3883   unsigned int was_undefined:1;
   3884 
   3885   /* Contexts in which symbol is used in the GOT (or TOC).
   3886      TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
   3887      corresponding relocs are encountered during check_relocs.
   3888      tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
   3889      indicate the corresponding GOT entry type is not needed.
   3890      tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
   3891      a TPREL one.  We use a separate flag rather than setting TPREL
   3892      just for convenience in distinguishing the two cases.  */
   3893 #define TLS_GD		 1	/* GD reloc. */
   3894 #define TLS_LD		 2	/* LD reloc. */
   3895 #define TLS_TPREL	 4	/* TPREL reloc, => IE. */
   3896 #define TLS_DTPREL	 8	/* DTPREL reloc, => LD. */
   3897 #define TLS_TLS		16	/* Any TLS reloc.  */
   3898 #define TLS_EXPLICIT	32	/* Marks TOC section TLS relocs. */
   3899 #define TLS_TPRELGD	64	/* TPREL reloc resulting from GD->IE. */
   3900 #define PLT_IFUNC      128	/* STT_GNU_IFUNC.  */
   3901   unsigned char tls_mask;
   3902 };
   3903 
   3904 /* ppc64 ELF linker hash table.  */
   3905 
   3906 struct ppc_link_hash_table
   3907 {
   3908   struct elf_link_hash_table elf;
   3909 
   3910   /* The stub hash table.  */
   3911   struct bfd_hash_table stub_hash_table;
   3912 
   3913   /* Another hash table for plt_branch stubs.  */
   3914   struct bfd_hash_table branch_hash_table;
   3915 
   3916   /* Hash table for function prologue tocsave.  */
   3917   htab_t tocsave_htab;
   3918 
   3919   /* Various options and other info passed from the linker.  */
   3920   struct ppc64_elf_params *params;
   3921 
   3922   /* Array to keep track of which stub sections have been created, and
   3923      information on stub grouping.  */
   3924   struct map_stub {
   3925     /* This is the section to which stubs in the group will be attached.  */
   3926     asection *link_sec;
   3927     /* The stub section.  */
   3928     asection *stub_sec;
   3929     /* Along with elf_gp, specifies the TOC pointer used in this group.  */
   3930     bfd_vma toc_off;
   3931   } *stub_group;
   3932 
   3933   /* Temp used when calculating TOC pointers.  */
   3934   bfd_vma toc_curr;
   3935   bfd *toc_bfd;
   3936   asection *toc_first_sec;
   3937 
   3938   /* Highest input section id.  */
   3939   int top_id;
   3940 
   3941   /* Highest output section index.  */
   3942   int top_index;
   3943 
   3944   /* Used when adding symbols.  */
   3945   struct ppc_link_hash_entry *dot_syms;
   3946 
   3947   /* List of input sections for each output section.  */
   3948   asection **input_list;
   3949 
   3950   /* Shortcuts to get to dynamic linker sections.  */
   3951   asection *dynbss;
   3952   asection *relbss;
   3953   asection *glink;
   3954   asection *sfpr;
   3955   asection *brlt;
   3956   asection *relbrlt;
   3957   asection *glink_eh_frame;
   3958 
   3959   /* Shortcut to .__tls_get_addr and __tls_get_addr.  */
   3960   struct ppc_link_hash_entry *tls_get_addr;
   3961   struct ppc_link_hash_entry *tls_get_addr_fd;
   3962 
   3963   /* The size of reliplt used by got entry relocs.  */
   3964   bfd_size_type got_reli_size;
   3965 
   3966   /* Statistics.  */
   3967   unsigned long stub_count[ppc_stub_global_entry];
   3968 
   3969   /* Number of stubs against global syms.  */
   3970   unsigned long stub_globals;
   3971 
   3972   /* Set if we're linking code with function descriptors.  */
   3973   unsigned int opd_abi:1;
   3974 
   3975   /* Support for multiple toc sections.  */
   3976   unsigned int do_multi_toc:1;
   3977   unsigned int multi_toc_needed:1;
   3978   unsigned int second_toc_pass:1;
   3979   unsigned int do_toc_opt:1;
   3980 
   3981   /* Set on error.  */
   3982   unsigned int stub_error:1;
   3983 
   3984   /* Temp used by ppc64_elf_before_check_relocs.  */
   3985   unsigned int twiddled_syms:1;
   3986 
   3987   /* Incremented every time we size stubs.  */
   3988   unsigned int stub_iteration;
   3989 
   3990   /* Small local sym cache.  */
   3991   struct sym_cache sym_cache;
   3992 };
   3993 
   3994 /* Rename some of the generic section flags to better document how they
   3995    are used here.  */
   3996 
   3997 /* Nonzero if this section has TLS related relocations.  */
   3998 #define has_tls_reloc sec_flg0
   3999 
   4000 /* Nonzero if this section has a call to __tls_get_addr.  */
   4001 #define has_tls_get_addr_call sec_flg1
   4002 
   4003 /* Nonzero if this section has any toc or got relocs.  */
   4004 #define has_toc_reloc sec_flg2
   4005 
   4006 /* Nonzero if this section has a call to another section that uses
   4007    the toc or got.  */
   4008 #define makes_toc_func_call sec_flg3
   4009 
   4010 /* Recursion protection when determining above flag.  */
   4011 #define call_check_in_progress sec_flg4
   4012 #define call_check_done sec_flg5
   4013 
   4014 /* Get the ppc64 ELF linker hash table from a link_info structure.  */
   4015 
   4016 #define ppc_hash_table(p) \
   4017   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
   4018   == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
   4019 
   4020 #define ppc_stub_hash_lookup(table, string, create, copy) \
   4021   ((struct ppc_stub_hash_entry *) \
   4022    bfd_hash_lookup ((table), (string), (create), (copy)))
   4023 
   4024 #define ppc_branch_hash_lookup(table, string, create, copy) \
   4025   ((struct ppc_branch_hash_entry *) \
   4026    bfd_hash_lookup ((table), (string), (create), (copy)))
   4027 
   4028 /* Create an entry in the stub hash table.  */
   4029 
   4030 static struct bfd_hash_entry *
   4031 stub_hash_newfunc (struct bfd_hash_entry *entry,
   4032 		   struct bfd_hash_table *table,
   4033 		   const char *string)
   4034 {
   4035   /* Allocate the structure if it has not already been allocated by a
   4036      subclass.  */
   4037   if (entry == NULL)
   4038     {
   4039       entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
   4040       if (entry == NULL)
   4041 	return entry;
   4042     }
   4043 
   4044   /* Call the allocation method of the superclass.  */
   4045   entry = bfd_hash_newfunc (entry, table, string);
   4046   if (entry != NULL)
   4047     {
   4048       struct ppc_stub_hash_entry *eh;
   4049 
   4050       /* Initialize the local fields.  */
   4051       eh = (struct ppc_stub_hash_entry *) entry;
   4052       eh->stub_type = ppc_stub_none;
   4053       eh->stub_sec = NULL;
   4054       eh->stub_offset = 0;
   4055       eh->target_value = 0;
   4056       eh->target_section = NULL;
   4057       eh->h = NULL;
   4058       eh->plt_ent = NULL;
   4059       eh->id_sec = NULL;
   4060       eh->other = 0;
   4061     }
   4062 
   4063   return entry;
   4064 }
   4065 
   4066 /* Create an entry in the branch hash table.  */
   4067 
   4068 static struct bfd_hash_entry *
   4069 branch_hash_newfunc (struct bfd_hash_entry *entry,
   4070 		     struct bfd_hash_table *table,
   4071 		     const char *string)
   4072 {
   4073   /* Allocate the structure if it has not already been allocated by a
   4074      subclass.  */
   4075   if (entry == NULL)
   4076     {
   4077       entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
   4078       if (entry == NULL)
   4079 	return entry;
   4080     }
   4081 
   4082   /* Call the allocation method of the superclass.  */
   4083   entry = bfd_hash_newfunc (entry, table, string);
   4084   if (entry != NULL)
   4085     {
   4086       struct ppc_branch_hash_entry *eh;
   4087 
   4088       /* Initialize the local fields.  */
   4089       eh = (struct ppc_branch_hash_entry *) entry;
   4090       eh->offset = 0;
   4091       eh->iter = 0;
   4092     }
   4093 
   4094   return entry;
   4095 }
   4096 
   4097 /* Create an entry in a ppc64 ELF linker hash table.  */
   4098 
   4099 static struct bfd_hash_entry *
   4100 link_hash_newfunc (struct bfd_hash_entry *entry,
   4101 		   struct bfd_hash_table *table,
   4102 		   const char *string)
   4103 {
   4104   /* Allocate the structure if it has not already been allocated by a
   4105      subclass.  */
   4106   if (entry == NULL)
   4107     {
   4108       entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
   4109       if (entry == NULL)
   4110 	return entry;
   4111     }
   4112 
   4113   /* Call the allocation method of the superclass.  */
   4114   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
   4115   if (entry != NULL)
   4116     {
   4117       struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
   4118 
   4119       memset (&eh->u.stub_cache, 0,
   4120 	      (sizeof (struct ppc_link_hash_entry)
   4121 	       - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
   4122 
   4123       /* When making function calls, old ABI code references function entry
   4124 	 points (dot symbols), while new ABI code references the function
   4125 	 descriptor symbol.  We need to make any combination of reference and
   4126 	 definition work together, without breaking archive linking.
   4127 
   4128 	 For a defined function "foo" and an undefined call to "bar":
   4129 	 An old object defines "foo" and ".foo", references ".bar" (possibly
   4130 	 "bar" too).
   4131 	 A new object defines "foo" and references "bar".
   4132 
   4133 	 A new object thus has no problem with its undefined symbols being
   4134 	 satisfied by definitions in an old object.  On the other hand, the
   4135 	 old object won't have ".bar" satisfied by a new object.
   4136 
   4137 	 Keep a list of newly added dot-symbols.  */
   4138 
   4139       if (string[0] == '.')
   4140 	{
   4141 	  struct ppc_link_hash_table *htab;
   4142 
   4143 	  htab = (struct ppc_link_hash_table *) table;
   4144 	  eh->u.next_dot_sym = htab->dot_syms;
   4145 	  htab->dot_syms = eh;
   4146 	}
   4147     }
   4148 
   4149   return entry;
   4150 }
   4151 
   4152 struct tocsave_entry {
   4153   asection *sec;
   4154   bfd_vma offset;
   4155 };
   4156 
   4157 static hashval_t
   4158 tocsave_htab_hash (const void *p)
   4159 {
   4160   const struct tocsave_entry *e = (const struct tocsave_entry *) p;
   4161   return ((bfd_vma)(intptr_t) e->sec ^ e->offset) >> 3;
   4162 }
   4163 
   4164 static int
   4165 tocsave_htab_eq (const void *p1, const void *p2)
   4166 {
   4167   const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
   4168   const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
   4169   return e1->sec == e2->sec && e1->offset == e2->offset;
   4170 }
   4171 
   4172 /* Destroy a ppc64 ELF linker hash table.  */
   4173 
   4174 static void
   4175 ppc64_elf_link_hash_table_free (bfd *obfd)
   4176 {
   4177   struct ppc_link_hash_table *htab;
   4178 
   4179   htab = (struct ppc_link_hash_table *) obfd->link.hash;
   4180   if (htab->tocsave_htab)
   4181     htab_delete (htab->tocsave_htab);
   4182   bfd_hash_table_free (&htab->branch_hash_table);
   4183   bfd_hash_table_free (&htab->stub_hash_table);
   4184   _bfd_elf_link_hash_table_free (obfd);
   4185 }
   4186 
   4187 /* Create a ppc64 ELF linker hash table.  */
   4188 
   4189 static struct bfd_link_hash_table *
   4190 ppc64_elf_link_hash_table_create (bfd *abfd)
   4191 {
   4192   struct ppc_link_hash_table *htab;
   4193   bfd_size_type amt = sizeof (struct ppc_link_hash_table);
   4194 
   4195   htab = bfd_zmalloc (amt);
   4196   if (htab == NULL)
   4197     return NULL;
   4198 
   4199   if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
   4200 				      sizeof (struct ppc_link_hash_entry),
   4201 				      PPC64_ELF_DATA))
   4202     {
   4203       free (htab);
   4204       return NULL;
   4205     }
   4206 
   4207   /* Init the stub hash table too.  */
   4208   if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
   4209 			    sizeof (struct ppc_stub_hash_entry)))
   4210     {
   4211       _bfd_elf_link_hash_table_free (abfd);
   4212       return NULL;
   4213     }
   4214 
   4215   /* And the branch hash table.  */
   4216   if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
   4217 			    sizeof (struct ppc_branch_hash_entry)))
   4218     {
   4219       bfd_hash_table_free (&htab->stub_hash_table);
   4220       _bfd_elf_link_hash_table_free (abfd);
   4221       return NULL;
   4222     }
   4223 
   4224   htab->tocsave_htab = htab_try_create (1024,
   4225 					tocsave_htab_hash,
   4226 					tocsave_htab_eq,
   4227 					NULL);
   4228   if (htab->tocsave_htab == NULL)
   4229     {
   4230       ppc64_elf_link_hash_table_free (abfd);
   4231       return NULL;
   4232     }
   4233   htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
   4234 
   4235   /* Initializing two fields of the union is just cosmetic.  We really
   4236      only care about glist, but when compiled on a 32-bit host the
   4237      bfd_vma fields are larger.  Setting the bfd_vma to zero makes
   4238      debugger inspection of these fields look nicer.  */
   4239   htab->elf.init_got_refcount.refcount = 0;
   4240   htab->elf.init_got_refcount.glist = NULL;
   4241   htab->elf.init_plt_refcount.refcount = 0;
   4242   htab->elf.init_plt_refcount.glist = NULL;
   4243   htab->elf.init_got_offset.offset = 0;
   4244   htab->elf.init_got_offset.glist = NULL;
   4245   htab->elf.init_plt_offset.offset = 0;
   4246   htab->elf.init_plt_offset.glist = NULL;
   4247 
   4248   return &htab->elf.root;
   4249 }
   4250 
   4251 /* Create sections for linker generated code.  */
   4252 
   4253 static bfd_boolean
   4254 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
   4255 {
   4256   struct ppc_link_hash_table *htab;
   4257   flagword flags;
   4258 
   4259   htab = ppc_hash_table (info);
   4260 
   4261   /* Create .sfpr for code to save and restore fp regs.  */
   4262   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
   4263 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   4264   htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
   4265 						   flags);
   4266   if (htab->sfpr == NULL
   4267       || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
   4268     return FALSE;
   4269 
   4270   /* Create .glink for lazy dynamic linking support.  */
   4271   htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
   4272 						    flags);
   4273   if (htab->glink == NULL
   4274       || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
   4275     return FALSE;
   4276 
   4277   if (!info->no_ld_generated_unwind_info)
   4278     {
   4279       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
   4280 	       | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   4281       htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
   4282 								 ".eh_frame",
   4283 								 flags);
   4284       if (htab->glink_eh_frame == NULL
   4285 	  || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
   4286 	return FALSE;
   4287     }
   4288 
   4289   flags = SEC_ALLOC | SEC_LINKER_CREATED;
   4290   htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
   4291   if (htab->elf.iplt == NULL
   4292       || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
   4293     return FALSE;
   4294 
   4295   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
   4296 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   4297   htab->elf.irelplt
   4298     = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
   4299   if (htab->elf.irelplt == NULL
   4300       || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
   4301     return FALSE;
   4302 
   4303   /* Create branch lookup table for plt_branch stubs.  */
   4304   flags = (SEC_ALLOC | SEC_LOAD
   4305 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   4306   htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
   4307 						   flags);
   4308   if (htab->brlt == NULL
   4309       || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
   4310     return FALSE;
   4311 
   4312   if (!info->shared)
   4313     return TRUE;
   4314 
   4315   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
   4316 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   4317   htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
   4318 						      ".rela.branch_lt",
   4319 						      flags);
   4320   if (htab->relbrlt == NULL
   4321       || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
   4322     return FALSE;
   4323 
   4324   return TRUE;
   4325 }
   4326 
   4327 /* Satisfy the ELF linker by filling in some fields in our fake bfd.  */
   4328 
   4329 bfd_boolean
   4330 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
   4331 			 struct ppc64_elf_params *params)
   4332 {
   4333   struct ppc_link_hash_table *htab;
   4334 
   4335   elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
   4336 
   4337 /* Always hook our dynamic sections into the first bfd, which is the
   4338    linker created stub bfd.  This ensures that the GOT header is at
   4339    the start of the output TOC section.  */
   4340   htab = ppc_hash_table (info);
   4341   if (htab == NULL)
   4342     return FALSE;
   4343   htab->elf.dynobj = params->stub_bfd;
   4344   htab->params = params;
   4345 
   4346   if (info->relocatable)
   4347     return TRUE;
   4348 
   4349   return create_linkage_sections (htab->elf.dynobj, info);
   4350 }
   4351 
   4352 /* Build a name for an entry in the stub hash table.  */
   4353 
   4354 static char *
   4355 ppc_stub_name (const asection *input_section,
   4356 	       const asection *sym_sec,
   4357 	       const struct ppc_link_hash_entry *h,
   4358 	       const Elf_Internal_Rela *rel)
   4359 {
   4360   char *stub_name;
   4361   ssize_t len;
   4362 
   4363   /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
   4364      offsets from a sym as a branch target?  In fact, we could
   4365      probably assume the addend is always zero.  */
   4366   BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
   4367 
   4368   if (h)
   4369     {
   4370       len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
   4371       stub_name = bfd_malloc (len);
   4372       if (stub_name == NULL)
   4373 	return stub_name;
   4374 
   4375       len = sprintf (stub_name, "%08x.%s+%x",
   4376 		     input_section->id & 0xffffffff,
   4377 		     h->elf.root.root.string,
   4378 		     (int) rel->r_addend & 0xffffffff);
   4379     }
   4380   else
   4381     {
   4382       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
   4383       stub_name = bfd_malloc (len);
   4384       if (stub_name == NULL)
   4385 	return stub_name;
   4386 
   4387       len = sprintf (stub_name, "%08x.%x:%x+%x",
   4388 		     input_section->id & 0xffffffff,
   4389 		     sym_sec->id & 0xffffffff,
   4390 		     (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
   4391 		     (int) rel->r_addend & 0xffffffff);
   4392     }
   4393   if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
   4394     stub_name[len - 2] = 0;
   4395   return stub_name;
   4396 }
   4397 
   4398 /* Look up an entry in the stub hash.  Stub entries are cached because
   4399    creating the stub name takes a bit of time.  */
   4400 
   4401 static struct ppc_stub_hash_entry *
   4402 ppc_get_stub_entry (const asection *input_section,
   4403 		    const asection *sym_sec,
   4404 		    struct ppc_link_hash_entry *h,
   4405 		    const Elf_Internal_Rela *rel,
   4406 		    struct ppc_link_hash_table *htab)
   4407 {
   4408   struct ppc_stub_hash_entry *stub_entry;
   4409   const asection *id_sec;
   4410 
   4411   /* If this input section is part of a group of sections sharing one
   4412      stub section, then use the id of the first section in the group.
   4413      Stub names need to include a section id, as there may well be
   4414      more than one stub used to reach say, printf, and we need to
   4415      distinguish between them.  */
   4416   id_sec = htab->stub_group[input_section->id].link_sec;
   4417 
   4418   if (h != NULL && h->u.stub_cache != NULL
   4419       && h->u.stub_cache->h == h
   4420       && h->u.stub_cache->id_sec == id_sec)
   4421     {
   4422       stub_entry = h->u.stub_cache;
   4423     }
   4424   else
   4425     {
   4426       char *stub_name;
   4427 
   4428       stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
   4429       if (stub_name == NULL)
   4430 	return NULL;
   4431 
   4432       stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
   4433 					 stub_name, FALSE, FALSE);
   4434       if (h != NULL)
   4435 	h->u.stub_cache = stub_entry;
   4436 
   4437       free (stub_name);
   4438     }
   4439 
   4440   return stub_entry;
   4441 }
   4442 
   4443 /* Add a new stub entry to the stub hash.  Not all fields of the new
   4444    stub entry are initialised.  */
   4445 
   4446 static struct ppc_stub_hash_entry *
   4447 ppc_add_stub (const char *stub_name,
   4448 	      asection *section,
   4449 	      struct bfd_link_info *info)
   4450 {
   4451   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   4452   asection *link_sec;
   4453   asection *stub_sec;
   4454   struct ppc_stub_hash_entry *stub_entry;
   4455 
   4456   link_sec = htab->stub_group[section->id].link_sec;
   4457   stub_sec = htab->stub_group[section->id].stub_sec;
   4458   if (stub_sec == NULL)
   4459     {
   4460       stub_sec = htab->stub_group[link_sec->id].stub_sec;
   4461       if (stub_sec == NULL)
   4462 	{
   4463 	  size_t namelen;
   4464 	  bfd_size_type len;
   4465 	  char *s_name;
   4466 
   4467 	  namelen = strlen (link_sec->name);
   4468 	  len = namelen + sizeof (STUB_SUFFIX);
   4469 	  s_name = bfd_alloc (htab->params->stub_bfd, len);
   4470 	  if (s_name == NULL)
   4471 	    return NULL;
   4472 
   4473 	  memcpy (s_name, link_sec->name, namelen);
   4474 	  memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
   4475 	  stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
   4476 	  if (stub_sec == NULL)
   4477 	    return NULL;
   4478 	  htab->stub_group[link_sec->id].stub_sec = stub_sec;
   4479 	}
   4480       htab->stub_group[section->id].stub_sec = stub_sec;
   4481     }
   4482 
   4483   /* Enter this entry into the linker stub hash table.  */
   4484   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
   4485 				     TRUE, FALSE);
   4486   if (stub_entry == NULL)
   4487     {
   4488       info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
   4489 			      section->owner, stub_name);
   4490       return NULL;
   4491     }
   4492 
   4493   stub_entry->stub_sec = stub_sec;
   4494   stub_entry->stub_offset = 0;
   4495   stub_entry->id_sec = link_sec;
   4496   return stub_entry;
   4497 }
   4498 
   4499 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
   4500    not already done.  */
   4501 
   4502 static bfd_boolean
   4503 create_got_section (bfd *abfd, struct bfd_link_info *info)
   4504 {
   4505   asection *got, *relgot;
   4506   flagword flags;
   4507   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   4508 
   4509   if (!is_ppc64_elf (abfd))
   4510     return FALSE;
   4511   if (htab == NULL)
   4512     return FALSE;
   4513 
   4514   if (!htab->elf.sgot
   4515       && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
   4516     return FALSE;
   4517 
   4518   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
   4519 	   | SEC_LINKER_CREATED);
   4520 
   4521   got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
   4522   if (!got
   4523       || !bfd_set_section_alignment (abfd, got, 3))
   4524     return FALSE;
   4525 
   4526   relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
   4527 					       flags | SEC_READONLY);
   4528   if (!relgot
   4529       || ! bfd_set_section_alignment (abfd, relgot, 3))
   4530     return FALSE;
   4531 
   4532   ppc64_elf_tdata (abfd)->got = got;
   4533   ppc64_elf_tdata (abfd)->relgot = relgot;
   4534   return TRUE;
   4535 }
   4536 
   4537 /* Create the dynamic sections, and set up shortcuts.  */
   4538 
   4539 static bfd_boolean
   4540 ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
   4541 {
   4542   struct ppc_link_hash_table *htab;
   4543 
   4544   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
   4545     return FALSE;
   4546 
   4547   htab = ppc_hash_table (info);
   4548   if (htab == NULL)
   4549     return FALSE;
   4550 
   4551   htab->dynbss = bfd_get_linker_section (dynobj, ".dynbss");
   4552   if (!info->shared)
   4553     htab->relbss = bfd_get_linker_section (dynobj, ".rela.bss");
   4554 
   4555   if (!htab->elf.sgot || !htab->elf.splt || !htab->elf.srelplt || !htab->dynbss
   4556       || (!info->shared && !htab->relbss))
   4557     abort ();
   4558 
   4559   return TRUE;
   4560 }
   4561 
   4562 /* Follow indirect and warning symbol links.  */
   4563 
   4564 static inline struct bfd_link_hash_entry *
   4565 follow_link (struct bfd_link_hash_entry *h)
   4566 {
   4567   while (h->type == bfd_link_hash_indirect
   4568 	 || h->type == bfd_link_hash_warning)
   4569     h = h->u.i.link;
   4570   return h;
   4571 }
   4572 
   4573 static inline struct elf_link_hash_entry *
   4574 elf_follow_link (struct elf_link_hash_entry *h)
   4575 {
   4576   return (struct elf_link_hash_entry *) follow_link (&h->root);
   4577 }
   4578 
   4579 static inline struct ppc_link_hash_entry *
   4580 ppc_follow_link (struct ppc_link_hash_entry *h)
   4581 {
   4582   return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
   4583 }
   4584 
   4585 /* Merge PLT info on FROM with that on TO.  */
   4586 
   4587 static void
   4588 move_plt_plist (struct ppc_link_hash_entry *from,
   4589 		struct ppc_link_hash_entry *to)
   4590 {
   4591   if (from->elf.plt.plist != NULL)
   4592     {
   4593       if (to->elf.plt.plist != NULL)
   4594 	{
   4595 	  struct plt_entry **entp;
   4596 	  struct plt_entry *ent;
   4597 
   4598 	  for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
   4599 	    {
   4600 	      struct plt_entry *dent;
   4601 
   4602 	      for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
   4603 		if (dent->addend == ent->addend)
   4604 		  {
   4605 		    dent->plt.refcount += ent->plt.refcount;
   4606 		    *entp = ent->next;
   4607 		    break;
   4608 		  }
   4609 	      if (dent == NULL)
   4610 		entp = &ent->next;
   4611 	    }
   4612 	  *entp = to->elf.plt.plist;
   4613 	}
   4614 
   4615       to->elf.plt.plist = from->elf.plt.plist;
   4616       from->elf.plt.plist = NULL;
   4617     }
   4618 }
   4619 
   4620 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
   4621 
   4622 static void
   4623 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
   4624 				struct elf_link_hash_entry *dir,
   4625 				struct elf_link_hash_entry *ind)
   4626 {
   4627   struct ppc_link_hash_entry *edir, *eind;
   4628 
   4629   edir = (struct ppc_link_hash_entry *) dir;
   4630   eind = (struct ppc_link_hash_entry *) ind;
   4631 
   4632   edir->is_func |= eind->is_func;
   4633   edir->is_func_descriptor |= eind->is_func_descriptor;
   4634   edir->tls_mask |= eind->tls_mask;
   4635   if (eind->oh != NULL)
   4636     edir->oh = ppc_follow_link (eind->oh);
   4637 
   4638   /* If called to transfer flags for a weakdef during processing
   4639      of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
   4640      We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
   4641   if (!(ELIMINATE_COPY_RELOCS
   4642 	&& eind->elf.root.type != bfd_link_hash_indirect
   4643 	&& edir->elf.dynamic_adjusted))
   4644     edir->elf.non_got_ref |= eind->elf.non_got_ref;
   4645 
   4646   edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
   4647   edir->elf.ref_regular |= eind->elf.ref_regular;
   4648   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
   4649   edir->elf.needs_plt |= eind->elf.needs_plt;
   4650   edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
   4651 
   4652   /* Copy over any dynamic relocs we may have on the indirect sym.  */
   4653   if (eind->dyn_relocs != NULL)
   4654     {
   4655       if (edir->dyn_relocs != NULL)
   4656 	{
   4657 	  struct elf_dyn_relocs **pp;
   4658 	  struct elf_dyn_relocs *p;
   4659 
   4660 	  /* Add reloc counts against the indirect sym to the direct sym
   4661 	     list.  Merge any entries against the same section.  */
   4662 	  for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
   4663 	    {
   4664 	      struct elf_dyn_relocs *q;
   4665 
   4666 	      for (q = edir->dyn_relocs; q != NULL; q = q->next)
   4667 		if (q->sec == p->sec)
   4668 		  {
   4669 		    q->pc_count += p->pc_count;
   4670 		    q->count += p->count;
   4671 		    *pp = p->next;
   4672 		    break;
   4673 		  }
   4674 	      if (q == NULL)
   4675 		pp = &p->next;
   4676 	    }
   4677 	  *pp = edir->dyn_relocs;
   4678 	}
   4679 
   4680       edir->dyn_relocs = eind->dyn_relocs;
   4681       eind->dyn_relocs = NULL;
   4682     }
   4683 
   4684   /* If we were called to copy over info for a weak sym, that's all.
   4685      You might think dyn_relocs need not be copied over;  After all,
   4686      both syms will be dynamic or both non-dynamic so we're just
   4687      moving reloc accounting around.  However, ELIMINATE_COPY_RELOCS
   4688      code in ppc64_elf_adjust_dynamic_symbol needs to check for
   4689      dyn_relocs in read-only sections, and it does so on what is the
   4690      DIR sym here.  */
   4691   if (eind->elf.root.type != bfd_link_hash_indirect)
   4692     return;
   4693 
   4694   /* Copy over got entries that we may have already seen to the
   4695      symbol which just became indirect.  */
   4696   if (eind->elf.got.glist != NULL)
   4697     {
   4698       if (edir->elf.got.glist != NULL)
   4699 	{
   4700 	  struct got_entry **entp;
   4701 	  struct got_entry *ent;
   4702 
   4703 	  for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
   4704 	    {
   4705 	      struct got_entry *dent;
   4706 
   4707 	      for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
   4708 		if (dent->addend == ent->addend
   4709 		    && dent->owner == ent->owner
   4710 		    && dent->tls_type == ent->tls_type)
   4711 		  {
   4712 		    dent->got.refcount += ent->got.refcount;
   4713 		    *entp = ent->next;
   4714 		    break;
   4715 		  }
   4716 	      if (dent == NULL)
   4717 		entp = &ent->next;
   4718 	    }
   4719 	  *entp = edir->elf.got.glist;
   4720 	}
   4721 
   4722       edir->elf.got.glist = eind->elf.got.glist;
   4723       eind->elf.got.glist = NULL;
   4724     }
   4725 
   4726   /* And plt entries.  */
   4727   move_plt_plist (eind, edir);
   4728 
   4729   if (eind->elf.dynindx != -1)
   4730     {
   4731       if (edir->elf.dynindx != -1)
   4732 	_bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
   4733 				edir->elf.dynstr_index);
   4734       edir->elf.dynindx = eind->elf.dynindx;
   4735       edir->elf.dynstr_index = eind->elf.dynstr_index;
   4736       eind->elf.dynindx = -1;
   4737       eind->elf.dynstr_index = 0;
   4738     }
   4739 }
   4740 
   4741 /* Find the function descriptor hash entry from the given function code
   4742    hash entry FH.  Link the entries via their OH fields.  */
   4743 
   4744 static struct ppc_link_hash_entry *
   4745 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
   4746 {
   4747   struct ppc_link_hash_entry *fdh = fh->oh;
   4748 
   4749   if (fdh == NULL)
   4750     {
   4751       const char *fd_name = fh->elf.root.root.string + 1;
   4752 
   4753       fdh = (struct ppc_link_hash_entry *)
   4754 	elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
   4755       if (fdh == NULL)
   4756 	return fdh;
   4757 
   4758       fdh->is_func_descriptor = 1;
   4759       fdh->oh = fh;
   4760       fh->is_func = 1;
   4761       fh->oh = fdh;
   4762     }
   4763 
   4764   return ppc_follow_link (fdh);
   4765 }
   4766 
   4767 /* Make a fake function descriptor sym for the code sym FH.  */
   4768 
   4769 static struct ppc_link_hash_entry *
   4770 make_fdh (struct bfd_link_info *info,
   4771 	  struct ppc_link_hash_entry *fh)
   4772 {
   4773   bfd *abfd;
   4774   asymbol *newsym;
   4775   struct bfd_link_hash_entry *bh;
   4776   struct ppc_link_hash_entry *fdh;
   4777 
   4778   abfd = fh->elf.root.u.undef.abfd;
   4779   newsym = bfd_make_empty_symbol (abfd);
   4780   newsym->name = fh->elf.root.root.string + 1;
   4781   newsym->section = bfd_und_section_ptr;
   4782   newsym->value = 0;
   4783   newsym->flags = BSF_WEAK;
   4784 
   4785   bh = NULL;
   4786   if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
   4787 					 newsym->flags, newsym->section,
   4788 					 newsym->value, NULL, FALSE, FALSE,
   4789 					 &bh))
   4790     return NULL;
   4791 
   4792   fdh = (struct ppc_link_hash_entry *) bh;
   4793   fdh->elf.non_elf = 0;
   4794   fdh->fake = 1;
   4795   fdh->is_func_descriptor = 1;
   4796   fdh->oh = fh;
   4797   fh->is_func = 1;
   4798   fh->oh = fdh;
   4799   return fdh;
   4800 }
   4801 
   4802 /* Fix function descriptor symbols defined in .opd sections to be
   4803    function type.  */
   4804 
   4805 static bfd_boolean
   4806 ppc64_elf_add_symbol_hook (bfd *ibfd,
   4807 			   struct bfd_link_info *info,
   4808 			   Elf_Internal_Sym *isym,
   4809 			   const char **name,
   4810 			   flagword *flags ATTRIBUTE_UNUSED,
   4811 			   asection **sec,
   4812 			   bfd_vma *value)
   4813 {
   4814   if ((ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
   4815        || ELF_ST_BIND (isym->st_info) == STB_GNU_UNIQUE)
   4816       && (ibfd->flags & DYNAMIC) == 0
   4817       && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
   4818     elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
   4819 
   4820   if (*sec != NULL
   4821       && strcmp ((*sec)->name, ".opd") == 0)
   4822     {
   4823       asection *code_sec;
   4824 
   4825       if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
   4826 	    || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
   4827 	isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
   4828 
   4829       /* If the symbol is a function defined in .opd, and the function
   4830 	 code is in a discarded group, let it appear to be undefined.  */
   4831       if (!info->relocatable
   4832 	  && (*sec)->reloc_count != 0
   4833 	  && opd_entry_value (*sec, *value, &code_sec, NULL,
   4834 			      FALSE) != (bfd_vma) -1
   4835 	  && discarded_section (code_sec))
   4836 	{
   4837 	  *sec = bfd_und_section_ptr;
   4838 	  isym->st_shndx = SHN_UNDEF;
   4839 	}
   4840     }
   4841 
   4842   if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
   4843     {
   4844       if (abiversion (ibfd) == 0)
   4845 	set_abiversion (ibfd, 2);
   4846       else if (abiversion (ibfd) == 1)
   4847 	{
   4848 	  info->callbacks->einfo (_("%P: symbol '%s' has invalid st_other"
   4849 				    " for ABI version 1\n"), name);
   4850 	  bfd_set_error (bfd_error_bad_value);
   4851 	  return FALSE;
   4852 	}
   4853     }
   4854 
   4855   return TRUE;
   4856 }
   4857 
   4858 /* Merge non-visibility st_other attributes: local entry point.  */
   4859 
   4860 static void
   4861 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
   4862 				  const Elf_Internal_Sym *isym,
   4863 				  bfd_boolean definition,
   4864 				  bfd_boolean dynamic)
   4865 {
   4866   if (definition && !dynamic)
   4867     h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
   4868 		| ELF_ST_VISIBILITY (h->other));
   4869 }
   4870 
   4871 /* This function makes an old ABI object reference to ".bar" cause the
   4872    inclusion of a new ABI object archive that defines "bar".
   4873    NAME is a symbol defined in an archive.  Return a symbol in the hash
   4874    table that might be satisfied by the archive symbols.  */
   4875 
   4876 static struct elf_link_hash_entry *
   4877 ppc64_elf_archive_symbol_lookup (bfd *abfd,
   4878 				 struct bfd_link_info *info,
   4879 				 const char *name)
   4880 {
   4881   struct elf_link_hash_entry *h;
   4882   char *dot_name;
   4883   size_t len;
   4884 
   4885   h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
   4886   if (h != NULL
   4887       /* Don't return this sym if it is a fake function descriptor
   4888 	 created by add_symbol_adjust.  */
   4889       && !(h->root.type == bfd_link_hash_undefweak
   4890 	   && ((struct ppc_link_hash_entry *) h)->fake))
   4891     return h;
   4892 
   4893   if (name[0] == '.')
   4894     return h;
   4895 
   4896   len = strlen (name);
   4897   dot_name = bfd_alloc (abfd, len + 2);
   4898   if (dot_name == NULL)
   4899     return (struct elf_link_hash_entry *) 0 - 1;
   4900   dot_name[0] = '.';
   4901   memcpy (dot_name + 1, name, len + 1);
   4902   h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
   4903   bfd_release (abfd, dot_name);
   4904   return h;
   4905 }
   4906 
   4907 /* This function satisfies all old ABI object references to ".bar" if a
   4908    new ABI object defines "bar".  Well, at least, undefined dot symbols
   4909    are made weak.  This stops later archive searches from including an
   4910    object if we already have a function descriptor definition.  It also
   4911    prevents the linker complaining about undefined symbols.
   4912    We also check and correct mismatched symbol visibility here.  The
   4913    most restrictive visibility of the function descriptor and the
   4914    function entry symbol is used.  */
   4915 
   4916 static bfd_boolean
   4917 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
   4918 {
   4919   struct ppc_link_hash_table *htab;
   4920   struct ppc_link_hash_entry *fdh;
   4921 
   4922   if (eh->elf.root.type == bfd_link_hash_indirect)
   4923     return TRUE;
   4924 
   4925   if (eh->elf.root.type == bfd_link_hash_warning)
   4926     eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
   4927 
   4928   if (eh->elf.root.root.string[0] != '.')
   4929     abort ();
   4930 
   4931   htab = ppc_hash_table (info);
   4932   if (htab == NULL)
   4933     return FALSE;
   4934 
   4935   fdh = lookup_fdh (eh, htab);
   4936   if (fdh == NULL)
   4937     {
   4938       if (!info->relocatable
   4939 	  && (eh->elf.root.type == bfd_link_hash_undefined
   4940 	      || eh->elf.root.type == bfd_link_hash_undefweak)
   4941 	  && eh->elf.ref_regular)
   4942 	{
   4943 	  /* Make an undefweak function descriptor sym, which is enough to
   4944 	     pull in an --as-needed shared lib, but won't cause link
   4945 	     errors.  Archives are handled elsewhere.  */
   4946 	  fdh = make_fdh (info, eh);
   4947 	  if (fdh == NULL)
   4948 	    return FALSE;
   4949 	  fdh->elf.ref_regular = 1;
   4950 	}
   4951     }
   4952   else
   4953     {
   4954       unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
   4955       unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
   4956       if (entry_vis < descr_vis)
   4957 	fdh->elf.other += entry_vis - descr_vis;
   4958       else if (entry_vis > descr_vis)
   4959 	eh->elf.other += descr_vis - entry_vis;
   4960 
   4961       if ((fdh->elf.root.type == bfd_link_hash_defined
   4962 	   || fdh->elf.root.type == bfd_link_hash_defweak)
   4963 	  && eh->elf.root.type == bfd_link_hash_undefined)
   4964 	{
   4965 	  eh->elf.root.type = bfd_link_hash_undefweak;
   4966 	  eh->was_undefined = 1;
   4967 	  htab->twiddled_syms = 1;
   4968 	}
   4969     }
   4970 
   4971   return TRUE;
   4972 }
   4973 
   4974 /* Set up opd section info and abiversion for IBFD, and process list
   4975    of dot-symbols we made in link_hash_newfunc.  */
   4976 
   4977 static bfd_boolean
   4978 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
   4979 {
   4980   struct ppc_link_hash_table *htab;
   4981   struct ppc_link_hash_entry **p, *eh;
   4982   asection *opd = bfd_get_section_by_name (ibfd, ".opd");
   4983 
   4984   if (opd != NULL && opd->size != 0)
   4985     {
   4986       if (abiversion (ibfd) == 0)
   4987 	set_abiversion (ibfd, 1);
   4988       else if (abiversion (ibfd) == 2)
   4989 	{
   4990 	  info->callbacks->einfo (_("%P: %B .opd not allowed in ABI"
   4991 				    " version %d\n"),
   4992 				  ibfd, abiversion (ibfd));
   4993 	  bfd_set_error (bfd_error_bad_value);
   4994 	  return FALSE;
   4995 	}
   4996 
   4997       if ((ibfd->flags & DYNAMIC) == 0
   4998 	  && (opd->flags & SEC_RELOC) != 0
   4999 	  && opd->reloc_count != 0
   5000 	  && !bfd_is_abs_section (opd->output_section))
   5001 	{
   5002 	  /* Garbage collection needs some extra help with .opd sections.
   5003 	     We don't want to necessarily keep everything referenced by
   5004 	     relocs in .opd, as that would keep all functions.  Instead,
   5005 	     if we reference an .opd symbol (a function descriptor), we
   5006 	     want to keep the function code symbol's section.  This is
   5007 	     easy for global symbols, but for local syms we need to keep
   5008 	     information about the associated function section.  */
   5009 	  bfd_size_type amt;
   5010 	  asection **opd_sym_map;
   5011 
   5012 	  amt = opd->size * sizeof (*opd_sym_map) / 8;
   5013 	  opd_sym_map = bfd_zalloc (ibfd, amt);
   5014 	  if (opd_sym_map == NULL)
   5015 	    return FALSE;
   5016 	  ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
   5017 	  BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
   5018 	  ppc64_elf_section_data (opd)->sec_type = sec_opd;
   5019 	}
   5020     }
   5021 
   5022   if (!is_ppc64_elf (info->output_bfd))
   5023     return TRUE;
   5024   htab = ppc_hash_table (info);
   5025   if (htab == NULL)
   5026     return FALSE;
   5027 
   5028   /* For input files without an explicit abiversion in e_flags
   5029      we should have flagged any with symbol st_other bits set
   5030      as ELFv1 and above flagged those with .opd as ELFv2.
   5031      Set the output abiversion if not yet set, and for any input
   5032      still ambiguous, take its abiversion from the output.
   5033      Differences in ABI are reported later.  */
   5034   if (abiversion (info->output_bfd) == 0)
   5035     set_abiversion (info->output_bfd, abiversion (ibfd));
   5036   else if (abiversion (ibfd) == 0)
   5037     set_abiversion (ibfd, abiversion (info->output_bfd));
   5038 
   5039   p = &htab->dot_syms;
   5040   while ((eh = *p) != NULL)
   5041     {
   5042       *p = NULL;
   5043       if (&eh->elf == htab->elf.hgot)
   5044 	;
   5045       else if (htab->elf.hgot == NULL
   5046 	       && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
   5047 	htab->elf.hgot = &eh->elf;
   5048       else if (!add_symbol_adjust (eh, info))
   5049 	return FALSE;
   5050       p = &eh->u.next_dot_sym;
   5051     }
   5052 
   5053   /* Clear the list for non-ppc64 input files.  */
   5054   p = &htab->dot_syms;
   5055   while ((eh = *p) != NULL)
   5056     {
   5057       *p = NULL;
   5058       p = &eh->u.next_dot_sym;
   5059     }
   5060 
   5061   /* We need to fix the undefs list for any syms we have twiddled to
   5062      undef_weak.  */
   5063   if (htab->twiddled_syms)
   5064     {
   5065       bfd_link_repair_undef_list (&htab->elf.root);
   5066       htab->twiddled_syms = 0;
   5067     }
   5068   return TRUE;
   5069 }
   5070 
   5071 /* Undo hash table changes when an --as-needed input file is determined
   5072    not to be needed.  */
   5073 
   5074 static bfd_boolean
   5075 ppc64_elf_notice_as_needed (bfd *ibfd,
   5076 			    struct bfd_link_info *info,
   5077 			    enum notice_asneeded_action act)
   5078 {
   5079   if (act == notice_not_needed)
   5080     {
   5081       struct ppc_link_hash_table *htab = ppc_hash_table (info);
   5082 
   5083       if (htab == NULL)
   5084 	return FALSE;
   5085 
   5086       htab->dot_syms = NULL;
   5087     }
   5088   return _bfd_elf_notice_as_needed (ibfd, info, act);
   5089 }
   5090 
   5091 /* If --just-symbols against a final linked binary, then assume we need
   5092    toc adjusting stubs when calling functions defined there.  */
   5093 
   5094 static void
   5095 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
   5096 {
   5097   if ((sec->flags & SEC_CODE) != 0
   5098       && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
   5099       && is_ppc64_elf (sec->owner))
   5100     {
   5101       if (abiversion (sec->owner) >= 2
   5102 	  || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
   5103 	sec->has_toc_reloc = 1;
   5104     }
   5105   _bfd_elf_link_just_syms (sec, info);
   5106 }
   5107 
   5108 static struct plt_entry **
   5109 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
   5110 		       unsigned long r_symndx, bfd_vma r_addend, int tls_type)
   5111 {
   5112   struct got_entry **local_got_ents = elf_local_got_ents (abfd);
   5113   struct plt_entry **local_plt;
   5114   unsigned char *local_got_tls_masks;
   5115 
   5116   if (local_got_ents == NULL)
   5117     {
   5118       bfd_size_type size = symtab_hdr->sh_info;
   5119 
   5120       size *= (sizeof (*local_got_ents)
   5121 	       + sizeof (*local_plt)
   5122 	       + sizeof (*local_got_tls_masks));
   5123       local_got_ents = bfd_zalloc (abfd, size);
   5124       if (local_got_ents == NULL)
   5125 	return NULL;
   5126       elf_local_got_ents (abfd) = local_got_ents;
   5127     }
   5128 
   5129   if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
   5130     {
   5131       struct got_entry *ent;
   5132 
   5133       for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
   5134 	if (ent->addend == r_addend
   5135 	    && ent->owner == abfd
   5136 	    && ent->tls_type == tls_type)
   5137 	  break;
   5138       if (ent == NULL)
   5139 	{
   5140 	  bfd_size_type amt = sizeof (*ent);
   5141 	  ent = bfd_alloc (abfd, amt);
   5142 	  if (ent == NULL)
   5143 	    return FALSE;
   5144 	  ent->next = local_got_ents[r_symndx];
   5145 	  ent->addend = r_addend;
   5146 	  ent->owner = abfd;
   5147 	  ent->tls_type = tls_type;
   5148 	  ent->is_indirect = FALSE;
   5149 	  ent->got.refcount = 0;
   5150 	  local_got_ents[r_symndx] = ent;
   5151 	}
   5152       ent->got.refcount += 1;
   5153     }
   5154 
   5155   local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
   5156   local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
   5157   local_got_tls_masks[r_symndx] |= tls_type;
   5158 
   5159   return local_plt + r_symndx;
   5160 }
   5161 
   5162 static bfd_boolean
   5163 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
   5164 {
   5165   struct plt_entry *ent;
   5166 
   5167   for (ent = *plist; ent != NULL; ent = ent->next)
   5168     if (ent->addend == addend)
   5169       break;
   5170   if (ent == NULL)
   5171     {
   5172       bfd_size_type amt = sizeof (*ent);
   5173       ent = bfd_alloc (abfd, amt);
   5174       if (ent == NULL)
   5175 	return FALSE;
   5176       ent->next = *plist;
   5177       ent->addend = addend;
   5178       ent->plt.refcount = 0;
   5179       *plist = ent;
   5180     }
   5181   ent->plt.refcount += 1;
   5182   return TRUE;
   5183 }
   5184 
   5185 static bfd_boolean
   5186 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
   5187 {
   5188   return (r_type == R_PPC64_REL24
   5189 	  || r_type == R_PPC64_REL14
   5190 	  || r_type == R_PPC64_REL14_BRTAKEN
   5191 	  || r_type == R_PPC64_REL14_BRNTAKEN
   5192 	  || r_type == R_PPC64_ADDR24
   5193 	  || r_type == R_PPC64_ADDR14
   5194 	  || r_type == R_PPC64_ADDR14_BRTAKEN
   5195 	  || r_type == R_PPC64_ADDR14_BRNTAKEN);
   5196 }
   5197 
   5198 /* Look through the relocs for a section during the first phase, and
   5199    calculate needed space in the global offset table, procedure
   5200    linkage table, and dynamic reloc sections.  */
   5201 
   5202 static bfd_boolean
   5203 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
   5204 			asection *sec, const Elf_Internal_Rela *relocs)
   5205 {
   5206   struct ppc_link_hash_table *htab;
   5207   Elf_Internal_Shdr *symtab_hdr;
   5208   struct elf_link_hash_entry **sym_hashes;
   5209   const Elf_Internal_Rela *rel;
   5210   const Elf_Internal_Rela *rel_end;
   5211   asection *sreloc;
   5212   asection **opd_sym_map;
   5213   struct elf_link_hash_entry *tga, *dottga;
   5214 
   5215   if (info->relocatable)
   5216     return TRUE;
   5217 
   5218   /* Don't do anything special with non-loaded, non-alloced sections.
   5219      In particular, any relocs in such sections should not affect GOT
   5220      and PLT reference counting (ie. we don't allow them to create GOT
   5221      or PLT entries), there's no possibility or desire to optimize TLS
   5222      relocs, and there's not much point in propagating relocs to shared
   5223      libs that the dynamic linker won't relocate.  */
   5224   if ((sec->flags & SEC_ALLOC) == 0)
   5225     return TRUE;
   5226 
   5227   BFD_ASSERT (is_ppc64_elf (abfd));
   5228 
   5229   htab = ppc_hash_table (info);
   5230   if (htab == NULL)
   5231     return FALSE;
   5232 
   5233   tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
   5234 			      FALSE, FALSE, TRUE);
   5235   dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
   5236 				 FALSE, FALSE, TRUE);
   5237   symtab_hdr = &elf_symtab_hdr (abfd);
   5238   sym_hashes = elf_sym_hashes (abfd);
   5239   sreloc = NULL;
   5240   opd_sym_map = NULL;
   5241   if (ppc64_elf_section_data (sec) != NULL
   5242       && ppc64_elf_section_data (sec)->sec_type == sec_opd)
   5243     opd_sym_map = ppc64_elf_section_data (sec)->u.opd.func_sec;
   5244 
   5245   rel_end = relocs + sec->reloc_count;
   5246   for (rel = relocs; rel < rel_end; rel++)
   5247     {
   5248       unsigned long r_symndx;
   5249       struct elf_link_hash_entry *h;
   5250       enum elf_ppc64_reloc_type r_type;
   5251       int tls_type;
   5252       struct _ppc64_elf_section_data *ppc64_sec;
   5253       struct plt_entry **ifunc;
   5254 
   5255       r_symndx = ELF64_R_SYM (rel->r_info);
   5256       if (r_symndx < symtab_hdr->sh_info)
   5257 	h = NULL;
   5258       else
   5259 	{
   5260 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   5261 	  h = elf_follow_link (h);
   5262 
   5263 	  /* PR15323, ref flags aren't set for references in the same
   5264 	     object.  */
   5265 	  h->root.non_ir_ref = 1;
   5266 
   5267 	  if (h == htab->elf.hgot)
   5268 	    sec->has_toc_reloc = 1;
   5269 	}
   5270 
   5271       tls_type = 0;
   5272       ifunc = NULL;
   5273       if (h != NULL)
   5274 	{
   5275 	  if (h->type == STT_GNU_IFUNC)
   5276 	    {
   5277 	      h->needs_plt = 1;
   5278 	      ifunc = &h->plt.plist;
   5279 	    }
   5280 	}
   5281       else
   5282 	{
   5283 	  Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
   5284 							  abfd, r_symndx);
   5285 	  if (isym == NULL)
   5286 	    return FALSE;
   5287 
   5288 	  if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
   5289 	    {
   5290 	      ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
   5291 					     rel->r_addend, PLT_IFUNC);
   5292 	      if (ifunc == NULL)
   5293 		return FALSE;
   5294 	    }
   5295 	}
   5296       r_type = ELF64_R_TYPE (rel->r_info);
   5297       if (is_branch_reloc (r_type))
   5298 	{
   5299 	  if (h != NULL && (h == tga || h == dottga))
   5300 	    {
   5301 	      if (rel != relocs
   5302 		  && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
   5303 		      || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
   5304 		/* We have a new-style __tls_get_addr call with a marker
   5305 		   reloc.  */
   5306 		;
   5307 	      else
   5308 		/* Mark this section as having an old-style call.  */
   5309 		sec->has_tls_get_addr_call = 1;
   5310 	    }
   5311 
   5312 	  /* STT_GNU_IFUNC symbols must have a PLT entry.  */
   5313 	  if (ifunc != NULL
   5314 	      && !update_plt_info (abfd, ifunc, rel->r_addend))
   5315 	    return FALSE;
   5316 	}
   5317 
   5318       switch (r_type)
   5319 	{
   5320 	case R_PPC64_TLSGD:
   5321 	case R_PPC64_TLSLD:
   5322 	  /* These special tls relocs tie a call to __tls_get_addr with
   5323 	     its parameter symbol.  */
   5324 	  break;
   5325 
   5326 	case R_PPC64_GOT_TLSLD16:
   5327 	case R_PPC64_GOT_TLSLD16_LO:
   5328 	case R_PPC64_GOT_TLSLD16_HI:
   5329 	case R_PPC64_GOT_TLSLD16_HA:
   5330 	  tls_type = TLS_TLS | TLS_LD;
   5331 	  goto dogottls;
   5332 
   5333 	case R_PPC64_GOT_TLSGD16:
   5334 	case R_PPC64_GOT_TLSGD16_LO:
   5335 	case R_PPC64_GOT_TLSGD16_HI:
   5336 	case R_PPC64_GOT_TLSGD16_HA:
   5337 	  tls_type = TLS_TLS | TLS_GD;
   5338 	  goto dogottls;
   5339 
   5340 	case R_PPC64_GOT_TPREL16_DS:
   5341 	case R_PPC64_GOT_TPREL16_LO_DS:
   5342 	case R_PPC64_GOT_TPREL16_HI:
   5343 	case R_PPC64_GOT_TPREL16_HA:
   5344 	  if (info->shared)
   5345 	    info->flags |= DF_STATIC_TLS;
   5346 	  tls_type = TLS_TLS | TLS_TPREL;
   5347 	  goto dogottls;
   5348 
   5349 	case R_PPC64_GOT_DTPREL16_DS:
   5350 	case R_PPC64_GOT_DTPREL16_LO_DS:
   5351 	case R_PPC64_GOT_DTPREL16_HI:
   5352 	case R_PPC64_GOT_DTPREL16_HA:
   5353 	  tls_type = TLS_TLS | TLS_DTPREL;
   5354 	dogottls:
   5355 	  sec->has_tls_reloc = 1;
   5356 	  /* Fall thru */
   5357 
   5358 	case R_PPC64_GOT16:
   5359 	case R_PPC64_GOT16_DS:
   5360 	case R_PPC64_GOT16_HA:
   5361 	case R_PPC64_GOT16_HI:
   5362 	case R_PPC64_GOT16_LO:
   5363 	case R_PPC64_GOT16_LO_DS:
   5364 	  /* This symbol requires a global offset table entry.  */
   5365 	  sec->has_toc_reloc = 1;
   5366 	  if (r_type == R_PPC64_GOT_TLSLD16
   5367 	      || r_type == R_PPC64_GOT_TLSGD16
   5368 	      || r_type == R_PPC64_GOT_TPREL16_DS
   5369 	      || r_type == R_PPC64_GOT_DTPREL16_DS
   5370 	      || r_type == R_PPC64_GOT16
   5371 	      || r_type == R_PPC64_GOT16_DS)
   5372 	    {
   5373 	      htab->do_multi_toc = 1;
   5374 	      ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
   5375 	    }
   5376 
   5377 	  if (ppc64_elf_tdata (abfd)->got == NULL
   5378 	      && !create_got_section (abfd, info))
   5379 	    return FALSE;
   5380 
   5381 	  if (h != NULL)
   5382 	    {
   5383 	      struct ppc_link_hash_entry *eh;
   5384 	      struct got_entry *ent;
   5385 
   5386 	      eh = (struct ppc_link_hash_entry *) h;
   5387 	      for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
   5388 		if (ent->addend == rel->r_addend
   5389 		    && ent->owner == abfd
   5390 		    && ent->tls_type == tls_type)
   5391 		  break;
   5392 	      if (ent == NULL)
   5393 		{
   5394 		  bfd_size_type amt = sizeof (*ent);
   5395 		  ent = bfd_alloc (abfd, amt);
   5396 		  if (ent == NULL)
   5397 		    return FALSE;
   5398 		  ent->next = eh->elf.got.glist;
   5399 		  ent->addend = rel->r_addend;
   5400 		  ent->owner = abfd;
   5401 		  ent->tls_type = tls_type;
   5402 		  ent->is_indirect = FALSE;
   5403 		  ent->got.refcount = 0;
   5404 		  eh->elf.got.glist = ent;
   5405 		}
   5406 	      ent->got.refcount += 1;
   5407 	      eh->tls_mask |= tls_type;
   5408 	    }
   5409 	  else
   5410 	    /* This is a global offset table entry for a local symbol.  */
   5411 	    if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
   5412 					rel->r_addend, tls_type))
   5413 	      return FALSE;
   5414 
   5415 	  /* We may also need a plt entry if the symbol turns out to be
   5416 	     an ifunc.  */
   5417 	  if (h != NULL && !info->shared && abiversion (abfd) != 1)
   5418 	    {
   5419 	      if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
   5420 		return FALSE;
   5421 	    }
   5422 	  break;
   5423 
   5424 	case R_PPC64_PLT16_HA:
   5425 	case R_PPC64_PLT16_HI:
   5426 	case R_PPC64_PLT16_LO:
   5427 	case R_PPC64_PLT32:
   5428 	case R_PPC64_PLT64:
   5429 	  /* This symbol requires a procedure linkage table entry.  We
   5430 	     actually build the entry in adjust_dynamic_symbol,
   5431 	     because this might be a case of linking PIC code without
   5432 	     linking in any dynamic objects, in which case we don't
   5433 	     need to generate a procedure linkage table after all.  */
   5434 	  if (h == NULL)
   5435 	    {
   5436 	      /* It does not make sense to have a procedure linkage
   5437 		 table entry for a local symbol.  */
   5438 	      bfd_set_error (bfd_error_bad_value);
   5439 	      return FALSE;
   5440 	    }
   5441 	  else
   5442 	    {
   5443 	      if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
   5444 		return FALSE;
   5445 	      h->needs_plt = 1;
   5446 	      if (h->root.root.string[0] == '.'
   5447 		  && h->root.root.string[1] != '\0')
   5448 		((struct ppc_link_hash_entry *) h)->is_func = 1;
   5449 	    }
   5450 	  break;
   5451 
   5452 	  /* The following relocations don't need to propagate the
   5453 	     relocation if linking a shared object since they are
   5454 	     section relative.  */
   5455 	case R_PPC64_SECTOFF:
   5456 	case R_PPC64_SECTOFF_LO:
   5457 	case R_PPC64_SECTOFF_HI:
   5458 	case R_PPC64_SECTOFF_HA:
   5459 	case R_PPC64_SECTOFF_DS:
   5460 	case R_PPC64_SECTOFF_LO_DS:
   5461 	case R_PPC64_DTPREL16:
   5462 	case R_PPC64_DTPREL16_LO:
   5463 	case R_PPC64_DTPREL16_HI:
   5464 	case R_PPC64_DTPREL16_HA:
   5465 	case R_PPC64_DTPREL16_DS:
   5466 	case R_PPC64_DTPREL16_LO_DS:
   5467 	case R_PPC64_DTPREL16_HIGH:
   5468 	case R_PPC64_DTPREL16_HIGHA:
   5469 	case R_PPC64_DTPREL16_HIGHER:
   5470 	case R_PPC64_DTPREL16_HIGHERA:
   5471 	case R_PPC64_DTPREL16_HIGHEST:
   5472 	case R_PPC64_DTPREL16_HIGHESTA:
   5473 	  break;
   5474 
   5475 	  /* Nor do these.  */
   5476 	case R_PPC64_REL16:
   5477 	case R_PPC64_REL16_LO:
   5478 	case R_PPC64_REL16_HI:
   5479 	case R_PPC64_REL16_HA:
   5480 	  break;
   5481 
   5482 	  /* Not supported as a dynamic relocation.  */
   5483 	case R_PPC64_ADDR64_LOCAL:
   5484 	  if (info->shared)
   5485 	    {
   5486 	      if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
   5487 		ppc_howto_init ();
   5488 	      info->callbacks->einfo (_("%P: %H: %s reloc unsupported "
   5489 					"in shared libraries and PIEs.\n"),
   5490 				      abfd, sec, rel->r_offset,
   5491 				      ppc64_elf_howto_table[r_type]->name);
   5492 	      bfd_set_error (bfd_error_bad_value);
   5493 	      return FALSE;
   5494 	    }
   5495 	  break;
   5496 
   5497 	case R_PPC64_TOC16:
   5498 	case R_PPC64_TOC16_DS:
   5499 	  htab->do_multi_toc = 1;
   5500 	  ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
   5501 	case R_PPC64_TOC16_LO:
   5502 	case R_PPC64_TOC16_HI:
   5503 	case R_PPC64_TOC16_HA:
   5504 	case R_PPC64_TOC16_LO_DS:
   5505 	  sec->has_toc_reloc = 1;
   5506 	  break;
   5507 
   5508 	  /* This relocation describes the C++ object vtable hierarchy.
   5509 	     Reconstruct it for later use during GC.  */
   5510 	case R_PPC64_GNU_VTINHERIT:
   5511 	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
   5512 	    return FALSE;
   5513 	  break;
   5514 
   5515 	  /* This relocation describes which C++ vtable entries are actually
   5516 	     used.  Record for later use during GC.  */
   5517 	case R_PPC64_GNU_VTENTRY:
   5518 	  BFD_ASSERT (h != NULL);
   5519 	  if (h != NULL
   5520 	      && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
   5521 	    return FALSE;
   5522 	  break;
   5523 
   5524 	case R_PPC64_REL14:
   5525 	case R_PPC64_REL14_BRTAKEN:
   5526 	case R_PPC64_REL14_BRNTAKEN:
   5527 	  {
   5528 	    asection *dest = NULL;
   5529 
   5530 	    /* Heuristic: If jumping outside our section, chances are
   5531 	       we are going to need a stub.  */
   5532 	    if (h != NULL)
   5533 	      {
   5534 		/* If the sym is weak it may be overridden later, so
   5535 		   don't assume we know where a weak sym lives.  */
   5536 		if (h->root.type == bfd_link_hash_defined)
   5537 		  dest = h->root.u.def.section;
   5538 	      }
   5539 	    else
   5540 	      {
   5541 		Elf_Internal_Sym *isym;
   5542 
   5543 		isym = bfd_sym_from_r_symndx (&htab->sym_cache,
   5544 					      abfd, r_symndx);
   5545 		if (isym == NULL)
   5546 		  return FALSE;
   5547 
   5548 		dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
   5549 	      }
   5550 
   5551 	    if (dest != sec)
   5552 	      ppc64_elf_section_data (sec)->has_14bit_branch = 1;
   5553 	  }
   5554 	  /* Fall through.  */
   5555 
   5556 	case R_PPC64_REL24:
   5557 	  if (h != NULL && ifunc == NULL)
   5558 	    {
   5559 	      /* We may need a .plt entry if the function this reloc
   5560 		 refers to is in a shared lib.  */
   5561 	      if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
   5562 		return FALSE;
   5563 	      h->needs_plt = 1;
   5564 	      if (h->root.root.string[0] == '.'
   5565 		  && h->root.root.string[1] != '\0')
   5566 		((struct ppc_link_hash_entry *) h)->is_func = 1;
   5567 	      if (h == tga || h == dottga)
   5568 		sec->has_tls_reloc = 1;
   5569 	    }
   5570 	  break;
   5571 
   5572 	case R_PPC64_TPREL64:
   5573 	  tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
   5574 	  if (info->shared)
   5575 	    info->flags |= DF_STATIC_TLS;
   5576 	  goto dotlstoc;
   5577 
   5578 	case R_PPC64_DTPMOD64:
   5579 	  if (rel + 1 < rel_end
   5580 	      && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
   5581 	      && rel[1].r_offset == rel->r_offset + 8)
   5582 	    tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
   5583 	  else
   5584 	    tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
   5585 	  goto dotlstoc;
   5586 
   5587 	case R_PPC64_DTPREL64:
   5588 	  tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
   5589 	  if (rel != relocs
   5590 	      && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
   5591 	      && rel[-1].r_offset == rel->r_offset - 8)
   5592 	    /* This is the second reloc of a dtpmod, dtprel pair.
   5593 	       Don't mark with TLS_DTPREL.  */
   5594 	    goto dodyn;
   5595 
   5596 	dotlstoc:
   5597 	  sec->has_tls_reloc = 1;
   5598 	  if (h != NULL)
   5599 	    {
   5600 	      struct ppc_link_hash_entry *eh;
   5601 	      eh = (struct ppc_link_hash_entry *) h;
   5602 	      eh->tls_mask |= tls_type;
   5603 	    }
   5604 	  else
   5605 	    if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
   5606 					rel->r_addend, tls_type))
   5607 	      return FALSE;
   5608 
   5609 	  ppc64_sec = ppc64_elf_section_data (sec);
   5610 	  if (ppc64_sec->sec_type != sec_toc)
   5611 	    {
   5612 	      bfd_size_type amt;
   5613 
   5614 	      /* One extra to simplify get_tls_mask.  */
   5615 	      amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
   5616 	      ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
   5617 	      if (ppc64_sec->u.toc.symndx == NULL)
   5618 		return FALSE;
   5619 	      amt = sec->size * sizeof (bfd_vma) / 8;
   5620 	      ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
   5621 	      if (ppc64_sec->u.toc.add == NULL)
   5622 		return FALSE;
   5623 	      BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
   5624 	      ppc64_sec->sec_type = sec_toc;
   5625 	    }
   5626 	  BFD_ASSERT (rel->r_offset % 8 == 0);
   5627 	  ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
   5628 	  ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
   5629 
   5630 	  /* Mark the second slot of a GD or LD entry.
   5631 	     -1 to indicate GD and -2 to indicate LD.  */
   5632 	  if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
   5633 	    ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
   5634 	  else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
   5635 	    ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
   5636 	  goto dodyn;
   5637 
   5638 	case R_PPC64_TPREL16:
   5639 	case R_PPC64_TPREL16_LO:
   5640 	case R_PPC64_TPREL16_HI:
   5641 	case R_PPC64_TPREL16_HA:
   5642 	case R_PPC64_TPREL16_DS:
   5643 	case R_PPC64_TPREL16_LO_DS:
   5644 	case R_PPC64_TPREL16_HIGH:
   5645 	case R_PPC64_TPREL16_HIGHA:
   5646 	case R_PPC64_TPREL16_HIGHER:
   5647 	case R_PPC64_TPREL16_HIGHERA:
   5648 	case R_PPC64_TPREL16_HIGHEST:
   5649 	case R_PPC64_TPREL16_HIGHESTA:
   5650 	  if (info->shared)
   5651 	    {
   5652 	      info->flags |= DF_STATIC_TLS;
   5653 	      goto dodyn;
   5654 	    }
   5655 	  break;
   5656 
   5657 	case R_PPC64_ADDR64:
   5658 	  if (opd_sym_map != NULL
   5659 	      && rel + 1 < rel_end
   5660 	      && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
   5661 	    {
   5662 	      if (h != NULL)
   5663 		{
   5664 		  if (h->root.root.string[0] == '.'
   5665 		      && h->root.root.string[1] != 0
   5666 		      && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
   5667 		    ;
   5668 		  else
   5669 		    ((struct ppc_link_hash_entry *) h)->is_func = 1;
   5670 		}
   5671 	      else
   5672 		{
   5673 		  asection *s;
   5674 		  Elf_Internal_Sym *isym;
   5675 
   5676 		  isym = bfd_sym_from_r_symndx (&htab->sym_cache,
   5677 						abfd, r_symndx);
   5678 		  if (isym == NULL)
   5679 		    return FALSE;
   5680 
   5681 		  s = bfd_section_from_elf_index (abfd, isym->st_shndx);
   5682 		  if (s != NULL && s != sec)
   5683 		    opd_sym_map[rel->r_offset / 8] = s;
   5684 		}
   5685 	    }
   5686 	  /* Fall through.  */
   5687 
   5688 	case R_PPC64_ADDR16:
   5689 	case R_PPC64_ADDR16_DS:
   5690 	case R_PPC64_ADDR16_HA:
   5691 	case R_PPC64_ADDR16_HI:
   5692 	case R_PPC64_ADDR16_HIGH:
   5693 	case R_PPC64_ADDR16_HIGHA:
   5694 	case R_PPC64_ADDR16_HIGHER:
   5695 	case R_PPC64_ADDR16_HIGHERA:
   5696 	case R_PPC64_ADDR16_HIGHEST:
   5697 	case R_PPC64_ADDR16_HIGHESTA:
   5698 	case R_PPC64_ADDR16_LO:
   5699 	case R_PPC64_ADDR16_LO_DS:
   5700 	  if (h != NULL && !info->shared && abiversion (abfd) != 1
   5701 	      && rel->r_addend == 0)
   5702 	    {
   5703 	      /* We may need a .plt entry if this reloc refers to a
   5704 		 function in a shared lib.  */
   5705 	      if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
   5706 		return FALSE;
   5707 	      h->pointer_equality_needed = 1;
   5708 	    }
   5709 	  /* Fall through.  */
   5710 
   5711 	case R_PPC64_REL30:
   5712 	case R_PPC64_REL32:
   5713 	case R_PPC64_REL64:
   5714 	case R_PPC64_ADDR14:
   5715 	case R_PPC64_ADDR14_BRNTAKEN:
   5716 	case R_PPC64_ADDR14_BRTAKEN:
   5717 	case R_PPC64_ADDR24:
   5718 	case R_PPC64_ADDR32:
   5719 	case R_PPC64_UADDR16:
   5720 	case R_PPC64_UADDR32:
   5721 	case R_PPC64_UADDR64:
   5722 	case R_PPC64_TOC:
   5723 	  if (h != NULL && !info->shared)
   5724 	    /* We may need a copy reloc.  */
   5725 	    h->non_got_ref = 1;
   5726 
   5727 	  /* Don't propagate .opd relocs.  */
   5728 	  if (NO_OPD_RELOCS && opd_sym_map != NULL)
   5729 	    break;
   5730 
   5731 	  /* If we are creating a shared library, and this is a reloc
   5732 	     against a global symbol, or a non PC relative reloc
   5733 	     against a local symbol, then we need to copy the reloc
   5734 	     into the shared library.  However, if we are linking with
   5735 	     -Bsymbolic, we do not need to copy a reloc against a
   5736 	     global symbol which is defined in an object we are
   5737 	     including in the link (i.e., DEF_REGULAR is set).  At
   5738 	     this point we have not seen all the input files, so it is
   5739 	     possible that DEF_REGULAR is not set now but will be set
   5740 	     later (it is never cleared).  In case of a weak definition,
   5741 	     DEF_REGULAR may be cleared later by a strong definition in
   5742 	     a shared library.  We account for that possibility below by
   5743 	     storing information in the dyn_relocs field of the hash
   5744 	     table entry.  A similar situation occurs when creating
   5745 	     shared libraries and symbol visibility changes render the
   5746 	     symbol local.
   5747 
   5748 	     If on the other hand, we are creating an executable, we
   5749 	     may need to keep relocations for symbols satisfied by a
   5750 	     dynamic library if we manage to avoid copy relocs for the
   5751 	     symbol.  */
   5752 	dodyn:
   5753 	  if ((info->shared
   5754 	       && (must_be_dyn_reloc (info, r_type)
   5755 		   || (h != NULL
   5756 		       && (!SYMBOLIC_BIND (info, h)
   5757 			   || h->root.type == bfd_link_hash_defweak
   5758 			   || !h->def_regular))))
   5759 	      || (ELIMINATE_COPY_RELOCS
   5760 		  && !info->shared
   5761 		  && h != NULL
   5762 		  && (h->root.type == bfd_link_hash_defweak
   5763 		      || !h->def_regular))
   5764 	      || (!info->shared
   5765 		  && ifunc != NULL))
   5766 	    {
   5767 	      /* We must copy these reloc types into the output file.
   5768 		 Create a reloc section in dynobj and make room for
   5769 		 this reloc.  */
   5770 	      if (sreloc == NULL)
   5771 		{
   5772 		  sreloc = _bfd_elf_make_dynamic_reloc_section
   5773 		    (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
   5774 
   5775 		  if (sreloc == NULL)
   5776 		    return FALSE;
   5777 		}
   5778 
   5779 	      /* If this is a global symbol, we count the number of
   5780 		 relocations we need for this symbol.  */
   5781 	      if (h != NULL)
   5782 		{
   5783 		  struct elf_dyn_relocs *p;
   5784 		  struct elf_dyn_relocs **head;
   5785 
   5786 		  head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
   5787 		  p = *head;
   5788 		  if (p == NULL || p->sec != sec)
   5789 		    {
   5790 		      p = bfd_alloc (htab->elf.dynobj, sizeof *p);
   5791 		      if (p == NULL)
   5792 			return FALSE;
   5793 		      p->next = *head;
   5794 		      *head = p;
   5795 		      p->sec = sec;
   5796 		      p->count = 0;
   5797 		      p->pc_count = 0;
   5798 		    }
   5799 		  p->count += 1;
   5800 		  if (!must_be_dyn_reloc (info, r_type))
   5801 		    p->pc_count += 1;
   5802 		}
   5803 	      else
   5804 		{
   5805 		  /* Track dynamic relocs needed for local syms too.
   5806 		     We really need local syms available to do this
   5807 		     easily.  Oh well.  */
   5808 		  struct ppc_dyn_relocs *p;
   5809 		  struct ppc_dyn_relocs **head;
   5810 		  bfd_boolean is_ifunc;
   5811 		  asection *s;
   5812 		  void *vpp;
   5813 		  Elf_Internal_Sym *isym;
   5814 
   5815 		  isym = bfd_sym_from_r_symndx (&htab->sym_cache,
   5816 						abfd, r_symndx);
   5817 		  if (isym == NULL)
   5818 		    return FALSE;
   5819 
   5820 		  s = bfd_section_from_elf_index (abfd, isym->st_shndx);
   5821 		  if (s == NULL)
   5822 		    s = sec;
   5823 
   5824 		  vpp = &elf_section_data (s)->local_dynrel;
   5825 		  head = (struct ppc_dyn_relocs **) vpp;
   5826 		  is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
   5827 		  p = *head;
   5828 		  if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
   5829 		    p = p->next;
   5830 		  if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
   5831 		    {
   5832 		      p = bfd_alloc (htab->elf.dynobj, sizeof *p);
   5833 		      if (p == NULL)
   5834 			return FALSE;
   5835 		      p->next = *head;
   5836 		      *head = p;
   5837 		      p->sec = sec;
   5838 		      p->ifunc = is_ifunc;
   5839 		      p->count = 0;
   5840 		    }
   5841 		  p->count += 1;
   5842 		}
   5843 	    }
   5844 	  break;
   5845 
   5846 	default:
   5847 	  break;
   5848 	}
   5849     }
   5850 
   5851   return TRUE;
   5852 }
   5853 
   5854 /* Merge backend specific data from an object file to the output
   5855    object file when linking.  */
   5856 
   5857 static bfd_boolean
   5858 ppc64_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
   5859 {
   5860   unsigned long iflags, oflags;
   5861 
   5862   if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
   5863     return TRUE;
   5864 
   5865   if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
   5866     return TRUE;
   5867 
   5868   if (!_bfd_generic_verify_endian_match (ibfd, obfd))
   5869     return FALSE;
   5870 
   5871   iflags = elf_elfheader (ibfd)->e_flags;
   5872   oflags = elf_elfheader (obfd)->e_flags;
   5873 
   5874   if (iflags & ~EF_PPC64_ABI)
   5875     {
   5876       (*_bfd_error_handler)
   5877 	(_("%B uses unknown e_flags 0x%lx"), ibfd, iflags);
   5878       bfd_set_error (bfd_error_bad_value);
   5879       return FALSE;
   5880     }
   5881   else if (iflags != oflags && iflags != 0)
   5882     {
   5883       (*_bfd_error_handler)
   5884 	(_("%B: ABI version %ld is not compatible with ABI version %ld output"),
   5885 	 ibfd, iflags, oflags);
   5886       bfd_set_error (bfd_error_bad_value);
   5887       return FALSE;
   5888     }
   5889 
   5890   /* Merge Tag_compatibility attributes and any common GNU ones.  */
   5891   _bfd_elf_merge_object_attributes (ibfd, obfd);
   5892 
   5893   return TRUE;
   5894 }
   5895 
   5896 static bfd_boolean
   5897 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
   5898 {
   5899   /* Print normal ELF private data.  */
   5900   _bfd_elf_print_private_bfd_data (abfd, ptr);
   5901 
   5902   if (elf_elfheader (abfd)->e_flags != 0)
   5903     {
   5904       FILE *file = ptr;
   5905 
   5906       /* xgettext:c-format */
   5907       fprintf (file, _("private flags = 0x%lx:"),
   5908 	       elf_elfheader (abfd)->e_flags);
   5909 
   5910       if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
   5911 	fprintf (file, _(" [abiv%ld]"),
   5912 		 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
   5913       fputc ('\n', file);
   5914     }
   5915 
   5916   return TRUE;
   5917 }
   5918 
   5919 /* OFFSET in OPD_SEC specifies a function descriptor.  Return the address
   5920    of the code entry point, and its section, which must be in the same
   5921    object as OPD_SEC.  Returns (bfd_vma) -1 on error.  */
   5922 
   5923 static bfd_vma
   5924 opd_entry_value (asection *opd_sec,
   5925 		 bfd_vma offset,
   5926 		 asection **code_sec,
   5927 		 bfd_vma *code_off,
   5928 		 bfd_boolean in_code_sec)
   5929 {
   5930   bfd *opd_bfd = opd_sec->owner;
   5931   Elf_Internal_Rela *relocs;
   5932   Elf_Internal_Rela *lo, *hi, *look;
   5933   bfd_vma val;
   5934 
   5935   /* No relocs implies we are linking a --just-symbols object, or looking
   5936      at a final linked executable with addr2line or somesuch.  */
   5937   if (opd_sec->reloc_count == 0)
   5938     {
   5939       bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
   5940 
   5941       if (contents == NULL)
   5942 	{
   5943 	  if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
   5944 	    return (bfd_vma) -1;
   5945 	  ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
   5946 	}
   5947 
   5948       val = bfd_get_64 (opd_bfd, contents + offset);
   5949       if (code_sec != NULL)
   5950 	{
   5951 	  asection *sec, *likely = NULL;
   5952 
   5953 	  if (in_code_sec)
   5954 	    {
   5955 	      sec = *code_sec;
   5956 	      if (sec->vma <= val
   5957 		  && val < sec->vma + sec->size)
   5958 		likely = sec;
   5959 	      else
   5960 		val = -1;
   5961 	    }
   5962 	  else
   5963 	    for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
   5964 	      if (sec->vma <= val
   5965 		  && (sec->flags & SEC_LOAD) != 0
   5966 		  && (sec->flags & SEC_ALLOC) != 0)
   5967 		likely = sec;
   5968 	  if (likely != NULL)
   5969 	    {
   5970 	      *code_sec = likely;
   5971 	      if (code_off != NULL)
   5972 		*code_off = val - likely->vma;
   5973 	    }
   5974 	}
   5975       return val;
   5976     }
   5977 
   5978   BFD_ASSERT (is_ppc64_elf (opd_bfd));
   5979 
   5980   relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
   5981   if (relocs == NULL)
   5982     relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
   5983 
   5984   /* Go find the opd reloc at the sym address.  */
   5985   lo = relocs;
   5986   BFD_ASSERT (lo != NULL);
   5987   hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
   5988   val = (bfd_vma) -1;
   5989   while (lo < hi)
   5990     {
   5991       look = lo + (hi - lo) / 2;
   5992       if (look->r_offset < offset)
   5993 	lo = look + 1;
   5994       else if (look->r_offset > offset)
   5995 	hi = look;
   5996       else
   5997 	{
   5998 	  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
   5999 
   6000 	  if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
   6001 	      && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
   6002 	    {
   6003 	      unsigned long symndx = ELF64_R_SYM (look->r_info);
   6004 	      asection *sec = NULL;
   6005 
   6006 	      if (symndx >= symtab_hdr->sh_info
   6007 		  && elf_sym_hashes (opd_bfd) != NULL)
   6008 		{
   6009 		  struct elf_link_hash_entry **sym_hashes;
   6010 		  struct elf_link_hash_entry *rh;
   6011 
   6012 		  sym_hashes = elf_sym_hashes (opd_bfd);
   6013 		  rh = sym_hashes[symndx - symtab_hdr->sh_info];
   6014 		  if (rh != NULL)
   6015 		    {
   6016 		      rh = elf_follow_link (rh);
   6017 		      BFD_ASSERT (rh->root.type == bfd_link_hash_defined
   6018 				  || rh->root.type == bfd_link_hash_defweak);
   6019 		      val = rh->root.u.def.value;
   6020 		      sec = rh->root.u.def.section;
   6021 		      if (sec->owner != opd_bfd)
   6022 			{
   6023 			  sec = NULL;
   6024 			  val = (bfd_vma) -1;
   6025 			}
   6026 		    }
   6027 		}
   6028 
   6029 	      if (sec == NULL)
   6030 		{
   6031 		  Elf_Internal_Sym *sym;
   6032 
   6033 		  if (symndx < symtab_hdr->sh_info)
   6034 		    {
   6035 		      sym = (Elf_Internal_Sym *) symtab_hdr->contents;
   6036 		      if (sym == NULL)
   6037 			{
   6038 			  size_t symcnt = symtab_hdr->sh_info;
   6039 			  sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
   6040 						      symcnt, 0,
   6041 						      NULL, NULL, NULL);
   6042 			  if (sym == NULL)
   6043 			    break;
   6044 			  symtab_hdr->contents = (bfd_byte *) sym;
   6045 			}
   6046 		      sym += symndx;
   6047 		    }
   6048 		  else
   6049 		    {
   6050 		      sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
   6051 						  1, symndx,
   6052 						  NULL, NULL, NULL);
   6053 		      if (sym == NULL)
   6054 			break;
   6055 		    }
   6056 		  sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
   6057 		  if (sec == NULL)
   6058 		    break;
   6059 		  BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
   6060 		  val = sym->st_value;
   6061 		}
   6062 
   6063 	      val += look->r_addend;
   6064 	      if (code_off != NULL)
   6065 		*code_off = val;
   6066 	      if (code_sec != NULL)
   6067 		{
   6068 		  if (in_code_sec && *code_sec != sec)
   6069 		    return -1;
   6070 		  else
   6071 		    *code_sec = sec;
   6072 		}
   6073 	      if (sec->output_section != NULL)
   6074 		val += sec->output_section->vma + sec->output_offset;
   6075 	    }
   6076 	  break;
   6077 	}
   6078     }
   6079 
   6080   return val;
   6081 }
   6082 
   6083 /* If the ELF symbol SYM might be a function in SEC, return the
   6084    function size and set *CODE_OFF to the function's entry point,
   6085    otherwise return zero.  */
   6086 
   6087 static bfd_size_type
   6088 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
   6089 			      bfd_vma *code_off)
   6090 {
   6091   bfd_size_type size;
   6092 
   6093   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
   6094 		     | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
   6095     return 0;
   6096 
   6097   size = 0;
   6098   if (!(sym->flags & BSF_SYNTHETIC))
   6099     size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
   6100 
   6101   if (strcmp (sym->section->name, ".opd") == 0)
   6102     {
   6103       if (opd_entry_value (sym->section, sym->value,
   6104 			   &sec, code_off, TRUE) == (bfd_vma) -1)
   6105 	return 0;
   6106       /* An old ABI binary with dot-syms has a size of 24 on the .opd
   6107 	 symbol.  This size has nothing to do with the code size of the
   6108 	 function, which is what we're supposed to return, but the
   6109 	 code size isn't available without looking up the dot-sym.
   6110 	 However, doing that would be a waste of time particularly
   6111 	 since elf_find_function will look at the dot-sym anyway.
   6112 	 Now, elf_find_function will keep the largest size of any
   6113 	 function sym found at the code address of interest, so return
   6114 	 1 here to avoid it incorrectly caching a larger function size
   6115 	 for a small function.  This does mean we return the wrong
   6116 	 size for a new-ABI function of size 24, but all that does is
   6117 	 disable caching for such functions.  */
   6118       if (size == 24)
   6119 	size = 1;
   6120     }
   6121   else
   6122     {
   6123       if (sym->section != sec)
   6124 	return 0;
   6125       *code_off = sym->value;
   6126     }
   6127   if (size == 0)
   6128     size = 1;
   6129   return size;
   6130 }
   6131 
   6132 /* Return true if symbol is defined in a regular object file.  */
   6133 
   6134 static bfd_boolean
   6135 is_static_defined (struct elf_link_hash_entry *h)
   6136 {
   6137   return ((h->root.type == bfd_link_hash_defined
   6138 	   || h->root.type == bfd_link_hash_defweak)
   6139 	  && h->root.u.def.section != NULL
   6140 	  && h->root.u.def.section->output_section != NULL);
   6141 }
   6142 
   6143 /* If FDH is a function descriptor symbol, return the associated code
   6144    entry symbol if it is defined.  Return NULL otherwise.  */
   6145 
   6146 static struct ppc_link_hash_entry *
   6147 defined_code_entry (struct ppc_link_hash_entry *fdh)
   6148 {
   6149   if (fdh->is_func_descriptor)
   6150     {
   6151       struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
   6152       if (fh->elf.root.type == bfd_link_hash_defined
   6153 	  || fh->elf.root.type == bfd_link_hash_defweak)
   6154 	return fh;
   6155     }
   6156   return NULL;
   6157 }
   6158 
   6159 /* If FH is a function code entry symbol, return the associated
   6160    function descriptor symbol if it is defined.  Return NULL otherwise.  */
   6161 
   6162 static struct ppc_link_hash_entry *
   6163 defined_func_desc (struct ppc_link_hash_entry *fh)
   6164 {
   6165   if (fh->oh != NULL
   6166       && fh->oh->is_func_descriptor)
   6167     {
   6168       struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
   6169       if (fdh->elf.root.type == bfd_link_hash_defined
   6170 	  || fdh->elf.root.type == bfd_link_hash_defweak)
   6171 	return fdh;
   6172     }
   6173   return NULL;
   6174 }
   6175 
   6176 /* Mark all our entry sym sections, both opd and code section.  */
   6177 
   6178 static void
   6179 ppc64_elf_gc_keep (struct bfd_link_info *info)
   6180 {
   6181   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   6182   struct bfd_sym_chain *sym;
   6183 
   6184   if (htab == NULL)
   6185     return;
   6186 
   6187   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
   6188     {
   6189       struct ppc_link_hash_entry *eh, *fh;
   6190       asection *sec;
   6191 
   6192       eh = (struct ppc_link_hash_entry *)
   6193 	elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
   6194       if (eh == NULL)
   6195 	continue;
   6196       if (eh->elf.root.type != bfd_link_hash_defined
   6197 	  && eh->elf.root.type != bfd_link_hash_defweak)
   6198 	continue;
   6199 
   6200       fh = defined_code_entry (eh);
   6201       if (fh != NULL)
   6202 	{
   6203 	  sec = fh->elf.root.u.def.section;
   6204 	  sec->flags |= SEC_KEEP;
   6205 	}
   6206       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
   6207 	       && opd_entry_value (eh->elf.root.u.def.section,
   6208 				   eh->elf.root.u.def.value,
   6209 				   &sec, NULL, FALSE) != (bfd_vma) -1)
   6210 	sec->flags |= SEC_KEEP;
   6211 
   6212       sec = eh->elf.root.u.def.section;
   6213       sec->flags |= SEC_KEEP;
   6214     }
   6215 }
   6216 
   6217 /* Mark sections containing dynamically referenced symbols.  When
   6218    building shared libraries, we must assume that any visible symbol is
   6219    referenced.  */
   6220 
   6221 static bfd_boolean
   6222 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
   6223 {
   6224   struct bfd_link_info *info = (struct bfd_link_info *) inf;
   6225   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
   6226   struct ppc_link_hash_entry *fdh;
   6227   struct bfd_elf_dynamic_list *d = info->dynamic_list;
   6228 
   6229   /* Dynamic linking info is on the func descriptor sym.  */
   6230   fdh = defined_func_desc (eh);
   6231   if (fdh != NULL)
   6232     eh = fdh;
   6233 
   6234   if ((eh->elf.root.type == bfd_link_hash_defined
   6235        || eh->elf.root.type == bfd_link_hash_defweak)
   6236       && (eh->elf.ref_dynamic
   6237 	  || (eh->elf.def_regular
   6238 	      && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
   6239 	      && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
   6240 	      && (!info->executable
   6241 		  || info->export_dynamic
   6242 		  || (eh->elf.dynamic
   6243 		      && d != NULL
   6244 		      && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
   6245 	      && (strchr (eh->elf.root.root.string, ELF_VER_CHR) != NULL
   6246 		  || !bfd_hide_sym_by_version (info->version_info,
   6247 					       eh->elf.root.root.string)))))
   6248     {
   6249       asection *code_sec;
   6250       struct ppc_link_hash_entry *fh;
   6251 
   6252       eh->elf.root.u.def.section->flags |= SEC_KEEP;
   6253 
   6254       /* Function descriptor syms cause the associated
   6255 	 function code sym section to be marked.  */
   6256       fh = defined_code_entry (eh);
   6257       if (fh != NULL)
   6258 	{
   6259 	  code_sec = fh->elf.root.u.def.section;
   6260 	  code_sec->flags |= SEC_KEEP;
   6261 	}
   6262       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
   6263 	       && opd_entry_value (eh->elf.root.u.def.section,
   6264 				   eh->elf.root.u.def.value,
   6265 				   &code_sec, NULL, FALSE) != (bfd_vma) -1)
   6266 	code_sec->flags |= SEC_KEEP;
   6267     }
   6268 
   6269   return TRUE;
   6270 }
   6271 
   6272 /* Return the section that should be marked against GC for a given
   6273    relocation.  */
   6274 
   6275 static asection *
   6276 ppc64_elf_gc_mark_hook (asection *sec,
   6277 			struct bfd_link_info *info,
   6278 			Elf_Internal_Rela *rel,
   6279 			struct elf_link_hash_entry *h,
   6280 			Elf_Internal_Sym *sym)
   6281 {
   6282   asection *rsec;
   6283 
   6284   /* Syms return NULL if we're marking .opd, so we avoid marking all
   6285      function sections, as all functions are referenced in .opd.  */
   6286   rsec = NULL;
   6287   if (get_opd_info (sec) != NULL)
   6288     return rsec;
   6289 
   6290   if (h != NULL)
   6291     {
   6292       enum elf_ppc64_reloc_type r_type;
   6293       struct ppc_link_hash_entry *eh, *fh, *fdh;
   6294 
   6295       r_type = ELF64_R_TYPE (rel->r_info);
   6296       switch (r_type)
   6297 	{
   6298 	case R_PPC64_GNU_VTINHERIT:
   6299 	case R_PPC64_GNU_VTENTRY:
   6300 	  break;
   6301 
   6302 	default:
   6303 	  switch (h->root.type)
   6304 	    {
   6305 	    case bfd_link_hash_defined:
   6306 	    case bfd_link_hash_defweak:
   6307 	      eh = (struct ppc_link_hash_entry *) h;
   6308 	      fdh = defined_func_desc (eh);
   6309 	      if (fdh != NULL)
   6310 		eh = fdh;
   6311 
   6312 	      /* Function descriptor syms cause the associated
   6313 		 function code sym section to be marked.  */
   6314 	      fh = defined_code_entry (eh);
   6315 	      if (fh != NULL)
   6316 		{
   6317 		  /* They also mark their opd section.  */
   6318 		  eh->elf.root.u.def.section->gc_mark = 1;
   6319 
   6320 		  rsec = fh->elf.root.u.def.section;
   6321 		}
   6322 	      else if (get_opd_info (eh->elf.root.u.def.section) != NULL
   6323 		       && opd_entry_value (eh->elf.root.u.def.section,
   6324 					   eh->elf.root.u.def.value,
   6325 					   &rsec, NULL, FALSE) != (bfd_vma) -1)
   6326 		eh->elf.root.u.def.section->gc_mark = 1;
   6327 	      else
   6328 		rsec = h->root.u.def.section;
   6329 	      break;
   6330 
   6331 	    case bfd_link_hash_common:
   6332 	      rsec = h->root.u.c.p->section;
   6333 	      break;
   6334 
   6335 	    default:
   6336 	      return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
   6337 	    }
   6338 	}
   6339     }
   6340   else
   6341     {
   6342       struct _opd_sec_data *opd;
   6343 
   6344       rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
   6345       opd = get_opd_info (rsec);
   6346       if (opd != NULL && opd->func_sec != NULL)
   6347 	{
   6348 	  rsec->gc_mark = 1;
   6349 
   6350 	  rsec = opd->func_sec[(sym->st_value + rel->r_addend) / 8];
   6351 	}
   6352     }
   6353 
   6354   return rsec;
   6355 }
   6356 
   6357 /* Update the .got, .plt. and dynamic reloc reference counts for the
   6358    section being removed.  */
   6359 
   6360 static bfd_boolean
   6361 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
   6362 			 asection *sec, const Elf_Internal_Rela *relocs)
   6363 {
   6364   struct ppc_link_hash_table *htab;
   6365   Elf_Internal_Shdr *symtab_hdr;
   6366   struct elf_link_hash_entry **sym_hashes;
   6367   struct got_entry **local_got_ents;
   6368   const Elf_Internal_Rela *rel, *relend;
   6369 
   6370   if (info->relocatable)
   6371     return TRUE;
   6372 
   6373   if ((sec->flags & SEC_ALLOC) == 0)
   6374     return TRUE;
   6375 
   6376   elf_section_data (sec)->local_dynrel = NULL;
   6377 
   6378   htab = ppc_hash_table (info);
   6379   if (htab == NULL)
   6380     return FALSE;
   6381 
   6382   symtab_hdr = &elf_symtab_hdr (abfd);
   6383   sym_hashes = elf_sym_hashes (abfd);
   6384   local_got_ents = elf_local_got_ents (abfd);
   6385 
   6386   relend = relocs + sec->reloc_count;
   6387   for (rel = relocs; rel < relend; rel++)
   6388     {
   6389       unsigned long r_symndx;
   6390       enum elf_ppc64_reloc_type r_type;
   6391       struct elf_link_hash_entry *h = NULL;
   6392       unsigned char tls_type = 0;
   6393 
   6394       r_symndx = ELF64_R_SYM (rel->r_info);
   6395       r_type = ELF64_R_TYPE (rel->r_info);
   6396       if (r_symndx >= symtab_hdr->sh_info)
   6397 	{
   6398 	  struct ppc_link_hash_entry *eh;
   6399 	  struct elf_dyn_relocs **pp;
   6400 	  struct elf_dyn_relocs *p;
   6401 
   6402 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   6403 	  h = elf_follow_link (h);
   6404 	  eh = (struct ppc_link_hash_entry *) h;
   6405 
   6406 	  for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
   6407 	    if (p->sec == sec)
   6408 	      {
   6409 		/* Everything must go for SEC.  */
   6410 		*pp = p->next;
   6411 		break;
   6412 	      }
   6413 	}
   6414 
   6415       if (is_branch_reloc (r_type))
   6416 	{
   6417 	  struct plt_entry **ifunc = NULL;
   6418 	  if (h != NULL)
   6419 	    {
   6420 	      if (h->type == STT_GNU_IFUNC)
   6421 		ifunc = &h->plt.plist;
   6422 	    }
   6423 	  else if (local_got_ents != NULL)
   6424 	    {
   6425 	      struct plt_entry **local_plt = (struct plt_entry **)
   6426 		(local_got_ents + symtab_hdr->sh_info);
   6427 	      unsigned char *local_got_tls_masks = (unsigned char *)
   6428 		(local_plt + symtab_hdr->sh_info);
   6429 	      if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
   6430 		ifunc = local_plt + r_symndx;
   6431 	    }
   6432 	  if (ifunc != NULL)
   6433 	    {
   6434 	      struct plt_entry *ent;
   6435 
   6436 	      for (ent = *ifunc; ent != NULL; ent = ent->next)
   6437 		if (ent->addend == rel->r_addend)
   6438 		  break;
   6439 	      if (ent == NULL)
   6440 		abort ();
   6441 	      if (ent->plt.refcount > 0)
   6442 		ent->plt.refcount -= 1;
   6443 	      continue;
   6444 	    }
   6445 	}
   6446 
   6447       switch (r_type)
   6448 	{
   6449 	case R_PPC64_GOT_TLSLD16:
   6450 	case R_PPC64_GOT_TLSLD16_LO:
   6451 	case R_PPC64_GOT_TLSLD16_HI:
   6452 	case R_PPC64_GOT_TLSLD16_HA:
   6453 	  tls_type = TLS_TLS | TLS_LD;
   6454 	  goto dogot;
   6455 
   6456 	case R_PPC64_GOT_TLSGD16:
   6457 	case R_PPC64_GOT_TLSGD16_LO:
   6458 	case R_PPC64_GOT_TLSGD16_HI:
   6459 	case R_PPC64_GOT_TLSGD16_HA:
   6460 	  tls_type = TLS_TLS | TLS_GD;
   6461 	  goto dogot;
   6462 
   6463 	case R_PPC64_GOT_TPREL16_DS:
   6464 	case R_PPC64_GOT_TPREL16_LO_DS:
   6465 	case R_PPC64_GOT_TPREL16_HI:
   6466 	case R_PPC64_GOT_TPREL16_HA:
   6467 	  tls_type = TLS_TLS | TLS_TPREL;
   6468 	  goto dogot;
   6469 
   6470 	case R_PPC64_GOT_DTPREL16_DS:
   6471 	case R_PPC64_GOT_DTPREL16_LO_DS:
   6472 	case R_PPC64_GOT_DTPREL16_HI:
   6473 	case R_PPC64_GOT_DTPREL16_HA:
   6474 	  tls_type = TLS_TLS | TLS_DTPREL;
   6475 	  goto dogot;
   6476 
   6477 	case R_PPC64_GOT16:
   6478 	case R_PPC64_GOT16_DS:
   6479 	case R_PPC64_GOT16_HA:
   6480 	case R_PPC64_GOT16_HI:
   6481 	case R_PPC64_GOT16_LO:
   6482 	case R_PPC64_GOT16_LO_DS:
   6483 	dogot:
   6484 	  {
   6485 	    struct got_entry *ent;
   6486 
   6487 	    if (h != NULL)
   6488 	      ent = h->got.glist;
   6489 	    else
   6490 	      ent = local_got_ents[r_symndx];
   6491 
   6492 	    for (; ent != NULL; ent = ent->next)
   6493 	      if (ent->addend == rel->r_addend
   6494 		  && ent->owner == abfd
   6495 		  && ent->tls_type == tls_type)
   6496 		break;
   6497 	    if (ent == NULL)
   6498 	      abort ();
   6499 	    if (ent->got.refcount > 0)
   6500 	      ent->got.refcount -= 1;
   6501 	  }
   6502 	  break;
   6503 
   6504 	case R_PPC64_PLT16_HA:
   6505 	case R_PPC64_PLT16_HI:
   6506 	case R_PPC64_PLT16_LO:
   6507 	case R_PPC64_PLT32:
   6508 	case R_PPC64_PLT64:
   6509 	case R_PPC64_REL14:
   6510 	case R_PPC64_REL14_BRNTAKEN:
   6511 	case R_PPC64_REL14_BRTAKEN:
   6512 	case R_PPC64_REL24:
   6513 	  if (h != NULL)
   6514 	    {
   6515 	      struct plt_entry *ent;
   6516 
   6517 	      for (ent = h->plt.plist; ent != NULL; ent = ent->next)
   6518 		if (ent->addend == rel->r_addend)
   6519 		  break;
   6520 	      if (ent != NULL && ent->plt.refcount > 0)
   6521 		ent->plt.refcount -= 1;
   6522 	    }
   6523 	  break;
   6524 
   6525 	default:
   6526 	  break;
   6527 	}
   6528     }
   6529   return TRUE;
   6530 }
   6531 
   6532 /* The maximum size of .sfpr.  */
   6533 #define SFPR_MAX (218*4)
   6534 
   6535 struct sfpr_def_parms
   6536 {
   6537   const char name[12];
   6538   unsigned char lo, hi;
   6539   bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
   6540   bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
   6541 };
   6542 
   6543 /* Auto-generate _save*, _rest* functions in .sfpr.  */
   6544 
   6545 static bfd_boolean
   6546 sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
   6547 {
   6548   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   6549   unsigned int i;
   6550   size_t len = strlen (parm->name);
   6551   bfd_boolean writing = FALSE;
   6552   char sym[16];
   6553 
   6554   if (htab == NULL)
   6555     return FALSE;
   6556 
   6557   memcpy (sym, parm->name, len);
   6558   sym[len + 2] = 0;
   6559 
   6560   for (i = parm->lo; i <= parm->hi; i++)
   6561     {
   6562       struct elf_link_hash_entry *h;
   6563 
   6564       sym[len + 0] = i / 10 + '0';
   6565       sym[len + 1] = i % 10 + '0';
   6566       h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
   6567       if (h != NULL
   6568 	  && !h->def_regular)
   6569 	{
   6570 	  h->root.type = bfd_link_hash_defined;
   6571 	  h->root.u.def.section = htab->sfpr;
   6572 	  h->root.u.def.value = htab->sfpr->size;
   6573 	  h->type = STT_FUNC;
   6574 	  h->def_regular = 1;
   6575 	  _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
   6576 	  writing = TRUE;
   6577 	  if (htab->sfpr->contents == NULL)
   6578 	    {
   6579 	      htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
   6580 	      if (htab->sfpr->contents == NULL)
   6581 		return FALSE;
   6582 	    }
   6583 	}
   6584       if (writing)
   6585 	{
   6586 	  bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
   6587 	  if (i != parm->hi)
   6588 	    p = (*parm->write_ent) (htab->elf.dynobj, p, i);
   6589 	  else
   6590 	    p = (*parm->write_tail) (htab->elf.dynobj, p, i);
   6591 	  htab->sfpr->size = p - htab->sfpr->contents;
   6592 	}
   6593     }
   6594 
   6595   return TRUE;
   6596 }
   6597 
   6598 static bfd_byte *
   6599 savegpr0 (bfd *abfd, bfd_byte *p, int r)
   6600 {
   6601   bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6602   return p + 4;
   6603 }
   6604 
   6605 static bfd_byte *
   6606 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
   6607 {
   6608   p = savegpr0 (abfd, p, r);
   6609   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
   6610   p = p + 4;
   6611   bfd_put_32 (abfd, BLR, p);
   6612   return p + 4;
   6613 }
   6614 
   6615 static bfd_byte *
   6616 restgpr0 (bfd *abfd, bfd_byte *p, int r)
   6617 {
   6618   bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6619   return p + 4;
   6620 }
   6621 
   6622 static bfd_byte *
   6623 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
   6624 {
   6625   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
   6626   p = p + 4;
   6627   p = restgpr0 (abfd, p, r);
   6628   bfd_put_32 (abfd, MTLR_R0, p);
   6629   p = p + 4;
   6630   if (r == 29)
   6631     {
   6632       p = restgpr0 (abfd, p, 30);
   6633       p = restgpr0 (abfd, p, 31);
   6634     }
   6635   bfd_put_32 (abfd, BLR, p);
   6636   return p + 4;
   6637 }
   6638 
   6639 static bfd_byte *
   6640 savegpr1 (bfd *abfd, bfd_byte *p, int r)
   6641 {
   6642   bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6643   return p + 4;
   6644 }
   6645 
   6646 static bfd_byte *
   6647 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
   6648 {
   6649   p = savegpr1 (abfd, p, r);
   6650   bfd_put_32 (abfd, BLR, p);
   6651   return p + 4;
   6652 }
   6653 
   6654 static bfd_byte *
   6655 restgpr1 (bfd *abfd, bfd_byte *p, int r)
   6656 {
   6657   bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6658   return p + 4;
   6659 }
   6660 
   6661 static bfd_byte *
   6662 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
   6663 {
   6664   p = restgpr1 (abfd, p, r);
   6665   bfd_put_32 (abfd, BLR, p);
   6666   return p + 4;
   6667 }
   6668 
   6669 static bfd_byte *
   6670 savefpr (bfd *abfd, bfd_byte *p, int r)
   6671 {
   6672   bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6673   return p + 4;
   6674 }
   6675 
   6676 static bfd_byte *
   6677 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
   6678 {
   6679   p = savefpr (abfd, p, r);
   6680   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
   6681   p = p + 4;
   6682   bfd_put_32 (abfd, BLR, p);
   6683   return p + 4;
   6684 }
   6685 
   6686 static bfd_byte *
   6687 restfpr (bfd *abfd, bfd_byte *p, int r)
   6688 {
   6689   bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6690   return p + 4;
   6691 }
   6692 
   6693 static bfd_byte *
   6694 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
   6695 {
   6696   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
   6697   p = p + 4;
   6698   p = restfpr (abfd, p, r);
   6699   bfd_put_32 (abfd, MTLR_R0, p);
   6700   p = p + 4;
   6701   if (r == 29)
   6702     {
   6703       p = restfpr (abfd, p, 30);
   6704       p = restfpr (abfd, p, 31);
   6705     }
   6706   bfd_put_32 (abfd, BLR, p);
   6707   return p + 4;
   6708 }
   6709 
   6710 static bfd_byte *
   6711 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
   6712 {
   6713   p = savefpr (abfd, p, r);
   6714   bfd_put_32 (abfd, BLR, p);
   6715   return p + 4;
   6716 }
   6717 
   6718 static bfd_byte *
   6719 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
   6720 {
   6721   p = restfpr (abfd, p, r);
   6722   bfd_put_32 (abfd, BLR, p);
   6723   return p + 4;
   6724 }
   6725 
   6726 static bfd_byte *
   6727 savevr (bfd *abfd, bfd_byte *p, int r)
   6728 {
   6729   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
   6730   p = p + 4;
   6731   bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
   6732   return p + 4;
   6733 }
   6734 
   6735 static bfd_byte *
   6736 savevr_tail (bfd *abfd, bfd_byte *p, int r)
   6737 {
   6738   p = savevr (abfd, p, r);
   6739   bfd_put_32 (abfd, BLR, p);
   6740   return p + 4;
   6741 }
   6742 
   6743 static bfd_byte *
   6744 restvr (bfd *abfd, bfd_byte *p, int r)
   6745 {
   6746   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
   6747   p = p + 4;
   6748   bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
   6749   return p + 4;
   6750 }
   6751 
   6752 static bfd_byte *
   6753 restvr_tail (bfd *abfd, bfd_byte *p, int r)
   6754 {
   6755   p = restvr (abfd, p, r);
   6756   bfd_put_32 (abfd, BLR, p);
   6757   return p + 4;
   6758 }
   6759 
   6760 /* Called via elf_link_hash_traverse to transfer dynamic linking
   6761    information on function code symbol entries to their corresponding
   6762    function descriptor symbol entries.  */
   6763 
   6764 static bfd_boolean
   6765 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
   6766 {
   6767   struct bfd_link_info *info;
   6768   struct ppc_link_hash_table *htab;
   6769   struct plt_entry *ent;
   6770   struct ppc_link_hash_entry *fh;
   6771   struct ppc_link_hash_entry *fdh;
   6772   bfd_boolean force_local;
   6773 
   6774   fh = (struct ppc_link_hash_entry *) h;
   6775   if (fh->elf.root.type == bfd_link_hash_indirect)
   6776     return TRUE;
   6777 
   6778   info = inf;
   6779   htab = ppc_hash_table (info);
   6780   if (htab == NULL)
   6781     return FALSE;
   6782 
   6783   /* Resolve undefined references to dot-symbols as the value
   6784      in the function descriptor, if we have one in a regular object.
   6785      This is to satisfy cases like ".quad .foo".  Calls to functions
   6786      in dynamic objects are handled elsewhere.  */
   6787   if (fh->elf.root.type == bfd_link_hash_undefweak
   6788       && fh->was_undefined
   6789       && (fdh = defined_func_desc (fh)) != NULL
   6790       && get_opd_info (fdh->elf.root.u.def.section) != NULL
   6791       && opd_entry_value (fdh->elf.root.u.def.section,
   6792 			  fdh->elf.root.u.def.value,
   6793 			  &fh->elf.root.u.def.section,
   6794 			  &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
   6795     {
   6796       fh->elf.root.type = fdh->elf.root.type;
   6797       fh->elf.forced_local = 1;
   6798       fh->elf.def_regular = fdh->elf.def_regular;
   6799       fh->elf.def_dynamic = fdh->elf.def_dynamic;
   6800     }
   6801 
   6802   /* If this is a function code symbol, transfer dynamic linking
   6803      information to the function descriptor symbol.  */
   6804   if (!fh->is_func)
   6805     return TRUE;
   6806 
   6807   for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
   6808     if (ent->plt.refcount > 0)
   6809       break;
   6810   if (ent == NULL
   6811       || fh->elf.root.root.string[0] != '.'
   6812       || fh->elf.root.root.string[1] == '\0')
   6813     return TRUE;
   6814 
   6815   /* Find the corresponding function descriptor symbol.  Create it
   6816      as undefined if necessary.  */
   6817 
   6818   fdh = lookup_fdh (fh, htab);
   6819   if (fdh == NULL
   6820       && !info->executable
   6821       && (fh->elf.root.type == bfd_link_hash_undefined
   6822 	  || fh->elf.root.type == bfd_link_hash_undefweak))
   6823     {
   6824       fdh = make_fdh (info, fh);
   6825       if (fdh == NULL)
   6826 	return FALSE;
   6827     }
   6828 
   6829   /* Fake function descriptors are made undefweak.  If the function
   6830      code symbol is strong undefined, make the fake sym the same.
   6831      If the function code symbol is defined, then force the fake
   6832      descriptor local;  We can't support overriding of symbols in a
   6833      shared library on a fake descriptor.  */
   6834 
   6835   if (fdh != NULL
   6836       && fdh->fake
   6837       && fdh->elf.root.type == bfd_link_hash_undefweak)
   6838     {
   6839       if (fh->elf.root.type == bfd_link_hash_undefined)
   6840 	{
   6841 	  fdh->elf.root.type = bfd_link_hash_undefined;
   6842 	  bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
   6843 	}
   6844       else if (fh->elf.root.type == bfd_link_hash_defined
   6845 	       || fh->elf.root.type == bfd_link_hash_defweak)
   6846 	{
   6847 	  _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
   6848 	}
   6849     }
   6850 
   6851   if (fdh != NULL
   6852       && !fdh->elf.forced_local
   6853       && (!info->executable
   6854 	  || fdh->elf.def_dynamic
   6855 	  || fdh->elf.ref_dynamic
   6856 	  || (fdh->elf.root.type == bfd_link_hash_undefweak
   6857 	      && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
   6858     {
   6859       if (fdh->elf.dynindx == -1)
   6860 	if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
   6861 	  return FALSE;
   6862       fdh->elf.ref_regular |= fh->elf.ref_regular;
   6863       fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
   6864       fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
   6865       fdh->elf.non_got_ref |= fh->elf.non_got_ref;
   6866       if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
   6867 	{
   6868 	  move_plt_plist (fh, fdh);
   6869 	  fdh->elf.needs_plt = 1;
   6870 	}
   6871       fdh->is_func_descriptor = 1;
   6872       fdh->oh = fh;
   6873       fh->oh = fdh;
   6874     }
   6875 
   6876   /* Now that the info is on the function descriptor, clear the
   6877      function code sym info.  Any function code syms for which we
   6878      don't have a definition in a regular file, we force local.
   6879      This prevents a shared library from exporting syms that have
   6880      been imported from another library.  Function code syms that
   6881      are really in the library we must leave global to prevent the
   6882      linker dragging in a definition from a static library.  */
   6883   force_local = (!fh->elf.def_regular
   6884 		 || fdh == NULL
   6885 		 || !fdh->elf.def_regular
   6886 		 || fdh->elf.forced_local);
   6887   _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
   6888 
   6889   return TRUE;
   6890 }
   6891 
   6892 /* Called near the start of bfd_elf_size_dynamic_sections.  We use
   6893    this hook to a) provide some gcc support functions, and b) transfer
   6894    dynamic linking information gathered so far on function code symbol
   6895    entries, to their corresponding function descriptor symbol entries.  */
   6896 
   6897 static bfd_boolean
   6898 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
   6899 			    struct bfd_link_info *info)
   6900 {
   6901   struct ppc_link_hash_table *htab;
   6902   unsigned int i;
   6903   static const struct sfpr_def_parms funcs[] =
   6904     {
   6905       { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
   6906       { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
   6907       { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
   6908       { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
   6909       { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
   6910       { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
   6911       { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
   6912       { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
   6913       { "._savef", 14, 31, savefpr, savefpr1_tail },
   6914       { "._restf", 14, 31, restfpr, restfpr1_tail },
   6915       { "_savevr_", 20, 31, savevr, savevr_tail },
   6916       { "_restvr_", 20, 31, restvr, restvr_tail }
   6917     };
   6918 
   6919   htab = ppc_hash_table (info);
   6920   if (htab == NULL)
   6921     return FALSE;
   6922 
   6923   if (!info->relocatable
   6924       && htab->elf.hgot != NULL)
   6925     {
   6926       _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
   6927       /* Make .TOC. defined so as to prevent it being made dynamic.
   6928 	 The wrong value here is fixed later in ppc64_elf_set_toc.  */
   6929       htab->elf.hgot->type = STT_OBJECT;
   6930       htab->elf.hgot->root.type = bfd_link_hash_defined;
   6931       htab->elf.hgot->root.u.def.value = 0;
   6932       htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
   6933       htab->elf.hgot->def_regular = 1;
   6934       htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
   6935 			       | STV_HIDDEN);
   6936     }
   6937 
   6938   if (htab->sfpr == NULL)
   6939     /* We don't have any relocs.  */
   6940     return TRUE;
   6941 
   6942   /* Provide any missing _save* and _rest* functions.  */
   6943   htab->sfpr->size = 0;
   6944   if (htab->params->save_restore_funcs)
   6945     for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
   6946       if (!sfpr_define (info, &funcs[i]))
   6947 	return FALSE;
   6948 
   6949   elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
   6950 
   6951   if (htab->sfpr->size == 0)
   6952     htab->sfpr->flags |= SEC_EXCLUDE;
   6953 
   6954   return TRUE;
   6955 }
   6956 
   6957 /* Return true if we have dynamic relocs that apply to read-only sections.  */
   6958 
   6959 static bfd_boolean
   6960 readonly_dynrelocs (struct elf_link_hash_entry *h)
   6961 {
   6962   struct ppc_link_hash_entry *eh;
   6963   struct elf_dyn_relocs *p;
   6964 
   6965   eh = (struct ppc_link_hash_entry *) h;
   6966   for (p = eh->dyn_relocs; p != NULL; p = p->next)
   6967     {
   6968       asection *s = p->sec->output_section;
   6969 
   6970       if (s != NULL && (s->flags & SEC_READONLY) != 0)
   6971 	return TRUE;
   6972     }
   6973   return FALSE;
   6974 }
   6975 
   6976 /* Adjust a symbol defined by a dynamic object and referenced by a
   6977    regular object.  The current definition is in some section of the
   6978    dynamic object, but we're not including those sections.  We have to
   6979    change the definition to something the rest of the link can
   6980    understand.  */
   6981 
   6982 static bfd_boolean
   6983 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
   6984 				 struct elf_link_hash_entry *h)
   6985 {
   6986   struct ppc_link_hash_table *htab;
   6987   asection *s;
   6988 
   6989   htab = ppc_hash_table (info);
   6990   if (htab == NULL)
   6991     return FALSE;
   6992 
   6993   /* Deal with function syms.  */
   6994   if (h->type == STT_FUNC
   6995       || h->type == STT_GNU_IFUNC
   6996       || h->needs_plt)
   6997     {
   6998       /* Clear procedure linkage table information for any symbol that
   6999 	 won't need a .plt entry.  */
   7000       struct plt_entry *ent;
   7001       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
   7002 	if (ent->plt.refcount > 0)
   7003 	  break;
   7004       if (ent == NULL
   7005 	  || (h->type != STT_GNU_IFUNC
   7006 	      && (SYMBOL_CALLS_LOCAL (info, h)
   7007 		  || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
   7008 		      && h->root.type == bfd_link_hash_undefweak))))
   7009 	{
   7010 	  h->plt.plist = NULL;
   7011 	  h->needs_plt = 0;
   7012 	  h->pointer_equality_needed = 0;
   7013 	}
   7014       else if (abiversion (info->output_bfd) == 2)
   7015 	{
   7016 	  /* Taking a function's address in a read/write section
   7017 	     doesn't require us to define the function symbol in the
   7018 	     executable on a global entry stub.  A dynamic reloc can
   7019 	     be used instead.  */
   7020 	  if (h->pointer_equality_needed
   7021 	      && h->type != STT_GNU_IFUNC
   7022 	      && !readonly_dynrelocs (h))
   7023 	    {
   7024 	      h->pointer_equality_needed = 0;
   7025 	      h->non_got_ref = 0;
   7026 	    }
   7027 
   7028 	  /* After adjust_dynamic_symbol, non_got_ref set in the
   7029 	     non-shared case means that we have allocated space in
   7030 	     .dynbss for the symbol and thus dyn_relocs for this
   7031 	     symbol should be discarded.
   7032 	     If we get here we know we are making a PLT entry for this
   7033 	     symbol, and in an executable we'd normally resolve
   7034 	     relocations against this symbol to the PLT entry.  Allow
   7035 	     dynamic relocs if the reference is weak, and the dynamic
   7036 	     relocs will not cause text relocation.  */
   7037 	  else if (!h->ref_regular_nonweak
   7038 		   && h->non_got_ref
   7039 		   && h->type != STT_GNU_IFUNC
   7040 		   && !readonly_dynrelocs (h))
   7041 	    h->non_got_ref = 0;
   7042 
   7043 	  /* If making a plt entry, then we don't need copy relocs.  */
   7044 	  return TRUE;
   7045 	}
   7046     }
   7047   else
   7048     h->plt.plist = NULL;
   7049 
   7050   /* If this is a weak symbol, and there is a real definition, the
   7051      processor independent code will have arranged for us to see the
   7052      real definition first, and we can just use the same value.  */
   7053   if (h->u.weakdef != NULL)
   7054     {
   7055       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
   7056 		  || h->u.weakdef->root.type == bfd_link_hash_defweak);
   7057       h->root.u.def.section = h->u.weakdef->root.u.def.section;
   7058       h->root.u.def.value = h->u.weakdef->root.u.def.value;
   7059       if (ELIMINATE_COPY_RELOCS)
   7060 	h->non_got_ref = h->u.weakdef->non_got_ref;
   7061       return TRUE;
   7062     }
   7063 
   7064   /* If we are creating a shared library, we must presume that the
   7065      only references to the symbol are via the global offset table.
   7066      For such cases we need not do anything here; the relocations will
   7067      be handled correctly by relocate_section.  */
   7068   if (info->shared)
   7069     return TRUE;
   7070 
   7071   /* If there are no references to this symbol that do not use the
   7072      GOT, we don't need to generate a copy reloc.  */
   7073   if (!h->non_got_ref)
   7074     return TRUE;
   7075 
   7076   /* Don't generate a copy reloc for symbols defined in the executable.  */
   7077   if (!h->def_dynamic || !h->ref_regular || h->def_regular)
   7078     return TRUE;
   7079 
   7080   /* If we didn't find any dynamic relocs in read-only sections, then
   7081      we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
   7082   if (ELIMINATE_COPY_RELOCS && !readonly_dynrelocs (h))
   7083     {
   7084       h->non_got_ref = 0;
   7085       return TRUE;
   7086     }
   7087 
   7088   if (h->plt.plist != NULL)
   7089     {
   7090       /* We should never get here, but unfortunately there are versions
   7091 	 of gcc out there that improperly (for this ABI) put initialized
   7092 	 function pointers, vtable refs and suchlike in read-only
   7093 	 sections.  Allow them to proceed, but warn that this might
   7094 	 break at runtime.  */
   7095       info->callbacks->einfo
   7096 	(_("%P: copy reloc against `%T' requires lazy plt linking; "
   7097 	   "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
   7098 	 h->root.root.string);
   7099     }
   7100 
   7101   /* This is a reference to a symbol defined by a dynamic object which
   7102      is not a function.  */
   7103 
   7104   /* We must allocate the symbol in our .dynbss section, which will
   7105      become part of the .bss section of the executable.  There will be
   7106      an entry for this symbol in the .dynsym section.  The dynamic
   7107      object will contain position independent code, so all references
   7108      from the dynamic object to this symbol will go through the global
   7109      offset table.  The dynamic linker will use the .dynsym entry to
   7110      determine the address it must put in the global offset table, so
   7111      both the dynamic object and the regular object will refer to the
   7112      same memory location for the variable.  */
   7113 
   7114   /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
   7115      to copy the initial value out of the dynamic object and into the
   7116      runtime process image.  We need to remember the offset into the
   7117      .rela.bss section we are going to use.  */
   7118   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
   7119     {
   7120       htab->relbss->size += sizeof (Elf64_External_Rela);
   7121       h->needs_copy = 1;
   7122     }
   7123 
   7124   s = htab->dynbss;
   7125 
   7126   return _bfd_elf_adjust_dynamic_copy (h, s);
   7127 }
   7128 
   7129 /* If given a function descriptor symbol, hide both the function code
   7130    sym and the descriptor.  */
   7131 static void
   7132 ppc64_elf_hide_symbol (struct bfd_link_info *info,
   7133 		       struct elf_link_hash_entry *h,
   7134 		       bfd_boolean force_local)
   7135 {
   7136   struct ppc_link_hash_entry *eh;
   7137   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
   7138 
   7139   eh = (struct ppc_link_hash_entry *) h;
   7140   if (eh->is_func_descriptor)
   7141     {
   7142       struct ppc_link_hash_entry *fh = eh->oh;
   7143 
   7144       if (fh == NULL)
   7145 	{
   7146 	  const char *p, *q;
   7147 	  struct ppc_link_hash_table *htab;
   7148 	  char save;
   7149 
   7150 	  /* We aren't supposed to use alloca in BFD because on
   7151 	     systems which do not have alloca the version in libiberty
   7152 	     calls xmalloc, which might cause the program to crash
   7153 	     when it runs out of memory.  This function doesn't have a
   7154 	     return status, so there's no way to gracefully return an
   7155 	     error.  So cheat.  We know that string[-1] can be safely
   7156 	     accessed;  It's either a string in an ELF string table,
   7157 	     or allocated in an objalloc structure.  */
   7158 
   7159 	  p = eh->elf.root.root.string - 1;
   7160 	  save = *p;
   7161 	  *(char *) p = '.';
   7162 	  htab = ppc_hash_table (info);
   7163 	  if (htab == NULL)
   7164 	    return;
   7165 
   7166 	  fh = (struct ppc_link_hash_entry *)
   7167 	    elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
   7168 	  *(char *) p = save;
   7169 
   7170 	  /* Unfortunately, if it so happens that the string we were
   7171 	     looking for was allocated immediately before this string,
   7172 	     then we overwrote the string terminator.  That's the only
   7173 	     reason the lookup should fail.  */
   7174 	  if (fh == NULL)
   7175 	    {
   7176 	      q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
   7177 	      while (q >= eh->elf.root.root.string && *q == *p)
   7178 		--q, --p;
   7179 	      if (q < eh->elf.root.root.string && *p == '.')
   7180 		fh = (struct ppc_link_hash_entry *)
   7181 		  elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
   7182 	    }
   7183 	  if (fh != NULL)
   7184 	    {
   7185 	      eh->oh = fh;
   7186 	      fh->oh = eh;
   7187 	    }
   7188 	}
   7189       if (fh != NULL)
   7190 	_bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
   7191     }
   7192 }
   7193 
   7194 static bfd_boolean
   7195 get_sym_h (struct elf_link_hash_entry **hp,
   7196 	   Elf_Internal_Sym **symp,
   7197 	   asection **symsecp,
   7198 	   unsigned char **tls_maskp,
   7199 	   Elf_Internal_Sym **locsymsp,
   7200 	   unsigned long r_symndx,
   7201 	   bfd *ibfd)
   7202 {
   7203   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
   7204 
   7205   if (r_symndx >= symtab_hdr->sh_info)
   7206     {
   7207       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
   7208       struct elf_link_hash_entry *h;
   7209 
   7210       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   7211       h = elf_follow_link (h);
   7212 
   7213       if (hp != NULL)
   7214 	*hp = h;
   7215 
   7216       if (symp != NULL)
   7217 	*symp = NULL;
   7218 
   7219       if (symsecp != NULL)
   7220 	{
   7221 	  asection *symsec = NULL;
   7222 	  if (h->root.type == bfd_link_hash_defined
   7223 	      || h->root.type == bfd_link_hash_defweak)
   7224 	    symsec = h->root.u.def.section;
   7225 	  *symsecp = symsec;
   7226 	}
   7227 
   7228       if (tls_maskp != NULL)
   7229 	{
   7230 	  struct ppc_link_hash_entry *eh;
   7231 
   7232 	  eh = (struct ppc_link_hash_entry *) h;
   7233 	  *tls_maskp = &eh->tls_mask;
   7234 	}
   7235     }
   7236   else
   7237     {
   7238       Elf_Internal_Sym *sym;
   7239       Elf_Internal_Sym *locsyms = *locsymsp;
   7240 
   7241       if (locsyms == NULL)
   7242 	{
   7243 	  locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
   7244 	  if (locsyms == NULL)
   7245 	    locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
   7246 					    symtab_hdr->sh_info,
   7247 					    0, NULL, NULL, NULL);
   7248 	  if (locsyms == NULL)
   7249 	    return FALSE;
   7250 	  *locsymsp = locsyms;
   7251 	}
   7252       sym = locsyms + r_symndx;
   7253 
   7254       if (hp != NULL)
   7255 	*hp = NULL;
   7256 
   7257       if (symp != NULL)
   7258 	*symp = sym;
   7259 
   7260       if (symsecp != NULL)
   7261 	*symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
   7262 
   7263       if (tls_maskp != NULL)
   7264 	{
   7265 	  struct got_entry **lgot_ents;
   7266 	  unsigned char *tls_mask;
   7267 
   7268 	  tls_mask = NULL;
   7269 	  lgot_ents = elf_local_got_ents (ibfd);
   7270 	  if (lgot_ents != NULL)
   7271 	    {
   7272 	      struct plt_entry **local_plt = (struct plt_entry **)
   7273 		(lgot_ents + symtab_hdr->sh_info);
   7274 	      unsigned char *lgot_masks = (unsigned char *)
   7275 		(local_plt + symtab_hdr->sh_info);
   7276 	      tls_mask = &lgot_masks[r_symndx];
   7277 	    }
   7278 	  *tls_maskp = tls_mask;
   7279 	}
   7280     }
   7281   return TRUE;
   7282 }
   7283 
   7284 /* Returns TLS_MASKP for the given REL symbol.  Function return is 0 on
   7285    error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
   7286    type suitable for optimization, and 1 otherwise.  */
   7287 
   7288 static int
   7289 get_tls_mask (unsigned char **tls_maskp,
   7290 	      unsigned long *toc_symndx,
   7291 	      bfd_vma *toc_addend,
   7292 	      Elf_Internal_Sym **locsymsp,
   7293 	      const Elf_Internal_Rela *rel,
   7294 	      bfd *ibfd)
   7295 {
   7296   unsigned long r_symndx;
   7297   int next_r;
   7298   struct elf_link_hash_entry *h;
   7299   Elf_Internal_Sym *sym;
   7300   asection *sec;
   7301   bfd_vma off;
   7302 
   7303   r_symndx = ELF64_R_SYM (rel->r_info);
   7304   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
   7305     return 0;
   7306 
   7307   if ((*tls_maskp != NULL && **tls_maskp != 0)
   7308       || sec == NULL
   7309       || ppc64_elf_section_data (sec) == NULL
   7310       || ppc64_elf_section_data (sec)->sec_type != sec_toc)
   7311     return 1;
   7312 
   7313   /* Look inside a TOC section too.  */
   7314   if (h != NULL)
   7315     {
   7316       BFD_ASSERT (h->root.type == bfd_link_hash_defined);
   7317       off = h->root.u.def.value;
   7318     }
   7319   else
   7320     off = sym->st_value;
   7321   off += rel->r_addend;
   7322   BFD_ASSERT (off % 8 == 0);
   7323   r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
   7324   next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
   7325   if (toc_symndx != NULL)
   7326     *toc_symndx = r_symndx;
   7327   if (toc_addend != NULL)
   7328     *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
   7329   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
   7330     return 0;
   7331   if ((h == NULL || is_static_defined (h))
   7332       && (next_r == -1 || next_r == -2))
   7333     return 1 - next_r;
   7334   return 1;
   7335 }
   7336 
   7337 /* Find (or create) an entry in the tocsave hash table.  */
   7338 
   7339 static struct tocsave_entry *
   7340 tocsave_find (struct ppc_link_hash_table *htab,
   7341 	      enum insert_option insert,
   7342 	      Elf_Internal_Sym **local_syms,
   7343 	      const Elf_Internal_Rela *irela,
   7344 	      bfd *ibfd)
   7345 {
   7346   unsigned long r_indx;
   7347   struct elf_link_hash_entry *h;
   7348   Elf_Internal_Sym *sym;
   7349   struct tocsave_entry ent, *p;
   7350   hashval_t hash;
   7351   struct tocsave_entry **slot;
   7352 
   7353   r_indx = ELF64_R_SYM (irela->r_info);
   7354   if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
   7355     return NULL;
   7356   if (ent.sec == NULL || ent.sec->output_section == NULL)
   7357     {
   7358       (*_bfd_error_handler)
   7359 	(_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"));
   7360       return NULL;
   7361     }
   7362 
   7363   if (h != NULL)
   7364     ent.offset = h->root.u.def.value;
   7365   else
   7366     ent.offset = sym->st_value;
   7367   ent.offset += irela->r_addend;
   7368 
   7369   hash = tocsave_htab_hash (&ent);
   7370   slot = ((struct tocsave_entry **)
   7371 	  htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
   7372   if (slot == NULL)
   7373     return NULL;
   7374 
   7375   if (*slot == NULL)
   7376     {
   7377       p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
   7378       if (p == NULL)
   7379 	return NULL;
   7380       *p = ent;
   7381       *slot = p;
   7382     }
   7383   return *slot;
   7384 }
   7385 
   7386 /* Adjust all global syms defined in opd sections.  In gcc generated
   7387    code for the old ABI, these will already have been done.  */
   7388 
   7389 static bfd_boolean
   7390 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
   7391 {
   7392   struct ppc_link_hash_entry *eh;
   7393   asection *sym_sec;
   7394   struct _opd_sec_data *opd;
   7395 
   7396   if (h->root.type == bfd_link_hash_indirect)
   7397     return TRUE;
   7398 
   7399   if (h->root.type != bfd_link_hash_defined
   7400       && h->root.type != bfd_link_hash_defweak)
   7401     return TRUE;
   7402 
   7403   eh = (struct ppc_link_hash_entry *) h;
   7404   if (eh->adjust_done)
   7405     return TRUE;
   7406 
   7407   sym_sec = eh->elf.root.u.def.section;
   7408   opd = get_opd_info (sym_sec);
   7409   if (opd != NULL && opd->adjust != NULL)
   7410     {
   7411       long adjust = opd->adjust[eh->elf.root.u.def.value / 8];
   7412       if (adjust == -1)
   7413 	{
   7414 	  /* This entry has been deleted.  */
   7415 	  asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
   7416 	  if (dsec == NULL)
   7417 	    {
   7418 	      for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
   7419 		if (discarded_section (dsec))
   7420 		  {
   7421 		    ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
   7422 		    break;
   7423 		  }
   7424 	    }
   7425 	  eh->elf.root.u.def.value = 0;
   7426 	  eh->elf.root.u.def.section = dsec;
   7427 	}
   7428       else
   7429 	eh->elf.root.u.def.value += adjust;
   7430       eh->adjust_done = 1;
   7431     }
   7432   return TRUE;
   7433 }
   7434 
   7435 /* Handles decrementing dynamic reloc counts for the reloc specified by
   7436    R_INFO in section SEC.  If LOCAL_SYMS is NULL, then H and SYM
   7437    have already been determined.  */
   7438 
   7439 static bfd_boolean
   7440 dec_dynrel_count (bfd_vma r_info,
   7441 		  asection *sec,
   7442 		  struct bfd_link_info *info,
   7443 		  Elf_Internal_Sym **local_syms,
   7444 		  struct elf_link_hash_entry *h,
   7445 		  Elf_Internal_Sym *sym)
   7446 {
   7447   enum elf_ppc64_reloc_type r_type;
   7448   asection *sym_sec = NULL;
   7449 
   7450   /* Can this reloc be dynamic?  This switch, and later tests here
   7451      should be kept in sync with the code in check_relocs.  */
   7452   r_type = ELF64_R_TYPE (r_info);
   7453   switch (r_type)
   7454     {
   7455     default:
   7456       return TRUE;
   7457 
   7458     case R_PPC64_TPREL16:
   7459     case R_PPC64_TPREL16_LO:
   7460     case R_PPC64_TPREL16_HI:
   7461     case R_PPC64_TPREL16_HA:
   7462     case R_PPC64_TPREL16_DS:
   7463     case R_PPC64_TPREL16_LO_DS:
   7464     case R_PPC64_TPREL16_HIGH:
   7465     case R_PPC64_TPREL16_HIGHA:
   7466     case R_PPC64_TPREL16_HIGHER:
   7467     case R_PPC64_TPREL16_HIGHERA:
   7468     case R_PPC64_TPREL16_HIGHEST:
   7469     case R_PPC64_TPREL16_HIGHESTA:
   7470       if (!info->shared)
   7471 	return TRUE;
   7472 
   7473     case R_PPC64_TPREL64:
   7474     case R_PPC64_DTPMOD64:
   7475     case R_PPC64_DTPREL64:
   7476     case R_PPC64_ADDR64:
   7477     case R_PPC64_REL30:
   7478     case R_PPC64_REL32:
   7479     case R_PPC64_REL64:
   7480     case R_PPC64_ADDR14:
   7481     case R_PPC64_ADDR14_BRNTAKEN:
   7482     case R_PPC64_ADDR14_BRTAKEN:
   7483     case R_PPC64_ADDR16:
   7484     case R_PPC64_ADDR16_DS:
   7485     case R_PPC64_ADDR16_HA:
   7486     case R_PPC64_ADDR16_HI:
   7487     case R_PPC64_ADDR16_HIGH:
   7488     case R_PPC64_ADDR16_HIGHA:
   7489     case R_PPC64_ADDR16_HIGHER:
   7490     case R_PPC64_ADDR16_HIGHERA:
   7491     case R_PPC64_ADDR16_HIGHEST:
   7492     case R_PPC64_ADDR16_HIGHESTA:
   7493     case R_PPC64_ADDR16_LO:
   7494     case R_PPC64_ADDR16_LO_DS:
   7495     case R_PPC64_ADDR24:
   7496     case R_PPC64_ADDR32:
   7497     case R_PPC64_UADDR16:
   7498     case R_PPC64_UADDR32:
   7499     case R_PPC64_UADDR64:
   7500     case R_PPC64_TOC:
   7501       break;
   7502     }
   7503 
   7504   if (local_syms != NULL)
   7505     {
   7506       unsigned long r_symndx;
   7507       bfd *ibfd = sec->owner;
   7508 
   7509       r_symndx = ELF64_R_SYM (r_info);
   7510       if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
   7511 	return FALSE;
   7512     }
   7513 
   7514   if ((info->shared
   7515        && (must_be_dyn_reloc (info, r_type)
   7516 	   || (h != NULL
   7517 	       && (!SYMBOLIC_BIND (info, h)
   7518 		   || h->root.type == bfd_link_hash_defweak
   7519 		   || !h->def_regular))))
   7520       || (ELIMINATE_COPY_RELOCS
   7521 	  && !info->shared
   7522 	  && h != NULL
   7523 	  && (h->root.type == bfd_link_hash_defweak
   7524 	      || !h->def_regular)))
   7525     ;
   7526   else
   7527     return TRUE;
   7528 
   7529   if (h != NULL)
   7530     {
   7531       struct elf_dyn_relocs *p;
   7532       struct elf_dyn_relocs **pp;
   7533       pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
   7534 
   7535       /* elf_gc_sweep may have already removed all dyn relocs associated
   7536 	 with local syms for a given section.  Also, symbol flags are
   7537 	 changed by elf_gc_sweep_symbol, confusing the test above.  Don't
   7538 	 report a dynreloc miscount.  */
   7539       if (*pp == NULL && info->gc_sections)
   7540 	return TRUE;
   7541 
   7542       while ((p = *pp) != NULL)
   7543 	{
   7544 	  if (p->sec == sec)
   7545 	    {
   7546 	      if (!must_be_dyn_reloc (info, r_type))
   7547 		p->pc_count -= 1;
   7548 	      p->count -= 1;
   7549 	      if (p->count == 0)
   7550 		*pp = p->next;
   7551 	      return TRUE;
   7552 	    }
   7553 	  pp = &p->next;
   7554 	}
   7555     }
   7556   else
   7557     {
   7558       struct ppc_dyn_relocs *p;
   7559       struct ppc_dyn_relocs **pp;
   7560       void *vpp;
   7561       bfd_boolean is_ifunc;
   7562 
   7563       if (local_syms == NULL)
   7564 	sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
   7565       if (sym_sec == NULL)
   7566 	sym_sec = sec;
   7567 
   7568       vpp = &elf_section_data (sym_sec)->local_dynrel;
   7569       pp = (struct ppc_dyn_relocs **) vpp;
   7570 
   7571       if (*pp == NULL && info->gc_sections)
   7572 	return TRUE;
   7573 
   7574       is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
   7575       while ((p = *pp) != NULL)
   7576 	{
   7577 	  if (p->sec == sec && p->ifunc == is_ifunc)
   7578 	    {
   7579 	      p->count -= 1;
   7580 	      if (p->count == 0)
   7581 		*pp = p->next;
   7582 	      return TRUE;
   7583 	    }
   7584 	  pp = &p->next;
   7585 	}
   7586     }
   7587 
   7588   info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
   7589 			  sec->owner, sec);
   7590   bfd_set_error (bfd_error_bad_value);
   7591   return FALSE;
   7592 }
   7593 
   7594 /* Remove unused Official Procedure Descriptor entries.  Currently we
   7595    only remove those associated with functions in discarded link-once
   7596    sections, or weakly defined functions that have been overridden.  It
   7597    would be possible to remove many more entries for statically linked
   7598    applications.  */
   7599 
   7600 bfd_boolean
   7601 ppc64_elf_edit_opd (struct bfd_link_info *info)
   7602 {
   7603   bfd *ibfd;
   7604   bfd_boolean some_edited = FALSE;
   7605   asection *need_pad = NULL;
   7606   struct ppc_link_hash_table *htab;
   7607 
   7608   htab = ppc_hash_table (info);
   7609   if (htab == NULL)
   7610     return FALSE;
   7611 
   7612   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   7613     {
   7614       asection *sec;
   7615       Elf_Internal_Rela *relstart, *rel, *relend;
   7616       Elf_Internal_Shdr *symtab_hdr;
   7617       Elf_Internal_Sym *local_syms;
   7618       bfd_vma offset;
   7619       struct _opd_sec_data *opd;
   7620       bfd_boolean need_edit, add_aux_fields;
   7621       bfd_size_type cnt_16b = 0;
   7622 
   7623       if (!is_ppc64_elf (ibfd))
   7624 	continue;
   7625 
   7626       sec = bfd_get_section_by_name (ibfd, ".opd");
   7627       if (sec == NULL || sec->size == 0)
   7628 	continue;
   7629 
   7630       if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   7631 	continue;
   7632 
   7633       if (sec->output_section == bfd_abs_section_ptr)
   7634 	continue;
   7635 
   7636       /* Look through the section relocs.  */
   7637       if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
   7638 	continue;
   7639 
   7640       local_syms = NULL;
   7641       symtab_hdr = &elf_symtab_hdr (ibfd);
   7642 
   7643       /* Read the relocations.  */
   7644       relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   7645 					    info->keep_memory);
   7646       if (relstart == NULL)
   7647 	return FALSE;
   7648 
   7649       /* First run through the relocs to check they are sane, and to
   7650 	 determine whether we need to edit this opd section.  */
   7651       need_edit = FALSE;
   7652       need_pad = sec;
   7653       offset = 0;
   7654       relend = relstart + sec->reloc_count;
   7655       for (rel = relstart; rel < relend; )
   7656 	{
   7657 	  enum elf_ppc64_reloc_type r_type;
   7658 	  unsigned long r_symndx;
   7659 	  asection *sym_sec;
   7660 	  struct elf_link_hash_entry *h;
   7661 	  Elf_Internal_Sym *sym;
   7662 
   7663 	  /* .opd contains a regular array of 16 or 24 byte entries.  We're
   7664 	     only interested in the reloc pointing to a function entry
   7665 	     point.  */
   7666 	  if (rel->r_offset != offset
   7667 	      || rel + 1 >= relend
   7668 	      || (rel + 1)->r_offset != offset + 8)
   7669 	    {
   7670 	      /* If someone messes with .opd alignment then after a
   7671 		 "ld -r" we might have padding in the middle of .opd.
   7672 		 Also, there's nothing to prevent someone putting
   7673 		 something silly in .opd with the assembler.  No .opd
   7674 		 optimization for them!  */
   7675 	    broken_opd:
   7676 	      (*_bfd_error_handler)
   7677 		(_("%B: .opd is not a regular array of opd entries"), ibfd);
   7678 	      need_edit = FALSE;
   7679 	      break;
   7680 	    }
   7681 
   7682 	  if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
   7683 	      || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
   7684 	    {
   7685 	      (*_bfd_error_handler)
   7686 		(_("%B: unexpected reloc type %u in .opd section"),
   7687 		 ibfd, r_type);
   7688 	      need_edit = FALSE;
   7689 	      break;
   7690 	    }
   7691 
   7692 	  r_symndx = ELF64_R_SYM (rel->r_info);
   7693 	  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   7694 			  r_symndx, ibfd))
   7695 	    goto error_ret;
   7696 
   7697 	  if (sym_sec == NULL || sym_sec->owner == NULL)
   7698 	    {
   7699 	      const char *sym_name;
   7700 	      if (h != NULL)
   7701 		sym_name = h->root.root.string;
   7702 	      else
   7703 		sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
   7704 					     sym_sec);
   7705 
   7706 	      (*_bfd_error_handler)
   7707 		(_("%B: undefined sym `%s' in .opd section"),
   7708 		 ibfd, sym_name);
   7709 	      need_edit = FALSE;
   7710 	      break;
   7711 	    }
   7712 
   7713 	  /* opd entries are always for functions defined in the
   7714 	     current input bfd.  If the symbol isn't defined in the
   7715 	     input bfd, then we won't be using the function in this
   7716 	     bfd;  It must be defined in a linkonce section in another
   7717 	     bfd, or is weak.  It's also possible that we are
   7718 	     discarding the function due to a linker script /DISCARD/,
   7719 	     which we test for via the output_section.  */
   7720 	  if (sym_sec->owner != ibfd
   7721 	      || sym_sec->output_section == bfd_abs_section_ptr)
   7722 	    need_edit = TRUE;
   7723 
   7724 	  rel += 2;
   7725 	  if (rel == relend
   7726 	      || (rel + 1 == relend && rel->r_offset == offset + 16))
   7727 	    {
   7728 	      if (sec->size == offset + 24)
   7729 		{
   7730 		  need_pad = NULL;
   7731 		  break;
   7732 		}
   7733 	      if (rel == relend && sec->size == offset + 16)
   7734 		{
   7735 		  cnt_16b++;
   7736 		  break;
   7737 		}
   7738 	      goto broken_opd;
   7739 	    }
   7740 
   7741 	  if (rel->r_offset == offset + 24)
   7742 	    offset += 24;
   7743 	  else if (rel->r_offset != offset + 16)
   7744 	    goto broken_opd;
   7745 	  else if (rel + 1 < relend
   7746 		   && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
   7747 		   && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
   7748 	    {
   7749 	      offset += 16;
   7750 	      cnt_16b++;
   7751 	    }
   7752 	  else if (rel + 2 < relend
   7753 		   && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
   7754 		   && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
   7755 	    {
   7756 	      offset += 24;
   7757 	      rel += 1;
   7758 	    }
   7759 	  else
   7760 	    goto broken_opd;
   7761 	}
   7762 
   7763       add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
   7764 
   7765       if (need_edit || add_aux_fields)
   7766 	{
   7767 	  Elf_Internal_Rela *write_rel;
   7768 	  Elf_Internal_Shdr *rel_hdr;
   7769 	  bfd_byte *rptr, *wptr;
   7770 	  bfd_byte *new_contents;
   7771 	  bfd_boolean skip;
   7772 	  long opd_ent_size;
   7773 	  bfd_size_type amt;
   7774 
   7775 	  new_contents = NULL;
   7776 	  amt = sec->size * sizeof (long) / 8;
   7777 	  opd = &ppc64_elf_section_data (sec)->u.opd;
   7778 	  opd->adjust = bfd_zalloc (sec->owner, amt);
   7779 	  if (opd->adjust == NULL)
   7780 	    return FALSE;
   7781 	  ppc64_elf_section_data (sec)->sec_type = sec_opd;
   7782 
   7783 	  /* This seems a waste of time as input .opd sections are all
   7784 	     zeros as generated by gcc, but I suppose there's no reason
   7785 	     this will always be so.  We might start putting something in
   7786 	     the third word of .opd entries.  */
   7787 	  if ((sec->flags & SEC_IN_MEMORY) == 0)
   7788 	    {
   7789 	      bfd_byte *loc;
   7790 	      if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
   7791 		{
   7792 		  if (loc != NULL)
   7793 		    free (loc);
   7794 		error_ret:
   7795 		  if (local_syms != NULL
   7796 		      && symtab_hdr->contents != (unsigned char *) local_syms)
   7797 		    free (local_syms);
   7798 		  if (elf_section_data (sec)->relocs != relstart)
   7799 		    free (relstart);
   7800 		  return FALSE;
   7801 		}
   7802 	      sec->contents = loc;
   7803 	      sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
   7804 	    }
   7805 
   7806 	  elf_section_data (sec)->relocs = relstart;
   7807 
   7808 	  new_contents = sec->contents;
   7809 	  if (add_aux_fields)
   7810 	    {
   7811 	      new_contents = bfd_malloc (sec->size + cnt_16b * 8);
   7812 	      if (new_contents == NULL)
   7813 		return FALSE;
   7814 	      need_pad = FALSE;
   7815 	    }
   7816 	  wptr = new_contents;
   7817 	  rptr = sec->contents;
   7818 
   7819 	  write_rel = relstart;
   7820 	  skip = FALSE;
   7821 	  offset = 0;
   7822 	  opd_ent_size = 0;
   7823 	  for (rel = relstart; rel < relend; rel++)
   7824 	    {
   7825 	      unsigned long r_symndx;
   7826 	      asection *sym_sec;
   7827 	      struct elf_link_hash_entry *h;
   7828 	      Elf_Internal_Sym *sym;
   7829 
   7830 	      r_symndx = ELF64_R_SYM (rel->r_info);
   7831 	      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   7832 			      r_symndx, ibfd))
   7833 		goto error_ret;
   7834 
   7835 	      if (rel->r_offset == offset)
   7836 		{
   7837 		  struct ppc_link_hash_entry *fdh = NULL;
   7838 
   7839 		  /* See if the .opd entry is full 24 byte or
   7840 		     16 byte (with fd_aux entry overlapped with next
   7841 		     fd_func).  */
   7842 		  opd_ent_size = 24;
   7843 		  if ((rel + 2 == relend && sec->size == offset + 16)
   7844 		      || (rel + 3 < relend
   7845 			  && rel[2].r_offset == offset + 16
   7846 			  && rel[3].r_offset == offset + 24
   7847 			  && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
   7848 			  && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
   7849 		    opd_ent_size = 16;
   7850 
   7851 		  if (h != NULL
   7852 		      && h->root.root.string[0] == '.')
   7853 		    {
   7854 		      fdh = lookup_fdh ((struct ppc_link_hash_entry *) h, htab);
   7855 		      if (fdh != NULL
   7856 			  && fdh->elf.root.type != bfd_link_hash_defined
   7857 			  && fdh->elf.root.type != bfd_link_hash_defweak)
   7858 			fdh = NULL;
   7859 		    }
   7860 
   7861 		  skip = (sym_sec->owner != ibfd
   7862 			  || sym_sec->output_section == bfd_abs_section_ptr);
   7863 		  if (skip)
   7864 		    {
   7865 		      if (fdh != NULL && sym_sec->owner == ibfd)
   7866 			{
   7867 			  /* Arrange for the function descriptor sym
   7868 			     to be dropped.  */
   7869 			  fdh->elf.root.u.def.value = 0;
   7870 			  fdh->elf.root.u.def.section = sym_sec;
   7871 			}
   7872 		      opd->adjust[rel->r_offset / 8] = -1;
   7873 		    }
   7874 		  else
   7875 		    {
   7876 		      /* We'll be keeping this opd entry.  */
   7877 
   7878 		      if (fdh != NULL)
   7879 			{
   7880 			  /* Redefine the function descriptor symbol to
   7881 			     this location in the opd section.  It is
   7882 			     necessary to update the value here rather
   7883 			     than using an array of adjustments as we do
   7884 			     for local symbols, because various places
   7885 			     in the generic ELF code use the value
   7886 			     stored in u.def.value.  */
   7887 			  fdh->elf.root.u.def.value = wptr - new_contents;
   7888 			  fdh->adjust_done = 1;
   7889 			}
   7890 
   7891 		      /* Local syms are a bit tricky.  We could
   7892 			 tweak them as they can be cached, but
   7893 			 we'd need to look through the local syms
   7894 			 for the function descriptor sym which we
   7895 			 don't have at the moment.  So keep an
   7896 			 array of adjustments.  */
   7897 		      opd->adjust[rel->r_offset / 8]
   7898 			= (wptr - new_contents) - (rptr - sec->contents);
   7899 
   7900 		      if (wptr != rptr)
   7901 			memcpy (wptr, rptr, opd_ent_size);
   7902 		      wptr += opd_ent_size;
   7903 		      if (add_aux_fields && opd_ent_size == 16)
   7904 			{
   7905 			  memset (wptr, '\0', 8);
   7906 			  wptr += 8;
   7907 			}
   7908 		    }
   7909 		  rptr += opd_ent_size;
   7910 		  offset += opd_ent_size;
   7911 		}
   7912 
   7913 	      if (skip)
   7914 		{
   7915 		  if (!NO_OPD_RELOCS
   7916 		      && !info->relocatable
   7917 		      && !dec_dynrel_count (rel->r_info, sec, info,
   7918 					    NULL, h, sym))
   7919 		    goto error_ret;
   7920 		}
   7921 	      else
   7922 		{
   7923 		  /* We need to adjust any reloc offsets to point to the
   7924 		     new opd entries.  While we're at it, we may as well
   7925 		     remove redundant relocs.  */
   7926 		  rel->r_offset += opd->adjust[(offset - opd_ent_size) / 8];
   7927 		  if (write_rel != rel)
   7928 		    memcpy (write_rel, rel, sizeof (*rel));
   7929 		  ++write_rel;
   7930 		}
   7931 	    }
   7932 
   7933 	  sec->size = wptr - new_contents;
   7934 	  sec->reloc_count = write_rel - relstart;
   7935 	  if (add_aux_fields)
   7936 	    {
   7937 	      free (sec->contents);
   7938 	      sec->contents = new_contents;
   7939 	    }
   7940 
   7941 	  /* Fudge the header size too, as this is used later in
   7942 	     elf_bfd_final_link if we are emitting relocs.  */
   7943 	  rel_hdr = _bfd_elf_single_rel_hdr (sec);
   7944 	  rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
   7945 	  some_edited = TRUE;
   7946 	}
   7947       else if (elf_section_data (sec)->relocs != relstart)
   7948 	free (relstart);
   7949 
   7950       if (local_syms != NULL
   7951 	  && symtab_hdr->contents != (unsigned char *) local_syms)
   7952 	{
   7953 	  if (!info->keep_memory)
   7954 	    free (local_syms);
   7955 	  else
   7956 	    symtab_hdr->contents = (unsigned char *) local_syms;
   7957 	}
   7958     }
   7959 
   7960   if (some_edited)
   7961     elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
   7962 
   7963   /* If we are doing a final link and the last .opd entry is just 16 byte
   7964      long, add a 8 byte padding after it.  */
   7965   if (need_pad != NULL && !info->relocatable)
   7966     {
   7967       bfd_byte *p;
   7968 
   7969       if ((need_pad->flags & SEC_IN_MEMORY) == 0)
   7970 	{
   7971 	  BFD_ASSERT (need_pad->size > 0);
   7972 
   7973 	  p = bfd_malloc (need_pad->size + 8);
   7974 	  if (p == NULL)
   7975 	    return FALSE;
   7976 
   7977 	  if (! bfd_get_section_contents (need_pad->owner, need_pad,
   7978 					  p, 0, need_pad->size))
   7979 	    return FALSE;
   7980 
   7981 	  need_pad->contents = p;
   7982 	  need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
   7983 	}
   7984       else
   7985 	{
   7986 	  p = bfd_realloc (need_pad->contents, need_pad->size + 8);
   7987 	  if (p == NULL)
   7988 	    return FALSE;
   7989 
   7990 	  need_pad->contents = p;
   7991 	}
   7992 
   7993       memset (need_pad->contents + need_pad->size, 0, 8);
   7994       need_pad->size += 8;
   7995     }
   7996 
   7997   return TRUE;
   7998 }
   7999 
   8000 /* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
   8001 
   8002 asection *
   8003 ppc64_elf_tls_setup (struct bfd_link_info *info)
   8004 {
   8005   struct ppc_link_hash_table *htab;
   8006 
   8007   htab = ppc_hash_table (info);
   8008   if (htab == NULL)
   8009     return NULL;
   8010 
   8011   if (abiversion (info->output_bfd) == 1)
   8012     htab->opd_abi = 1;
   8013 
   8014   if (htab->params->no_multi_toc)
   8015     htab->do_multi_toc = 0;
   8016   else if (!htab->do_multi_toc)
   8017     htab->params->no_multi_toc = 1;
   8018 
   8019   htab->tls_get_addr = ((struct ppc_link_hash_entry *)
   8020 			elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
   8021 					      FALSE, FALSE, TRUE));
   8022   /* Move dynamic linking info to the function descriptor sym.  */
   8023   if (htab->tls_get_addr != NULL)
   8024     func_desc_adjust (&htab->tls_get_addr->elf, info);
   8025   htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
   8026 			   elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
   8027 						 FALSE, FALSE, TRUE));
   8028   if (!htab->params->no_tls_get_addr_opt)
   8029     {
   8030       struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
   8031 
   8032       opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
   8033 				  FALSE, FALSE, TRUE);
   8034       if (opt != NULL)
   8035 	func_desc_adjust (opt, info);
   8036       opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
   8037 				     FALSE, FALSE, TRUE);
   8038       if (opt_fd != NULL
   8039 	  && (opt_fd->root.type == bfd_link_hash_defined
   8040 	      || opt_fd->root.type == bfd_link_hash_defweak))
   8041 	{
   8042 	  /* If glibc supports an optimized __tls_get_addr call stub,
   8043 	     signalled by the presence of __tls_get_addr_opt, and we'll
   8044 	     be calling __tls_get_addr via a plt call stub, then
   8045 	     make __tls_get_addr point to __tls_get_addr_opt.  */
   8046 	  tga_fd = &htab->tls_get_addr_fd->elf;
   8047 	  if (htab->elf.dynamic_sections_created
   8048 	      && tga_fd != NULL
   8049 	      && (tga_fd->type == STT_FUNC
   8050 		  || tga_fd->needs_plt)
   8051 	      && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
   8052 		   || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
   8053 		       && tga_fd->root.type == bfd_link_hash_undefweak)))
   8054 	    {
   8055 	      struct plt_entry *ent;
   8056 
   8057 	      for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
   8058 		if (ent->plt.refcount > 0)
   8059 		  break;
   8060 	      if (ent != NULL)
   8061 		{
   8062 		  tga_fd->root.type = bfd_link_hash_indirect;
   8063 		  tga_fd->root.u.i.link = &opt_fd->root;
   8064 		  ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
   8065 		  if (opt_fd->dynindx != -1)
   8066 		    {
   8067 		      /* Use __tls_get_addr_opt in dynamic relocations.  */
   8068 		      opt_fd->dynindx = -1;
   8069 		      _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
   8070 					      opt_fd->dynstr_index);
   8071 		      if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
   8072 			return NULL;
   8073 		    }
   8074 		  htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
   8075 		  tga = &htab->tls_get_addr->elf;
   8076 		  if (opt != NULL && tga != NULL)
   8077 		    {
   8078 		      tga->root.type = bfd_link_hash_indirect;
   8079 		      tga->root.u.i.link = &opt->root;
   8080 		      ppc64_elf_copy_indirect_symbol (info, opt, tga);
   8081 		      _bfd_elf_link_hash_hide_symbol (info, opt,
   8082 						      tga->forced_local);
   8083 		      htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
   8084 		    }
   8085 		  htab->tls_get_addr_fd->oh = htab->tls_get_addr;
   8086 		  htab->tls_get_addr_fd->is_func_descriptor = 1;
   8087 		  if (htab->tls_get_addr != NULL)
   8088 		    {
   8089 		      htab->tls_get_addr->oh = htab->tls_get_addr_fd;
   8090 		      htab->tls_get_addr->is_func = 1;
   8091 		    }
   8092 		}
   8093 	    }
   8094 	}
   8095       else
   8096 	htab->params->no_tls_get_addr_opt = TRUE;
   8097     }
   8098   return _bfd_elf_tls_setup (info->output_bfd, info);
   8099 }
   8100 
   8101 /* Return TRUE iff REL is a branch reloc with a global symbol matching
   8102    HASH1 or HASH2.  */
   8103 
   8104 static bfd_boolean
   8105 branch_reloc_hash_match (const bfd *ibfd,
   8106 			 const Elf_Internal_Rela *rel,
   8107 			 const struct ppc_link_hash_entry *hash1,
   8108 			 const struct ppc_link_hash_entry *hash2)
   8109 {
   8110   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
   8111   enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
   8112   unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
   8113 
   8114   if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
   8115     {
   8116       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
   8117       struct elf_link_hash_entry *h;
   8118 
   8119       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   8120       h = elf_follow_link (h);
   8121       if (h == &hash1->elf || h == &hash2->elf)
   8122 	return TRUE;
   8123     }
   8124   return FALSE;
   8125 }
   8126 
   8127 /* Run through all the TLS relocs looking for optimization
   8128    opportunities.  The linker has been hacked (see ppc64elf.em) to do
   8129    a preliminary section layout so that we know the TLS segment
   8130    offsets.  We can't optimize earlier because some optimizations need
   8131    to know the tp offset, and we need to optimize before allocating
   8132    dynamic relocations.  */
   8133 
   8134 bfd_boolean
   8135 ppc64_elf_tls_optimize (struct bfd_link_info *info)
   8136 {
   8137   bfd *ibfd;
   8138   asection *sec;
   8139   struct ppc_link_hash_table *htab;
   8140   unsigned char *toc_ref;
   8141   int pass;
   8142 
   8143   if (info->relocatable || !info->executable)
   8144     return TRUE;
   8145 
   8146   htab = ppc_hash_table (info);
   8147   if (htab == NULL)
   8148     return FALSE;
   8149 
   8150   /* Make two passes over the relocs.  On the first pass, mark toc
   8151      entries involved with tls relocs, and check that tls relocs
   8152      involved in setting up a tls_get_addr call are indeed followed by
   8153      such a call.  If they are not, we can't do any tls optimization.
   8154      On the second pass twiddle tls_mask flags to notify
   8155      relocate_section that optimization can be done, and adjust got
   8156      and plt refcounts.  */
   8157   toc_ref = NULL;
   8158   for (pass = 0; pass < 2; ++pass)
   8159     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   8160       {
   8161 	Elf_Internal_Sym *locsyms = NULL;
   8162 	asection *toc = bfd_get_section_by_name (ibfd, ".toc");
   8163 
   8164 	for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   8165 	  if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
   8166 	    {
   8167 	      Elf_Internal_Rela *relstart, *rel, *relend;
   8168 	      bfd_boolean found_tls_get_addr_arg = 0;
   8169 
   8170 	      /* Read the relocations.  */
   8171 	      relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   8172 						    info->keep_memory);
   8173 	      if (relstart == NULL)
   8174 		{
   8175 		  free (toc_ref);
   8176 		  return FALSE;
   8177 		}
   8178 
   8179 	      relend = relstart + sec->reloc_count;
   8180 	      for (rel = relstart; rel < relend; rel++)
   8181 		{
   8182 		  enum elf_ppc64_reloc_type r_type;
   8183 		  unsigned long r_symndx;
   8184 		  struct elf_link_hash_entry *h;
   8185 		  Elf_Internal_Sym *sym;
   8186 		  asection *sym_sec;
   8187 		  unsigned char *tls_mask;
   8188 		  unsigned char tls_set, tls_clear, tls_type = 0;
   8189 		  bfd_vma value;
   8190 		  bfd_boolean ok_tprel, is_local;
   8191 		  long toc_ref_index = 0;
   8192 		  int expecting_tls_get_addr = 0;
   8193 		  bfd_boolean ret = FALSE;
   8194 
   8195 		  r_symndx = ELF64_R_SYM (rel->r_info);
   8196 		  if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
   8197 				  r_symndx, ibfd))
   8198 		    {
   8199 		    err_free_rel:
   8200 		      if (elf_section_data (sec)->relocs != relstart)
   8201 			free (relstart);
   8202 		      if (toc_ref != NULL)
   8203 			free (toc_ref);
   8204 		      if (locsyms != NULL
   8205 			  && (elf_symtab_hdr (ibfd).contents
   8206 			      != (unsigned char *) locsyms))
   8207 			free (locsyms);
   8208 		      return ret;
   8209 		    }
   8210 
   8211 		  if (h != NULL)
   8212 		    {
   8213 		      if (h->root.type == bfd_link_hash_defined
   8214 			  || h->root.type == bfd_link_hash_defweak)
   8215 			value = h->root.u.def.value;
   8216 		      else if (h->root.type == bfd_link_hash_undefweak)
   8217 			value = 0;
   8218 		      else
   8219 			{
   8220 			  found_tls_get_addr_arg = 0;
   8221 			  continue;
   8222 			}
   8223 		    }
   8224 		  else
   8225 		    /* Symbols referenced by TLS relocs must be of type
   8226 		       STT_TLS.  So no need for .opd local sym adjust.  */
   8227 		    value = sym->st_value;
   8228 
   8229 		  ok_tprel = FALSE;
   8230 		  is_local = FALSE;
   8231 		  if (h == NULL
   8232 		      || !h->def_dynamic)
   8233 		    {
   8234 		      is_local = TRUE;
   8235 		      if (h != NULL
   8236 			  && h->root.type == bfd_link_hash_undefweak)
   8237 			ok_tprel = TRUE;
   8238 		      else
   8239 			{
   8240 			  value += sym_sec->output_offset;
   8241 			  value += sym_sec->output_section->vma;
   8242 			  value -= htab->elf.tls_sec->vma;
   8243 			  ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
   8244 				      < (bfd_vma) 1 << 32);
   8245 			}
   8246 		    }
   8247 
   8248 		  r_type = ELF64_R_TYPE (rel->r_info);
   8249 		  /* If this section has old-style __tls_get_addr calls
   8250 		     without marker relocs, then check that each
   8251 		     __tls_get_addr call reloc is preceded by a reloc
   8252 		     that conceivably belongs to the __tls_get_addr arg
   8253 		     setup insn.  If we don't find matching arg setup
   8254 		     relocs, don't do any tls optimization.  */
   8255 		  if (pass == 0
   8256 		      && sec->has_tls_get_addr_call
   8257 		      && h != NULL
   8258 		      && (h == &htab->tls_get_addr->elf
   8259 			  || h == &htab->tls_get_addr_fd->elf)
   8260 		      && !found_tls_get_addr_arg
   8261 		      && is_branch_reloc (r_type))
   8262 		    {
   8263 		      info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
   8264 						"TLS optimization disabled\n"),
   8265 					      ibfd, sec, rel->r_offset);
   8266 		      ret = TRUE;
   8267 		      goto err_free_rel;
   8268 		    }
   8269 
   8270 		  found_tls_get_addr_arg = 0;
   8271 		  switch (r_type)
   8272 		    {
   8273 		    case R_PPC64_GOT_TLSLD16:
   8274 		    case R_PPC64_GOT_TLSLD16_LO:
   8275 		      expecting_tls_get_addr = 1;
   8276 		      found_tls_get_addr_arg = 1;
   8277 		      /* Fall thru */
   8278 
   8279 		    case R_PPC64_GOT_TLSLD16_HI:
   8280 		    case R_PPC64_GOT_TLSLD16_HA:
   8281 		      /* These relocs should never be against a symbol
   8282 			 defined in a shared lib.  Leave them alone if
   8283 			 that turns out to be the case.  */
   8284 		      if (!is_local)
   8285 			continue;
   8286 
   8287 		      /* LD -> LE */
   8288 		      tls_set = 0;
   8289 		      tls_clear = TLS_LD;
   8290 		      tls_type = TLS_TLS | TLS_LD;
   8291 		      break;
   8292 
   8293 		    case R_PPC64_GOT_TLSGD16:
   8294 		    case R_PPC64_GOT_TLSGD16_LO:
   8295 		      expecting_tls_get_addr = 1;
   8296 		      found_tls_get_addr_arg = 1;
   8297 		      /* Fall thru */
   8298 
   8299 		    case R_PPC64_GOT_TLSGD16_HI:
   8300 		    case R_PPC64_GOT_TLSGD16_HA:
   8301 		      if (ok_tprel)
   8302 			/* GD -> LE */
   8303 			tls_set = 0;
   8304 		      else
   8305 			/* GD -> IE */
   8306 			tls_set = TLS_TLS | TLS_TPRELGD;
   8307 		      tls_clear = TLS_GD;
   8308 		      tls_type = TLS_TLS | TLS_GD;
   8309 		      break;
   8310 
   8311 		    case R_PPC64_GOT_TPREL16_DS:
   8312 		    case R_PPC64_GOT_TPREL16_LO_DS:
   8313 		    case R_PPC64_GOT_TPREL16_HI:
   8314 		    case R_PPC64_GOT_TPREL16_HA:
   8315 		      if (ok_tprel)
   8316 			{
   8317 			  /* IE -> LE */
   8318 			  tls_set = 0;
   8319 			  tls_clear = TLS_TPREL;
   8320 			  tls_type = TLS_TLS | TLS_TPREL;
   8321 			  break;
   8322 			}
   8323 		      continue;
   8324 
   8325 		    case R_PPC64_TLSGD:
   8326 		    case R_PPC64_TLSLD:
   8327 		      found_tls_get_addr_arg = 1;
   8328 		      /* Fall thru */
   8329 
   8330 		    case R_PPC64_TLS:
   8331 		    case R_PPC64_TOC16:
   8332 		    case R_PPC64_TOC16_LO:
   8333 		      if (sym_sec == NULL || sym_sec != toc)
   8334 			continue;
   8335 
   8336 		      /* Mark this toc entry as referenced by a TLS
   8337 			 code sequence.  We can do that now in the
   8338 			 case of R_PPC64_TLS, and after checking for
   8339 			 tls_get_addr for the TOC16 relocs.  */
   8340 		      if (toc_ref == NULL)
   8341 			toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
   8342 		      if (toc_ref == NULL)
   8343 			goto err_free_rel;
   8344 
   8345 		      if (h != NULL)
   8346 			value = h->root.u.def.value;
   8347 		      else
   8348 			value = sym->st_value;
   8349 		      value += rel->r_addend;
   8350 		      if (value % 8 != 0)
   8351 			continue;
   8352 		      BFD_ASSERT (value < toc->size
   8353 				  && toc->output_offset % 8 == 0);
   8354 		      toc_ref_index = (value + toc->output_offset) / 8;
   8355 		      if (r_type == R_PPC64_TLS
   8356 			  || r_type == R_PPC64_TLSGD
   8357 			  || r_type == R_PPC64_TLSLD)
   8358 			{
   8359 			  toc_ref[toc_ref_index] = 1;
   8360 			  continue;
   8361 			}
   8362 
   8363 		      if (pass != 0 && toc_ref[toc_ref_index] == 0)
   8364 			continue;
   8365 
   8366 		      tls_set = 0;
   8367 		      tls_clear = 0;
   8368 		      expecting_tls_get_addr = 2;
   8369 		      break;
   8370 
   8371 		    case R_PPC64_TPREL64:
   8372 		      if (pass == 0
   8373 			  || sec != toc
   8374 			  || toc_ref == NULL
   8375 			  || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
   8376 			continue;
   8377 		      if (ok_tprel)
   8378 			{
   8379 			  /* IE -> LE */
   8380 			  tls_set = TLS_EXPLICIT;
   8381 			  tls_clear = TLS_TPREL;
   8382 			  break;
   8383 			}
   8384 		      continue;
   8385 
   8386 		    case R_PPC64_DTPMOD64:
   8387 		      if (pass == 0
   8388 			  || sec != toc
   8389 			  || toc_ref == NULL
   8390 			  || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
   8391 			continue;
   8392 		      if (rel + 1 < relend
   8393 			  && (rel[1].r_info
   8394 			      == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
   8395 			  && rel[1].r_offset == rel->r_offset + 8)
   8396 			{
   8397 			  if (ok_tprel)
   8398 			    /* GD -> LE */
   8399 			    tls_set = TLS_EXPLICIT | TLS_GD;
   8400 			  else
   8401 			    /* GD -> IE */
   8402 			    tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
   8403 			  tls_clear = TLS_GD;
   8404 			}
   8405 		      else
   8406 			{
   8407 			  if (!is_local)
   8408 			    continue;
   8409 
   8410 			  /* LD -> LE */
   8411 			  tls_set = TLS_EXPLICIT;
   8412 			  tls_clear = TLS_LD;
   8413 			}
   8414 		      break;
   8415 
   8416 		    default:
   8417 		      continue;
   8418 		    }
   8419 
   8420 		  if (pass == 0)
   8421 		    {
   8422 		      if (!expecting_tls_get_addr
   8423 			  || !sec->has_tls_get_addr_call)
   8424 			continue;
   8425 
   8426 		      if (rel + 1 < relend
   8427 			  && branch_reloc_hash_match (ibfd, rel + 1,
   8428 						      htab->tls_get_addr,
   8429 						      htab->tls_get_addr_fd))
   8430 			{
   8431 			  if (expecting_tls_get_addr == 2)
   8432 			    {
   8433 			      /* Check for toc tls entries.  */
   8434 			      unsigned char *toc_tls;
   8435 			      int retval;
   8436 
   8437 			      retval = get_tls_mask (&toc_tls, NULL, NULL,
   8438 						     &locsyms,
   8439 						     rel, ibfd);
   8440 			      if (retval == 0)
   8441 				goto err_free_rel;
   8442 			      if (toc_tls != NULL)
   8443 				{
   8444 				  if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
   8445 				    found_tls_get_addr_arg = 1;
   8446 				  if (retval > 1)
   8447 				    toc_ref[toc_ref_index] = 1;
   8448 				}
   8449 			    }
   8450 			  continue;
   8451 			}
   8452 
   8453 		      if (expecting_tls_get_addr != 1)
   8454 			continue;
   8455 
   8456 		      /* Uh oh, we didn't find the expected call.  We
   8457 			 could just mark this symbol to exclude it
   8458 			 from tls optimization but it's safer to skip
   8459 			 the entire optimization.  */
   8460 		      info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
   8461 						"TLS optimization disabled\n"),
   8462 					      ibfd, sec, rel->r_offset);
   8463 		      ret = TRUE;
   8464 		      goto err_free_rel;
   8465 		    }
   8466 
   8467 		  if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
   8468 		    {
   8469 		      struct plt_entry *ent;
   8470 		      for (ent = htab->tls_get_addr->elf.plt.plist;
   8471 			   ent != NULL;
   8472 			   ent = ent->next)
   8473 			if (ent->addend == 0)
   8474 			  {
   8475 			    if (ent->plt.refcount > 0)
   8476 			      {
   8477 				ent->plt.refcount -= 1;
   8478 				expecting_tls_get_addr = 0;
   8479 			      }
   8480 			    break;
   8481 			  }
   8482 		    }
   8483 
   8484 		  if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
   8485 		    {
   8486 		      struct plt_entry *ent;
   8487 		      for (ent = htab->tls_get_addr_fd->elf.plt.plist;
   8488 			   ent != NULL;
   8489 			   ent = ent->next)
   8490 			if (ent->addend == 0)
   8491 			  {
   8492 			    if (ent->plt.refcount > 0)
   8493 			      ent->plt.refcount -= 1;
   8494 			    break;
   8495 			  }
   8496 		    }
   8497 
   8498 		  if (tls_clear == 0)
   8499 		    continue;
   8500 
   8501 		  if ((tls_set & TLS_EXPLICIT) == 0)
   8502 		    {
   8503 		      struct got_entry *ent;
   8504 
   8505 		      /* Adjust got entry for this reloc.  */
   8506 		      if (h != NULL)
   8507 			ent = h->got.glist;
   8508 		      else
   8509 			ent = elf_local_got_ents (ibfd)[r_symndx];
   8510 
   8511 		      for (; ent != NULL; ent = ent->next)
   8512 			if (ent->addend == rel->r_addend
   8513 			    && ent->owner == ibfd
   8514 			    && ent->tls_type == tls_type)
   8515 			  break;
   8516 		      if (ent == NULL)
   8517 			abort ();
   8518 
   8519 		      if (tls_set == 0)
   8520 			{
   8521 			  /* We managed to get rid of a got entry.  */
   8522 			  if (ent->got.refcount > 0)
   8523 			    ent->got.refcount -= 1;
   8524 			}
   8525 		    }
   8526 		  else
   8527 		    {
   8528 		      /* If we got rid of a DTPMOD/DTPREL reloc pair then
   8529 			 we'll lose one or two dyn relocs.  */
   8530 		      if (!dec_dynrel_count (rel->r_info, sec, info,
   8531 					     NULL, h, sym))
   8532 			return FALSE;
   8533 
   8534 		      if (tls_set == (TLS_EXPLICIT | TLS_GD))
   8535 			{
   8536 			  if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
   8537 						 NULL, h, sym))
   8538 			    return FALSE;
   8539 			}
   8540 		    }
   8541 
   8542 		  *tls_mask |= tls_set;
   8543 		  *tls_mask &= ~tls_clear;
   8544 		}
   8545 
   8546 	      if (elf_section_data (sec)->relocs != relstart)
   8547 		free (relstart);
   8548 	    }
   8549 
   8550 	if (locsyms != NULL
   8551 	    && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
   8552 	  {
   8553 	    if (!info->keep_memory)
   8554 	      free (locsyms);
   8555 	    else
   8556 	      elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
   8557 	  }
   8558       }
   8559 
   8560   if (toc_ref != NULL)
   8561     free (toc_ref);
   8562   return TRUE;
   8563 }
   8564 
   8565 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
   8566    the values of any global symbols in a toc section that has been
   8567    edited.  Globals in toc sections should be a rarity, so this function
   8568    sets a flag if any are found in toc sections other than the one just
   8569    edited, so that futher hash table traversals can be avoided.  */
   8570 
   8571 struct adjust_toc_info
   8572 {
   8573   asection *toc;
   8574   unsigned long *skip;
   8575   bfd_boolean global_toc_syms;
   8576 };
   8577 
   8578 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
   8579 
   8580 static bfd_boolean
   8581 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
   8582 {
   8583   struct ppc_link_hash_entry *eh;
   8584   struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
   8585   unsigned long i;
   8586 
   8587   if (h->root.type != bfd_link_hash_defined
   8588       && h->root.type != bfd_link_hash_defweak)
   8589     return TRUE;
   8590 
   8591   eh = (struct ppc_link_hash_entry *) h;
   8592   if (eh->adjust_done)
   8593     return TRUE;
   8594 
   8595   if (eh->elf.root.u.def.section == toc_inf->toc)
   8596     {
   8597       if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
   8598 	i = toc_inf->toc->rawsize >> 3;
   8599       else
   8600 	i = eh->elf.root.u.def.value >> 3;
   8601 
   8602       if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
   8603 	{
   8604 	  (*_bfd_error_handler)
   8605 	    (_("%s defined on removed toc entry"), eh->elf.root.root.string);
   8606 	  do
   8607 	    ++i;
   8608 	  while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
   8609 	  eh->elf.root.u.def.value = (bfd_vma) i << 3;
   8610 	}
   8611 
   8612       eh->elf.root.u.def.value -= toc_inf->skip[i];
   8613       eh->adjust_done = 1;
   8614     }
   8615   else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
   8616     toc_inf->global_toc_syms = TRUE;
   8617 
   8618   return TRUE;
   8619 }
   8620 
   8621 /* Return TRUE iff INSN is one we expect on a _LO variety toc/got reloc.  */
   8622 
   8623 static bfd_boolean
   8624 ok_lo_toc_insn (unsigned int insn)
   8625 {
   8626   return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
   8627 	  || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
   8628 	  || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
   8629 	  || (insn & (0x3f << 26)) == 36u << 26 /* stw */
   8630 	  || (insn & (0x3f << 26)) == 38u << 26 /* stb */
   8631 	  || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
   8632 	  || (insn & (0x3f << 26)) == 42u << 26 /* lha */
   8633 	  || (insn & (0x3f << 26)) == 44u << 26 /* sth */
   8634 	  || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
   8635 	  || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
   8636 	  || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
   8637 	  || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
   8638 	  || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
   8639 	  || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
   8640 	  || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
   8641 	      && (insn & 3) != 1)
   8642 	  || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
   8643 	      && ((insn & 3) == 0 || (insn & 3) == 3))
   8644 	  || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
   8645 }
   8646 
   8647 /* Examine all relocs referencing .toc sections in order to remove
   8648    unused .toc entries.  */
   8649 
   8650 bfd_boolean
   8651 ppc64_elf_edit_toc (struct bfd_link_info *info)
   8652 {
   8653   bfd *ibfd;
   8654   struct adjust_toc_info toc_inf;
   8655   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   8656 
   8657   htab->do_toc_opt = 1;
   8658   toc_inf.global_toc_syms = TRUE;
   8659   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   8660     {
   8661       asection *toc, *sec;
   8662       Elf_Internal_Shdr *symtab_hdr;
   8663       Elf_Internal_Sym *local_syms;
   8664       Elf_Internal_Rela *relstart, *rel, *toc_relocs;
   8665       unsigned long *skip, *drop;
   8666       unsigned char *used;
   8667       unsigned char *keep, last, some_unused;
   8668 
   8669       if (!is_ppc64_elf (ibfd))
   8670 	continue;
   8671 
   8672       toc = bfd_get_section_by_name (ibfd, ".toc");
   8673       if (toc == NULL
   8674 	  || toc->size == 0
   8675 	  || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
   8676 	  || discarded_section (toc))
   8677 	continue;
   8678 
   8679       toc_relocs = NULL;
   8680       local_syms = NULL;
   8681       symtab_hdr = &elf_symtab_hdr (ibfd);
   8682 
   8683       /* Look at sections dropped from the final link.  */
   8684       skip = NULL;
   8685       relstart = NULL;
   8686       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   8687 	{
   8688 	  if (sec->reloc_count == 0
   8689 	      || !discarded_section (sec)
   8690 	      || get_opd_info (sec)
   8691 	      || (sec->flags & SEC_ALLOC) == 0
   8692 	      || (sec->flags & SEC_DEBUGGING) != 0)
   8693 	    continue;
   8694 
   8695 	  relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
   8696 	  if (relstart == NULL)
   8697 	    goto error_ret;
   8698 
   8699 	  /* Run through the relocs to see which toc entries might be
   8700 	     unused.  */
   8701 	  for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
   8702 	    {
   8703 	      enum elf_ppc64_reloc_type r_type;
   8704 	      unsigned long r_symndx;
   8705 	      asection *sym_sec;
   8706 	      struct elf_link_hash_entry *h;
   8707 	      Elf_Internal_Sym *sym;
   8708 	      bfd_vma val;
   8709 
   8710 	      r_type = ELF64_R_TYPE (rel->r_info);
   8711 	      switch (r_type)
   8712 		{
   8713 		default:
   8714 		  continue;
   8715 
   8716 		case R_PPC64_TOC16:
   8717 		case R_PPC64_TOC16_LO:
   8718 		case R_PPC64_TOC16_HI:
   8719 		case R_PPC64_TOC16_HA:
   8720 		case R_PPC64_TOC16_DS:
   8721 		case R_PPC64_TOC16_LO_DS:
   8722 		  break;
   8723 		}
   8724 
   8725 	      r_symndx = ELF64_R_SYM (rel->r_info);
   8726 	      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   8727 			      r_symndx, ibfd))
   8728 		goto error_ret;
   8729 
   8730 	      if (sym_sec != toc)
   8731 		continue;
   8732 
   8733 	      if (h != NULL)
   8734 		val = h->root.u.def.value;
   8735 	      else
   8736 		val = sym->st_value;
   8737 	      val += rel->r_addend;
   8738 
   8739 	      if (val >= toc->size)
   8740 		continue;
   8741 
   8742 	      /* Anything in the toc ought to be aligned to 8 bytes.
   8743 		 If not, don't mark as unused.  */
   8744 	      if (val & 7)
   8745 		continue;
   8746 
   8747 	      if (skip == NULL)
   8748 		{
   8749 		  skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
   8750 		  if (skip == NULL)
   8751 		    goto error_ret;
   8752 		}
   8753 
   8754 	      skip[val >> 3] = ref_from_discarded;
   8755 	    }
   8756 
   8757 	  if (elf_section_data (sec)->relocs != relstart)
   8758 	    free (relstart);
   8759 	}
   8760 
   8761       /* For largetoc loads of address constants, we can convert
   8762 	 .  addis rx,2,addr@got@ha
   8763 	 .  ld ry,addr@got@l(rx)
   8764 	 to
   8765 	 .  addis rx,2,addr@toc@ha
   8766 	 .  addi ry,rx,addr@toc@l
   8767 	 when addr is within 2G of the toc pointer.  This then means
   8768 	 that the word storing "addr" in the toc is no longer needed.  */
   8769 
   8770       if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
   8771 	  && toc->output_section->rawsize < (bfd_vma) 1 << 31
   8772 	  && toc->reloc_count != 0)
   8773 	{
   8774 	  /* Read toc relocs.  */
   8775 	  toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
   8776 						  info->keep_memory);
   8777 	  if (toc_relocs == NULL)
   8778 	    goto error_ret;
   8779 
   8780 	  for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
   8781 	    {
   8782 	      enum elf_ppc64_reloc_type r_type;
   8783 	      unsigned long r_symndx;
   8784 	      asection *sym_sec;
   8785 	      struct elf_link_hash_entry *h;
   8786 	      Elf_Internal_Sym *sym;
   8787 	      bfd_vma val, addr;
   8788 
   8789 	      r_type = ELF64_R_TYPE (rel->r_info);
   8790 	      if (r_type != R_PPC64_ADDR64)
   8791 		continue;
   8792 
   8793 	      r_symndx = ELF64_R_SYM (rel->r_info);
   8794 	      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   8795 			      r_symndx, ibfd))
   8796 		goto error_ret;
   8797 
   8798 	      if (sym_sec == NULL
   8799 		  || discarded_section (sym_sec))
   8800 		continue;
   8801 
   8802 	      if (!SYMBOL_REFERENCES_LOCAL (info, h))
   8803 		continue;
   8804 
   8805 	      if (h != NULL)
   8806 		{
   8807 		  if (h->type == STT_GNU_IFUNC)
   8808 		    continue;
   8809 		  val = h->root.u.def.value;
   8810 		}
   8811 	      else
   8812 		{
   8813 		  if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
   8814 		    continue;
   8815 		  val = sym->st_value;
   8816 		}
   8817 	      val += rel->r_addend;
   8818 	      val += sym_sec->output_section->vma + sym_sec->output_offset;
   8819 
   8820 	      /* We don't yet know the exact toc pointer value, but we
   8821 		 know it will be somewhere in the toc section.  Don't
   8822 		 optimize if the difference from any possible toc
   8823 		 pointer is outside [ff..f80008000, 7fff7fff].  */
   8824 	      addr = toc->output_section->vma + TOC_BASE_OFF;
   8825 	      if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
   8826 		continue;
   8827 
   8828 	      addr = toc->output_section->vma + toc->output_section->rawsize;
   8829 	      if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
   8830 		continue;
   8831 
   8832 	      if (skip == NULL)
   8833 		{
   8834 		  skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
   8835 		  if (skip == NULL)
   8836 		    goto error_ret;
   8837 		}
   8838 
   8839 	      skip[rel->r_offset >> 3]
   8840 		|= can_optimize | ((rel - toc_relocs) << 2);
   8841 	    }
   8842 	}
   8843 
   8844       if (skip == NULL)
   8845 	continue;
   8846 
   8847       used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
   8848       if (used == NULL)
   8849 	{
   8850 	error_ret:
   8851 	  if (local_syms != NULL
   8852 	      && symtab_hdr->contents != (unsigned char *) local_syms)
   8853 	    free (local_syms);
   8854 	  if (sec != NULL
   8855 	      && relstart != NULL
   8856 	      && elf_section_data (sec)->relocs != relstart)
   8857 	    free (relstart);
   8858 	  if (toc_relocs != NULL
   8859 	      && elf_section_data (toc)->relocs != toc_relocs)
   8860 	    free (toc_relocs);
   8861 	  if (skip != NULL)
   8862 	    free (skip);
   8863 	  return FALSE;
   8864 	}
   8865 
   8866       /* Now check all kept sections that might reference the toc.
   8867 	 Check the toc itself last.  */
   8868       for (sec = (ibfd->sections == toc && toc->next ? toc->next
   8869 		  : ibfd->sections);
   8870 	   sec != NULL;
   8871 	   sec = (sec == toc ? NULL
   8872 		  : sec->next == NULL ? toc
   8873 		  : sec->next == toc && toc->next ? toc->next
   8874 		  : sec->next))
   8875 	{
   8876 	  int repeat;
   8877 
   8878 	  if (sec->reloc_count == 0
   8879 	      || discarded_section (sec)
   8880 	      || get_opd_info (sec)
   8881 	      || (sec->flags & SEC_ALLOC) == 0
   8882 	      || (sec->flags & SEC_DEBUGGING) != 0)
   8883 	    continue;
   8884 
   8885 	  relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   8886 						info->keep_memory);
   8887 	  if (relstart == NULL)
   8888 	    {
   8889 	      free (used);
   8890 	      goto error_ret;
   8891 	    }
   8892 
   8893 	  /* Mark toc entries referenced as used.  */
   8894 	  do
   8895 	    {
   8896 	      repeat = 0;
   8897 	      for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
   8898 		{
   8899 		  enum elf_ppc64_reloc_type r_type;
   8900 		  unsigned long r_symndx;
   8901 		  asection *sym_sec;
   8902 		  struct elf_link_hash_entry *h;
   8903 		  Elf_Internal_Sym *sym;
   8904 		  bfd_vma val;
   8905 		  enum {no_check, check_lo, check_ha} insn_check;
   8906 
   8907 		  r_type = ELF64_R_TYPE (rel->r_info);
   8908 		  switch (r_type)
   8909 		    {
   8910 		    default:
   8911 		      insn_check = no_check;
   8912 		      break;
   8913 
   8914 		    case R_PPC64_GOT_TLSLD16_HA:
   8915 		    case R_PPC64_GOT_TLSGD16_HA:
   8916 		    case R_PPC64_GOT_TPREL16_HA:
   8917 		    case R_PPC64_GOT_DTPREL16_HA:
   8918 		    case R_PPC64_GOT16_HA:
   8919 		    case R_PPC64_TOC16_HA:
   8920 		      insn_check = check_ha;
   8921 		      break;
   8922 
   8923 		    case R_PPC64_GOT_TLSLD16_LO:
   8924 		    case R_PPC64_GOT_TLSGD16_LO:
   8925 		    case R_PPC64_GOT_TPREL16_LO_DS:
   8926 		    case R_PPC64_GOT_DTPREL16_LO_DS:
   8927 		    case R_PPC64_GOT16_LO:
   8928 		    case R_PPC64_GOT16_LO_DS:
   8929 		    case R_PPC64_TOC16_LO:
   8930 		    case R_PPC64_TOC16_LO_DS:
   8931 		      insn_check = check_lo;
   8932 		      break;
   8933 		    }
   8934 
   8935 		  if (insn_check != no_check)
   8936 		    {
   8937 		      bfd_vma off = rel->r_offset & ~3;
   8938 		      unsigned char buf[4];
   8939 		      unsigned int insn;
   8940 
   8941 		      if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
   8942 			{
   8943 			  free (used);
   8944 			  goto error_ret;
   8945 			}
   8946 		      insn = bfd_get_32 (ibfd, buf);
   8947 		      if (insn_check == check_lo
   8948 			  ? !ok_lo_toc_insn (insn)
   8949 			  : ((insn & ((0x3f << 26) | 0x1f << 16))
   8950 			     != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
   8951 			{
   8952 			  char str[12];
   8953 
   8954 			  ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
   8955 			  sprintf (str, "%#08x", insn);
   8956 			  info->callbacks->einfo
   8957 			    (_("%P: %H: toc optimization is not supported for"
   8958 			       " %s instruction.\n"),
   8959 			     ibfd, sec, rel->r_offset & ~3, str);
   8960 			}
   8961 		    }
   8962 
   8963 		  switch (r_type)
   8964 		    {
   8965 		    case R_PPC64_TOC16:
   8966 		    case R_PPC64_TOC16_LO:
   8967 		    case R_PPC64_TOC16_HI:
   8968 		    case R_PPC64_TOC16_HA:
   8969 		    case R_PPC64_TOC16_DS:
   8970 		    case R_PPC64_TOC16_LO_DS:
   8971 		      /* In case we're taking addresses of toc entries.  */
   8972 		    case R_PPC64_ADDR64:
   8973 		      break;
   8974 
   8975 		    default:
   8976 		      continue;
   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 		    {
   8983 		      free (used);
   8984 		      goto error_ret;
   8985 		    }
   8986 
   8987 		  if (sym_sec != toc)
   8988 		    continue;
   8989 
   8990 		  if (h != NULL)
   8991 		    val = h->root.u.def.value;
   8992 		  else
   8993 		    val = sym->st_value;
   8994 		  val += rel->r_addend;
   8995 
   8996 		  if (val >= toc->size)
   8997 		    continue;
   8998 
   8999 		  if ((skip[val >> 3] & can_optimize) != 0)
   9000 		    {
   9001 		      bfd_vma off;
   9002 		      unsigned char opc;
   9003 
   9004 		      switch (r_type)
   9005 			{
   9006 			case R_PPC64_TOC16_HA:
   9007 			  break;
   9008 
   9009 			case R_PPC64_TOC16_LO_DS:
   9010 			  off = rel->r_offset;
   9011 			  off += (bfd_big_endian (ibfd) ? -2 : 3);
   9012 			  if (!bfd_get_section_contents (ibfd, sec, &opc,
   9013 							 off, 1))
   9014 			    {
   9015 			      free (used);
   9016 			      goto error_ret;
   9017 			    }
   9018 			  if ((opc & (0x3f << 2)) == (58u << 2))
   9019 			    break;
   9020 			  /* Fall thru */
   9021 
   9022 			default:
   9023 			  /* Wrong sort of reloc, or not a ld.  We may
   9024 			     as well clear ref_from_discarded too.  */
   9025 			  skip[val >> 3] = 0;
   9026 			}
   9027 		    }
   9028 
   9029 		  if (sec != toc)
   9030 		    used[val >> 3] = 1;
   9031 		  /* For the toc section, we only mark as used if this
   9032 		     entry itself isn't unused.  */
   9033 		  else if ((used[rel->r_offset >> 3]
   9034 			    || !(skip[rel->r_offset >> 3] & ref_from_discarded))
   9035 			   && !used[val >> 3])
   9036 		    {
   9037 		      /* Do all the relocs again, to catch reference
   9038 			 chains.  */
   9039 		      repeat = 1;
   9040 		      used[val >> 3] = 1;
   9041 		    }
   9042 		}
   9043 	    }
   9044 	  while (repeat);
   9045 
   9046 	  if (elf_section_data (sec)->relocs != relstart)
   9047 	    free (relstart);
   9048 	}
   9049 
   9050       /* Merge the used and skip arrays.  Assume that TOC
   9051 	 doublewords not appearing as either used or unused belong
   9052 	 to to an entry more than one doubleword in size.  */
   9053       for (drop = skip, keep = used, last = 0, some_unused = 0;
   9054 	   drop < skip + (toc->size + 7) / 8;
   9055 	   ++drop, ++keep)
   9056 	{
   9057 	  if (*keep)
   9058 	    {
   9059 	      *drop &= ~ref_from_discarded;
   9060 	      if ((*drop & can_optimize) != 0)
   9061 		some_unused = 1;
   9062 	      last = 0;
   9063 	    }
   9064 	  else if ((*drop & ref_from_discarded) != 0)
   9065 	    {
   9066 	      some_unused = 1;
   9067 	      last = ref_from_discarded;
   9068 	    }
   9069 	  else
   9070 	    *drop = last;
   9071 	}
   9072 
   9073       free (used);
   9074 
   9075       if (some_unused)
   9076 	{
   9077 	  bfd_byte *contents, *src;
   9078 	  unsigned long off;
   9079 	  Elf_Internal_Sym *sym;
   9080 	  bfd_boolean local_toc_syms = FALSE;
   9081 
   9082 	  /* Shuffle the toc contents, and at the same time convert the
   9083 	     skip array from booleans into offsets.  */
   9084 	  if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
   9085 	    goto error_ret;
   9086 
   9087 	  elf_section_data (toc)->this_hdr.contents = contents;
   9088 
   9089 	  for (src = contents, off = 0, drop = skip;
   9090 	       src < contents + toc->size;
   9091 	       src += 8, ++drop)
   9092 	    {
   9093 	      if ((*drop & (can_optimize | ref_from_discarded)) != 0)
   9094 		off += 8;
   9095 	      else if (off != 0)
   9096 		{
   9097 		  *drop = off;
   9098 		  memcpy (src - off, src, 8);
   9099 		}
   9100 	    }
   9101 	  *drop = off;
   9102 	  toc->rawsize = toc->size;
   9103 	  toc->size = src - contents - off;
   9104 
   9105 	  /* Adjust addends for relocs against the toc section sym,
   9106 	     and optimize any accesses we can.  */
   9107 	  for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   9108 	    {
   9109 	      if (sec->reloc_count == 0
   9110 		  || discarded_section (sec))
   9111 		continue;
   9112 
   9113 	      relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   9114 						    info->keep_memory);
   9115 	      if (relstart == NULL)
   9116 		goto error_ret;
   9117 
   9118 	      for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
   9119 		{
   9120 		  enum elf_ppc64_reloc_type r_type;
   9121 		  unsigned long r_symndx;
   9122 		  asection *sym_sec;
   9123 		  struct elf_link_hash_entry *h;
   9124 		  bfd_vma val;
   9125 
   9126 		  r_type = ELF64_R_TYPE (rel->r_info);
   9127 		  switch (r_type)
   9128 		    {
   9129 		    default:
   9130 		      continue;
   9131 
   9132 		    case R_PPC64_TOC16:
   9133 		    case R_PPC64_TOC16_LO:
   9134 		    case R_PPC64_TOC16_HI:
   9135 		    case R_PPC64_TOC16_HA:
   9136 		    case R_PPC64_TOC16_DS:
   9137 		    case R_PPC64_TOC16_LO_DS:
   9138 		    case R_PPC64_ADDR64:
   9139 		      break;
   9140 		    }
   9141 
   9142 		  r_symndx = ELF64_R_SYM (rel->r_info);
   9143 		  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   9144 				  r_symndx, ibfd))
   9145 		    goto error_ret;
   9146 
   9147 		  if (sym_sec != toc)
   9148 		    continue;
   9149 
   9150 		  if (h != NULL)
   9151 		    val = h->root.u.def.value;
   9152 		  else
   9153 		    {
   9154 		      val = sym->st_value;
   9155 		      if (val != 0)
   9156 			local_toc_syms = TRUE;
   9157 		    }
   9158 
   9159 		  val += rel->r_addend;
   9160 
   9161 		  if (val > toc->rawsize)
   9162 		    val = toc->rawsize;
   9163 		  else if ((skip[val >> 3] & ref_from_discarded) != 0)
   9164 		    continue;
   9165 		  else if ((skip[val >> 3] & can_optimize) != 0)
   9166 		    {
   9167 		      Elf_Internal_Rela *tocrel
   9168 			= toc_relocs + (skip[val >> 3] >> 2);
   9169 		      unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
   9170 
   9171 		      switch (r_type)
   9172 			{
   9173 			case R_PPC64_TOC16_HA:
   9174 			  rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
   9175 			  break;
   9176 
   9177 			case R_PPC64_TOC16_LO_DS:
   9178 			  rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
   9179 			  break;
   9180 
   9181 			default:
   9182 			  if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
   9183 			    ppc_howto_init ();
   9184 			  info->callbacks->einfo
   9185 			    (_("%P: %H: %s references "
   9186 			       "optimized away TOC entry\n"),
   9187 			     ibfd, sec, rel->r_offset,
   9188 			     ppc64_elf_howto_table[r_type]->name);
   9189 			  bfd_set_error (bfd_error_bad_value);
   9190 			  goto error_ret;
   9191 			}
   9192 		      rel->r_addend = tocrel->r_addend;
   9193 		      elf_section_data (sec)->relocs = relstart;
   9194 		      continue;
   9195 		    }
   9196 
   9197 		  if (h != NULL || sym->st_value != 0)
   9198 		    continue;
   9199 
   9200 		  rel->r_addend -= skip[val >> 3];
   9201 		  elf_section_data (sec)->relocs = relstart;
   9202 		}
   9203 
   9204 	      if (elf_section_data (sec)->relocs != relstart)
   9205 		free (relstart);
   9206 	    }
   9207 
   9208 	  /* We shouldn't have local or global symbols defined in the TOC,
   9209 	     but handle them anyway.  */
   9210 	  if (local_syms != NULL)
   9211 	    for (sym = local_syms;
   9212 		 sym < local_syms + symtab_hdr->sh_info;
   9213 		 ++sym)
   9214 	      if (sym->st_value != 0
   9215 		  && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
   9216 		{
   9217 		  unsigned long i;
   9218 
   9219 		  if (sym->st_value > toc->rawsize)
   9220 		    i = toc->rawsize >> 3;
   9221 		  else
   9222 		    i = sym->st_value >> 3;
   9223 
   9224 		  if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
   9225 		    {
   9226 		      if (local_toc_syms)
   9227 			(*_bfd_error_handler)
   9228 			  (_("%s defined on removed toc entry"),
   9229 			   bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
   9230 		      do
   9231 			++i;
   9232 		      while ((skip[i] & (ref_from_discarded | can_optimize)));
   9233 		      sym->st_value = (bfd_vma) i << 3;
   9234 		    }
   9235 
   9236 		  sym->st_value -= skip[i];
   9237 		  symtab_hdr->contents = (unsigned char *) local_syms;
   9238 		}
   9239 
   9240 	  /* Adjust any global syms defined in this toc input section.  */
   9241 	  if (toc_inf.global_toc_syms)
   9242 	    {
   9243 	      toc_inf.toc = toc;
   9244 	      toc_inf.skip = skip;
   9245 	      toc_inf.global_toc_syms = FALSE;
   9246 	      elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
   9247 				      &toc_inf);
   9248 	    }
   9249 
   9250 	  if (toc->reloc_count != 0)
   9251 	    {
   9252 	      Elf_Internal_Shdr *rel_hdr;
   9253 	      Elf_Internal_Rela *wrel;
   9254 	      bfd_size_type sz;
   9255 
   9256 	      /* Remove unused toc relocs, and adjust those we keep.  */
   9257 	      if (toc_relocs == NULL)
   9258 		toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
   9259 							info->keep_memory);
   9260 	      if (toc_relocs == NULL)
   9261 		goto error_ret;
   9262 
   9263 	      wrel = toc_relocs;
   9264 	      for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
   9265 		if ((skip[rel->r_offset >> 3]
   9266 		     & (ref_from_discarded | can_optimize)) == 0)
   9267 		  {
   9268 		    wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
   9269 		    wrel->r_info = rel->r_info;
   9270 		    wrel->r_addend = rel->r_addend;
   9271 		    ++wrel;
   9272 		  }
   9273 		else if (!dec_dynrel_count (rel->r_info, toc, info,
   9274 					    &local_syms, NULL, NULL))
   9275 		  goto error_ret;
   9276 
   9277 	      elf_section_data (toc)->relocs = toc_relocs;
   9278 	      toc->reloc_count = wrel - toc_relocs;
   9279 	      rel_hdr = _bfd_elf_single_rel_hdr (toc);
   9280 	      sz = rel_hdr->sh_entsize;
   9281 	      rel_hdr->sh_size = toc->reloc_count * sz;
   9282 	    }
   9283 	}
   9284       else if (toc_relocs != NULL
   9285 	       && elf_section_data (toc)->relocs != toc_relocs)
   9286 	free (toc_relocs);
   9287 
   9288       if (local_syms != NULL
   9289 	  && symtab_hdr->contents != (unsigned char *) local_syms)
   9290 	{
   9291 	  if (!info->keep_memory)
   9292 	    free (local_syms);
   9293 	  else
   9294 	    symtab_hdr->contents = (unsigned char *) local_syms;
   9295 	}
   9296       free (skip);
   9297     }
   9298 
   9299   return TRUE;
   9300 }
   9301 
   9302 /* Return true iff input section I references the TOC using
   9303    instructions limited to +/-32k offsets.  */
   9304 
   9305 bfd_boolean
   9306 ppc64_elf_has_small_toc_reloc (asection *i)
   9307 {
   9308   return (is_ppc64_elf (i->owner)
   9309 	  && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
   9310 }
   9311 
   9312 /* Allocate space for one GOT entry.  */
   9313 
   9314 static void
   9315 allocate_got (struct elf_link_hash_entry *h,
   9316 	      struct bfd_link_info *info,
   9317 	      struct got_entry *gent)
   9318 {
   9319   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   9320   bfd_boolean dyn;
   9321   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
   9322   int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
   9323 		 ? 16 : 8);
   9324   int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
   9325 		  ? 2 : 1) * sizeof (Elf64_External_Rela);
   9326   asection *got = ppc64_elf_tdata (gent->owner)->got;
   9327 
   9328   gent->got.offset = got->size;
   9329   got->size += entsize;
   9330 
   9331   dyn = htab->elf.dynamic_sections_created;
   9332   if (h->type == STT_GNU_IFUNC)
   9333     {
   9334       htab->elf.irelplt->size += rentsize;
   9335       htab->got_reli_size += rentsize;
   9336     }
   9337   else if ((info->shared
   9338 	    || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
   9339 	   && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   9340 	       || h->root.type != bfd_link_hash_undefweak))
   9341     {
   9342       asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
   9343       relgot->size += rentsize;
   9344     }
   9345 }
   9346 
   9347 /* This function merges got entries in the same toc group.  */
   9348 
   9349 static void
   9350 merge_got_entries (struct got_entry **pent)
   9351 {
   9352   struct got_entry *ent, *ent2;
   9353 
   9354   for (ent = *pent; ent != NULL; ent = ent->next)
   9355     if (!ent->is_indirect)
   9356       for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
   9357 	if (!ent2->is_indirect
   9358 	    && ent2->addend == ent->addend
   9359 	    && ent2->tls_type == ent->tls_type
   9360 	    && elf_gp (ent2->owner) == elf_gp (ent->owner))
   9361 	  {
   9362 	    ent2->is_indirect = TRUE;
   9363 	    ent2->got.ent = ent;
   9364 	  }
   9365 }
   9366 
   9367 /* Allocate space in .plt, .got and associated reloc sections for
   9368    dynamic relocs.  */
   9369 
   9370 static bfd_boolean
   9371 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
   9372 {
   9373   struct bfd_link_info *info;
   9374   struct ppc_link_hash_table *htab;
   9375   asection *s;
   9376   struct ppc_link_hash_entry *eh;
   9377   struct elf_dyn_relocs *p;
   9378   struct got_entry **pgent, *gent;
   9379 
   9380   if (h->root.type == bfd_link_hash_indirect)
   9381     return TRUE;
   9382 
   9383   info = (struct bfd_link_info *) inf;
   9384   htab = ppc_hash_table (info);
   9385   if (htab == NULL)
   9386     return FALSE;
   9387 
   9388   if ((htab->elf.dynamic_sections_created
   9389        && h->dynindx != -1
   9390        && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
   9391       || h->type == STT_GNU_IFUNC)
   9392     {
   9393       struct plt_entry *pent;
   9394       bfd_boolean doneone = FALSE;
   9395       for (pent = h->plt.plist; pent != NULL; pent = pent->next)
   9396 	if (pent->plt.refcount > 0)
   9397 	  {
   9398 	    if (!htab->elf.dynamic_sections_created
   9399 		|| h->dynindx == -1)
   9400 	      {
   9401 		s = htab->elf.iplt;
   9402 		pent->plt.offset = s->size;
   9403 		s->size += PLT_ENTRY_SIZE (htab);
   9404 		s = htab->elf.irelplt;
   9405 	      }
   9406 	    else
   9407 	      {
   9408 		/* If this is the first .plt entry, make room for the special
   9409 		   first entry.  */
   9410 		s = htab->elf.splt;
   9411 		if (s->size == 0)
   9412 		  s->size += PLT_INITIAL_ENTRY_SIZE (htab);
   9413 
   9414 		pent->plt.offset = s->size;
   9415 
   9416 		/* Make room for this entry.  */
   9417 		s->size += PLT_ENTRY_SIZE (htab);
   9418 
   9419 		/* Make room for the .glink code.  */
   9420 		s = htab->glink;
   9421 		if (s->size == 0)
   9422 		  s->size += GLINK_CALL_STUB_SIZE;
   9423 		if (htab->opd_abi)
   9424 		  {
   9425 		    /* We need bigger stubs past index 32767.  */
   9426 		    if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
   9427 		      s->size += 4;
   9428 		    s->size += 2*4;
   9429 		  }
   9430 		else
   9431 		  s->size += 4;
   9432 
   9433 		/* We also need to make an entry in the .rela.plt section.  */
   9434 		s = htab->elf.srelplt;
   9435 	      }
   9436 	    s->size += sizeof (Elf64_External_Rela);
   9437 	    doneone = TRUE;
   9438 	  }
   9439 	else
   9440 	  pent->plt.offset = (bfd_vma) -1;
   9441       if (!doneone)
   9442 	{
   9443 	  h->plt.plist = NULL;
   9444 	  h->needs_plt = 0;
   9445 	}
   9446     }
   9447   else
   9448     {
   9449       h->plt.plist = NULL;
   9450       h->needs_plt = 0;
   9451     }
   9452 
   9453   eh = (struct ppc_link_hash_entry *) h;
   9454   /* Run through the TLS GD got entries first if we're changing them
   9455      to TPREL.  */
   9456   if ((eh->tls_mask & TLS_TPRELGD) != 0)
   9457     for (gent = h->got.glist; gent != NULL; gent = gent->next)
   9458       if (gent->got.refcount > 0
   9459 	  && (gent->tls_type & TLS_GD) != 0)
   9460 	{
   9461 	  /* This was a GD entry that has been converted to TPREL.  If
   9462 	     there happens to be a TPREL entry we can use that one.  */
   9463 	  struct got_entry *ent;
   9464 	  for (ent = h->got.glist; ent != NULL; ent = ent->next)
   9465 	    if (ent->got.refcount > 0
   9466 		&& (ent->tls_type & TLS_TPREL) != 0
   9467 		&& ent->addend == gent->addend
   9468 		&& ent->owner == gent->owner)
   9469 	      {
   9470 		gent->got.refcount = 0;
   9471 		break;
   9472 	      }
   9473 
   9474 	  /* If not, then we'll be using our own TPREL entry.  */
   9475 	  if (gent->got.refcount != 0)
   9476 	    gent->tls_type = TLS_TLS | TLS_TPREL;
   9477 	}
   9478 
   9479   /* Remove any list entry that won't generate a word in the GOT before
   9480      we call merge_got_entries.  Otherwise we risk merging to empty
   9481      entries.  */
   9482   pgent = &h->got.glist;
   9483   while ((gent = *pgent) != NULL)
   9484     if (gent->got.refcount > 0)
   9485       {
   9486 	if ((gent->tls_type & TLS_LD) != 0
   9487 	    && !h->def_dynamic)
   9488 	  {
   9489 	    ppc64_tlsld_got (gent->owner)->got.refcount += 1;
   9490 	    *pgent = gent->next;
   9491 	  }
   9492 	else
   9493 	  pgent = &gent->next;
   9494       }
   9495     else
   9496       *pgent = gent->next;
   9497 
   9498   if (!htab->do_multi_toc)
   9499     merge_got_entries (&h->got.glist);
   9500 
   9501   for (gent = h->got.glist; gent != NULL; gent = gent->next)
   9502     if (!gent->is_indirect)
   9503       {
   9504 	/* Make sure this symbol is output as a dynamic symbol.
   9505 	   Undefined weak syms won't yet be marked as dynamic,
   9506 	   nor will all TLS symbols.  */
   9507 	if (h->dynindx == -1
   9508 	    && !h->forced_local
   9509 	    && h->type != STT_GNU_IFUNC
   9510 	    && htab->elf.dynamic_sections_created)
   9511 	  {
   9512 	    if (! bfd_elf_link_record_dynamic_symbol (info, h))
   9513 	      return FALSE;
   9514 	  }
   9515 
   9516 	if (!is_ppc64_elf (gent->owner))
   9517 	  abort ();
   9518 
   9519 	allocate_got (h, info, gent);
   9520       }
   9521 
   9522   if (eh->dyn_relocs == NULL
   9523       || (!htab->elf.dynamic_sections_created
   9524 	  && h->type != STT_GNU_IFUNC))
   9525     return TRUE;
   9526 
   9527   /* In the shared -Bsymbolic case, discard space allocated for
   9528      dynamic pc-relative relocs against symbols which turn out to be
   9529      defined in regular objects.  For the normal shared case, discard
   9530      space for relocs that have become local due to symbol visibility
   9531      changes.  */
   9532 
   9533   if (info->shared)
   9534     {
   9535       /* Relocs that use pc_count are those that appear on a call insn,
   9536 	 or certain REL relocs (see must_be_dyn_reloc) that can be
   9537 	 generated via assembly.  We want calls to protected symbols to
   9538 	 resolve directly to the function rather than going via the plt.
   9539 	 If people want function pointer comparisons to work as expected
   9540 	 then they should avoid writing weird assembly.  */
   9541       if (SYMBOL_CALLS_LOCAL (info, h))
   9542 	{
   9543 	  struct elf_dyn_relocs **pp;
   9544 
   9545 	  for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
   9546 	    {
   9547 	      p->count -= p->pc_count;
   9548 	      p->pc_count = 0;
   9549 	      if (p->count == 0)
   9550 		*pp = p->next;
   9551 	      else
   9552 		pp = &p->next;
   9553 	    }
   9554 	}
   9555 
   9556       /* Also discard relocs on undefined weak syms with non-default
   9557 	 visibility.  */
   9558       if (eh->dyn_relocs != NULL
   9559 	  && h->root.type == bfd_link_hash_undefweak)
   9560 	{
   9561 	  if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
   9562 	    eh->dyn_relocs = NULL;
   9563 
   9564 	  /* Make sure this symbol is output as a dynamic symbol.
   9565 	     Undefined weak syms won't yet be marked as dynamic.  */
   9566 	  else if (h->dynindx == -1
   9567 		   && !h->forced_local)
   9568 	    {
   9569 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   9570 		return FALSE;
   9571 	    }
   9572 	}
   9573     }
   9574   else if (h->type == STT_GNU_IFUNC)
   9575     {
   9576       if (!h->non_got_ref)
   9577 	eh->dyn_relocs = NULL;
   9578     }
   9579   else if (ELIMINATE_COPY_RELOCS)
   9580     {
   9581       /* For the non-shared case, discard space for relocs against
   9582 	 symbols which turn out to need copy relocs or are not
   9583 	 dynamic.  */
   9584 
   9585       if (!h->non_got_ref
   9586 	  && !h->def_regular)
   9587 	{
   9588 	  /* Make sure this symbol is output as a dynamic symbol.
   9589 	     Undefined weak syms won't yet be marked as dynamic.  */
   9590 	  if (h->dynindx == -1
   9591 	      && !h->forced_local)
   9592 	    {
   9593 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   9594 		return FALSE;
   9595 	    }
   9596 
   9597 	  /* If that succeeded, we know we'll be keeping all the
   9598 	     relocs.  */
   9599 	  if (h->dynindx != -1)
   9600 	    goto keep;
   9601 	}
   9602 
   9603       eh->dyn_relocs = NULL;
   9604 
   9605     keep: ;
   9606     }
   9607 
   9608   /* Finally, allocate space.  */
   9609   for (p = eh->dyn_relocs; p != NULL; p = p->next)
   9610     {
   9611       asection *sreloc = elf_section_data (p->sec)->sreloc;
   9612       if (eh->elf.type == STT_GNU_IFUNC)
   9613 	sreloc = htab->elf.irelplt;
   9614       sreloc->size += p->count * sizeof (Elf64_External_Rela);
   9615     }
   9616 
   9617   return TRUE;
   9618 }
   9619 
   9620 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
   9621    to set up space for global entry stubs.  These are put in glink,
   9622    after the branch table.  */
   9623 
   9624 static bfd_boolean
   9625 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
   9626 {
   9627   struct bfd_link_info *info;
   9628   struct ppc_link_hash_table *htab;
   9629   struct plt_entry *pent;
   9630   asection *s;
   9631 
   9632   if (h->root.type == bfd_link_hash_indirect)
   9633     return TRUE;
   9634 
   9635   if (!h->pointer_equality_needed)
   9636     return TRUE;
   9637 
   9638   if (h->def_regular)
   9639     return TRUE;
   9640 
   9641   info = inf;
   9642   htab = ppc_hash_table (info);
   9643   if (htab == NULL)
   9644     return FALSE;
   9645 
   9646   s = htab->glink;
   9647   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
   9648     if (pent->plt.offset != (bfd_vma) -1
   9649 	&& pent->addend == 0)
   9650       {
   9651 	/* For ELFv2, if this symbol is not defined in a regular file
   9652 	   and we are not generating a shared library or pie, then we
   9653 	   need to define the symbol in the executable on a call stub.
   9654 	   This is to avoid text relocations.  */
   9655 	s->size = (s->size + 15) & -16;
   9656 	h->root.u.def.section = s;
   9657 	h->root.u.def.value = s->size;
   9658 	s->size += 16;
   9659 	break;
   9660       }
   9661   return TRUE;
   9662 }
   9663 
   9664 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
   9665    read-only sections.  */
   9666 
   9667 static bfd_boolean
   9668 maybe_set_textrel (struct elf_link_hash_entry *h, void *info)
   9669 {
   9670   if (h->root.type == bfd_link_hash_indirect)
   9671     return TRUE;
   9672 
   9673   if (readonly_dynrelocs (h))
   9674     {
   9675       ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
   9676 
   9677       /* Not an error, just cut short the traversal.  */
   9678       return FALSE;
   9679     }
   9680   return TRUE;
   9681 }
   9682 
   9683 /* Set the sizes of the dynamic sections.  */
   9684 
   9685 static bfd_boolean
   9686 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
   9687 				 struct bfd_link_info *info)
   9688 {
   9689   struct ppc_link_hash_table *htab;
   9690   bfd *dynobj;
   9691   asection *s;
   9692   bfd_boolean relocs;
   9693   bfd *ibfd;
   9694   struct got_entry *first_tlsld;
   9695 
   9696   htab = ppc_hash_table (info);
   9697   if (htab == NULL)
   9698     return FALSE;
   9699 
   9700   dynobj = htab->elf.dynobj;
   9701   if (dynobj == NULL)
   9702     abort ();
   9703 
   9704   if (htab->elf.dynamic_sections_created)
   9705     {
   9706       /* Set the contents of the .interp section to the interpreter.  */
   9707       if (info->executable)
   9708 	{
   9709 	  s = bfd_get_linker_section (dynobj, ".interp");
   9710 	  if (s == NULL)
   9711 	    abort ();
   9712 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
   9713 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
   9714 	}
   9715     }
   9716 
   9717   /* Set up .got offsets for local syms, and space for local dynamic
   9718      relocs.  */
   9719   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   9720     {
   9721       struct got_entry **lgot_ents;
   9722       struct got_entry **end_lgot_ents;
   9723       struct plt_entry **local_plt;
   9724       struct plt_entry **end_local_plt;
   9725       unsigned char *lgot_masks;
   9726       bfd_size_type locsymcount;
   9727       Elf_Internal_Shdr *symtab_hdr;
   9728 
   9729       if (!is_ppc64_elf (ibfd))
   9730 	continue;
   9731 
   9732       for (s = ibfd->sections; s != NULL; s = s->next)
   9733 	{
   9734 	  struct ppc_dyn_relocs *p;
   9735 
   9736 	  for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
   9737 	    {
   9738 	      if (!bfd_is_abs_section (p->sec)
   9739 		  && bfd_is_abs_section (p->sec->output_section))
   9740 		{
   9741 		  /* Input section has been discarded, either because
   9742 		     it is a copy of a linkonce section or due to
   9743 		     linker script /DISCARD/, so we'll be discarding
   9744 		     the relocs too.  */
   9745 		}
   9746 	      else if (p->count != 0)
   9747 		{
   9748 		  asection *srel = elf_section_data (p->sec)->sreloc;
   9749 		  if (p->ifunc)
   9750 		    srel = htab->elf.irelplt;
   9751 		  srel->size += p->count * sizeof (Elf64_External_Rela);
   9752 		  if ((p->sec->output_section->flags & SEC_READONLY) != 0)
   9753 		    info->flags |= DF_TEXTREL;
   9754 		}
   9755 	    }
   9756 	}
   9757 
   9758       lgot_ents = elf_local_got_ents (ibfd);
   9759       if (!lgot_ents)
   9760 	continue;
   9761 
   9762       symtab_hdr = &elf_symtab_hdr (ibfd);
   9763       locsymcount = symtab_hdr->sh_info;
   9764       end_lgot_ents = lgot_ents + locsymcount;
   9765       local_plt = (struct plt_entry **) end_lgot_ents;
   9766       end_local_plt = local_plt + locsymcount;
   9767       lgot_masks = (unsigned char *) end_local_plt;
   9768       s = ppc64_elf_tdata (ibfd)->got;
   9769       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
   9770 	{
   9771 	  struct got_entry **pent, *ent;
   9772 
   9773 	  pent = lgot_ents;
   9774 	  while ((ent = *pent) != NULL)
   9775 	    if (ent->got.refcount > 0)
   9776 	      {
   9777 		if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
   9778 		  {
   9779 		    ppc64_tlsld_got (ibfd)->got.refcount += 1;
   9780 		    *pent = ent->next;
   9781 		  }
   9782 		else
   9783 		  {
   9784 		    unsigned int ent_size = 8;
   9785 		    unsigned int rel_size = sizeof (Elf64_External_Rela);
   9786 
   9787 		    ent->got.offset = s->size;
   9788 		    if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
   9789 		      {
   9790 			ent_size *= 2;
   9791 			rel_size *= 2;
   9792 		      }
   9793 		    s->size += ent_size;
   9794 		    if ((*lgot_masks & PLT_IFUNC) != 0)
   9795 		      {
   9796 			htab->elf.irelplt->size += rel_size;
   9797 			htab->got_reli_size += rel_size;
   9798 		      }
   9799 		    else if (info->shared)
   9800 		      {
   9801 			asection *srel = ppc64_elf_tdata (ibfd)->relgot;
   9802 			srel->size += rel_size;
   9803 		      }
   9804 		    pent = &ent->next;
   9805 		  }
   9806 	      }
   9807 	    else
   9808 	      *pent = ent->next;
   9809 	}
   9810 
   9811       /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt.  */
   9812       for (; local_plt < end_local_plt; ++local_plt)
   9813 	{
   9814 	  struct plt_entry *ent;
   9815 
   9816 	  for (ent = *local_plt; ent != NULL; ent = ent->next)
   9817 	    if (ent->plt.refcount > 0)
   9818 	      {
   9819 		s = htab->elf.iplt;
   9820 		ent->plt.offset = s->size;
   9821 		s->size += PLT_ENTRY_SIZE (htab);
   9822 
   9823 		htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
   9824 	      }
   9825 	    else
   9826 	      ent->plt.offset = (bfd_vma) -1;
   9827 	}
   9828     }
   9829 
   9830   /* Allocate global sym .plt and .got entries, and space for global
   9831      sym dynamic relocs.  */
   9832   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
   9833   /* Stash the end of glink branch table.  */
   9834   if (htab->glink != NULL)
   9835     htab->glink->rawsize = htab->glink->size;
   9836 
   9837   if (!htab->opd_abi && !info->shared)
   9838     elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
   9839 
   9840   first_tlsld = NULL;
   9841   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   9842     {
   9843       struct got_entry *ent;
   9844 
   9845       if (!is_ppc64_elf (ibfd))
   9846 	continue;
   9847 
   9848       ent = ppc64_tlsld_got (ibfd);
   9849       if (ent->got.refcount > 0)
   9850 	{
   9851 	  if (!htab->do_multi_toc && first_tlsld != NULL)
   9852 	    {
   9853 	      ent->is_indirect = TRUE;
   9854 	      ent->got.ent = first_tlsld;
   9855 	    }
   9856 	  else
   9857 	    {
   9858 	      if (first_tlsld == NULL)
   9859 		first_tlsld = ent;
   9860 	      s = ppc64_elf_tdata (ibfd)->got;
   9861 	      ent->got.offset = s->size;
   9862 	      ent->owner = ibfd;
   9863 	      s->size += 16;
   9864 	      if (info->shared)
   9865 		{
   9866 		  asection *srel = ppc64_elf_tdata (ibfd)->relgot;
   9867 		  srel->size += sizeof (Elf64_External_Rela);
   9868 		}
   9869 	    }
   9870 	}
   9871       else
   9872 	ent->got.offset = (bfd_vma) -1;
   9873     }
   9874 
   9875   /* We now have determined the sizes of the various dynamic sections.
   9876      Allocate memory for them.  */
   9877   relocs = FALSE;
   9878   for (s = dynobj->sections; s != NULL; s = s->next)
   9879     {
   9880       if ((s->flags & SEC_LINKER_CREATED) == 0)
   9881 	continue;
   9882 
   9883       if (s == htab->brlt || s == htab->relbrlt)
   9884 	/* These haven't been allocated yet;  don't strip.  */
   9885 	continue;
   9886       else if (s == htab->elf.sgot
   9887 	       || s == htab->elf.splt
   9888 	       || s == htab->elf.iplt
   9889 	       || s == htab->glink
   9890 	       || s == htab->dynbss)
   9891 	{
   9892 	  /* Strip this section if we don't need it; see the
   9893 	     comment below.  */
   9894 	}
   9895       else if (s == htab->glink_eh_frame)
   9896 	{
   9897 	  if (!bfd_is_abs_section (s->output_section))
   9898 	    /* Not sized yet.  */
   9899 	    continue;
   9900 	}
   9901       else if (CONST_STRNEQ (s->name, ".rela"))
   9902 	{
   9903 	  if (s->size != 0)
   9904 	    {
   9905 	      if (s != htab->elf.srelplt)
   9906 		relocs = TRUE;
   9907 
   9908 	      /* We use the reloc_count field as a counter if we need
   9909 		 to copy relocs into the output file.  */
   9910 	      s->reloc_count = 0;
   9911 	    }
   9912 	}
   9913       else
   9914 	{
   9915 	  /* It's not one of our sections, so don't allocate space.  */
   9916 	  continue;
   9917 	}
   9918 
   9919       if (s->size == 0)
   9920 	{
   9921 	  /* If we don't need this section, strip it from the
   9922 	     output file.  This is mostly to handle .rela.bss and
   9923 	     .rela.plt.  We must create both sections in
   9924 	     create_dynamic_sections, because they must be created
   9925 	     before the linker maps input sections to output
   9926 	     sections.  The linker does that before
   9927 	     adjust_dynamic_symbol is called, and it is that
   9928 	     function which decides whether anything needs to go
   9929 	     into these sections.  */
   9930 	  s->flags |= SEC_EXCLUDE;
   9931 	  continue;
   9932 	}
   9933 
   9934       if ((s->flags & SEC_HAS_CONTENTS) == 0)
   9935 	continue;
   9936 
   9937       /* Allocate memory for the section contents.  We use bfd_zalloc
   9938 	 here in case unused entries are not reclaimed before the
   9939 	 section's contents are written out.  This should not happen,
   9940 	 but this way if it does we get a R_PPC64_NONE reloc in .rela
   9941 	 sections instead of garbage.
   9942 	 We also rely on the section contents being zero when writing
   9943 	 the GOT.  */
   9944       s->contents = bfd_zalloc (dynobj, s->size);
   9945       if (s->contents == NULL)
   9946 	return FALSE;
   9947     }
   9948 
   9949   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   9950     {
   9951       if (!is_ppc64_elf (ibfd))
   9952 	continue;
   9953 
   9954       s = ppc64_elf_tdata (ibfd)->got;
   9955       if (s != NULL && s != htab->elf.sgot)
   9956 	{
   9957 	  if (s->size == 0)
   9958 	    s->flags |= SEC_EXCLUDE;
   9959 	  else
   9960 	    {
   9961 	      s->contents = bfd_zalloc (ibfd, s->size);
   9962 	      if (s->contents == NULL)
   9963 		return FALSE;
   9964 	    }
   9965 	}
   9966       s = ppc64_elf_tdata (ibfd)->relgot;
   9967       if (s != NULL)
   9968 	{
   9969 	  if (s->size == 0)
   9970 	    s->flags |= SEC_EXCLUDE;
   9971 	  else
   9972 	    {
   9973 	      s->contents = bfd_zalloc (ibfd, s->size);
   9974 	      if (s->contents == NULL)
   9975 		return FALSE;
   9976 	      relocs = TRUE;
   9977 	      s->reloc_count = 0;
   9978 	    }
   9979 	}
   9980     }
   9981 
   9982   if (htab->elf.dynamic_sections_created)
   9983     {
   9984       bfd_boolean tls_opt;
   9985 
   9986       /* Add some entries to the .dynamic section.  We fill in the
   9987 	 values later, in ppc64_elf_finish_dynamic_sections, but we
   9988 	 must add the entries now so that we get the correct size for
   9989 	 the .dynamic section.  The DT_DEBUG entry is filled in by the
   9990 	 dynamic linker and used by the debugger.  */
   9991 #define add_dynamic_entry(TAG, VAL) \
   9992   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
   9993 
   9994       if (info->executable)
   9995 	{
   9996 	  if (!add_dynamic_entry (DT_DEBUG, 0))
   9997 	    return FALSE;
   9998 	}
   9999 
   10000       if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
   10001 	{
   10002 	  if (!add_dynamic_entry (DT_PLTGOT, 0)
   10003 	      || !add_dynamic_entry (DT_PLTRELSZ, 0)
   10004 	      || !add_dynamic_entry (DT_PLTREL, DT_RELA)
   10005 	      || !add_dynamic_entry (DT_JMPREL, 0)
   10006 	      || !add_dynamic_entry (DT_PPC64_GLINK, 0))
   10007 	    return FALSE;
   10008 	}
   10009 
   10010       if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
   10011 	{
   10012 	  if (!add_dynamic_entry (DT_PPC64_OPD, 0)
   10013 	      || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
   10014 	    return FALSE;
   10015 	}
   10016 
   10017       tls_opt = (!htab->params->no_tls_get_addr_opt
   10018 		 && htab->tls_get_addr_fd != NULL
   10019 		 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
   10020       if (tls_opt || !htab->opd_abi)
   10021 	{
   10022 	  if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
   10023 	    return FALSE;
   10024 	}
   10025 
   10026       if (relocs)
   10027 	{
   10028 	  if (!add_dynamic_entry (DT_RELA, 0)
   10029 	      || !add_dynamic_entry (DT_RELASZ, 0)
   10030 	      || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
   10031 	    return FALSE;
   10032 
   10033 	  /* If any dynamic relocs apply to a read-only section,
   10034 	     then we need a DT_TEXTREL entry.  */
   10035 	  if ((info->flags & DF_TEXTREL) == 0)
   10036 	    elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
   10037 
   10038 	  if ((info->flags & DF_TEXTREL) != 0)
   10039 	    {
   10040 	      if (!add_dynamic_entry (DT_TEXTREL, 0))
   10041 		return FALSE;
   10042 	    }
   10043 	}
   10044     }
   10045 #undef add_dynamic_entry
   10046 
   10047   return TRUE;
   10048 }
   10049 
   10050 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
   10051 
   10052 static bfd_boolean
   10053 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
   10054 {
   10055   if (h->plt.plist != NULL
   10056       && !h->def_regular
   10057       && !h->pointer_equality_needed)
   10058     return FALSE;
   10059 
   10060   return _bfd_elf_hash_symbol (h);
   10061 }
   10062 
   10063 /* Determine the type of stub needed, if any, for a call.  */
   10064 
   10065 static inline enum ppc_stub_type
   10066 ppc_type_of_stub (asection *input_sec,
   10067 		  const Elf_Internal_Rela *rel,
   10068 		  struct ppc_link_hash_entry **hash,
   10069 		  struct plt_entry **plt_ent,
   10070 		  bfd_vma destination,
   10071 		  unsigned long local_off)
   10072 {
   10073   struct ppc_link_hash_entry *h = *hash;
   10074   bfd_vma location;
   10075   bfd_vma branch_offset;
   10076   bfd_vma max_branch_offset;
   10077   enum elf_ppc64_reloc_type r_type;
   10078 
   10079   if (h != NULL)
   10080     {
   10081       struct plt_entry *ent;
   10082       struct ppc_link_hash_entry *fdh = h;
   10083       if (h->oh != NULL
   10084 	  && h->oh->is_func_descriptor)
   10085 	{
   10086 	  fdh = ppc_follow_link (h->oh);
   10087 	  *hash = fdh;
   10088 	}
   10089 
   10090       for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
   10091 	if (ent->addend == rel->r_addend
   10092 	    && ent->plt.offset != (bfd_vma) -1)
   10093 	  {
   10094 	    *plt_ent = ent;
   10095 	    return ppc_stub_plt_call;
   10096 	  }
   10097 
   10098       /* Here, we know we don't have a plt entry.  If we don't have a
   10099 	 either a defined function descriptor or a defined entry symbol
   10100 	 in a regular object file, then it is pointless trying to make
   10101 	 any other type of stub.  */
   10102       if (!is_static_defined (&fdh->elf)
   10103 	  && !is_static_defined (&h->elf))
   10104 	return ppc_stub_none;
   10105     }
   10106   else if (elf_local_got_ents (input_sec->owner) != NULL)
   10107     {
   10108       Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
   10109       struct plt_entry **local_plt = (struct plt_entry **)
   10110 	elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
   10111       unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
   10112 
   10113       if (local_plt[r_symndx] != NULL)
   10114 	{
   10115 	  struct plt_entry *ent;
   10116 
   10117 	  for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
   10118 	    if (ent->addend == rel->r_addend
   10119 		&& ent->plt.offset != (bfd_vma) -1)
   10120 	      {
   10121 		*plt_ent = ent;
   10122 		return ppc_stub_plt_call;
   10123 	      }
   10124 	}
   10125     }
   10126 
   10127   /* Determine where the call point is.  */
   10128   location = (input_sec->output_offset
   10129 	      + input_sec->output_section->vma
   10130 	      + rel->r_offset);
   10131 
   10132   branch_offset = destination - location;
   10133   r_type = ELF64_R_TYPE (rel->r_info);
   10134 
   10135   /* Determine if a long branch stub is needed.  */
   10136   max_branch_offset = 1 << 25;
   10137   if (r_type != R_PPC64_REL24)
   10138     max_branch_offset = 1 << 15;
   10139 
   10140   if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
   10141     /* We need a stub.  Figure out whether a long_branch or plt_branch
   10142        is needed later.  */
   10143     return ppc_stub_long_branch;
   10144 
   10145   return ppc_stub_none;
   10146 }
   10147 
   10148 /* With power7 weakly ordered memory model, it is possible for ld.so
   10149    to update a plt entry in one thread and have another thread see a
   10150    stale zero toc entry.  To avoid this we need some sort of acquire
   10151    barrier in the call stub.  One solution is to make the load of the
   10152    toc word seem to appear to depend on the load of the function entry
   10153    word.  Another solution is to test for r2 being zero, and branch to
   10154    the appropriate glink entry if so.
   10155 
   10156    .	fake dep barrier	compare
   10157    .	ld 12,xxx(2)		ld 12,xxx(2)
   10158    .	mtctr 12		mtctr 12
   10159    .	xor 11,12,12		ld 2,xxx+8(2)
   10160    .	add 2,2,11		cmpldi 2,0
   10161    .	ld 2,xxx+8(2)		bnectr+
   10162    .	bctr			b <glink_entry>
   10163 
   10164    The solution involving the compare turns out to be faster, so
   10165    that's what we use unless the branch won't reach.  */
   10166 
   10167 #define ALWAYS_USE_FAKE_DEP 0
   10168 #define ALWAYS_EMIT_R2SAVE 0
   10169 
   10170 #define PPC_LO(v) ((v) & 0xffff)
   10171 #define PPC_HI(v) (((v) >> 16) & 0xffff)
   10172 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
   10173 
   10174 static inline unsigned int
   10175 plt_stub_size (struct ppc_link_hash_table *htab,
   10176 	       struct ppc_stub_hash_entry *stub_entry,
   10177 	       bfd_vma off)
   10178 {
   10179   unsigned size = 12;
   10180 
   10181   if (ALWAYS_EMIT_R2SAVE
   10182       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   10183     size += 4;
   10184   if (PPC_HA (off) != 0)
   10185     size += 4;
   10186   if (htab->opd_abi)
   10187     {
   10188       size += 4;
   10189       if (htab->params->plt_static_chain)
   10190 	size += 4;
   10191       if (htab->params->plt_thread_safe)
   10192 	size += 8;
   10193       if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
   10194 	size += 4;
   10195     }
   10196   if (stub_entry->h != NULL
   10197       && (stub_entry->h == htab->tls_get_addr_fd
   10198 	  || stub_entry->h == htab->tls_get_addr)
   10199       && !htab->params->no_tls_get_addr_opt)
   10200     size += 13 * 4;
   10201   return size;
   10202 }
   10203 
   10204 /* If this stub would cross fewer 2**plt_stub_align boundaries if we align,
   10205    then return the padding needed to do so.  */
   10206 static inline unsigned int
   10207 plt_stub_pad (struct ppc_link_hash_table *htab,
   10208 	      struct ppc_stub_hash_entry *stub_entry,
   10209 	      bfd_vma plt_off)
   10210 {
   10211   int stub_align = 1 << htab->params->plt_stub_align;
   10212   unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
   10213   bfd_vma stub_off = stub_entry->stub_sec->size;
   10214 
   10215   if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
   10216       > (stub_size & -stub_align))
   10217     return stub_align - (stub_off & (stub_align - 1));
   10218   return 0;
   10219 }
   10220 
   10221 /* Build a .plt call stub.  */
   10222 
   10223 static inline bfd_byte *
   10224 build_plt_stub (struct ppc_link_hash_table *htab,
   10225 		struct ppc_stub_hash_entry *stub_entry,
   10226 		bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
   10227 {
   10228   bfd *obfd = htab->params->stub_bfd;
   10229   bfd_boolean plt_load_toc = htab->opd_abi;
   10230   bfd_boolean plt_static_chain = htab->params->plt_static_chain;
   10231   bfd_boolean plt_thread_safe = htab->params->plt_thread_safe;
   10232   bfd_boolean use_fake_dep = plt_thread_safe;
   10233   bfd_vma cmp_branch_off = 0;
   10234 
   10235   if (!ALWAYS_USE_FAKE_DEP
   10236       && plt_load_toc
   10237       && plt_thread_safe
   10238       && !(stub_entry->h != NULL
   10239 	   && (stub_entry->h == htab->tls_get_addr_fd
   10240 	       || stub_entry->h == htab->tls_get_addr)
   10241 	   && !htab->params->no_tls_get_addr_opt))
   10242     {
   10243       bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
   10244       bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
   10245 			  / PLT_ENTRY_SIZE (htab));
   10246       bfd_vma glinkoff = GLINK_CALL_STUB_SIZE + pltindex * 8;
   10247       bfd_vma to, from;
   10248 
   10249       if (pltindex > 32768)
   10250 	glinkoff += (pltindex - 32768) * 4;
   10251       to = (glinkoff
   10252 	    + htab->glink->output_offset
   10253 	    + htab->glink->output_section->vma);
   10254       from = (p - stub_entry->stub_sec->contents
   10255 	      + 4 * (ALWAYS_EMIT_R2SAVE
   10256 		     || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   10257 	      + 4 * (PPC_HA (offset) != 0)
   10258 	      + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
   10259 		     != PPC_HA (offset))
   10260 	      + 4 * (plt_static_chain != 0)
   10261 	      + 20
   10262 	      + stub_entry->stub_sec->output_offset
   10263 	      + stub_entry->stub_sec->output_section->vma);
   10264       cmp_branch_off = to - from;
   10265       use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
   10266     }
   10267 
   10268   if (PPC_HA (offset) != 0)
   10269     {
   10270       if (r != NULL)
   10271 	{
   10272 	  if (ALWAYS_EMIT_R2SAVE
   10273 	      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   10274 	    r[0].r_offset += 4;
   10275 	  r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
   10276 	  r[1].r_offset = r[0].r_offset + 4;
   10277 	  r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
   10278 	  r[1].r_addend = r[0].r_addend;
   10279 	  if (plt_load_toc)
   10280 	    {
   10281 	      if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
   10282 		{
   10283 		  r[2].r_offset = r[1].r_offset + 4;
   10284 		  r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
   10285 		  r[2].r_addend = r[0].r_addend;
   10286 		}
   10287 	      else
   10288 		{
   10289 		  r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
   10290 		  r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
   10291 		  r[2].r_addend = r[0].r_addend + 8;
   10292 		  if (plt_static_chain)
   10293 		    {
   10294 		      r[3].r_offset = r[2].r_offset + 4;
   10295 		      r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
   10296 		      r[3].r_addend = r[0].r_addend + 16;
   10297 		    }
   10298 		}
   10299 	    }
   10300 	}
   10301       if (ALWAYS_EMIT_R2SAVE
   10302 	  || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   10303 	bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),	p += 4;
   10304       if (plt_load_toc)
   10305 	{
   10306 	  bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p),	p += 4;
   10307 	  bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p),	p += 4;
   10308 	}
   10309       else
   10310 	{
   10311 	  bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p),	p += 4;
   10312 	  bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p),	p += 4;
   10313 	}
   10314       if (plt_load_toc
   10315 	  && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
   10316 	{
   10317 	  bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p),	p += 4;
   10318 	  offset = 0;
   10319 	}
   10320       bfd_put_32 (obfd, MTCTR_R12, p),				p += 4;
   10321       if (plt_load_toc)
   10322 	{
   10323 	  if (use_fake_dep)
   10324 	    {
   10325 	      bfd_put_32 (obfd, XOR_R2_R12_R12, p),		p += 4;
   10326 	      bfd_put_32 (obfd, ADD_R11_R11_R2, p),		p += 4;
   10327 	    }
   10328 	  bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
   10329 	  if (plt_static_chain)
   10330 	    bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
   10331 	}
   10332     }
   10333   else
   10334     {
   10335       if (r != NULL)
   10336 	{
   10337 	  if (ALWAYS_EMIT_R2SAVE
   10338 	      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   10339 	    r[0].r_offset += 4;
   10340 	  r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
   10341 	  if (plt_load_toc)
   10342 	    {
   10343 	      if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
   10344 		{
   10345 		  r[1].r_offset = r[0].r_offset + 4;
   10346 		  r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
   10347 		  r[1].r_addend = r[0].r_addend;
   10348 		}
   10349 	      else
   10350 		{
   10351 		  r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
   10352 		  r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
   10353 		  r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
   10354 		  if (plt_static_chain)
   10355 		    {
   10356 		      r[2].r_offset = r[1].r_offset + 4;
   10357 		      r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
   10358 		      r[2].r_addend = r[0].r_addend + 8;
   10359 		    }
   10360 		}
   10361 	    }
   10362 	}
   10363       if (ALWAYS_EMIT_R2SAVE
   10364 	  || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   10365 	bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),	p += 4;
   10366       bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p),	p += 4;
   10367       if (plt_load_toc
   10368 	  && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
   10369 	{
   10370 	  bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p),	p += 4;
   10371 	  offset = 0;
   10372 	}
   10373       bfd_put_32 (obfd, MTCTR_R12, p),				p += 4;
   10374       if (plt_load_toc)
   10375 	{
   10376 	  if (use_fake_dep)
   10377 	    {
   10378 	      bfd_put_32 (obfd, XOR_R11_R12_R12, p),		p += 4;
   10379 	      bfd_put_32 (obfd, ADD_R2_R2_R11, p),		p += 4;
   10380 	    }
   10381 	  if (plt_static_chain)
   10382 	    bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
   10383 	  bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
   10384 	}
   10385     }
   10386   if (plt_load_toc && plt_thread_safe && !use_fake_dep)
   10387     {
   10388       bfd_put_32 (obfd, CMPLDI_R2_0, p),			p += 4;
   10389       bfd_put_32 (obfd, BNECTR_P4, p),				p += 4;
   10390       bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
   10391     }
   10392   else
   10393     bfd_put_32 (obfd, BCTR, p),					p += 4;
   10394   return p;
   10395 }
   10396 
   10397 /* Build a special .plt call stub for __tls_get_addr.  */
   10398 
   10399 #define LD_R11_0R3	0xe9630000
   10400 #define LD_R12_0R3	0xe9830000
   10401 #define MR_R0_R3	0x7c601b78
   10402 #define CMPDI_R11_0	0x2c2b0000
   10403 #define ADD_R3_R12_R13	0x7c6c6a14
   10404 #define BEQLR		0x4d820020
   10405 #define MR_R3_R0	0x7c030378
   10406 #define STD_R11_0R1	0xf9610000
   10407 #define BCTRL		0x4e800421
   10408 #define LD_R11_0R1	0xe9610000
   10409 #define MTLR_R11	0x7d6803a6
   10410 
   10411 static inline bfd_byte *
   10412 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
   10413 			 struct ppc_stub_hash_entry *stub_entry,
   10414 			 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
   10415 {
   10416   bfd *obfd = htab->params->stub_bfd;
   10417 
   10418   bfd_put_32 (obfd, LD_R11_0R3 + 0, p),		p += 4;
   10419   bfd_put_32 (obfd, LD_R12_0R3 + 8, p),		p += 4;
   10420   bfd_put_32 (obfd, MR_R0_R3, p),		p += 4;
   10421   bfd_put_32 (obfd, CMPDI_R11_0, p),		p += 4;
   10422   bfd_put_32 (obfd, ADD_R3_R12_R13, p),		p += 4;
   10423   bfd_put_32 (obfd, BEQLR, p),			p += 4;
   10424   bfd_put_32 (obfd, MR_R3_R0, p),		p += 4;
   10425   bfd_put_32 (obfd, MFLR_R11, p),		p += 4;
   10426   bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
   10427 
   10428   if (r != NULL)
   10429     r[0].r_offset += 9 * 4;
   10430   p = build_plt_stub (htab, stub_entry, p, offset, r);
   10431   bfd_put_32 (obfd, BCTRL, p - 4);
   10432 
   10433   bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p),	p += 4;
   10434   bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p),	p += 4;
   10435   bfd_put_32 (obfd, MTLR_R11, p),		p += 4;
   10436   bfd_put_32 (obfd, BLR, p),			p += 4;
   10437 
   10438   return p;
   10439 }
   10440 
   10441 static Elf_Internal_Rela *
   10442 get_relocs (asection *sec, int count)
   10443 {
   10444   Elf_Internal_Rela *relocs;
   10445   struct bfd_elf_section_data *elfsec_data;
   10446 
   10447   elfsec_data = elf_section_data (sec);
   10448   relocs = elfsec_data->relocs;
   10449   if (relocs == NULL)
   10450     {
   10451       bfd_size_type relsize;
   10452       relsize = sec->reloc_count * sizeof (*relocs);
   10453       relocs = bfd_alloc (sec->owner, relsize);
   10454       if (relocs == NULL)
   10455 	return NULL;
   10456       elfsec_data->relocs = relocs;
   10457       elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
   10458 					  sizeof (Elf_Internal_Shdr));
   10459       if (elfsec_data->rela.hdr == NULL)
   10460 	return NULL;
   10461       elfsec_data->rela.hdr->sh_size = (sec->reloc_count
   10462 					* sizeof (Elf64_External_Rela));
   10463       elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
   10464       sec->reloc_count = 0;
   10465     }
   10466   relocs += sec->reloc_count;
   10467   sec->reloc_count += count;
   10468   return relocs;
   10469 }
   10470 
   10471 static bfd_vma
   10472 get_r2off (struct bfd_link_info *info,
   10473 	   struct ppc_stub_hash_entry *stub_entry)
   10474 {
   10475   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   10476   bfd_vma r2off = htab->stub_group[stub_entry->target_section->id].toc_off;
   10477 
   10478   if (r2off == 0)
   10479     {
   10480       /* Support linking -R objects.  Get the toc pointer from the
   10481 	 opd entry.  */
   10482       char buf[8];
   10483       if (!htab->opd_abi)
   10484 	return r2off;
   10485       asection *opd = stub_entry->h->elf.root.u.def.section;
   10486       bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
   10487 
   10488       if (strcmp (opd->name, ".opd") != 0
   10489 	  || opd->reloc_count != 0)
   10490 	{
   10491 	  info->callbacks->einfo (_("%P: cannot find opd entry toc for `%T'\n"),
   10492 				  stub_entry->h->elf.root.root.string);
   10493 	  bfd_set_error (bfd_error_bad_value);
   10494 	  return 0;
   10495 	}
   10496       if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
   10497 	return 0;
   10498       r2off = bfd_get_64 (opd->owner, buf);
   10499       r2off -= elf_gp (info->output_bfd);
   10500     }
   10501   r2off -= htab->stub_group[stub_entry->id_sec->id].toc_off;
   10502   return r2off;
   10503 }
   10504 
   10505 static bfd_boolean
   10506 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
   10507 {
   10508   struct ppc_stub_hash_entry *stub_entry;
   10509   struct ppc_branch_hash_entry *br_entry;
   10510   struct bfd_link_info *info;
   10511   struct ppc_link_hash_table *htab;
   10512   bfd_byte *loc;
   10513   bfd_byte *p;
   10514   bfd_vma dest, off;
   10515   int size;
   10516   Elf_Internal_Rela *r;
   10517   asection *plt;
   10518 
   10519   /* Massage our args to the form they really have.  */
   10520   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
   10521   info = in_arg;
   10522 
   10523   htab = ppc_hash_table (info);
   10524   if (htab == NULL)
   10525     return FALSE;
   10526 
   10527   /* Make a note of the offset within the stubs for this entry.  */
   10528   stub_entry->stub_offset = stub_entry->stub_sec->size;
   10529   loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
   10530 
   10531   htab->stub_count[stub_entry->stub_type - 1] += 1;
   10532   switch (stub_entry->stub_type)
   10533     {
   10534     case ppc_stub_long_branch:
   10535     case ppc_stub_long_branch_r2off:
   10536       /* Branches are relative.  This is where we are going to.  */
   10537       dest = (stub_entry->target_value
   10538 	      + stub_entry->target_section->output_offset
   10539 	      + stub_entry->target_section->output_section->vma);
   10540       dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
   10541       off = dest;
   10542 
   10543       /* And this is where we are coming from.  */
   10544       off -= (stub_entry->stub_offset
   10545 	      + stub_entry->stub_sec->output_offset
   10546 	      + stub_entry->stub_sec->output_section->vma);
   10547 
   10548       size = 4;
   10549       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
   10550 	{
   10551 	  bfd_vma r2off = get_r2off (info, stub_entry);
   10552 
   10553 	  if (r2off == 0)
   10554 	    {
   10555 	      htab->stub_error = TRUE;
   10556 	      return FALSE;
   10557 	    }
   10558 	  bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
   10559 	  loc += 4;
   10560 	  size = 12;
   10561 	  if (PPC_HA (r2off) != 0)
   10562 	    {
   10563 	      size = 16;
   10564 	      bfd_put_32 (htab->params->stub_bfd,
   10565 			  ADDIS_R2_R2 | PPC_HA (r2off), loc);
   10566 	      loc += 4;
   10567 	    }
   10568 	  bfd_put_32 (htab->params->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
   10569 	  loc += 4;
   10570 	  off -= size - 4;
   10571 	}
   10572       bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
   10573 
   10574       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
   10575 	{
   10576 	  info->callbacks->einfo
   10577 	    (_("%P: long branch stub `%s' offset overflow\n"),
   10578 	     stub_entry->root.string);
   10579 	  htab->stub_error = TRUE;
   10580 	  return FALSE;
   10581 	}
   10582 
   10583       if (info->emitrelocations)
   10584 	{
   10585 	  r = get_relocs (stub_entry->stub_sec, 1);
   10586 	  if (r == NULL)
   10587 	    return FALSE;
   10588 	  r->r_offset = loc - stub_entry->stub_sec->contents;
   10589 	  r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
   10590 	  r->r_addend = dest;
   10591 	  if (stub_entry->h != NULL)
   10592 	    {
   10593 	      struct elf_link_hash_entry **hashes;
   10594 	      unsigned long symndx;
   10595 	      struct ppc_link_hash_entry *h;
   10596 
   10597 	      hashes = elf_sym_hashes (htab->params->stub_bfd);
   10598 	      if (hashes == NULL)
   10599 		{
   10600 		  bfd_size_type hsize;
   10601 
   10602 		  hsize = (htab->stub_globals + 1) * sizeof (*hashes);
   10603 		  hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
   10604 		  if (hashes == NULL)
   10605 		    return FALSE;
   10606 		  elf_sym_hashes (htab->params->stub_bfd) = hashes;
   10607 		  htab->stub_globals = 1;
   10608 		}
   10609 	      symndx = htab->stub_globals++;
   10610 	      h = stub_entry->h;
   10611 	      hashes[symndx] = &h->elf;
   10612 	      r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
   10613 	      if (h->oh != NULL && h->oh->is_func)
   10614 		h = ppc_follow_link (h->oh);
   10615 	      if (h->elf.root.u.def.section != stub_entry->target_section)
   10616 		/* H is an opd symbol.  The addend must be zero.  */
   10617 		r->r_addend = 0;
   10618 	      else
   10619 		{
   10620 		  off = (h->elf.root.u.def.value
   10621 			 + h->elf.root.u.def.section->output_offset
   10622 			 + h->elf.root.u.def.section->output_section->vma);
   10623 		  r->r_addend -= off;
   10624 		}
   10625 	    }
   10626 	}
   10627       break;
   10628 
   10629     case ppc_stub_plt_branch:
   10630     case ppc_stub_plt_branch_r2off:
   10631       br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
   10632 					 stub_entry->root.string + 9,
   10633 					 FALSE, FALSE);
   10634       if (br_entry == NULL)
   10635 	{
   10636 	  info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
   10637 				  stub_entry->root.string);
   10638 	  htab->stub_error = TRUE;
   10639 	  return FALSE;
   10640 	}
   10641 
   10642       dest = (stub_entry->target_value
   10643 	      + stub_entry->target_section->output_offset
   10644 	      + stub_entry->target_section->output_section->vma);
   10645       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
   10646 	dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
   10647 
   10648       bfd_put_64 (htab->brlt->owner, dest,
   10649 		  htab->brlt->contents + br_entry->offset);
   10650 
   10651       if (br_entry->iter == htab->stub_iteration)
   10652 	{
   10653 	  br_entry->iter = 0;
   10654 
   10655 	  if (htab->relbrlt != NULL)
   10656 	    {
   10657 	      /* Create a reloc for the branch lookup table entry.  */
   10658 	      Elf_Internal_Rela rela;
   10659 	      bfd_byte *rl;
   10660 
   10661 	      rela.r_offset = (br_entry->offset
   10662 			       + htab->brlt->output_offset
   10663 			       + htab->brlt->output_section->vma);
   10664 	      rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   10665 	      rela.r_addend = dest;
   10666 
   10667 	      rl = htab->relbrlt->contents;
   10668 	      rl += (htab->relbrlt->reloc_count++
   10669 		     * sizeof (Elf64_External_Rela));
   10670 	      bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
   10671 	    }
   10672 	  else if (info->emitrelocations)
   10673 	    {
   10674 	      r = get_relocs (htab->brlt, 1);
   10675 	      if (r == NULL)
   10676 		return FALSE;
   10677 	      /* brlt, being SEC_LINKER_CREATED does not go through the
   10678 		 normal reloc processing.  Symbols and offsets are not
   10679 		 translated from input file to output file form, so
   10680 		 set up the offset per the output file.  */
   10681 	      r->r_offset = (br_entry->offset
   10682 			     + htab->brlt->output_offset
   10683 			     + htab->brlt->output_section->vma);
   10684 	      r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   10685 	      r->r_addend = dest;
   10686 	    }
   10687 	}
   10688 
   10689       dest = (br_entry->offset
   10690 	      + htab->brlt->output_offset
   10691 	      + htab->brlt->output_section->vma);
   10692 
   10693       off = (dest
   10694 	     - elf_gp (htab->brlt->output_section->owner)
   10695 	     - htab->stub_group[stub_entry->id_sec->id].toc_off);
   10696 
   10697       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
   10698 	{
   10699 	  info->callbacks->einfo
   10700 	    (_("%P: linkage table error against `%T'\n"),
   10701 	     stub_entry->root.string);
   10702 	  bfd_set_error (bfd_error_bad_value);
   10703 	  htab->stub_error = TRUE;
   10704 	  return FALSE;
   10705 	}
   10706 
   10707       if (info->emitrelocations)
   10708 	{
   10709 	  r = get_relocs (stub_entry->stub_sec, 1 + (PPC_HA (off) != 0));
   10710 	  if (r == NULL)
   10711 	    return FALSE;
   10712 	  r[0].r_offset = loc - stub_entry->stub_sec->contents;
   10713 	  if (bfd_big_endian (info->output_bfd))
   10714 	    r[0].r_offset += 2;
   10715 	  if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
   10716 	    r[0].r_offset += 4;
   10717 	  r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
   10718 	  r[0].r_addend = dest;
   10719 	  if (PPC_HA (off) != 0)
   10720 	    {
   10721 	      r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
   10722 	      r[1].r_offset = r[0].r_offset + 4;
   10723 	      r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
   10724 	      r[1].r_addend = r[0].r_addend;
   10725 	    }
   10726 	}
   10727 
   10728       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
   10729 	{
   10730 	  if (PPC_HA (off) != 0)
   10731 	    {
   10732 	      size = 16;
   10733 	      bfd_put_32 (htab->params->stub_bfd,
   10734 			  ADDIS_R12_R2 | PPC_HA (off), loc);
   10735 	      loc += 4;
   10736 	      bfd_put_32 (htab->params->stub_bfd,
   10737 			  LD_R12_0R12 | PPC_LO (off), loc);
   10738 	    }
   10739 	  else
   10740 	    {
   10741 	      size = 12;
   10742 	      bfd_put_32 (htab->params->stub_bfd,
   10743 			  LD_R12_0R2 | PPC_LO (off), loc);
   10744 	    }
   10745 	}
   10746       else
   10747 	{
   10748 	  bfd_vma r2off = get_r2off (info, stub_entry);
   10749 
   10750 	  if (r2off == 0 && htab->opd_abi)
   10751 	    {
   10752 	      htab->stub_error = TRUE;
   10753 	      return FALSE;
   10754 	    }
   10755 
   10756 	  bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
   10757 	  loc += 4;
   10758 	  size = 16;
   10759 	  if (PPC_HA (off) != 0)
   10760 	    {
   10761 	      size += 4;
   10762 	      bfd_put_32 (htab->params->stub_bfd,
   10763 			  ADDIS_R12_R2 | PPC_HA (off), loc);
   10764 	      loc += 4;
   10765 	      bfd_put_32 (htab->params->stub_bfd,
   10766 			  LD_R12_0R12 | PPC_LO (off), loc);
   10767 	    }
   10768 	  else
   10769 	    bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
   10770 
   10771 	  if (PPC_HA (r2off) != 0)
   10772 	    {
   10773 	      size += 4;
   10774 	      loc += 4;
   10775 	      bfd_put_32 (htab->params->stub_bfd,
   10776 			  ADDIS_R2_R2 | PPC_HA (r2off), loc);
   10777 	    }
   10778 	  if (PPC_LO (r2off) != 0)
   10779 	    {
   10780 	      size += 4;
   10781 	      loc += 4;
   10782 	      bfd_put_32 (htab->params->stub_bfd,
   10783 			  ADDI_R2_R2 | PPC_LO (r2off), loc);
   10784 	    }
   10785 	}
   10786       loc += 4;
   10787       bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, loc);
   10788       loc += 4;
   10789       bfd_put_32 (htab->params->stub_bfd, BCTR, loc);
   10790       break;
   10791 
   10792     case ppc_stub_plt_call:
   10793     case ppc_stub_plt_call_r2save:
   10794       if (stub_entry->h != NULL
   10795 	  && stub_entry->h->is_func_descriptor
   10796 	  && stub_entry->h->oh != NULL)
   10797 	{
   10798 	  struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
   10799 
   10800 	  /* If the old-ABI "dot-symbol" is undefined make it weak so
   10801 	     we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.
   10802 	     FIXME: We used to define the symbol on one of the call
   10803 	     stubs instead, which is why we test symbol section id
   10804 	     against htab->top_id in various places.  Likely all
   10805 	     these checks could now disappear.  */
   10806 	  if (fh->elf.root.type == bfd_link_hash_undefined)
   10807 	    fh->elf.root.type = bfd_link_hash_undefweak;
   10808 	  /* Stop undo_symbol_twiddle changing it back to undefined.  */
   10809 	  fh->was_undefined = 0;
   10810 	}
   10811 
   10812       /* Now build the stub.  */
   10813       dest = stub_entry->plt_ent->plt.offset & ~1;
   10814       if (dest >= (bfd_vma) -2)
   10815 	abort ();
   10816 
   10817       plt = htab->elf.splt;
   10818       if (!htab->elf.dynamic_sections_created
   10819 	  || stub_entry->h == NULL
   10820 	  || stub_entry->h->elf.dynindx == -1)
   10821 	plt = htab->elf.iplt;
   10822 
   10823       dest += plt->output_offset + plt->output_section->vma;
   10824 
   10825       if (stub_entry->h == NULL
   10826 	  && (stub_entry->plt_ent->plt.offset & 1) == 0)
   10827 	{
   10828 	  Elf_Internal_Rela rela;
   10829 	  bfd_byte *rl;
   10830 
   10831 	  rela.r_offset = dest;
   10832 	  if (htab->opd_abi)
   10833 	    rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
   10834 	  else
   10835 	    rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
   10836 	  rela.r_addend = (stub_entry->target_value
   10837 			   + stub_entry->target_section->output_offset
   10838 			   + stub_entry->target_section->output_section->vma);
   10839 
   10840 	  rl = (htab->elf.irelplt->contents
   10841 		+ (htab->elf.irelplt->reloc_count++
   10842 		   * sizeof (Elf64_External_Rela)));
   10843 	  bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
   10844 	  stub_entry->plt_ent->plt.offset |= 1;
   10845 	}
   10846 
   10847       off = (dest
   10848 	     - elf_gp (plt->output_section->owner)
   10849 	     - htab->stub_group[stub_entry->id_sec->id].toc_off);
   10850 
   10851       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
   10852 	{
   10853 	  info->callbacks->einfo
   10854 	    (_("%P: linkage table error against `%T'\n"),
   10855 	     stub_entry->h != NULL
   10856 	     ? stub_entry->h->elf.root.root.string
   10857 	     : "<local sym>");
   10858 	  bfd_set_error (bfd_error_bad_value);
   10859 	  htab->stub_error = TRUE;
   10860 	  return FALSE;
   10861 	}
   10862 
   10863       if (htab->params->plt_stub_align != 0)
   10864 	{
   10865 	  unsigned pad = plt_stub_pad (htab, stub_entry, off);
   10866 
   10867 	  stub_entry->stub_sec->size += pad;
   10868 	  stub_entry->stub_offset = stub_entry->stub_sec->size;
   10869 	  loc += pad;
   10870 	}
   10871 
   10872       r = NULL;
   10873       if (info->emitrelocations)
   10874 	{
   10875 	  r = get_relocs (stub_entry->stub_sec,
   10876 			  ((PPC_HA (off) != 0)
   10877 			   + (htab->opd_abi
   10878 			      ? 2 + (htab->params->plt_static_chain
   10879 				     && PPC_HA (off + 16) == PPC_HA (off))
   10880 			      : 1)));
   10881 	  if (r == NULL)
   10882 	    return FALSE;
   10883 	  r[0].r_offset = loc - stub_entry->stub_sec->contents;
   10884 	  if (bfd_big_endian (info->output_bfd))
   10885 	    r[0].r_offset += 2;
   10886 	  r[0].r_addend = dest;
   10887 	}
   10888       if (stub_entry->h != NULL
   10889 	  && (stub_entry->h == htab->tls_get_addr_fd
   10890 	      || stub_entry->h == htab->tls_get_addr)
   10891 	  && !htab->params->no_tls_get_addr_opt)
   10892 	p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
   10893       else
   10894 	p = build_plt_stub (htab, stub_entry, loc, off, r);
   10895       size = p - loc;
   10896       break;
   10897 
   10898     default:
   10899       BFD_FAIL ();
   10900       return FALSE;
   10901     }
   10902 
   10903   stub_entry->stub_sec->size += size;
   10904 
   10905   if (htab->params->emit_stub_syms)
   10906     {
   10907       struct elf_link_hash_entry *h;
   10908       size_t len1, len2;
   10909       char *name;
   10910       const char *const stub_str[] = { "long_branch",
   10911 				       "long_branch_r2off",
   10912 				       "plt_branch",
   10913 				       "plt_branch_r2off",
   10914 				       "plt_call",
   10915 				       "plt_call" };
   10916 
   10917       len1 = strlen (stub_str[stub_entry->stub_type - 1]);
   10918       len2 = strlen (stub_entry->root.string);
   10919       name = bfd_malloc (len1 + len2 + 2);
   10920       if (name == NULL)
   10921 	return FALSE;
   10922       memcpy (name, stub_entry->root.string, 9);
   10923       memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
   10924       memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
   10925       h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
   10926       if (h == NULL)
   10927 	return FALSE;
   10928       if (h->root.type == bfd_link_hash_new)
   10929 	{
   10930 	  h->root.type = bfd_link_hash_defined;
   10931 	  h->root.u.def.section = stub_entry->stub_sec;
   10932 	  h->root.u.def.value = stub_entry->stub_offset;
   10933 	  h->ref_regular = 1;
   10934 	  h->def_regular = 1;
   10935 	  h->ref_regular_nonweak = 1;
   10936 	  h->forced_local = 1;
   10937 	  h->non_elf = 0;
   10938 	}
   10939     }
   10940 
   10941   return TRUE;
   10942 }
   10943 
   10944 /* As above, but don't actually build the stub.  Just bump offset so
   10945    we know stub section sizes, and select plt_branch stubs where
   10946    long_branch stubs won't do.  */
   10947 
   10948 static bfd_boolean
   10949 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
   10950 {
   10951   struct ppc_stub_hash_entry *stub_entry;
   10952   struct bfd_link_info *info;
   10953   struct ppc_link_hash_table *htab;
   10954   bfd_vma off;
   10955   int size;
   10956 
   10957   /* Massage our args to the form they really have.  */
   10958   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
   10959   info = in_arg;
   10960 
   10961   htab = ppc_hash_table (info);
   10962   if (htab == NULL)
   10963     return FALSE;
   10964 
   10965   if (stub_entry->stub_type == ppc_stub_plt_call
   10966       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   10967     {
   10968       asection *plt;
   10969       off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
   10970       if (off >= (bfd_vma) -2)
   10971 	abort ();
   10972       plt = htab->elf.splt;
   10973       if (!htab->elf.dynamic_sections_created
   10974 	  || stub_entry->h == NULL
   10975 	  || stub_entry->h->elf.dynindx == -1)
   10976 	plt = htab->elf.iplt;
   10977       off += (plt->output_offset
   10978 	      + plt->output_section->vma
   10979 	      - elf_gp (plt->output_section->owner)
   10980 	      - htab->stub_group[stub_entry->id_sec->id].toc_off);
   10981 
   10982       size = plt_stub_size (htab, stub_entry, off);
   10983       if (htab->params->plt_stub_align)
   10984 	size += plt_stub_pad (htab, stub_entry, off);
   10985       if (info->emitrelocations)
   10986 	{
   10987 	  stub_entry->stub_sec->reloc_count
   10988 	    += ((PPC_HA (off) != 0)
   10989 		+ (htab->opd_abi
   10990 		   ? 2 + (htab->params->plt_static_chain
   10991 			  && PPC_HA (off + 16) == PPC_HA (off))
   10992 		   : 1));
   10993 	  stub_entry->stub_sec->flags |= SEC_RELOC;
   10994 	}
   10995     }
   10996   else
   10997     {
   10998       /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
   10999 	 variants.  */
   11000       bfd_vma r2off = 0;
   11001       bfd_vma local_off = 0;
   11002 
   11003       off = (stub_entry->target_value
   11004 	     + stub_entry->target_section->output_offset
   11005 	     + stub_entry->target_section->output_section->vma);
   11006       off -= (stub_entry->stub_sec->size
   11007 	      + stub_entry->stub_sec->output_offset
   11008 	      + stub_entry->stub_sec->output_section->vma);
   11009 
   11010       /* Reset the stub type from the plt variant in case we now
   11011 	 can reach with a shorter stub.  */
   11012       if (stub_entry->stub_type >= ppc_stub_plt_branch)
   11013 	stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
   11014 
   11015       size = 4;
   11016       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
   11017 	{
   11018 	  r2off = get_r2off (info, stub_entry);
   11019 	  if (r2off == 0 && htab->opd_abi)
   11020 	    {
   11021 	      htab->stub_error = TRUE;
   11022 	      return FALSE;
   11023 	    }
   11024 	  size = 12;
   11025 	  if (PPC_HA (r2off) != 0)
   11026 	    size = 16;
   11027 	  off -= size - 4;
   11028 	}
   11029 
   11030       local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
   11031 
   11032       /* If the branch offset if too big, use a ppc_stub_plt_branch.
   11033 	 Do the same for -R objects without function descriptors.  */
   11034       if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
   11035 	  || (stub_entry->stub_type == ppc_stub_long_branch_r2off
   11036 	      && r2off == 0))
   11037 	{
   11038 	  struct ppc_branch_hash_entry *br_entry;
   11039 
   11040 	  br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
   11041 					     stub_entry->root.string + 9,
   11042 					     TRUE, FALSE);
   11043 	  if (br_entry == NULL)
   11044 	    {
   11045 	      info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
   11046 				      stub_entry->root.string);
   11047 	      htab->stub_error = TRUE;
   11048 	      return FALSE;
   11049 	    }
   11050 
   11051 	  if (br_entry->iter != htab->stub_iteration)
   11052 	    {
   11053 	      br_entry->iter = htab->stub_iteration;
   11054 	      br_entry->offset = htab->brlt->size;
   11055 	      htab->brlt->size += 8;
   11056 
   11057 	      if (htab->relbrlt != NULL)
   11058 		htab->relbrlt->size += sizeof (Elf64_External_Rela);
   11059 	      else if (info->emitrelocations)
   11060 		{
   11061 		  htab->brlt->reloc_count += 1;
   11062 		  htab->brlt->flags |= SEC_RELOC;
   11063 		}
   11064 	    }
   11065 
   11066 	  stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
   11067 	  off = (br_entry->offset
   11068 		 + htab->brlt->output_offset
   11069 		 + htab->brlt->output_section->vma
   11070 		 - elf_gp (htab->brlt->output_section->owner)
   11071 		 - htab->stub_group[stub_entry->id_sec->id].toc_off);
   11072 
   11073 	  if (info->emitrelocations)
   11074 	    {
   11075 	      stub_entry->stub_sec->reloc_count += 1 + (PPC_HA (off) != 0);
   11076 	      stub_entry->stub_sec->flags |= SEC_RELOC;
   11077 	    }
   11078 
   11079 	  if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
   11080 	    {
   11081 	      size = 12;
   11082 	      if (PPC_HA (off) != 0)
   11083 		size = 16;
   11084 	    }
   11085 	  else
   11086 	    {
   11087 	      size = 16;
   11088 	      if (PPC_HA (off) != 0)
   11089 		size += 4;
   11090 
   11091 	      if (PPC_HA (r2off) != 0)
   11092 		size += 4;
   11093 	      if (PPC_LO (r2off) != 0)
   11094 		size += 4;
   11095 	    }
   11096 	}
   11097       else if (info->emitrelocations)
   11098 	{
   11099 	  stub_entry->stub_sec->reloc_count += 1;
   11100 	  stub_entry->stub_sec->flags |= SEC_RELOC;
   11101 	}
   11102     }
   11103 
   11104   stub_entry->stub_sec->size += size;
   11105   return TRUE;
   11106 }
   11107 
   11108 /* Set up various things so that we can make a list of input sections
   11109    for each output section included in the link.  Returns -1 on error,
   11110    0 when no stubs will be needed, and 1 on success.  */
   11111 
   11112 int
   11113 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
   11114 {
   11115   bfd *input_bfd;
   11116   int top_id, top_index, id;
   11117   asection *section;
   11118   asection **input_list;
   11119   bfd_size_type amt;
   11120   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   11121 
   11122   if (htab == NULL)
   11123     return -1;
   11124 
   11125   /* Find the top input section id.  */
   11126   for (input_bfd = info->input_bfds, top_id = 3;
   11127        input_bfd != NULL;
   11128        input_bfd = input_bfd->link.next)
   11129     {
   11130       for (section = input_bfd->sections;
   11131 	   section != NULL;
   11132 	   section = section->next)
   11133 	{
   11134 	  if (top_id < section->id)
   11135 	    top_id = section->id;
   11136 	}
   11137     }
   11138 
   11139   htab->top_id = top_id;
   11140   amt = sizeof (struct map_stub) * (top_id + 1);
   11141   htab->stub_group = bfd_zmalloc (amt);
   11142   if (htab->stub_group == NULL)
   11143     return -1;
   11144 
   11145   /* Set toc_off for com, und, abs and ind sections.  */
   11146   for (id = 0; id < 3; id++)
   11147     htab->stub_group[id].toc_off = TOC_BASE_OFF;
   11148 
   11149   /* We can't use output_bfd->section_count here to find the top output
   11150      section index as some sections may have been removed, and
   11151      strip_excluded_output_sections doesn't renumber the indices.  */
   11152   for (section = info->output_bfd->sections, top_index = 0;
   11153        section != NULL;
   11154        section = section->next)
   11155     {
   11156       if (top_index < section->index)
   11157 	top_index = section->index;
   11158     }
   11159 
   11160   htab->top_index = top_index;
   11161   amt = sizeof (asection *) * (top_index + 1);
   11162   input_list = bfd_zmalloc (amt);
   11163   htab->input_list = input_list;
   11164   if (input_list == NULL)
   11165     return -1;
   11166 
   11167   return 1;
   11168 }
   11169 
   11170 /* Set up for first pass at multitoc partitioning.  */
   11171 
   11172 void
   11173 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
   11174 {
   11175   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   11176 
   11177   htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
   11178   htab->toc_bfd = NULL;
   11179   htab->toc_first_sec = NULL;
   11180 }
   11181 
   11182 /* The linker repeatedly calls this function for each TOC input section
   11183    and linker generated GOT section.  Group input bfds such that the toc
   11184    within a group is less than 64k in size.  */
   11185 
   11186 bfd_boolean
   11187 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
   11188 {
   11189   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   11190   bfd_vma addr, off, limit;
   11191 
   11192   if (htab == NULL)
   11193     return FALSE;
   11194 
   11195   if (!htab->second_toc_pass)
   11196     {
   11197       /* Keep track of the first .toc or .got section for this input bfd.  */
   11198       bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
   11199 
   11200       if (new_bfd)
   11201 	{
   11202 	  htab->toc_bfd = isec->owner;
   11203 	  htab->toc_first_sec = isec;
   11204 	}
   11205 
   11206       addr = isec->output_offset + isec->output_section->vma;
   11207       off = addr - htab->toc_curr;
   11208       limit = 0x80008000;
   11209       if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
   11210 	limit = 0x10000;
   11211       if (off + isec->size > limit)
   11212 	{
   11213 	  addr = (htab->toc_first_sec->output_offset
   11214 		  + htab->toc_first_sec->output_section->vma);
   11215 	  htab->toc_curr = addr;
   11216 	}
   11217 
   11218       /* toc_curr is the base address of this toc group.  Set elf_gp
   11219 	 for the input section to be the offset relative to the
   11220 	 output toc base plus 0x8000.  Making the input elf_gp an
   11221 	 offset allows us to move the toc as a whole without
   11222 	 recalculating input elf_gp.  */
   11223       off = htab->toc_curr - elf_gp (isec->output_section->owner);
   11224       off += TOC_BASE_OFF;
   11225 
   11226       /* Die if someone uses a linker script that doesn't keep input
   11227 	 file .toc and .got together.  */
   11228       if (new_bfd
   11229 	  && elf_gp (isec->owner) != 0
   11230 	  && elf_gp (isec->owner) != off)
   11231 	return FALSE;
   11232 
   11233       elf_gp (isec->owner) = off;
   11234       return TRUE;
   11235     }
   11236 
   11237   /* During the second pass toc_first_sec points to the start of
   11238      a toc group, and toc_curr is used to track the old elf_gp.
   11239      We use toc_bfd to ensure we only look at each bfd once.  */
   11240   if (htab->toc_bfd == isec->owner)
   11241     return TRUE;
   11242   htab->toc_bfd = isec->owner;
   11243 
   11244   if (htab->toc_first_sec == NULL
   11245       || htab->toc_curr != elf_gp (isec->owner))
   11246     {
   11247       htab->toc_curr = elf_gp (isec->owner);
   11248       htab->toc_first_sec = isec;
   11249     }
   11250   addr = (htab->toc_first_sec->output_offset
   11251 	  + htab->toc_first_sec->output_section->vma);
   11252   off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
   11253   elf_gp (isec->owner) = off;
   11254 
   11255   return TRUE;
   11256 }
   11257 
   11258 /* Called via elf_link_hash_traverse to merge GOT entries for global
   11259    symbol H.  */
   11260 
   11261 static bfd_boolean
   11262 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
   11263 {
   11264   if (h->root.type == bfd_link_hash_indirect)
   11265     return TRUE;
   11266 
   11267   merge_got_entries (&h->got.glist);
   11268 
   11269   return TRUE;
   11270 }
   11271 
   11272 /* Called via elf_link_hash_traverse to allocate GOT entries for global
   11273    symbol H.  */
   11274 
   11275 static bfd_boolean
   11276 reallocate_got (struct elf_link_hash_entry *h, void *inf)
   11277 {
   11278   struct got_entry *gent;
   11279 
   11280   if (h->root.type == bfd_link_hash_indirect)
   11281     return TRUE;
   11282 
   11283   for (gent = h->got.glist; gent != NULL; gent = gent->next)
   11284     if (!gent->is_indirect)
   11285       allocate_got (h, (struct bfd_link_info *) inf, gent);
   11286   return TRUE;
   11287 }
   11288 
   11289 /* Called on the first multitoc pass after the last call to
   11290    ppc64_elf_next_toc_section.  This function removes duplicate GOT
   11291    entries.  */
   11292 
   11293 bfd_boolean
   11294 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
   11295 {
   11296   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   11297   struct bfd *ibfd, *ibfd2;
   11298   bfd_boolean done_something;
   11299 
   11300   htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
   11301 
   11302   if (!htab->do_multi_toc)
   11303     return FALSE;
   11304 
   11305   /* Merge global sym got entries within a toc group.  */
   11306   elf_link_hash_traverse (&htab->elf, merge_global_got, info);
   11307 
   11308   /* And tlsld_got.  */
   11309   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   11310     {
   11311       struct got_entry *ent, *ent2;
   11312 
   11313       if (!is_ppc64_elf (ibfd))
   11314 	continue;
   11315 
   11316       ent = ppc64_tlsld_got (ibfd);
   11317       if (!ent->is_indirect
   11318 	  && ent->got.offset != (bfd_vma) -1)
   11319 	{
   11320 	  for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
   11321 	    {
   11322 	      if (!is_ppc64_elf (ibfd2))
   11323 		continue;
   11324 
   11325 	      ent2 = ppc64_tlsld_got (ibfd2);
   11326 	      if (!ent2->is_indirect
   11327 		  && ent2->got.offset != (bfd_vma) -1
   11328 		  && elf_gp (ibfd2) == elf_gp (ibfd))
   11329 		{
   11330 		  ent2->is_indirect = TRUE;
   11331 		  ent2->got.ent = ent;
   11332 		}
   11333 	    }
   11334 	}
   11335     }
   11336 
   11337   /* Zap sizes of got sections.  */
   11338   htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
   11339   htab->elf.irelplt->size -= htab->got_reli_size;
   11340   htab->got_reli_size = 0;
   11341 
   11342   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   11343     {
   11344       asection *got, *relgot;
   11345 
   11346       if (!is_ppc64_elf (ibfd))
   11347 	continue;
   11348 
   11349       got = ppc64_elf_tdata (ibfd)->got;
   11350       if (got != NULL)
   11351 	{
   11352 	  got->rawsize = got->size;
   11353 	  got->size = 0;
   11354 	  relgot = ppc64_elf_tdata (ibfd)->relgot;
   11355 	  relgot->rawsize = relgot->size;
   11356 	  relgot->size = 0;
   11357 	}
   11358     }
   11359 
   11360   /* Now reallocate the got, local syms first.  We don't need to
   11361      allocate section contents again since we never increase size.  */
   11362   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   11363     {
   11364       struct got_entry **lgot_ents;
   11365       struct got_entry **end_lgot_ents;
   11366       struct plt_entry **local_plt;
   11367       struct plt_entry **end_local_plt;
   11368       unsigned char *lgot_masks;
   11369       bfd_size_type locsymcount;
   11370       Elf_Internal_Shdr *symtab_hdr;
   11371       asection *s;
   11372 
   11373       if (!is_ppc64_elf (ibfd))
   11374 	continue;
   11375 
   11376       lgot_ents = elf_local_got_ents (ibfd);
   11377       if (!lgot_ents)
   11378 	continue;
   11379 
   11380       symtab_hdr = &elf_symtab_hdr (ibfd);
   11381       locsymcount = symtab_hdr->sh_info;
   11382       end_lgot_ents = lgot_ents + locsymcount;
   11383       local_plt = (struct plt_entry **) end_lgot_ents;
   11384       end_local_plt = local_plt + locsymcount;
   11385       lgot_masks = (unsigned char *) end_local_plt;
   11386       s = ppc64_elf_tdata (ibfd)->got;
   11387       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
   11388 	{
   11389 	  struct got_entry *ent;
   11390 
   11391 	  for (ent = *lgot_ents; ent != NULL; ent = ent->next)
   11392 	    {
   11393 	      unsigned int ent_size = 8;
   11394 	      unsigned int rel_size = sizeof (Elf64_External_Rela);
   11395 
   11396 	      ent->got.offset = s->size;
   11397 	      if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
   11398 		{
   11399 		  ent_size *= 2;
   11400 		  rel_size *= 2;
   11401 		}
   11402 	      s->size += ent_size;
   11403 	      if ((*lgot_masks & PLT_IFUNC) != 0)
   11404 		{
   11405 		  htab->elf.irelplt->size += rel_size;
   11406 		  htab->got_reli_size += rel_size;
   11407 		}
   11408 	      else if (info->shared)
   11409 		{
   11410 		  asection *srel = ppc64_elf_tdata (ibfd)->relgot;
   11411 		  srel->size += rel_size;
   11412 		}
   11413 	    }
   11414 	}
   11415     }
   11416 
   11417   elf_link_hash_traverse (&htab->elf, reallocate_got, info);
   11418 
   11419   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   11420     {
   11421       struct got_entry *ent;
   11422 
   11423       if (!is_ppc64_elf (ibfd))
   11424 	continue;
   11425 
   11426       ent = ppc64_tlsld_got (ibfd);
   11427       if (!ent->is_indirect
   11428 	  && ent->got.offset != (bfd_vma) -1)
   11429 	{
   11430 	  asection *s = ppc64_elf_tdata (ibfd)->got;
   11431 	  ent->got.offset = s->size;
   11432 	  s->size += 16;
   11433 	  if (info->shared)
   11434 	    {
   11435 	      asection *srel = ppc64_elf_tdata (ibfd)->relgot;
   11436 	      srel->size += sizeof (Elf64_External_Rela);
   11437 	    }
   11438 	}
   11439     }
   11440 
   11441   done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
   11442   if (!done_something)
   11443     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   11444       {
   11445 	asection *got;
   11446 
   11447 	if (!is_ppc64_elf (ibfd))
   11448 	  continue;
   11449 
   11450 	got = ppc64_elf_tdata (ibfd)->got;
   11451 	if (got != NULL)
   11452 	  {
   11453 	    done_something = got->rawsize != got->size;
   11454 	    if (done_something)
   11455 	      break;
   11456 	  }
   11457       }
   11458 
   11459   if (done_something)
   11460     (*htab->params->layout_sections_again) ();
   11461 
   11462   /* Set up for second pass over toc sections to recalculate elf_gp
   11463      on input sections.  */
   11464   htab->toc_bfd = NULL;
   11465   htab->toc_first_sec = NULL;
   11466   htab->second_toc_pass = TRUE;
   11467   return done_something;
   11468 }
   11469 
   11470 /* Called after second pass of multitoc partitioning.  */
   11471 
   11472 void
   11473 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
   11474 {
   11475   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   11476 
   11477   /* After the second pass, toc_curr tracks the TOC offset used
   11478      for code sections below in ppc64_elf_next_input_section.  */
   11479   htab->toc_curr = TOC_BASE_OFF;
   11480 }
   11481 
   11482 /* No toc references were found in ISEC.  If the code in ISEC makes no
   11483    calls, then there's no need to use toc adjusting stubs when branching
   11484    into ISEC.  Actually, indirect calls from ISEC are OK as they will
   11485    load r2.  Returns -1 on error, 0 for no stub needed, 1 for stub
   11486    needed, and 2 if a cyclical call-graph was found but no other reason
   11487    for a stub was detected.  If called from the top level, a return of
   11488    2 means the same as a return of 0.  */
   11489 
   11490 static int
   11491 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
   11492 {
   11493   int ret;
   11494 
   11495   /* Mark this section as checked.  */
   11496   isec->call_check_done = 1;
   11497 
   11498   /* We know none of our code bearing sections will need toc stubs.  */
   11499   if ((isec->flags & SEC_LINKER_CREATED) != 0)
   11500     return 0;
   11501 
   11502   if (isec->size == 0)
   11503     return 0;
   11504 
   11505   if (isec->output_section == NULL)
   11506     return 0;
   11507 
   11508   ret = 0;
   11509   if (isec->reloc_count != 0)
   11510     {
   11511       Elf_Internal_Rela *relstart, *rel;
   11512       Elf_Internal_Sym *local_syms;
   11513       struct ppc_link_hash_table *htab;
   11514 
   11515       relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
   11516 					    info->keep_memory);
   11517       if (relstart == NULL)
   11518 	return -1;
   11519 
   11520       /* Look for branches to outside of this section.  */
   11521       local_syms = NULL;
   11522       htab = ppc_hash_table (info);
   11523       if (htab == NULL)
   11524 	return -1;
   11525 
   11526       for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
   11527 	{
   11528 	  enum elf_ppc64_reloc_type r_type;
   11529 	  unsigned long r_symndx;
   11530 	  struct elf_link_hash_entry *h;
   11531 	  struct ppc_link_hash_entry *eh;
   11532 	  Elf_Internal_Sym *sym;
   11533 	  asection *sym_sec;
   11534 	  struct _opd_sec_data *opd;
   11535 	  bfd_vma sym_value;
   11536 	  bfd_vma dest;
   11537 
   11538 	  r_type = ELF64_R_TYPE (rel->r_info);
   11539 	  if (r_type != R_PPC64_REL24
   11540 	      && r_type != R_PPC64_REL14
   11541 	      && r_type != R_PPC64_REL14_BRTAKEN
   11542 	      && r_type != R_PPC64_REL14_BRNTAKEN)
   11543 	    continue;
   11544 
   11545 	  r_symndx = ELF64_R_SYM (rel->r_info);
   11546 	  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
   11547 			  isec->owner))
   11548 	    {
   11549 	      ret = -1;
   11550 	      break;
   11551 	    }
   11552 
   11553 	  /* Calls to dynamic lib functions go through a plt call stub
   11554 	     that uses r2.  */
   11555 	  eh = (struct ppc_link_hash_entry *) h;
   11556 	  if (eh != NULL
   11557 	      && (eh->elf.plt.plist != NULL
   11558 		  || (eh->oh != NULL
   11559 		      && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
   11560 	    {
   11561 	      ret = 1;
   11562 	      break;
   11563 	    }
   11564 
   11565 	  if (sym_sec == NULL)
   11566 	    /* Ignore other undefined symbols.  */
   11567 	    continue;
   11568 
   11569 	  /* Assume branches to other sections not included in the
   11570 	     link need stubs too, to cover -R and absolute syms.  */
   11571 	  if (sym_sec->output_section == NULL)
   11572 	    {
   11573 	      ret = 1;
   11574 	      break;
   11575 	    }
   11576 
   11577 	  if (h == NULL)
   11578 	    sym_value = sym->st_value;
   11579 	  else
   11580 	    {
   11581 	      if (h->root.type != bfd_link_hash_defined
   11582 		  && h->root.type != bfd_link_hash_defweak)
   11583 		abort ();
   11584 	      sym_value = h->root.u.def.value;
   11585 	    }
   11586 	  sym_value += rel->r_addend;
   11587 
   11588 	  /* If this branch reloc uses an opd sym, find the code section.  */
   11589 	  opd = get_opd_info (sym_sec);
   11590 	  if (opd != NULL)
   11591 	    {
   11592 	      if (h == NULL && opd->adjust != NULL)
   11593 		{
   11594 		  long adjust;
   11595 
   11596 		  adjust = opd->adjust[sym->st_value / 8];
   11597 		  if (adjust == -1)
   11598 		    /* Assume deleted functions won't ever be called.  */
   11599 		    continue;
   11600 		  sym_value += adjust;
   11601 		}
   11602 
   11603 	      dest = opd_entry_value (sym_sec, sym_value,
   11604 				      &sym_sec, NULL, FALSE);
   11605 	      if (dest == (bfd_vma) -1)
   11606 		continue;
   11607 	    }
   11608 	  else
   11609 	    dest = (sym_value
   11610 		    + sym_sec->output_offset
   11611 		    + sym_sec->output_section->vma);
   11612 
   11613 	  /* Ignore branch to self.  */
   11614 	  if (sym_sec == isec)
   11615 	    continue;
   11616 
   11617 	  /* If the called function uses the toc, we need a stub.  */
   11618 	  if (sym_sec->has_toc_reloc
   11619 	      || sym_sec->makes_toc_func_call)
   11620 	    {
   11621 	      ret = 1;
   11622 	      break;
   11623 	    }
   11624 
   11625 	  /* Assume any branch that needs a long branch stub might in fact
   11626 	     need a plt_branch stub.  A plt_branch stub uses r2.  */
   11627 	  else if (dest - (isec->output_offset
   11628 			   + isec->output_section->vma
   11629 			   + rel->r_offset) + (1 << 25)
   11630 		   >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
   11631 							     ? h->other
   11632 							     : sym->st_other))
   11633 	    {
   11634 	      ret = 1;
   11635 	      break;
   11636 	    }
   11637 
   11638 	  /* If calling back to a section in the process of being
   11639 	     tested, we can't say for sure that no toc adjusting stubs
   11640 	     are needed, so don't return zero.  */
   11641 	  else if (sym_sec->call_check_in_progress)
   11642 	    ret = 2;
   11643 
   11644 	  /* Branches to another section that itself doesn't have any TOC
   11645 	     references are OK.  Recursively call ourselves to check.  */
   11646 	  else if (!sym_sec->call_check_done)
   11647 	    {
   11648 	      int recur;
   11649 
   11650 	      /* Mark current section as indeterminate, so that other
   11651 		 sections that call back to current won't be marked as
   11652 		 known.  */
   11653 	      isec->call_check_in_progress = 1;
   11654 	      recur = toc_adjusting_stub_needed (info, sym_sec);
   11655 	      isec->call_check_in_progress = 0;
   11656 
   11657 	      if (recur != 0)
   11658 		{
   11659 		  ret = recur;
   11660 		  if (recur != 2)
   11661 		    break;
   11662 		}
   11663 	    }
   11664 	}
   11665 
   11666       if (local_syms != NULL
   11667 	  && (elf_symtab_hdr (isec->owner).contents
   11668 	      != (unsigned char *) local_syms))
   11669 	free (local_syms);
   11670       if (elf_section_data (isec)->relocs != relstart)
   11671 	free (relstart);
   11672     }
   11673 
   11674   if ((ret & 1) == 0
   11675       && isec->map_head.s != NULL
   11676       && (strcmp (isec->output_section->name, ".init") == 0
   11677 	  || strcmp (isec->output_section->name, ".fini") == 0))
   11678     {
   11679       if (isec->map_head.s->has_toc_reloc
   11680 	  || isec->map_head.s->makes_toc_func_call)
   11681 	ret = 1;
   11682       else if (!isec->map_head.s->call_check_done)
   11683 	{
   11684 	  int recur;
   11685 	  isec->call_check_in_progress = 1;
   11686 	  recur = toc_adjusting_stub_needed (info, isec->map_head.s);
   11687 	  isec->call_check_in_progress = 0;
   11688 	  if (recur != 0)
   11689 	    ret = recur;
   11690 	}
   11691     }
   11692 
   11693   if (ret == 1)
   11694     isec->makes_toc_func_call = 1;
   11695 
   11696   return ret;
   11697 }
   11698 
   11699 /* The linker repeatedly calls this function for each input section,
   11700    in the order that input sections are linked into output sections.
   11701    Build lists of input sections to determine groupings between which
   11702    we may insert linker stubs.  */
   11703 
   11704 bfd_boolean
   11705 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
   11706 {
   11707   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   11708 
   11709   if (htab == NULL)
   11710     return FALSE;
   11711 
   11712   if ((isec->output_section->flags & SEC_CODE) != 0
   11713       && isec->output_section->index <= htab->top_index)
   11714     {
   11715       asection **list = htab->input_list + isec->output_section->index;
   11716       /* Steal the link_sec pointer for our list.  */
   11717 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
   11718       /* This happens to make the list in reverse order,
   11719 	 which is what we want.  */
   11720       PREV_SEC (isec) = *list;
   11721       *list = isec;
   11722     }
   11723 
   11724   if (htab->multi_toc_needed)
   11725     {
   11726       /* Analyse sections that aren't already flagged as needing a
   11727 	 valid toc pointer.  Exclude .fixup for the linux kernel.
   11728 	 .fixup contains branches, but only back to the function that
   11729 	 hit an exception.  */
   11730       if (!(isec->has_toc_reloc
   11731 	    || (isec->flags & SEC_CODE) == 0
   11732 	    || strcmp (isec->name, ".fixup") == 0
   11733 	    || isec->call_check_done))
   11734 	{
   11735 	  if (toc_adjusting_stub_needed (info, isec) < 0)
   11736 	    return FALSE;
   11737 	}
   11738       /* Make all sections use the TOC assigned for this object file.
   11739 	 This will be wrong for pasted sections;  We fix that in
   11740 	 check_pasted_section().  */
   11741       if (elf_gp (isec->owner) != 0)
   11742 	htab->toc_curr = elf_gp (isec->owner);
   11743     }
   11744 
   11745   htab->stub_group[isec->id].toc_off = htab->toc_curr;
   11746   return TRUE;
   11747 }
   11748 
   11749 /* Check that all .init and .fini sections use the same toc, if they
   11750    have toc relocs.  */
   11751 
   11752 static bfd_boolean
   11753 check_pasted_section (struct bfd_link_info *info, const char *name)
   11754 {
   11755   asection *o = bfd_get_section_by_name (info->output_bfd, name);
   11756 
   11757   if (o != NULL)
   11758     {
   11759       struct ppc_link_hash_table *htab = ppc_hash_table (info);
   11760       bfd_vma toc_off = 0;
   11761       asection *i;
   11762 
   11763       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   11764 	if (i->has_toc_reloc)
   11765 	  {
   11766 	    if (toc_off == 0)
   11767 	      toc_off = htab->stub_group[i->id].toc_off;
   11768 	    else if (toc_off != htab->stub_group[i->id].toc_off)
   11769 	      return FALSE;
   11770 	  }
   11771 
   11772       if (toc_off == 0)
   11773 	for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   11774 	  if (i->makes_toc_func_call)
   11775 	    {
   11776 	      toc_off = htab->stub_group[i->id].toc_off;
   11777 	      break;
   11778 	    }
   11779 
   11780       /* Make sure the whole pasted function uses the same toc offset.  */
   11781       if (toc_off != 0)
   11782 	for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   11783 	  htab->stub_group[i->id].toc_off = toc_off;
   11784     }
   11785   return TRUE;
   11786 }
   11787 
   11788 bfd_boolean
   11789 ppc64_elf_check_init_fini (struct bfd_link_info *info)
   11790 {
   11791   return (check_pasted_section (info, ".init")
   11792 	  & check_pasted_section (info, ".fini"));
   11793 }
   11794 
   11795 /* See whether we can group stub sections together.  Grouping stub
   11796    sections may result in fewer stubs.  More importantly, we need to
   11797    put all .init* and .fini* stubs at the beginning of the .init or
   11798    .fini output sections respectively, because glibc splits the
   11799    _init and _fini functions into multiple parts.  Putting a stub in
   11800    the middle of a function is not a good idea.  */
   11801 
   11802 static void
   11803 group_sections (struct ppc_link_hash_table *htab,
   11804 		bfd_size_type stub_group_size,
   11805 		bfd_boolean stubs_always_before_branch)
   11806 {
   11807   asection **list;
   11808   bfd_size_type stub14_group_size;
   11809   bfd_boolean suppress_size_errors;
   11810 
   11811   suppress_size_errors = FALSE;
   11812   stub14_group_size = stub_group_size >> 10;
   11813   if (stub_group_size == 1)
   11814     {
   11815       /* Default values.  */
   11816       if (stubs_always_before_branch)
   11817 	{
   11818 	  stub_group_size = 0x1e00000;
   11819 	  stub14_group_size = 0x7800;
   11820 	}
   11821       else
   11822 	{
   11823 	  stub_group_size = 0x1c00000;
   11824 	  stub14_group_size = 0x7000;
   11825 	}
   11826       suppress_size_errors = TRUE;
   11827     }
   11828 
   11829   list = htab->input_list + htab->top_index;
   11830   do
   11831     {
   11832       asection *tail = *list;
   11833       while (tail != NULL)
   11834 	{
   11835 	  asection *curr;
   11836 	  asection *prev;
   11837 	  bfd_size_type total;
   11838 	  bfd_boolean big_sec;
   11839 	  bfd_vma curr_toc;
   11840 
   11841 	  curr = tail;
   11842 	  total = tail->size;
   11843 	  big_sec = total > (ppc64_elf_section_data (tail) != NULL
   11844 			     && ppc64_elf_section_data (tail)->has_14bit_branch
   11845 			     ? stub14_group_size : stub_group_size);
   11846 	  if (big_sec && !suppress_size_errors)
   11847 	    (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
   11848 				     tail->owner, tail);
   11849 	  curr_toc = htab->stub_group[tail->id].toc_off;
   11850 
   11851 	  while ((prev = PREV_SEC (curr)) != NULL
   11852 		 && ((total += curr->output_offset - prev->output_offset)
   11853 		     < (ppc64_elf_section_data (prev) != NULL
   11854 			&& ppc64_elf_section_data (prev)->has_14bit_branch
   11855 			? stub14_group_size : stub_group_size))
   11856 		 && htab->stub_group[prev->id].toc_off == curr_toc)
   11857 	    curr = prev;
   11858 
   11859 	  /* OK, the size from the start of CURR to the end is less
   11860 	     than stub_group_size and thus can be handled by one stub
   11861 	     section.  (or the tail section is itself larger than
   11862 	     stub_group_size, in which case we may be toast.)  We
   11863 	     should really be keeping track of the total size of stubs
   11864 	     added here, as stubs contribute to the final output
   11865 	     section size.  That's a little tricky, and this way will
   11866 	     only break if stubs added make the total size more than
   11867 	     2^25, ie. for the default stub_group_size, if stubs total
   11868 	     more than 2097152 bytes, or nearly 75000 plt call stubs.  */
   11869 	  do
   11870 	    {
   11871 	      prev = PREV_SEC (tail);
   11872 	      /* Set up this stub group.  */
   11873 	      htab->stub_group[tail->id].link_sec = curr;
   11874 	    }
   11875 	  while (tail != curr && (tail = prev) != NULL);
   11876 
   11877 	  /* But wait, there's more!  Input sections up to stub_group_size
   11878 	     bytes before the stub section can be handled by it too.
   11879 	     Don't do this if we have a really large section after the
   11880 	     stubs, as adding more stubs increases the chance that
   11881 	     branches may not reach into the stub section.  */
   11882 	  if (!stubs_always_before_branch && !big_sec)
   11883 	    {
   11884 	      total = 0;
   11885 	      while (prev != NULL
   11886 		     && ((total += tail->output_offset - prev->output_offset)
   11887 			 < (ppc64_elf_section_data (prev) != NULL
   11888 			    && ppc64_elf_section_data (prev)->has_14bit_branch
   11889 			    ? stub14_group_size : stub_group_size))
   11890 		     && htab->stub_group[prev->id].toc_off == curr_toc)
   11891 		{
   11892 		  tail = prev;
   11893 		  prev = PREV_SEC (tail);
   11894 		  htab->stub_group[tail->id].link_sec = curr;
   11895 		}
   11896 	    }
   11897 	  tail = prev;
   11898 	}
   11899     }
   11900   while (list-- != htab->input_list);
   11901   free (htab->input_list);
   11902 #undef PREV_SEC
   11903 }
   11904 
   11905 static const unsigned char glink_eh_frame_cie[] =
   11906 {
   11907   0, 0, 0, 16,				/* length.  */
   11908   0, 0, 0, 0,				/* id.  */
   11909   1,					/* CIE version.  */
   11910   'z', 'R', 0,				/* Augmentation string.  */
   11911   4,					/* Code alignment.  */
   11912   0x78,					/* Data alignment.  */
   11913   65,					/* RA reg.  */
   11914   1,					/* Augmentation size.  */
   11915   DW_EH_PE_pcrel | DW_EH_PE_sdata4,	/* FDE encoding.  */
   11916   DW_CFA_def_cfa, 1, 0,			/* def_cfa: r1 offset 0.  */
   11917   0, 0, 0, 0
   11918 };
   11919 
   11920 /* Stripping output sections is normally done before dynamic section
   11921    symbols have been allocated.  This function is called later, and
   11922    handles cases like htab->brlt which is mapped to its own output
   11923    section.  */
   11924 
   11925 static void
   11926 maybe_strip_output (struct bfd_link_info *info, asection *isec)
   11927 {
   11928   if (isec->size == 0
   11929       && isec->output_section->size == 0
   11930       && !(isec->output_section->flags & SEC_KEEP)
   11931       && !bfd_section_removed_from_list (info->output_bfd,
   11932 					 isec->output_section)
   11933       && elf_section_data (isec->output_section)->dynindx == 0)
   11934     {
   11935       isec->output_section->flags |= SEC_EXCLUDE;
   11936       bfd_section_list_remove (info->output_bfd, isec->output_section);
   11937       info->output_bfd->section_count--;
   11938     }
   11939 }
   11940 
   11941 /* Determine and set the size of the stub section for a final link.
   11942 
   11943    The basic idea here is to examine all the relocations looking for
   11944    PC-relative calls to a target that is unreachable with a "bl"
   11945    instruction.  */
   11946 
   11947 bfd_boolean
   11948 ppc64_elf_size_stubs (struct bfd_link_info *info)
   11949 {
   11950   bfd_size_type stub_group_size;
   11951   bfd_boolean stubs_always_before_branch;
   11952   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   11953 
   11954   if (htab == NULL)
   11955     return FALSE;
   11956 
   11957   if (htab->params->plt_thread_safe == -1 && !info->executable)
   11958     htab->params->plt_thread_safe = 1;
   11959   if (!htab->opd_abi)
   11960     htab->params->plt_thread_safe = 0;
   11961   else if (htab->params->plt_thread_safe == -1)
   11962     {
   11963       static const char *const thread_starter[] =
   11964 	{
   11965 	  "pthread_create",
   11966 	  /* libstdc++ */
   11967 	  "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
   11968 	  /* librt */
   11969 	  "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
   11970 	  "mq_notify", "create_timer",
   11971 	  /* libanl */
   11972 	  "getaddrinfo_a",
   11973 	  /* libgomp */
   11974 	  "GOMP_parallel",
   11975 	  "GOMP_parallel_start",
   11976 	  "GOMP_parallel_loop_static",
   11977 	  "GOMP_parallel_loop_static_start",
   11978 	  "GOMP_parallel_loop_dynamic",
   11979 	  "GOMP_parallel_loop_dynamic_start",
   11980 	  "GOMP_parallel_loop_guided",
   11981 	  "GOMP_parallel_loop_guided_start",
   11982 	  "GOMP_parallel_loop_runtime",
   11983 	  "GOMP_parallel_loop_runtime_start",
   11984 	  "GOMP_parallel_sections",
   11985 	  "GOMP_parallel_sections_start",
   11986 	  /* libgo */
   11987 	  "__go_go",
   11988 	};
   11989       unsigned i;
   11990 
   11991       for (i = 0; i < sizeof (thread_starter)/ sizeof (thread_starter[0]); i++)
   11992 	{
   11993 	  struct elf_link_hash_entry *h;
   11994 	  h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
   11995 				    FALSE, FALSE, TRUE);
   11996 	  htab->params->plt_thread_safe = h != NULL && h->ref_regular;
   11997 	  if (htab->params->plt_thread_safe)
   11998 	    break;
   11999 	}
   12000     }
   12001   stubs_always_before_branch = htab->params->group_size < 0;
   12002   if (htab->params->group_size < 0)
   12003     stub_group_size = -htab->params->group_size;
   12004   else
   12005     stub_group_size = htab->params->group_size;
   12006 
   12007   group_sections (htab, stub_group_size, stubs_always_before_branch);
   12008 
   12009   while (1)
   12010     {
   12011       bfd *input_bfd;
   12012       unsigned int bfd_indx;
   12013       asection *stub_sec;
   12014 
   12015       htab->stub_iteration += 1;
   12016 
   12017       for (input_bfd = info->input_bfds, bfd_indx = 0;
   12018 	   input_bfd != NULL;
   12019 	   input_bfd = input_bfd->link.next, bfd_indx++)
   12020 	{
   12021 	  Elf_Internal_Shdr *symtab_hdr;
   12022 	  asection *section;
   12023 	  Elf_Internal_Sym *local_syms = NULL;
   12024 
   12025 	  if (!is_ppc64_elf (input_bfd))
   12026 	    continue;
   12027 
   12028 	  /* We'll need the symbol table in a second.  */
   12029 	  symtab_hdr = &elf_symtab_hdr (input_bfd);
   12030 	  if (symtab_hdr->sh_info == 0)
   12031 	    continue;
   12032 
   12033 	  /* Walk over each section attached to the input bfd.  */
   12034 	  for (section = input_bfd->sections;
   12035 	       section != NULL;
   12036 	       section = section->next)
   12037 	    {
   12038 	      Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
   12039 
   12040 	      /* If there aren't any relocs, then there's nothing more
   12041 		 to do.  */
   12042 	      if ((section->flags & SEC_RELOC) == 0
   12043 		  || (section->flags & SEC_ALLOC) == 0
   12044 		  || (section->flags & SEC_LOAD) == 0
   12045 		  || (section->flags & SEC_CODE) == 0
   12046 		  || section->reloc_count == 0)
   12047 		continue;
   12048 
   12049 	      /* If this section is a link-once section that will be
   12050 		 discarded, then don't create any stubs.  */
   12051 	      if (section->output_section == NULL
   12052 		  || section->output_section->owner != info->output_bfd)
   12053 		continue;
   12054 
   12055 	      /* Get the relocs.  */
   12056 	      internal_relocs
   12057 		= _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
   12058 					     info->keep_memory);
   12059 	      if (internal_relocs == NULL)
   12060 		goto error_ret_free_local;
   12061 
   12062 	      /* Now examine each relocation.  */
   12063 	      irela = internal_relocs;
   12064 	      irelaend = irela + section->reloc_count;
   12065 	      for (; irela < irelaend; irela++)
   12066 		{
   12067 		  enum elf_ppc64_reloc_type r_type;
   12068 		  unsigned int r_indx;
   12069 		  enum ppc_stub_type stub_type;
   12070 		  struct ppc_stub_hash_entry *stub_entry;
   12071 		  asection *sym_sec, *code_sec;
   12072 		  bfd_vma sym_value, code_value;
   12073 		  bfd_vma destination;
   12074 		  unsigned long local_off;
   12075 		  bfd_boolean ok_dest;
   12076 		  struct ppc_link_hash_entry *hash;
   12077 		  struct ppc_link_hash_entry *fdh;
   12078 		  struct elf_link_hash_entry *h;
   12079 		  Elf_Internal_Sym *sym;
   12080 		  char *stub_name;
   12081 		  const asection *id_sec;
   12082 		  struct _opd_sec_data *opd;
   12083 		  struct plt_entry *plt_ent;
   12084 
   12085 		  r_type = ELF64_R_TYPE (irela->r_info);
   12086 		  r_indx = ELF64_R_SYM (irela->r_info);
   12087 
   12088 		  if (r_type >= R_PPC64_max)
   12089 		    {
   12090 		      bfd_set_error (bfd_error_bad_value);
   12091 		      goto error_ret_free_internal;
   12092 		    }
   12093 
   12094 		  /* Only look for stubs on branch instructions.  */
   12095 		  if (r_type != R_PPC64_REL24
   12096 		      && r_type != R_PPC64_REL14
   12097 		      && r_type != R_PPC64_REL14_BRTAKEN
   12098 		      && r_type != R_PPC64_REL14_BRNTAKEN)
   12099 		    continue;
   12100 
   12101 		  /* Now determine the call target, its name, value,
   12102 		     section.  */
   12103 		  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   12104 				  r_indx, input_bfd))
   12105 		    goto error_ret_free_internal;
   12106 		  hash = (struct ppc_link_hash_entry *) h;
   12107 
   12108 		  ok_dest = FALSE;
   12109 		  fdh = NULL;
   12110 		  sym_value = 0;
   12111 		  if (hash == NULL)
   12112 		    {
   12113 		      sym_value = sym->st_value;
   12114 		      ok_dest = TRUE;
   12115 		    }
   12116 		  else if (hash->elf.root.type == bfd_link_hash_defined
   12117 			   || hash->elf.root.type == bfd_link_hash_defweak)
   12118 		    {
   12119 		      sym_value = hash->elf.root.u.def.value;
   12120 		      if (sym_sec->output_section != NULL)
   12121 			ok_dest = TRUE;
   12122 		    }
   12123 		  else if (hash->elf.root.type == bfd_link_hash_undefweak
   12124 			   || hash->elf.root.type == bfd_link_hash_undefined)
   12125 		    {
   12126 		      /* Recognise an old ABI func code entry sym, and
   12127 			 use the func descriptor sym instead if it is
   12128 			 defined.  */
   12129 		      if (hash->elf.root.root.string[0] == '.'
   12130 			  && (fdh = lookup_fdh (hash, htab)) != NULL)
   12131 			{
   12132 			  if (fdh->elf.root.type == bfd_link_hash_defined
   12133 			      || fdh->elf.root.type == bfd_link_hash_defweak)
   12134 			    {
   12135 			      sym_sec = fdh->elf.root.u.def.section;
   12136 			      sym_value = fdh->elf.root.u.def.value;
   12137 			      if (sym_sec->output_section != NULL)
   12138 				ok_dest = TRUE;
   12139 			    }
   12140 			  else
   12141 			    fdh = NULL;
   12142 			}
   12143 		    }
   12144 		  else
   12145 		    {
   12146 		      bfd_set_error (bfd_error_bad_value);
   12147 		      goto error_ret_free_internal;
   12148 		    }
   12149 
   12150 		  destination = 0;
   12151 		  local_off = 0;
   12152 		  if (ok_dest)
   12153 		    {
   12154 		      sym_value += irela->r_addend;
   12155 		      destination = (sym_value
   12156 				     + sym_sec->output_offset
   12157 				     + sym_sec->output_section->vma);
   12158 		      local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
   12159 							    ? hash->elf.other
   12160 							    : sym->st_other);
   12161 		    }
   12162 
   12163 		  code_sec = sym_sec;
   12164 		  code_value = sym_value;
   12165 		  opd = get_opd_info (sym_sec);
   12166 		  if (opd != NULL)
   12167 		    {
   12168 		      bfd_vma dest;
   12169 
   12170 		      if (hash == NULL && opd->adjust != NULL)
   12171 			{
   12172 			  long adjust = opd->adjust[sym_value / 8];
   12173 			  if (adjust == -1)
   12174 			    continue;
   12175 			  code_value += adjust;
   12176 			  sym_value += adjust;
   12177 			}
   12178 		      dest = opd_entry_value (sym_sec, sym_value,
   12179 					      &code_sec, &code_value, FALSE);
   12180 		      if (dest != (bfd_vma) -1)
   12181 			{
   12182 			  destination = dest;
   12183 			  if (fdh != NULL)
   12184 			    {
   12185 			      /* Fixup old ABI sym to point at code
   12186 				 entry.  */
   12187 			      hash->elf.root.type = bfd_link_hash_defweak;
   12188 			      hash->elf.root.u.def.section = code_sec;
   12189 			      hash->elf.root.u.def.value = code_value;
   12190 			    }
   12191 			}
   12192 		    }
   12193 
   12194 		  /* Determine what (if any) linker stub is needed.  */
   12195 		  plt_ent = NULL;
   12196 		  stub_type = ppc_type_of_stub (section, irela, &hash,
   12197 						&plt_ent, destination,
   12198 						local_off);
   12199 
   12200 		  if (stub_type != ppc_stub_plt_call)
   12201 		    {
   12202 		      /* Check whether we need a TOC adjusting stub.
   12203 			 Since the linker pastes together pieces from
   12204 			 different object files when creating the
   12205 			 _init and _fini functions, it may be that a
   12206 			 call to what looks like a local sym is in
   12207 			 fact a call needing a TOC adjustment.  */
   12208 		      if (code_sec != NULL
   12209 			  && code_sec->output_section != NULL
   12210 			  && (htab->stub_group[code_sec->id].toc_off
   12211 			      != htab->stub_group[section->id].toc_off)
   12212 			  && (code_sec->has_toc_reloc
   12213 			      || code_sec->makes_toc_func_call))
   12214 			stub_type = ppc_stub_long_branch_r2off;
   12215 		    }
   12216 
   12217 		  if (stub_type == ppc_stub_none)
   12218 		    continue;
   12219 
   12220 		  /* __tls_get_addr calls might be eliminated.  */
   12221 		  if (stub_type != ppc_stub_plt_call
   12222 		      && hash != NULL
   12223 		      && (hash == htab->tls_get_addr
   12224 			  || hash == htab->tls_get_addr_fd)
   12225 		      && section->has_tls_reloc
   12226 		      && irela != internal_relocs)
   12227 		    {
   12228 		      /* Get tls info.  */
   12229 		      unsigned char *tls_mask;
   12230 
   12231 		      if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
   12232 					 irela - 1, input_bfd))
   12233 			goto error_ret_free_internal;
   12234 		      if (*tls_mask != 0)
   12235 			continue;
   12236 		    }
   12237 
   12238 		  if (stub_type == ppc_stub_plt_call
   12239 		      && irela + 1 < irelaend
   12240 		      && irela[1].r_offset == irela->r_offset + 4
   12241 		      && ELF64_R_TYPE (irela[1].r_info) == R_PPC64_TOCSAVE)
   12242 		    {
   12243 		      if (!tocsave_find (htab, INSERT,
   12244 					 &local_syms, irela + 1, input_bfd))
   12245 			goto error_ret_free_internal;
   12246 		    }
   12247 		  else if (stub_type == ppc_stub_plt_call)
   12248 		    stub_type = ppc_stub_plt_call_r2save;
   12249 
   12250 		  /* Support for grouping stub sections.  */
   12251 		  id_sec = htab->stub_group[section->id].link_sec;
   12252 
   12253 		  /* Get the name of this stub.  */
   12254 		  stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
   12255 		  if (!stub_name)
   12256 		    goto error_ret_free_internal;
   12257 
   12258 		  stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
   12259 						     stub_name, FALSE, FALSE);
   12260 		  if (stub_entry != NULL)
   12261 		    {
   12262 		      /* The proper stub has already been created.  */
   12263 		      free (stub_name);
   12264 		      if (stub_type == ppc_stub_plt_call_r2save)
   12265 			stub_entry->stub_type = stub_type;
   12266 		      continue;
   12267 		    }
   12268 
   12269 		  stub_entry = ppc_add_stub (stub_name, section, info);
   12270 		  if (stub_entry == NULL)
   12271 		    {
   12272 		      free (stub_name);
   12273 		    error_ret_free_internal:
   12274 		      if (elf_section_data (section)->relocs == NULL)
   12275 			free (internal_relocs);
   12276 		    error_ret_free_local:
   12277 		      if (local_syms != NULL
   12278 			  && (symtab_hdr->contents
   12279 			      != (unsigned char *) local_syms))
   12280 			free (local_syms);
   12281 		      return FALSE;
   12282 		    }
   12283 
   12284 		  stub_entry->stub_type = stub_type;
   12285 		  if (stub_type != ppc_stub_plt_call
   12286 		      && stub_type != ppc_stub_plt_call_r2save)
   12287 		    {
   12288 		      stub_entry->target_value = code_value;
   12289 		      stub_entry->target_section = code_sec;
   12290 		    }
   12291 		  else
   12292 		    {
   12293 		      stub_entry->target_value = sym_value;
   12294 		      stub_entry->target_section = sym_sec;
   12295 		    }
   12296 		  stub_entry->h = hash;
   12297 		  stub_entry->plt_ent = plt_ent;
   12298 		  stub_entry->other = hash ? hash->elf.other : sym->st_other;
   12299 
   12300 		  if (stub_entry->h != NULL)
   12301 		    htab->stub_globals += 1;
   12302 		}
   12303 
   12304 	      /* We're done with the internal relocs, free them.  */
   12305 	      if (elf_section_data (section)->relocs != internal_relocs)
   12306 		free (internal_relocs);
   12307 	    }
   12308 
   12309 	  if (local_syms != NULL
   12310 	      && symtab_hdr->contents != (unsigned char *) local_syms)
   12311 	    {
   12312 	      if (!info->keep_memory)
   12313 		free (local_syms);
   12314 	      else
   12315 		symtab_hdr->contents = (unsigned char *) local_syms;
   12316 	    }
   12317 	}
   12318 
   12319       /* We may have added some stubs.  Find out the new size of the
   12320 	 stub sections.  */
   12321       for (stub_sec = htab->params->stub_bfd->sections;
   12322 	   stub_sec != NULL;
   12323 	   stub_sec = stub_sec->next)
   12324 	if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
   12325 	  {
   12326 	    stub_sec->rawsize = stub_sec->size;
   12327 	    stub_sec->size = 0;
   12328 	    stub_sec->reloc_count = 0;
   12329 	    stub_sec->flags &= ~SEC_RELOC;
   12330 	  }
   12331 
   12332       htab->brlt->size = 0;
   12333       htab->brlt->reloc_count = 0;
   12334       htab->brlt->flags &= ~SEC_RELOC;
   12335       if (htab->relbrlt != NULL)
   12336 	htab->relbrlt->size = 0;
   12337 
   12338       bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
   12339 
   12340       if (info->emitrelocations
   12341 	  && htab->glink != NULL && htab->glink->size != 0)
   12342 	{
   12343 	  htab->glink->reloc_count = 1;
   12344 	  htab->glink->flags |= SEC_RELOC;
   12345 	}
   12346 
   12347       if (htab->glink_eh_frame != NULL
   12348 	  && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
   12349 	  && htab->glink_eh_frame->output_section->size != 0)
   12350 	{
   12351 	  size_t size = 0, align;
   12352 
   12353 	  for (stub_sec = htab->params->stub_bfd->sections;
   12354 	       stub_sec != NULL;
   12355 	       stub_sec = stub_sec->next)
   12356 	    if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
   12357 	      size += 24;
   12358 	  if (htab->glink != NULL && htab->glink->size != 0)
   12359 	    size += 24;
   12360 	  if (size != 0)
   12361 	    size += sizeof (glink_eh_frame_cie);
   12362 	  align = 1;
   12363 	  align <<= htab->glink_eh_frame->output_section->alignment_power;
   12364 	  align -= 1;
   12365 	  size = (size + align) & ~align;
   12366 	  htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
   12367 	  htab->glink_eh_frame->size = size;
   12368 	}
   12369 
   12370       if (htab->params->plt_stub_align != 0)
   12371 	for (stub_sec = htab->params->stub_bfd->sections;
   12372 	     stub_sec != NULL;
   12373 	     stub_sec = stub_sec->next)
   12374 	  if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
   12375 	    stub_sec->size = ((stub_sec->size
   12376 			       + (1 << htab->params->plt_stub_align) - 1)
   12377 			      & (-1 << htab->params->plt_stub_align));
   12378 
   12379       for (stub_sec = htab->params->stub_bfd->sections;
   12380 	   stub_sec != NULL;
   12381 	   stub_sec = stub_sec->next)
   12382 	if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
   12383 	    && stub_sec->rawsize != stub_sec->size)
   12384 	  break;
   12385 
   12386       /* Exit from this loop when no stubs have been added, and no stubs
   12387 	 have changed size.  */
   12388       if (stub_sec == NULL
   12389 	  && (htab->glink_eh_frame == NULL
   12390 	      || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
   12391 	break;
   12392 
   12393       /* Ask the linker to do its stuff.  */
   12394       (*htab->params->layout_sections_again) ();
   12395     }
   12396 
   12397   if (htab->glink_eh_frame != NULL
   12398       && htab->glink_eh_frame->size != 0)
   12399     {
   12400       bfd_vma val;
   12401       bfd_byte *p, *last_fde;
   12402       size_t last_fde_len, size, align, pad;
   12403       asection *stub_sec;
   12404 
   12405       p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
   12406       if (p == NULL)
   12407 	return FALSE;
   12408       htab->glink_eh_frame->contents = p;
   12409       last_fde = p;
   12410 
   12411       memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
   12412       /* CIE length (rewrite in case little-endian).  */
   12413       last_fde_len = sizeof (glink_eh_frame_cie) - 4;
   12414       bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
   12415       p += sizeof (glink_eh_frame_cie);
   12416 
   12417       for (stub_sec = htab->params->stub_bfd->sections;
   12418 	   stub_sec != NULL;
   12419 	   stub_sec = stub_sec->next)
   12420 	if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
   12421 	  {
   12422 	    last_fde = p;
   12423 	    last_fde_len = 20;
   12424 	    /* FDE length.  */
   12425 	    bfd_put_32 (htab->elf.dynobj, 20, p);
   12426 	    p += 4;
   12427 	    /* CIE pointer.  */
   12428 	    val = p - htab->glink_eh_frame->contents;
   12429 	    bfd_put_32 (htab->elf.dynobj, val, p);
   12430 	    p += 4;
   12431 	    /* Offset to stub section, written later.  */
   12432 	    p += 4;
   12433 	    /* stub section size.  */
   12434 	    bfd_put_32 (htab->elf.dynobj, stub_sec->size, p);
   12435 	    p += 4;
   12436 	    /* Augmentation.  */
   12437 	    p += 1;
   12438 	    /* Pad.  */
   12439 	    p += 7;
   12440 	  }
   12441       if (htab->glink != NULL && htab->glink->size != 0)
   12442 	{
   12443 	  last_fde = p;
   12444 	  last_fde_len = 20;
   12445 	  /* FDE length.  */
   12446 	  bfd_put_32 (htab->elf.dynobj, 20, p);
   12447 	  p += 4;
   12448 	  /* CIE pointer.  */
   12449 	  val = p - htab->glink_eh_frame->contents;
   12450 	  bfd_put_32 (htab->elf.dynobj, val, p);
   12451 	  p += 4;
   12452 	  /* Offset to .glink, written later.  */
   12453 	  p += 4;
   12454 	  /* .glink size.  */
   12455 	  bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
   12456 	  p += 4;
   12457 	  /* Augmentation.  */
   12458 	  p += 1;
   12459 
   12460 	  *p++ = DW_CFA_advance_loc + 1;
   12461 	  *p++ = DW_CFA_register;
   12462 	  *p++ = 65;
   12463 	  *p++ = 12;
   12464 	  *p++ = DW_CFA_advance_loc + 4;
   12465 	  *p++ = DW_CFA_restore_extended;
   12466 	  *p++ = 65;
   12467 	}
   12468       /* Subsume any padding into the last FDE if user .eh_frame
   12469 	 sections are aligned more than glink_eh_frame.  Otherwise any
   12470 	 zero padding will be seen as a terminator.  */
   12471       size = p - htab->glink_eh_frame->contents;
   12472       align = 1;
   12473       align <<= htab->glink_eh_frame->output_section->alignment_power;
   12474       align -= 1;
   12475       pad = ((size + align) & ~align) - size;
   12476       htab->glink_eh_frame->size = size + pad;
   12477       bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
   12478     }
   12479 
   12480   maybe_strip_output (info, htab->brlt);
   12481   if (htab->glink_eh_frame != NULL)
   12482     maybe_strip_output (info, htab->glink_eh_frame);
   12483 
   12484   return TRUE;
   12485 }
   12486 
   12487 /* Called after we have determined section placement.  If sections
   12488    move, we'll be called again.  Provide a value for TOCstart.  */
   12489 
   12490 bfd_vma
   12491 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
   12492 {
   12493   asection *s;
   12494   bfd_vma TOCstart;
   12495 
   12496   /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
   12497      order.  The TOC starts where the first of these sections starts.  */
   12498   s = bfd_get_section_by_name (obfd, ".got");
   12499   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
   12500     s = bfd_get_section_by_name (obfd, ".toc");
   12501   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
   12502     s = bfd_get_section_by_name (obfd, ".tocbss");
   12503   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
   12504     s = bfd_get_section_by_name (obfd, ".plt");
   12505   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
   12506     {
   12507       /* This may happen for
   12508 	 o  references to TOC base (SYM@toc / TOC[tc0]) without a
   12509 	 .toc directive
   12510 	 o  bad linker script
   12511 	 o --gc-sections and empty TOC sections
   12512 
   12513 	 FIXME: Warn user?  */
   12514 
   12515       /* Look for a likely section.  We probably won't even be
   12516 	 using TOCstart.  */
   12517       for (s = obfd->sections; s != NULL; s = s->next)
   12518 	if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
   12519 			 | SEC_EXCLUDE))
   12520 	    == (SEC_ALLOC | SEC_SMALL_DATA))
   12521 	  break;
   12522       if (s == NULL)
   12523 	for (s = obfd->sections; s != NULL; s = s->next)
   12524 	  if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
   12525 	      == (SEC_ALLOC | SEC_SMALL_DATA))
   12526 	    break;
   12527       if (s == NULL)
   12528 	for (s = obfd->sections; s != NULL; s = s->next)
   12529 	  if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
   12530 	      == SEC_ALLOC)
   12531 	    break;
   12532       if (s == NULL)
   12533 	for (s = obfd->sections; s != NULL; s = s->next)
   12534 	  if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
   12535 	    break;
   12536     }
   12537 
   12538   TOCstart = 0;
   12539   if (s != NULL)
   12540     TOCstart = s->output_section->vma + s->output_offset;
   12541 
   12542   _bfd_set_gp_value (obfd, TOCstart);
   12543 
   12544   if (info != NULL && s != NULL)
   12545     {
   12546       struct ppc_link_hash_table *htab = ppc_hash_table (info);
   12547 
   12548       if (htab != NULL)
   12549 	{
   12550 	  if (htab->elf.hgot != NULL)
   12551 	    {
   12552 	      htab->elf.hgot->root.u.def.value = TOC_BASE_OFF;
   12553 	      htab->elf.hgot->root.u.def.section = s;
   12554 	    }
   12555 	}
   12556       else
   12557 	{
   12558 	  struct bfd_link_hash_entry *bh = NULL;
   12559 	  _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
   12560 					    s, TOC_BASE_OFF, NULL, FALSE,
   12561 					    FALSE, &bh);
   12562 	}
   12563     }
   12564   return TOCstart;
   12565 }
   12566 
   12567 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
   12568    write out any global entry stubs.  */
   12569 
   12570 static bfd_boolean
   12571 build_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
   12572 {
   12573   struct bfd_link_info *info;
   12574   struct ppc_link_hash_table *htab;
   12575   struct plt_entry *pent;
   12576   asection *s;
   12577 
   12578   if (h->root.type == bfd_link_hash_indirect)
   12579     return TRUE;
   12580 
   12581   if (!h->pointer_equality_needed)
   12582     return TRUE;
   12583 
   12584   if (h->def_regular)
   12585     return TRUE;
   12586 
   12587   info = inf;
   12588   htab = ppc_hash_table (info);
   12589   if (htab == NULL)
   12590     return FALSE;
   12591 
   12592   s = htab->glink;
   12593   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
   12594     if (pent->plt.offset != (bfd_vma) -1
   12595 	&& pent->addend == 0)
   12596       {
   12597 	bfd_byte *p;
   12598 	asection *plt;
   12599 	bfd_vma off;
   12600 
   12601 	p = s->contents + h->root.u.def.value;
   12602 	plt = htab->elf.splt;
   12603 	if (!htab->elf.dynamic_sections_created
   12604 	    || h->dynindx == -1)
   12605 	  plt = htab->elf.iplt;
   12606 	off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
   12607 	off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
   12608 
   12609 	if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
   12610 	  {
   12611 	    info->callbacks->einfo
   12612 	      (_("%P: linkage table error against `%T'\n"),
   12613 	       h->root.root.string);
   12614 	    bfd_set_error (bfd_error_bad_value);
   12615 	    htab->stub_error = TRUE;
   12616 	  }
   12617 
   12618 	htab->stub_count[ppc_stub_global_entry - 1] += 1;
   12619 	if (htab->params->emit_stub_syms)
   12620 	  {
   12621 	    size_t len = strlen (h->root.root.string);
   12622 	    char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
   12623 
   12624 	    if (name == NULL)
   12625 	      return FALSE;
   12626 
   12627 	    sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
   12628 	    h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
   12629 	    if (h == NULL)
   12630 	      return FALSE;
   12631 	    if (h->root.type == bfd_link_hash_new)
   12632 	      {
   12633 		h->root.type = bfd_link_hash_defined;
   12634 		h->root.u.def.section = s;
   12635 		h->root.u.def.value = p - s->contents;
   12636 		h->ref_regular = 1;
   12637 		h->def_regular = 1;
   12638 		h->ref_regular_nonweak = 1;
   12639 		h->forced_local = 1;
   12640 		h->non_elf = 0;
   12641 	      }
   12642 	  }
   12643 
   12644 	if (PPC_HA (off) != 0)
   12645 	  {
   12646 	    bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
   12647 	    p += 4;
   12648 	  }
   12649 	bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
   12650 	p += 4;
   12651 	bfd_put_32 (s->owner, MTCTR_R12, p);
   12652 	p += 4;
   12653 	bfd_put_32 (s->owner, BCTR, p);
   12654 	break;
   12655       }
   12656   return TRUE;
   12657 }
   12658 
   12659 /* Build all the stubs associated with the current output file.
   12660    The stubs are kept in a hash table attached to the main linker
   12661    hash table.  This function is called via gldelf64ppc_finish.  */
   12662 
   12663 bfd_boolean
   12664 ppc64_elf_build_stubs (struct bfd_link_info *info,
   12665 		       char **stats)
   12666 {
   12667   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   12668   asection *stub_sec;
   12669   bfd_byte *p;
   12670   int stub_sec_count = 0;
   12671 
   12672   if (htab == NULL)
   12673     return FALSE;
   12674 
   12675   /* Allocate memory to hold the linker stubs.  */
   12676   for (stub_sec = htab->params->stub_bfd->sections;
   12677        stub_sec != NULL;
   12678        stub_sec = stub_sec->next)
   12679     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
   12680 	&& stub_sec->size != 0)
   12681       {
   12682 	stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
   12683 	if (stub_sec->contents == NULL)
   12684 	  return FALSE;
   12685 	/* We want to check that built size is the same as calculated
   12686 	   size.  rawsize is a convenient location to use.  */
   12687 	stub_sec->rawsize = stub_sec->size;
   12688 	stub_sec->size = 0;
   12689       }
   12690 
   12691   if (htab->glink != NULL && htab->glink->size != 0)
   12692     {
   12693       unsigned int indx;
   12694       bfd_vma plt0;
   12695 
   12696       /* Build the .glink plt call stub.  */
   12697       if (htab->params->emit_stub_syms)
   12698 	{
   12699 	  struct elf_link_hash_entry *h;
   12700 	  h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
   12701 				    TRUE, FALSE, FALSE);
   12702 	  if (h == NULL)
   12703 	    return FALSE;
   12704 	  if (h->root.type == bfd_link_hash_new)
   12705 	    {
   12706 	      h->root.type = bfd_link_hash_defined;
   12707 	      h->root.u.def.section = htab->glink;
   12708 	      h->root.u.def.value = 8;
   12709 	      h->ref_regular = 1;
   12710 	      h->def_regular = 1;
   12711 	      h->ref_regular_nonweak = 1;
   12712 	      h->forced_local = 1;
   12713 	      h->non_elf = 0;
   12714 	    }
   12715 	}
   12716       plt0 = (htab->elf.splt->output_section->vma
   12717 	      + htab->elf.splt->output_offset
   12718 	      - 16);
   12719       if (info->emitrelocations)
   12720 	{
   12721 	  Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
   12722 	  if (r == NULL)
   12723 	    return FALSE;
   12724 	  r->r_offset = (htab->glink->output_offset
   12725 			 + htab->glink->output_section->vma);
   12726 	  r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
   12727 	  r->r_addend = plt0;
   12728 	}
   12729       p = htab->glink->contents;
   12730       plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
   12731       bfd_put_64 (htab->glink->owner, plt0, p);
   12732       p += 8;
   12733       if (htab->opd_abi)
   12734 	{
   12735 	  bfd_put_32 (htab->glink->owner, MFLR_R12, p);
   12736 	  p += 4;
   12737 	  bfd_put_32 (htab->glink->owner, BCL_20_31, p);
   12738 	  p += 4;
   12739 	  bfd_put_32 (htab->glink->owner, MFLR_R11, p);
   12740 	  p += 4;
   12741 	  bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
   12742 	  p += 4;
   12743 	  bfd_put_32 (htab->glink->owner, MTLR_R12, p);
   12744 	  p += 4;
   12745 	  bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
   12746 	  p += 4;
   12747 	  bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
   12748 	  p += 4;
   12749 	  bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
   12750 	  p += 4;
   12751 	  bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
   12752 	  p += 4;
   12753 	  bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
   12754 	  p += 4;
   12755 	}
   12756       else
   12757 	{
   12758 	  bfd_put_32 (htab->glink->owner, MFLR_R0, p);
   12759 	  p += 4;
   12760 	  bfd_put_32 (htab->glink->owner, BCL_20_31, p);
   12761 	  p += 4;
   12762 	  bfd_put_32 (htab->glink->owner, MFLR_R11, p);
   12763 	  p += 4;
   12764 	  bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
   12765 	  p += 4;
   12766 	  bfd_put_32 (htab->glink->owner, MTLR_R0, p);
   12767 	  p += 4;
   12768 	  bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
   12769 	  p += 4;
   12770 	  bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
   12771 	  p += 4;
   12772 	  bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
   12773 	  p += 4;
   12774 	  bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
   12775 	  p += 4;
   12776 	  bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
   12777 	  p += 4;
   12778 	  bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
   12779 	  p += 4;
   12780 	  bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
   12781 	  p += 4;
   12782 	}
   12783       bfd_put_32 (htab->glink->owner, BCTR, p);
   12784       p += 4;
   12785       while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
   12786 	{
   12787 	  bfd_put_32 (htab->glink->owner, NOP, p);
   12788 	  p += 4;
   12789 	}
   12790 
   12791       /* Build the .glink lazy link call stubs.  */
   12792       indx = 0;
   12793       while (p < htab->glink->contents + htab->glink->rawsize)
   12794 	{
   12795 	  if (htab->opd_abi)
   12796 	    {
   12797 	      if (indx < 0x8000)
   12798 		{
   12799 		  bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
   12800 		  p += 4;
   12801 		}
   12802 	      else
   12803 		{
   12804 		  bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
   12805 		  p += 4;
   12806 		  bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
   12807 			      p);
   12808 		  p += 4;
   12809 		}
   12810 	    }
   12811 	  bfd_put_32 (htab->glink->owner,
   12812 		      B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
   12813 	  indx++;
   12814 	  p += 4;
   12815 	}
   12816 
   12817       /* Build .glink global entry stubs.  */
   12818       if (htab->glink->size > htab->glink->rawsize)
   12819 	elf_link_hash_traverse (&htab->elf, build_global_entry_stubs, info);
   12820     }
   12821 
   12822   if (htab->brlt != NULL && htab->brlt->size != 0)
   12823     {
   12824       htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
   12825 					 htab->brlt->size);
   12826       if (htab->brlt->contents == NULL)
   12827 	return FALSE;
   12828     }
   12829   if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
   12830     {
   12831       htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
   12832 					    htab->relbrlt->size);
   12833       if (htab->relbrlt->contents == NULL)
   12834 	return FALSE;
   12835     }
   12836 
   12837   /* Build the stubs as directed by the stub hash table.  */
   12838   bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
   12839 
   12840   if (htab->relbrlt != NULL)
   12841     htab->relbrlt->reloc_count = 0;
   12842 
   12843   if (htab->params->plt_stub_align != 0)
   12844     for (stub_sec = htab->params->stub_bfd->sections;
   12845 	 stub_sec != NULL;
   12846 	 stub_sec = stub_sec->next)
   12847       if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
   12848 	stub_sec->size = ((stub_sec->size
   12849 			   + (1 << htab->params->plt_stub_align) - 1)
   12850 			  & (-1 << htab->params->plt_stub_align));
   12851 
   12852   for (stub_sec = htab->params->stub_bfd->sections;
   12853        stub_sec != NULL;
   12854        stub_sec = stub_sec->next)
   12855     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
   12856       {
   12857 	stub_sec_count += 1;
   12858 	if (stub_sec->rawsize != stub_sec->size)
   12859 	  break;
   12860       }
   12861 
   12862   /* Note that the glink_eh_frame check here is not only testing that
   12863      the generated size matched the calculated size but also that
   12864      bfd_elf_discard_info didn't make any changes to the section.  */
   12865   if (stub_sec != NULL
   12866       || (htab->glink_eh_frame != NULL
   12867 	  && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
   12868     {
   12869       htab->stub_error = TRUE;
   12870       info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
   12871     }
   12872 
   12873   if (htab->stub_error)
   12874     return FALSE;
   12875 
   12876   if (stats != NULL)
   12877     {
   12878       *stats = bfd_malloc (500);
   12879       if (*stats == NULL)
   12880 	return FALSE;
   12881 
   12882       sprintf (*stats, _("linker stubs in %u group%s\n"
   12883 			 "  branch       %lu\n"
   12884 			 "  toc adjust   %lu\n"
   12885 			 "  long branch  %lu\n"
   12886 			 "  long toc adj %lu\n"
   12887 			 "  plt call     %lu\n"
   12888 			 "  plt call toc %lu\n"
   12889 			 "  global entry %lu"),
   12890 	       stub_sec_count,
   12891 	       stub_sec_count == 1 ? "" : "s",
   12892 	       htab->stub_count[ppc_stub_long_branch - 1],
   12893 	       htab->stub_count[ppc_stub_long_branch_r2off - 1],
   12894 	       htab->stub_count[ppc_stub_plt_branch - 1],
   12895 	       htab->stub_count[ppc_stub_plt_branch_r2off - 1],
   12896 	       htab->stub_count[ppc_stub_plt_call - 1],
   12897 	       htab->stub_count[ppc_stub_plt_call_r2save - 1],
   12898 	       htab->stub_count[ppc_stub_global_entry - 1]);
   12899     }
   12900   return TRUE;
   12901 }
   12902 
   12903 /* This function undoes the changes made by add_symbol_adjust.  */
   12904 
   12905 static bfd_boolean
   12906 undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
   12907 {
   12908   struct ppc_link_hash_entry *eh;
   12909 
   12910   if (h->root.type == bfd_link_hash_indirect)
   12911     return TRUE;
   12912 
   12913   eh = (struct ppc_link_hash_entry *) h;
   12914   if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
   12915     return TRUE;
   12916 
   12917   eh->elf.root.type = bfd_link_hash_undefined;
   12918   return TRUE;
   12919 }
   12920 
   12921 void
   12922 ppc64_elf_restore_symbols (struct bfd_link_info *info)
   12923 {
   12924   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   12925 
   12926   if (htab != NULL)
   12927     elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
   12928 }
   12929 
   12930 /* What to do when ld finds relocations against symbols defined in
   12931    discarded sections.  */
   12932 
   12933 static unsigned int
   12934 ppc64_elf_action_discarded (asection *sec)
   12935 {
   12936   if (strcmp (".opd", sec->name) == 0)
   12937     return 0;
   12938 
   12939   if (strcmp (".toc", sec->name) == 0)
   12940     return 0;
   12941 
   12942   if (strcmp (".toc1", sec->name) == 0)
   12943     return 0;
   12944 
   12945   return _bfd_elf_default_action_discarded (sec);
   12946 }
   12947 
   12948 /* The RELOCATE_SECTION function is called by the ELF backend linker
   12949    to handle the relocations for a section.
   12950 
   12951    The relocs are always passed as Rela structures; if the section
   12952    actually uses Rel structures, the r_addend field will always be
   12953    zero.
   12954 
   12955    This function is responsible for adjust the section contents as
   12956    necessary, and (if using Rela relocs and generating a
   12957    relocatable output file) adjusting the reloc addend as
   12958    necessary.
   12959 
   12960    This function does not have to worry about setting the reloc
   12961    address or the reloc symbol index.
   12962 
   12963    LOCAL_SYMS is a pointer to the swapped in local symbols.
   12964 
   12965    LOCAL_SECTIONS is an array giving the section in the input file
   12966    corresponding to the st_shndx field of each local symbol.
   12967 
   12968    The global hash table entry for the global symbols can be found
   12969    via elf_sym_hashes (input_bfd).
   12970 
   12971    When generating relocatable output, this function must handle
   12972    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
   12973    going to be the section symbol corresponding to the output
   12974    section, which means that the addend must be adjusted
   12975    accordingly.  */
   12976 
   12977 static bfd_boolean
   12978 ppc64_elf_relocate_section (bfd *output_bfd,
   12979 			    struct bfd_link_info *info,
   12980 			    bfd *input_bfd,
   12981 			    asection *input_section,
   12982 			    bfd_byte *contents,
   12983 			    Elf_Internal_Rela *relocs,
   12984 			    Elf_Internal_Sym *local_syms,
   12985 			    asection **local_sections)
   12986 {
   12987   struct ppc_link_hash_table *htab;
   12988   Elf_Internal_Shdr *symtab_hdr;
   12989   struct elf_link_hash_entry **sym_hashes;
   12990   Elf_Internal_Rela *rel;
   12991   Elf_Internal_Rela *relend;
   12992   Elf_Internal_Rela outrel;
   12993   bfd_byte *loc;
   12994   struct got_entry **local_got_ents;
   12995   bfd_vma TOCstart;
   12996   bfd_boolean ret = TRUE;
   12997   bfd_boolean is_opd;
   12998   /* Assume 'at' branch hints.  */
   12999   bfd_boolean is_isa_v2 = TRUE;
   13000   bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
   13001 
   13002   /* Initialize howto table if needed.  */
   13003   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
   13004     ppc_howto_init ();
   13005 
   13006   htab = ppc_hash_table (info);
   13007   if (htab == NULL)
   13008     return FALSE;
   13009 
   13010   /* Don't relocate stub sections.  */
   13011   if (input_section->owner == htab->params->stub_bfd)
   13012     return TRUE;
   13013 
   13014   BFD_ASSERT (is_ppc64_elf (input_bfd));
   13015 
   13016   local_got_ents = elf_local_got_ents (input_bfd);
   13017   TOCstart = elf_gp (output_bfd);
   13018   symtab_hdr = &elf_symtab_hdr (input_bfd);
   13019   sym_hashes = elf_sym_hashes (input_bfd);
   13020   is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
   13021 
   13022   rel = relocs;
   13023   relend = relocs + input_section->reloc_count;
   13024   for (; rel < relend; rel++)
   13025     {
   13026       enum elf_ppc64_reloc_type r_type;
   13027       bfd_vma addend;
   13028       bfd_reloc_status_type r;
   13029       Elf_Internal_Sym *sym;
   13030       asection *sec;
   13031       struct elf_link_hash_entry *h_elf;
   13032       struct ppc_link_hash_entry *h;
   13033       struct ppc_link_hash_entry *fdh;
   13034       const char *sym_name;
   13035       unsigned long r_symndx, toc_symndx;
   13036       bfd_vma toc_addend;
   13037       unsigned char tls_mask, tls_gd, tls_type;
   13038       unsigned char sym_type;
   13039       bfd_vma relocation;
   13040       bfd_boolean unresolved_reloc;
   13041       bfd_boolean warned;
   13042       enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
   13043       unsigned int insn;
   13044       unsigned int mask;
   13045       struct ppc_stub_hash_entry *stub_entry;
   13046       bfd_vma max_br_offset;
   13047       bfd_vma from;
   13048       const Elf_Internal_Rela orig_rel = *rel;
   13049       reloc_howto_type *howto;
   13050       struct reloc_howto_struct alt_howto;
   13051 
   13052       r_type = ELF64_R_TYPE (rel->r_info);
   13053       r_symndx = ELF64_R_SYM (rel->r_info);
   13054 
   13055       /* For old style R_PPC64_TOC relocs with a zero symbol, use the
   13056 	 symbol of the previous ADDR64 reloc.  The symbol gives us the
   13057 	 proper TOC base to use.  */
   13058       if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
   13059 	  && rel != relocs
   13060 	  && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
   13061 	  && is_opd)
   13062 	r_symndx = ELF64_R_SYM (rel[-1].r_info);
   13063 
   13064       sym = NULL;
   13065       sec = NULL;
   13066       h_elf = NULL;
   13067       sym_name = NULL;
   13068       unresolved_reloc = FALSE;
   13069       warned = FALSE;
   13070 
   13071       if (r_symndx < symtab_hdr->sh_info)
   13072 	{
   13073 	  /* It's a local symbol.  */
   13074 	  struct _opd_sec_data *opd;
   13075 
   13076 	  sym = local_syms + r_symndx;
   13077 	  sec = local_sections[r_symndx];
   13078 	  sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
   13079 	  sym_type = ELF64_ST_TYPE (sym->st_info);
   13080 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
   13081 	  opd = get_opd_info (sec);
   13082 	  if (opd != NULL && opd->adjust != NULL)
   13083 	    {
   13084 	      long adjust = opd->adjust[(sym->st_value + rel->r_addend) / 8];
   13085 	      if (adjust == -1)
   13086 		relocation = 0;
   13087 	      else
   13088 		{
   13089 		  /* If this is a relocation against the opd section sym
   13090 		     and we have edited .opd, adjust the reloc addend so
   13091 		     that ld -r and ld --emit-relocs output is correct.
   13092 		     If it is a reloc against some other .opd symbol,
   13093 		     then the symbol value will be adjusted later.  */
   13094 		  if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
   13095 		    rel->r_addend += adjust;
   13096 		  else
   13097 		    relocation += adjust;
   13098 		}
   13099 	    }
   13100 	}
   13101       else
   13102 	{
   13103 	  bfd_boolean ignored;
   13104 
   13105 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
   13106 				   r_symndx, symtab_hdr, sym_hashes,
   13107 				   h_elf, sec, relocation,
   13108 				   unresolved_reloc, warned, ignored);
   13109 	  sym_name = h_elf->root.root.string;
   13110 	  sym_type = h_elf->type;
   13111 	  if (sec != NULL
   13112 	      && sec->owner == output_bfd
   13113 	      && strcmp (sec->name, ".opd") == 0)
   13114 	    {
   13115 	      /* This is a symbol defined in a linker script.  All
   13116 		 such are defined in output sections, even those
   13117 		 defined by simple assignment from a symbol defined in
   13118 		 an input section.  Transfer the symbol to an
   13119 		 appropriate input .opd section, so that a branch to
   13120 		 this symbol will be mapped to the location specified
   13121 		 by the opd entry.  */
   13122 	      struct bfd_link_order *lo;
   13123 	      for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
   13124 		if (lo->type == bfd_indirect_link_order)
   13125 		  {
   13126 		    asection *isec = lo->u.indirect.section;
   13127 		    if (h_elf->root.u.def.value >= isec->output_offset
   13128 			&& h_elf->root.u.def.value < (isec->output_offset
   13129 						      + isec->size))
   13130 		      {
   13131 			h_elf->root.u.def.value -= isec->output_offset;
   13132 			h_elf->root.u.def.section = isec;
   13133 			sec = isec;
   13134 			break;
   13135 		      }
   13136 		  }
   13137 	    }
   13138 	}
   13139       h = (struct ppc_link_hash_entry *) h_elf;
   13140 
   13141       if (sec != NULL && discarded_section (sec))
   13142 	RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
   13143 					 rel, 1, relend,
   13144 					 ppc64_elf_howto_table[r_type], 0,
   13145 					 contents);
   13146 
   13147       if (info->relocatable)
   13148 	continue;
   13149 
   13150       if (h != NULL && &h->elf == htab->elf.hgot)
   13151 	{
   13152 	  relocation = (TOCstart
   13153 			+ htab->stub_group[input_section->id].toc_off);
   13154 	  sec = bfd_abs_section_ptr;
   13155 	  unresolved_reloc = FALSE;
   13156 	}
   13157 
   13158       /* TLS optimizations.  Replace instruction sequences and relocs
   13159 	 based on information we collected in tls_optimize.  We edit
   13160 	 RELOCS so that --emit-relocs will output something sensible
   13161 	 for the final instruction stream.  */
   13162       tls_mask = 0;
   13163       tls_gd = 0;
   13164       toc_symndx = 0;
   13165       if (h != NULL)
   13166 	tls_mask = h->tls_mask;
   13167       else if (local_got_ents != NULL)
   13168 	{
   13169 	  struct plt_entry **local_plt = (struct plt_entry **)
   13170 	    (local_got_ents + symtab_hdr->sh_info);
   13171 	  unsigned char *lgot_masks = (unsigned char *)
   13172 	    (local_plt + symtab_hdr->sh_info);
   13173 	  tls_mask = lgot_masks[r_symndx];
   13174 	}
   13175       if (tls_mask == 0
   13176 	  && (r_type == R_PPC64_TLS
   13177 	      || r_type == R_PPC64_TLSGD
   13178 	      || r_type == R_PPC64_TLSLD))
   13179 	{
   13180 	  /* Check for toc tls entries.  */
   13181 	  unsigned char *toc_tls;
   13182 
   13183 	  if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
   13184 			     &local_syms, rel, input_bfd))
   13185 	    return FALSE;
   13186 
   13187 	  if (toc_tls)
   13188 	    tls_mask = *toc_tls;
   13189 	}
   13190 
   13191       /* Check that tls relocs are used with tls syms, and non-tls
   13192 	 relocs are used with non-tls syms.  */
   13193       if (r_symndx != STN_UNDEF
   13194 	  && r_type != R_PPC64_NONE
   13195 	  && (h == NULL
   13196 	      || h->elf.root.type == bfd_link_hash_defined
   13197 	      || h->elf.root.type == bfd_link_hash_defweak)
   13198 	  && (IS_PPC64_TLS_RELOC (r_type)
   13199 	      != (sym_type == STT_TLS
   13200 		  || (sym_type == STT_SECTION
   13201 		      && (sec->flags & SEC_THREAD_LOCAL) != 0))))
   13202 	{
   13203 	  if (tls_mask != 0
   13204 	      && (r_type == R_PPC64_TLS
   13205 		  || r_type == R_PPC64_TLSGD
   13206 		  || r_type == R_PPC64_TLSLD))
   13207 	    /* R_PPC64_TLS is OK against a symbol in the TOC.  */
   13208 	    ;
   13209 	  else
   13210 	    info->callbacks->einfo
   13211 	      (!IS_PPC64_TLS_RELOC (r_type)
   13212 	       ? _("%P: %H: %s used with TLS symbol `%T'\n")
   13213 	       : _("%P: %H: %s used with non-TLS symbol `%T'\n"),
   13214 	       input_bfd, input_section, rel->r_offset,
   13215 	       ppc64_elf_howto_table[r_type]->name,
   13216 	       sym_name);
   13217 	}
   13218 
   13219       /* Ensure reloc mapping code below stays sane.  */
   13220       if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
   13221 	  || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
   13222 	  || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TLSGD16 & 3)
   13223 	  || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
   13224 	  || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
   13225 	  || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
   13226 	  || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TPREL16_DS & 3)
   13227 	  || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
   13228 	  || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
   13229 	  || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
   13230 	abort ();
   13231 
   13232       switch (r_type)
   13233 	{
   13234 	default:
   13235 	  break;
   13236 
   13237 	case R_PPC64_LO_DS_OPT:
   13238 	  insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
   13239 	  if ((insn & (0x3f << 26)) != 58u << 26)
   13240 	    abort ();
   13241 	  insn += (14u << 26) - (58u << 26);
   13242 	  bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
   13243 	  r_type = R_PPC64_TOC16_LO;
   13244 	  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   13245 	  break;
   13246 
   13247 	case R_PPC64_TOC16:
   13248 	case R_PPC64_TOC16_LO:
   13249 	case R_PPC64_TOC16_DS:
   13250 	case R_PPC64_TOC16_LO_DS:
   13251 	  {
   13252 	    /* Check for toc tls entries.  */
   13253 	    unsigned char *toc_tls;
   13254 	    int retval;
   13255 
   13256 	    retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
   13257 				   &local_syms, rel, input_bfd);
   13258 	    if (retval == 0)
   13259 	      return FALSE;
   13260 
   13261 	    if (toc_tls)
   13262 	      {
   13263 		tls_mask = *toc_tls;
   13264 		if (r_type == R_PPC64_TOC16_DS
   13265 		    || r_type == R_PPC64_TOC16_LO_DS)
   13266 		  {
   13267 		    if (tls_mask != 0
   13268 			&& (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
   13269 		      goto toctprel;
   13270 		  }
   13271 		else
   13272 		  {
   13273 		    /* If we found a GD reloc pair, then we might be
   13274 		       doing a GD->IE transition.  */
   13275 		    if (retval == 2)
   13276 		      {
   13277 			tls_gd = TLS_TPRELGD;
   13278 			if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
   13279 			  goto tls_ldgd_opt;
   13280 		      }
   13281 		    else if (retval == 3)
   13282 		      {
   13283 			if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
   13284 			  goto tls_ldgd_opt;
   13285 		      }
   13286 		  }
   13287 	      }
   13288 	  }
   13289 	  break;
   13290 
   13291 	case R_PPC64_GOT_TPREL16_HI:
   13292 	case R_PPC64_GOT_TPREL16_HA:
   13293 	  if (tls_mask != 0
   13294 	      && (tls_mask & TLS_TPREL) == 0)
   13295 	    {
   13296 	      rel->r_offset -= d_offset;
   13297 	      bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
   13298 	      r_type = R_PPC64_NONE;
   13299 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   13300 	    }
   13301 	  break;
   13302 
   13303 	case R_PPC64_GOT_TPREL16_DS:
   13304 	case R_PPC64_GOT_TPREL16_LO_DS:
   13305 	  if (tls_mask != 0
   13306 	      && (tls_mask & TLS_TPREL) == 0)
   13307 	    {
   13308 	    toctprel:
   13309 	      insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
   13310 	      insn &= 31 << 21;
   13311 	      insn |= 0x3c0d0000;	/* addis 0,13,0 */
   13312 	      bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
   13313 	      r_type = R_PPC64_TPREL16_HA;
   13314 	      if (toc_symndx != 0)
   13315 		{
   13316 		  rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
   13317 		  rel->r_addend = toc_addend;
   13318 		  /* We changed the symbol.  Start over in order to
   13319 		     get h, sym, sec etc. right.  */
   13320 		  rel--;
   13321 		  continue;
   13322 		}
   13323 	      else
   13324 		rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   13325 	    }
   13326 	  break;
   13327 
   13328 	case R_PPC64_TLS:
   13329 	  if (tls_mask != 0
   13330 	      && (tls_mask & TLS_TPREL) == 0)
   13331 	    {
   13332 	      insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
   13333 	      insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
   13334 	      if (insn == 0)
   13335 		abort ();
   13336 	      bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
   13337 	      /* Was PPC64_TLS which sits on insn boundary, now
   13338 		 PPC64_TPREL16_LO which is at low-order half-word.  */
   13339 	      rel->r_offset += d_offset;
   13340 	      r_type = R_PPC64_TPREL16_LO;
   13341 	      if (toc_symndx != 0)
   13342 		{
   13343 		  rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
   13344 		  rel->r_addend = toc_addend;
   13345 		  /* We changed the symbol.  Start over in order to
   13346 		     get h, sym, sec etc. right.  */
   13347 		  rel--;
   13348 		  continue;
   13349 		}
   13350 	      else
   13351 		rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   13352 	    }
   13353 	  break;
   13354 
   13355 	case R_PPC64_GOT_TLSGD16_HI:
   13356 	case R_PPC64_GOT_TLSGD16_HA:
   13357 	  tls_gd = TLS_TPRELGD;
   13358 	  if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
   13359 	    goto tls_gdld_hi;
   13360 	  break;
   13361 
   13362 	case R_PPC64_GOT_TLSLD16_HI:
   13363 	case R_PPC64_GOT_TLSLD16_HA:
   13364 	  if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
   13365 	    {
   13366 	    tls_gdld_hi:
   13367 	      if ((tls_mask & tls_gd) != 0)
   13368 		r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
   13369 			  + R_PPC64_GOT_TPREL16_DS);
   13370 	      else
   13371 		{
   13372 		  rel->r_offset -= d_offset;
   13373 		  bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
   13374 		  r_type = R_PPC64_NONE;
   13375 		}
   13376 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   13377 	    }
   13378 	  break;
   13379 
   13380 	case R_PPC64_GOT_TLSGD16:
   13381 	case R_PPC64_GOT_TLSGD16_LO:
   13382 	  tls_gd = TLS_TPRELGD;
   13383 	  if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
   13384 	    goto tls_ldgd_opt;
   13385 	  break;
   13386 
   13387 	case R_PPC64_GOT_TLSLD16:
   13388 	case R_PPC64_GOT_TLSLD16_LO:
   13389 	  if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
   13390 	    {
   13391 	      unsigned int insn1, insn2, insn3;
   13392 	      bfd_vma offset;
   13393 
   13394 	    tls_ldgd_opt:
   13395 	      offset = (bfd_vma) -1;
   13396 	      /* If not using the newer R_PPC64_TLSGD/LD to mark
   13397 		 __tls_get_addr calls, we must trust that the call
   13398 		 stays with its arg setup insns, ie. that the next
   13399 		 reloc is the __tls_get_addr call associated with
   13400 		 the current reloc.  Edit both insns.  */
   13401 	      if (input_section->has_tls_get_addr_call
   13402 		  && rel + 1 < relend
   13403 		  && branch_reloc_hash_match (input_bfd, rel + 1,
   13404 					      htab->tls_get_addr,
   13405 					      htab->tls_get_addr_fd))
   13406 		offset = rel[1].r_offset;
   13407 	      if ((tls_mask & tls_gd) != 0)
   13408 		{
   13409 		  /* IE */
   13410 		  insn1 = bfd_get_32 (output_bfd,
   13411 				      contents + rel->r_offset - d_offset);
   13412 		  insn1 &= (1 << 26) - (1 << 2);
   13413 		  insn1 |= 58 << 26;	/* ld */
   13414 		  insn2 = 0x7c636a14;	/* add 3,3,13 */
   13415 		  if (offset != (bfd_vma) -1)
   13416 		    rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
   13417 		  if ((tls_mask & TLS_EXPLICIT) == 0)
   13418 		    r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
   13419 			      + R_PPC64_GOT_TPREL16_DS);
   13420 		  else
   13421 		    r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
   13422 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   13423 		}
   13424 	      else
   13425 		{
   13426 		  /* LE */
   13427 		  insn1 = 0x3c6d0000;	/* addis 3,13,0 */
   13428 		  insn2 = 0x38630000;	/* addi 3,3,0 */
   13429 		  if (tls_gd == 0)
   13430 		    {
   13431 		      /* Was an LD reloc.  */
   13432 		      if (toc_symndx)
   13433 			sec = local_sections[toc_symndx];
   13434 		      for (r_symndx = 0;
   13435 			   r_symndx < symtab_hdr->sh_info;
   13436 			   r_symndx++)
   13437 			if (local_sections[r_symndx] == sec)
   13438 			  break;
   13439 		      if (r_symndx >= symtab_hdr->sh_info)
   13440 			r_symndx = STN_UNDEF;
   13441 		      rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
   13442 		      if (r_symndx != STN_UNDEF)
   13443 			rel->r_addend -= (local_syms[r_symndx].st_value
   13444 					  + sec->output_offset
   13445 					  + sec->output_section->vma);
   13446 		    }
   13447 		  else if (toc_symndx != 0)
   13448 		    {
   13449 		      r_symndx = toc_symndx;
   13450 		      rel->r_addend = toc_addend;
   13451 		    }
   13452 		  r_type = R_PPC64_TPREL16_HA;
   13453 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   13454 		  if (offset != (bfd_vma) -1)
   13455 		    {
   13456 		      rel[1].r_info = ELF64_R_INFO (r_symndx,
   13457 						    R_PPC64_TPREL16_LO);
   13458 		      rel[1].r_offset = offset + d_offset;
   13459 		      rel[1].r_addend = rel->r_addend;
   13460 		    }
   13461 		}
   13462 	      bfd_put_32 (output_bfd, insn1,
   13463 			  contents + rel->r_offset - d_offset);
   13464 	      if (offset != (bfd_vma) -1)
   13465 		{
   13466 		  insn3 = bfd_get_32 (output_bfd,
   13467 				      contents + offset + 4);
   13468 		  if (insn3 == NOP
   13469 		      || insn3 == CROR_151515 || insn3 == CROR_313131)
   13470 		    {
   13471 		      rel[1].r_offset += 4;
   13472 		      bfd_put_32 (output_bfd, insn2, contents + offset + 4);
   13473 		      insn2 = NOP;
   13474 		    }
   13475 		  bfd_put_32 (output_bfd, insn2, contents + offset);
   13476 		}
   13477 	      if ((tls_mask & tls_gd) == 0
   13478 		  && (tls_gd == 0 || toc_symndx != 0))
   13479 		{
   13480 		  /* We changed the symbol.  Start over in order
   13481 		     to get h, sym, sec etc. right.  */
   13482 		  rel--;
   13483 		  continue;
   13484 		}
   13485 	    }
   13486 	  break;
   13487 
   13488 	case R_PPC64_TLSGD:
   13489 	  if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
   13490 	    {
   13491 	      unsigned int insn2, insn3;
   13492 	      bfd_vma offset = rel->r_offset;
   13493 
   13494 	      if ((tls_mask & TLS_TPRELGD) != 0)
   13495 		{
   13496 		  /* IE */
   13497 		  r_type = R_PPC64_NONE;
   13498 		  insn2 = 0x7c636a14;	/* add 3,3,13 */
   13499 		}
   13500 	      else
   13501 		{
   13502 		  /* LE */
   13503 		  if (toc_symndx != 0)
   13504 		    {
   13505 		      r_symndx = toc_symndx;
   13506 		      rel->r_addend = toc_addend;
   13507 		    }
   13508 		  r_type = R_PPC64_TPREL16_LO;
   13509 		  rel->r_offset = offset + d_offset;
   13510 		  insn2 = 0x38630000;	/* addi 3,3,0 */
   13511 		}
   13512 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   13513 	      /* Zap the reloc on the _tls_get_addr call too.  */
   13514 	      BFD_ASSERT (offset == rel[1].r_offset);
   13515 	      rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
   13516 	      insn3 = bfd_get_32 (output_bfd,
   13517 				  contents + offset + 4);
   13518 	      if (insn3 == NOP
   13519 		  || insn3 == CROR_151515 || insn3 == CROR_313131)
   13520 		{
   13521 		  rel->r_offset += 4;
   13522 		  bfd_put_32 (output_bfd, insn2, contents + offset + 4);
   13523 		  insn2 = NOP;
   13524 		}
   13525 	      bfd_put_32 (output_bfd, insn2, contents + offset);
   13526 	      if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
   13527 		{
   13528 		  rel--;
   13529 		  continue;
   13530 		}
   13531 	    }
   13532 	  break;
   13533 
   13534 	case R_PPC64_TLSLD:
   13535 	  if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
   13536 	    {
   13537 	      unsigned int insn2, insn3;
   13538 	      bfd_vma offset = rel->r_offset;
   13539 
   13540 	      if (toc_symndx)
   13541 		sec = local_sections[toc_symndx];
   13542 	      for (r_symndx = 0;
   13543 		   r_symndx < symtab_hdr->sh_info;
   13544 		   r_symndx++)
   13545 		if (local_sections[r_symndx] == sec)
   13546 		  break;
   13547 	      if (r_symndx >= symtab_hdr->sh_info)
   13548 		r_symndx = STN_UNDEF;
   13549 	      rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
   13550 	      if (r_symndx != STN_UNDEF)
   13551 		rel->r_addend -= (local_syms[r_symndx].st_value
   13552 				  + sec->output_offset
   13553 				  + sec->output_section->vma);
   13554 
   13555 	      r_type = R_PPC64_TPREL16_LO;
   13556 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   13557 	      rel->r_offset = offset + d_offset;
   13558 	      /* Zap the reloc on the _tls_get_addr call too.  */
   13559 	      BFD_ASSERT (offset == rel[1].r_offset);
   13560 	      rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
   13561 	      insn2 = 0x38630000;	/* addi 3,3,0 */
   13562 	      insn3 = bfd_get_32 (output_bfd,
   13563 				  contents + offset + 4);
   13564 	      if (insn3 == NOP
   13565 		  || insn3 == CROR_151515 || insn3 == CROR_313131)
   13566 		{
   13567 		  rel->r_offset += 4;
   13568 		  bfd_put_32 (output_bfd, insn2, contents + offset + 4);
   13569 		  insn2 = NOP;
   13570 		}
   13571 	      bfd_put_32 (output_bfd, insn2, contents + offset);
   13572 	      rel--;
   13573 	      continue;
   13574 	    }
   13575 	  break;
   13576 
   13577 	case R_PPC64_DTPMOD64:
   13578 	  if (rel + 1 < relend
   13579 	      && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
   13580 	      && rel[1].r_offset == rel->r_offset + 8)
   13581 	    {
   13582 	      if ((tls_mask & TLS_GD) == 0)
   13583 		{
   13584 		  rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
   13585 		  if ((tls_mask & TLS_TPRELGD) != 0)
   13586 		    r_type = R_PPC64_TPREL64;
   13587 		  else
   13588 		    {
   13589 		      bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
   13590 		      r_type = R_PPC64_NONE;
   13591 		    }
   13592 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   13593 		}
   13594 	    }
   13595 	  else
   13596 	    {
   13597 	      if ((tls_mask & TLS_LD) == 0)
   13598 		{
   13599 		  bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
   13600 		  r_type = R_PPC64_NONE;
   13601 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   13602 		}
   13603 	    }
   13604 	  break;
   13605 
   13606 	case R_PPC64_TPREL64:
   13607 	  if ((tls_mask & TLS_TPREL) == 0)
   13608 	    {
   13609 	      r_type = R_PPC64_NONE;
   13610 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   13611 	    }
   13612 	  break;
   13613 
   13614 	case R_PPC64_REL16_HA:
   13615 	  /* If we are generating a non-PIC executable, edit
   13616 	     .	0:	addis 2,12,.TOC.-0b@ha
   13617 	     .		addi 2,2,.TOC.-0b@l
   13618 	     used by ELFv2 global entry points to set up r2, to
   13619 	     .		lis 2,.TOC.@ha
   13620 	     .		addi 2,2,.TOC.@l
   13621 	     if .TOC. is in range.  */
   13622 	  if (!info->shared
   13623 	      && !info->traditional_format
   13624 	      && h != NULL && &h->elf == htab->elf.hgot
   13625 	      && rel + 1 < relend
   13626 	      && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
   13627 	      && rel[1].r_offset == rel->r_offset + 4
   13628 	      && rel[1].r_addend == rel->r_addend + 4
   13629 	      && relocation + 0x80008000 <= 0xffffffff)
   13630 	    {
   13631 	      unsigned int insn1, insn2;
   13632 	      bfd_vma offset = rel->r_offset - d_offset;
   13633 	      insn1 = bfd_get_32 (output_bfd, contents + offset);
   13634 	      insn2 = bfd_get_32 (output_bfd, contents + offset + 4);
   13635 	      if ((insn1 & 0xffff0000) == 0x3c4c0000 /* addis 2,12 */
   13636 		  && (insn2 & 0xffff0000) == 0x38420000 /* addi 2,2 */)
   13637 		{
   13638 		  r_type = R_PPC64_ADDR16_HA;
   13639 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   13640 		  rel->r_addend -= d_offset;
   13641 		  rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
   13642 		  rel[1].r_addend -= d_offset + 4;
   13643 		  bfd_put_32 (output_bfd, 0x3c400000, contents + offset);
   13644 		}
   13645 	    }
   13646 	  break;
   13647 	}
   13648 
   13649       /* Handle other relocations that tweak non-addend part of insn.  */
   13650       insn = 0;
   13651       max_br_offset = 1 << 25;
   13652       addend = rel->r_addend;
   13653       reloc_dest = DEST_NORMAL;
   13654       switch (r_type)
   13655 	{
   13656 	default:
   13657 	  break;
   13658 
   13659 	case R_PPC64_TOCSAVE:
   13660 	  if (relocation + addend == (rel->r_offset
   13661 				      + input_section->output_offset
   13662 				      + input_section->output_section->vma)
   13663 	      && tocsave_find (htab, NO_INSERT,
   13664 			       &local_syms, rel, input_bfd))
   13665 	    {
   13666 	      insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   13667 	      if (insn == NOP
   13668 		  || insn == CROR_151515 || insn == CROR_313131)
   13669 		bfd_put_32 (input_bfd,
   13670 			    STD_R2_0R1 + STK_TOC (htab),
   13671 			    contents + rel->r_offset);
   13672 	    }
   13673 	  break;
   13674 
   13675 	  /* Branch taken prediction relocations.  */
   13676 	case R_PPC64_ADDR14_BRTAKEN:
   13677 	case R_PPC64_REL14_BRTAKEN:
   13678 	  insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
   13679 	  /* Fall thru.  */
   13680 
   13681 	  /* Branch not taken prediction relocations.  */
   13682 	case R_PPC64_ADDR14_BRNTAKEN:
   13683 	case R_PPC64_REL14_BRNTAKEN:
   13684 	  insn |= bfd_get_32 (output_bfd,
   13685 			      contents + rel->r_offset) & ~(0x01 << 21);
   13686 	  /* Fall thru.  */
   13687 
   13688 	case R_PPC64_REL14:
   13689 	  max_br_offset = 1 << 15;
   13690 	  /* Fall thru.  */
   13691 
   13692 	case R_PPC64_REL24:
   13693 	  /* Calls to functions with a different TOC, such as calls to
   13694 	     shared objects, need to alter the TOC pointer.  This is
   13695 	     done using a linkage stub.  A REL24 branching to these
   13696 	     linkage stubs needs to be followed by a nop, as the nop
   13697 	     will be replaced with an instruction to restore the TOC
   13698 	     base pointer.  */
   13699 	  fdh = h;
   13700 	  if (h != NULL
   13701 	      && h->oh != NULL
   13702 	      && h->oh->is_func_descriptor)
   13703 	    fdh = ppc_follow_link (h->oh);
   13704 	  stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
   13705 					   htab);
   13706 	  if (stub_entry != NULL
   13707 	      && (stub_entry->stub_type == ppc_stub_plt_call
   13708 		  || stub_entry->stub_type == ppc_stub_plt_call_r2save
   13709 		  || stub_entry->stub_type == ppc_stub_plt_branch_r2off
   13710 		  || stub_entry->stub_type == ppc_stub_long_branch_r2off))
   13711 	    {
   13712 	      bfd_boolean can_plt_call = FALSE;
   13713 
   13714 	      /* All of these stubs will modify r2, so there must be a
   13715 		 branch and link followed by a nop.  The nop is
   13716 		 replaced by an insn to restore r2.  */
   13717 	      if (rel->r_offset + 8 <= input_section->size)
   13718 		{
   13719 		  unsigned long br;
   13720 
   13721 		  br = bfd_get_32 (input_bfd,
   13722 				   contents + rel->r_offset);
   13723 		  if ((br & 1) != 0)
   13724 		    {
   13725 		      unsigned long nop;
   13726 
   13727 		      nop = bfd_get_32 (input_bfd,
   13728 					contents + rel->r_offset + 4);
   13729 		      if (nop == NOP
   13730 			  || nop == CROR_151515 || nop == CROR_313131)
   13731 			{
   13732 			  if (h != NULL
   13733 			      && (h == htab->tls_get_addr_fd
   13734 				  || h == htab->tls_get_addr)
   13735 			      && !htab->params->no_tls_get_addr_opt)
   13736 			    {
   13737 			      /* Special stub used, leave nop alone.  */
   13738 			    }
   13739 			  else
   13740 			    bfd_put_32 (input_bfd,
   13741 					LD_R2_0R1 + STK_TOC (htab),
   13742 					contents + rel->r_offset + 4);
   13743 			  can_plt_call = TRUE;
   13744 			}
   13745 		    }
   13746 		}
   13747 
   13748 	      if (!can_plt_call && h != NULL)
   13749 		{
   13750 		  const char *name = h->elf.root.root.string;
   13751 
   13752 		  if (*name == '.')
   13753 		    ++name;
   13754 
   13755 		  if (strncmp (name, "__libc_start_main", 17) == 0
   13756 		      && (name[17] == 0 || name[17] == '@'))
   13757 		    {
   13758 		      /* Allow crt1 branch to go via a toc adjusting
   13759 			 stub.  Other calls that never return could do
   13760 			 the same, if we could detect such.  */
   13761 		      can_plt_call = TRUE;
   13762 		    }
   13763 		}
   13764 
   13765 	      if (!can_plt_call)
   13766 		{
   13767 		  /* g++ as of 20130507 emits self-calls without a
   13768 		     following nop.  This is arguably wrong since we
   13769 		     have conflicting information.  On the one hand a
   13770 		     global symbol and on the other a local call
   13771 		     sequence, but don't error for this special case.
   13772 		     It isn't possible to cheaply verify we have
   13773 		     exactly such a call.  Allow all calls to the same
   13774 		     section.  */
   13775 		  asection *code_sec = sec;
   13776 
   13777 		  if (get_opd_info (sec) != NULL)
   13778 		    {
   13779 		      bfd_vma off = (relocation + addend
   13780 				     - sec->output_section->vma
   13781 				     - sec->output_offset);
   13782 
   13783 		      opd_entry_value (sec, off, &code_sec, NULL, FALSE);
   13784 		    }
   13785 		  if (code_sec == input_section)
   13786 		    can_plt_call = TRUE;
   13787 		}
   13788 
   13789 	      if (!can_plt_call)
   13790 		{
   13791 		  if (stub_entry->stub_type == ppc_stub_plt_call
   13792 		      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   13793 		    info->callbacks->einfo
   13794 		      (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
   13795 			 "recompile with -fPIC\n"),
   13796 		       input_bfd, input_section, rel->r_offset, sym_name);
   13797 		  else
   13798 		    info->callbacks->einfo
   13799 		      (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
   13800 			 "(-mcmodel=small toc adjust stub)\n"),
   13801 		       input_bfd, input_section, rel->r_offset, sym_name);
   13802 
   13803 		  bfd_set_error (bfd_error_bad_value);
   13804 		  ret = FALSE;
   13805 		}
   13806 
   13807 	      if (can_plt_call
   13808 		  && (stub_entry->stub_type == ppc_stub_plt_call
   13809 		      || stub_entry->stub_type == ppc_stub_plt_call_r2save))
   13810 		unresolved_reloc = FALSE;
   13811 	    }
   13812 
   13813 	  if ((stub_entry == NULL
   13814 	       || stub_entry->stub_type == ppc_stub_long_branch
   13815 	       || stub_entry->stub_type == ppc_stub_plt_branch)
   13816 	      && get_opd_info (sec) != NULL)
   13817 	    {
   13818 	      /* The branch destination is the value of the opd entry. */
   13819 	      bfd_vma off = (relocation + addend
   13820 			     - sec->output_section->vma
   13821 			     - sec->output_offset);
   13822 	      bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
   13823 	      if (dest != (bfd_vma) -1)
   13824 		{
   13825 		  relocation = dest;
   13826 		  addend = 0;
   13827 		  reloc_dest = DEST_OPD;
   13828 		}
   13829 	    }
   13830 
   13831 	  /* If the branch is out of reach we ought to have a long
   13832 	     branch stub.  */
   13833 	  from = (rel->r_offset
   13834 		  + input_section->output_offset
   13835 		  + input_section->output_section->vma);
   13836 
   13837 	  relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
   13838 						  ? fdh->elf.other
   13839 						  : sym->st_other);
   13840 
   13841 	  if (stub_entry != NULL
   13842 	      && (stub_entry->stub_type == ppc_stub_long_branch
   13843 		  || stub_entry->stub_type == ppc_stub_plt_branch)
   13844 	      && (r_type == R_PPC64_ADDR14_BRTAKEN
   13845 		  || r_type == R_PPC64_ADDR14_BRNTAKEN
   13846 		  || (relocation + addend - from + max_br_offset
   13847 		      < 2 * max_br_offset)))
   13848 	    /* Don't use the stub if this branch is in range.  */
   13849 	    stub_entry = NULL;
   13850 
   13851 	  if (stub_entry != NULL)
   13852 	    {
   13853 	      /* Munge up the value and addend so that we call the stub
   13854 		 rather than the procedure directly.  */
   13855 	      relocation = (stub_entry->stub_offset
   13856 			    + stub_entry->stub_sec->output_offset
   13857 			    + stub_entry->stub_sec->output_section->vma);
   13858 	      addend = 0;
   13859 	      reloc_dest = DEST_STUB;
   13860 
   13861  	      if ((stub_entry->stub_type == ppc_stub_plt_call
   13862 		   || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   13863 		  && (ALWAYS_EMIT_R2SAVE
   13864 		      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   13865 		  && rel + 1 < relend
   13866 		  && rel[1].r_offset == rel->r_offset + 4
   13867 		  && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
   13868 		relocation += 4;
   13869 	    }
   13870 
   13871 	  if (insn != 0)
   13872 	    {
   13873 	      if (is_isa_v2)
   13874 		{
   13875 		  /* Set 'a' bit.  This is 0b00010 in BO field for branch
   13876 		     on CR(BI) insns (BO == 001at or 011at), and 0b01000
   13877 		     for branch on CTR insns (BO == 1a00t or 1a01t).  */
   13878 		  if ((insn & (0x14 << 21)) == (0x04 << 21))
   13879 		    insn |= 0x02 << 21;
   13880 		  else if ((insn & (0x14 << 21)) == (0x10 << 21))
   13881 		    insn |= 0x08 << 21;
   13882 		  else
   13883 		    break;
   13884 		}
   13885 	      else
   13886 		{
   13887 		  /* Invert 'y' bit if not the default.  */
   13888 		  if ((bfd_signed_vma) (relocation + addend - from) < 0)
   13889 		    insn ^= 0x01 << 21;
   13890 		}
   13891 
   13892 	      bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
   13893 	    }
   13894 
   13895 	  /* NOP out calls to undefined weak functions.
   13896 	     We can thus call a weak function without first
   13897 	     checking whether the function is defined.  */
   13898 	  else if (h != NULL
   13899 		   && h->elf.root.type == bfd_link_hash_undefweak
   13900 		   && h->elf.dynindx == -1
   13901 		   && r_type == R_PPC64_REL24
   13902 		   && relocation == 0
   13903 		   && addend == 0)
   13904 	    {
   13905 	      bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
   13906 	      continue;
   13907 	    }
   13908 	  break;
   13909 	}
   13910 
   13911       /* Set `addend'.  */
   13912       tls_type = 0;
   13913       switch (r_type)
   13914 	{
   13915 	default:
   13916 	  info->callbacks->einfo
   13917 	    (_("%P: %B: unknown relocation type %d for `%T'\n"),
   13918 	     input_bfd, (int) r_type, sym_name);
   13919 
   13920 	  bfd_set_error (bfd_error_bad_value);
   13921 	  ret = FALSE;
   13922 	  continue;
   13923 
   13924 	case R_PPC64_NONE:
   13925 	case R_PPC64_TLS:
   13926 	case R_PPC64_TLSGD:
   13927 	case R_PPC64_TLSLD:
   13928 	case R_PPC64_TOCSAVE:
   13929 	case R_PPC64_GNU_VTINHERIT:
   13930 	case R_PPC64_GNU_VTENTRY:
   13931 	  continue;
   13932 
   13933 	  /* GOT16 relocations.  Like an ADDR16 using the symbol's
   13934 	     address in the GOT as relocation value instead of the
   13935 	     symbol's value itself.  Also, create a GOT entry for the
   13936 	     symbol and put the symbol value there.  */
   13937 	case R_PPC64_GOT_TLSGD16:
   13938 	case R_PPC64_GOT_TLSGD16_LO:
   13939 	case R_PPC64_GOT_TLSGD16_HI:
   13940 	case R_PPC64_GOT_TLSGD16_HA:
   13941 	  tls_type = TLS_TLS | TLS_GD;
   13942 	  goto dogot;
   13943 
   13944 	case R_PPC64_GOT_TLSLD16:
   13945 	case R_PPC64_GOT_TLSLD16_LO:
   13946 	case R_PPC64_GOT_TLSLD16_HI:
   13947 	case R_PPC64_GOT_TLSLD16_HA:
   13948 	  tls_type = TLS_TLS | TLS_LD;
   13949 	  goto dogot;
   13950 
   13951 	case R_PPC64_GOT_TPREL16_DS:
   13952 	case R_PPC64_GOT_TPREL16_LO_DS:
   13953 	case R_PPC64_GOT_TPREL16_HI:
   13954 	case R_PPC64_GOT_TPREL16_HA:
   13955 	  tls_type = TLS_TLS | TLS_TPREL;
   13956 	  goto dogot;
   13957 
   13958 	case R_PPC64_GOT_DTPREL16_DS:
   13959 	case R_PPC64_GOT_DTPREL16_LO_DS:
   13960 	case R_PPC64_GOT_DTPREL16_HI:
   13961 	case R_PPC64_GOT_DTPREL16_HA:
   13962 	  tls_type = TLS_TLS | TLS_DTPREL;
   13963 	  goto dogot;
   13964 
   13965 	case R_PPC64_GOT16:
   13966 	case R_PPC64_GOT16_LO:
   13967 	case R_PPC64_GOT16_HI:
   13968 	case R_PPC64_GOT16_HA:
   13969 	case R_PPC64_GOT16_DS:
   13970 	case R_PPC64_GOT16_LO_DS:
   13971 	dogot:
   13972 	  {
   13973 	    /* Relocation is to the entry for this symbol in the global
   13974 	       offset table.  */
   13975 	    asection *got;
   13976 	    bfd_vma *offp;
   13977 	    bfd_vma off;
   13978 	    unsigned long indx = 0;
   13979 	    struct got_entry *ent;
   13980 
   13981 	    if (tls_type == (TLS_TLS | TLS_LD)
   13982 		&& (h == NULL
   13983 		    || !h->elf.def_dynamic))
   13984 	      ent = ppc64_tlsld_got (input_bfd);
   13985 	    else
   13986 	      {
   13987 
   13988 		if (h != NULL)
   13989 		  {
   13990 		    bfd_boolean dyn = htab->elf.dynamic_sections_created;
   13991 		    if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
   13992 							  &h->elf)
   13993 			|| (info->shared
   13994 			    && SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
   13995 		      /* This is actually a static link, or it is a
   13996 			 -Bsymbolic link and the symbol is defined
   13997 			 locally, or the symbol was forced to be local
   13998 			 because of a version file.  */
   13999 		      ;
   14000 		    else
   14001 		      {
   14002 			BFD_ASSERT (h->elf.dynindx != -1);
   14003 			indx = h->elf.dynindx;
   14004 			unresolved_reloc = FALSE;
   14005 		      }
   14006 		    ent = h->elf.got.glist;
   14007 		  }
   14008 		else
   14009 		  {
   14010 		    if (local_got_ents == NULL)
   14011 		      abort ();
   14012 		    ent = local_got_ents[r_symndx];
   14013 		  }
   14014 
   14015 		for (; ent != NULL; ent = ent->next)
   14016 		  if (ent->addend == orig_rel.r_addend
   14017 		      && ent->owner == input_bfd
   14018 		      && ent->tls_type == tls_type)
   14019 		    break;
   14020 	      }
   14021 
   14022 	    if (ent == NULL)
   14023 	      abort ();
   14024 	    if (ent->is_indirect)
   14025 	      ent = ent->got.ent;
   14026 	    offp = &ent->got.offset;
   14027 	    got = ppc64_elf_tdata (ent->owner)->got;
   14028 	    if (got == NULL)
   14029 	      abort ();
   14030 
   14031 	    /* The offset must always be a multiple of 8.  We use the
   14032 	       least significant bit to record whether we have already
   14033 	       processed this entry.  */
   14034 	    off = *offp;
   14035 	    if ((off & 1) != 0)
   14036 	      off &= ~1;
   14037 	    else
   14038 	      {
   14039 		/* Generate relocs for the dynamic linker, except in
   14040 		   the case of TLSLD where we'll use one entry per
   14041 		   module.  */
   14042 		asection *relgot;
   14043 		bfd_boolean ifunc;
   14044 
   14045 		*offp = off | 1;
   14046 		relgot = NULL;
   14047 		ifunc = (h != NULL
   14048 			 ? h->elf.type == STT_GNU_IFUNC
   14049 			 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
   14050 		if (ifunc)
   14051 		  relgot = htab->elf.irelplt;
   14052 		else if ((info->shared || indx != 0)
   14053 			 && (h == NULL
   14054 			     || (tls_type == (TLS_TLS | TLS_LD)
   14055 				 && !h->elf.def_dynamic)
   14056 			     || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
   14057 			     || h->elf.root.type != bfd_link_hash_undefweak))
   14058 		  relgot = ppc64_elf_tdata (ent->owner)->relgot;
   14059 		if (relgot != NULL)
   14060 		  {
   14061 		    outrel.r_offset = (got->output_section->vma
   14062 				       + got->output_offset
   14063 				       + off);
   14064 		    outrel.r_addend = addend;
   14065 		    if (tls_type & (TLS_LD | TLS_GD))
   14066 		      {
   14067 			outrel.r_addend = 0;
   14068 			outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
   14069 			if (tls_type == (TLS_TLS | TLS_GD))
   14070 			  {
   14071 			    loc = relgot->contents;
   14072 			    loc += (relgot->reloc_count++
   14073 				    * sizeof (Elf64_External_Rela));
   14074 			    bfd_elf64_swap_reloca_out (output_bfd,
   14075 						       &outrel, loc);
   14076 			    outrel.r_offset += 8;
   14077 			    outrel.r_addend = addend;
   14078 			    outrel.r_info
   14079 			      = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
   14080 			  }
   14081 		      }
   14082 		    else if (tls_type == (TLS_TLS | TLS_DTPREL))
   14083 		      outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
   14084 		    else if (tls_type == (TLS_TLS | TLS_TPREL))
   14085 		      outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
   14086 		    else if (indx != 0)
   14087 		      outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
   14088 		    else
   14089 		      {
   14090 			if (ifunc)
   14091 			  outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
   14092 			else
   14093 			  outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   14094 
   14095 			/* Write the .got section contents for the sake
   14096 			   of prelink.  */
   14097 			loc = got->contents + off;
   14098 			bfd_put_64 (output_bfd, outrel.r_addend + relocation,
   14099 				    loc);
   14100 		      }
   14101 
   14102 		    if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
   14103 		      {
   14104 			outrel.r_addend += relocation;
   14105 			if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
   14106 			  outrel.r_addend -= htab->elf.tls_sec->vma;
   14107 		      }
   14108 		    loc = relgot->contents;
   14109 		    loc += (relgot->reloc_count++
   14110 			    * sizeof (Elf64_External_Rela));
   14111 		    bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
   14112 		  }
   14113 
   14114 		/* Init the .got section contents here if we're not
   14115 		   emitting a reloc.  */
   14116 		else
   14117 		  {
   14118 		    relocation += addend;
   14119 		    if (tls_type == (TLS_TLS | TLS_LD))
   14120 		      relocation = 1;
   14121 		    else if (tls_type != 0)
   14122 		      {
   14123 			relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
   14124 			if (tls_type == (TLS_TLS | TLS_TPREL))
   14125 			  relocation += DTP_OFFSET - TP_OFFSET;
   14126 
   14127 			if (tls_type == (TLS_TLS | TLS_GD))
   14128 			  {
   14129 			    bfd_put_64 (output_bfd, relocation,
   14130 					got->contents + off + 8);
   14131 			    relocation = 1;
   14132 			  }
   14133 		      }
   14134 
   14135 		    bfd_put_64 (output_bfd, relocation,
   14136 				got->contents + off);
   14137 		  }
   14138 	      }
   14139 
   14140 	    if (off >= (bfd_vma) -2)
   14141 	      abort ();
   14142 
   14143 	    relocation = got->output_section->vma + got->output_offset + off;
   14144 	    addend = -(TOCstart + htab->stub_group[input_section->id].toc_off);
   14145 	  }
   14146 	  break;
   14147 
   14148 	case R_PPC64_PLT16_HA:
   14149 	case R_PPC64_PLT16_HI:
   14150 	case R_PPC64_PLT16_LO:
   14151 	case R_PPC64_PLT32:
   14152 	case R_PPC64_PLT64:
   14153 	  /* Relocation is to the entry for this symbol in the
   14154 	     procedure linkage table.  */
   14155 
   14156 	  /* Resolve a PLT reloc against a local symbol directly,
   14157 	     without using the procedure linkage table.  */
   14158 	  if (h == NULL)
   14159 	    break;
   14160 
   14161 	  /* It's possible that we didn't make a PLT entry for this
   14162 	     symbol.  This happens when statically linking PIC code,
   14163 	     or when using -Bsymbolic.  Go find a match if there is a
   14164 	     PLT entry.  */
   14165 	  if (htab->elf.splt != NULL)
   14166 	    {
   14167 	      struct plt_entry *ent;
   14168 	      for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
   14169 		if (ent->plt.offset != (bfd_vma) -1
   14170 		    && ent->addend == orig_rel.r_addend)
   14171 		  {
   14172 		    relocation = (htab->elf.splt->output_section->vma
   14173 				  + htab->elf.splt->output_offset
   14174 				  + ent->plt.offset);
   14175 		    unresolved_reloc = FALSE;
   14176 		    break;
   14177 		  }
   14178 	    }
   14179 	  break;
   14180 
   14181 	case R_PPC64_TOC:
   14182 	  /* Relocation value is TOC base.  */
   14183 	  relocation = TOCstart;
   14184 	  if (r_symndx == STN_UNDEF)
   14185 	    relocation += htab->stub_group[input_section->id].toc_off;
   14186 	  else if (unresolved_reloc)
   14187 	    ;
   14188 	  else if (sec != NULL && sec->id <= htab->top_id)
   14189 	    relocation += htab->stub_group[sec->id].toc_off;
   14190 	  else
   14191 	    unresolved_reloc = TRUE;
   14192 	  goto dodyn;
   14193 
   14194 	  /* TOC16 relocs.  We want the offset relative to the TOC base,
   14195 	     which is the address of the start of the TOC plus 0x8000.
   14196 	     The TOC consists of sections .got, .toc, .tocbss, and .plt,
   14197 	     in this order.  */
   14198 	case R_PPC64_TOC16:
   14199 	case R_PPC64_TOC16_LO:
   14200 	case R_PPC64_TOC16_HI:
   14201 	case R_PPC64_TOC16_DS:
   14202 	case R_PPC64_TOC16_LO_DS:
   14203 	case R_PPC64_TOC16_HA:
   14204 	  addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
   14205 	  break;
   14206 
   14207 	  /* Relocate against the beginning of the section.  */
   14208 	case R_PPC64_SECTOFF:
   14209 	case R_PPC64_SECTOFF_LO:
   14210 	case R_PPC64_SECTOFF_HI:
   14211 	case R_PPC64_SECTOFF_DS:
   14212 	case R_PPC64_SECTOFF_LO_DS:
   14213 	case R_PPC64_SECTOFF_HA:
   14214 	  if (sec != NULL)
   14215 	    addend -= sec->output_section->vma;
   14216 	  break;
   14217 
   14218 	case R_PPC64_REL16:
   14219 	case R_PPC64_REL16_LO:
   14220 	case R_PPC64_REL16_HI:
   14221 	case R_PPC64_REL16_HA:
   14222 	  break;
   14223 
   14224 	case R_PPC64_REL14:
   14225 	case R_PPC64_REL14_BRNTAKEN:
   14226 	case R_PPC64_REL14_BRTAKEN:
   14227 	case R_PPC64_REL24:
   14228 	  break;
   14229 
   14230 	case R_PPC64_TPREL16:
   14231 	case R_PPC64_TPREL16_LO:
   14232 	case R_PPC64_TPREL16_HI:
   14233 	case R_PPC64_TPREL16_HA:
   14234 	case R_PPC64_TPREL16_DS:
   14235 	case R_PPC64_TPREL16_LO_DS:
   14236 	case R_PPC64_TPREL16_HIGH:
   14237 	case R_PPC64_TPREL16_HIGHA:
   14238 	case R_PPC64_TPREL16_HIGHER:
   14239 	case R_PPC64_TPREL16_HIGHERA:
   14240 	case R_PPC64_TPREL16_HIGHEST:
   14241 	case R_PPC64_TPREL16_HIGHESTA:
   14242 	  if (h != NULL
   14243 	      && h->elf.root.type == bfd_link_hash_undefweak
   14244 	      && h->elf.dynindx == -1)
   14245 	    {
   14246 	      /* Make this relocation against an undefined weak symbol
   14247 		 resolve to zero.  This is really just a tweak, since
   14248 		 code using weak externs ought to check that they are
   14249 		 defined before using them.  */
   14250 	      bfd_byte *p = contents + rel->r_offset - d_offset;
   14251 
   14252 	      insn = bfd_get_32 (output_bfd, p);
   14253 	      insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
   14254 	      if (insn != 0)
   14255 		bfd_put_32 (output_bfd, insn, p);
   14256 	      break;
   14257 	    }
   14258 	  addend -= htab->elf.tls_sec->vma + TP_OFFSET;
   14259 	  if (info->shared)
   14260 	    /* The TPREL16 relocs shouldn't really be used in shared
   14261 	       libs as they will result in DT_TEXTREL being set, but
   14262 	       support them anyway.  */
   14263 	    goto dodyn;
   14264 	  break;
   14265 
   14266 	case R_PPC64_DTPREL16:
   14267 	case R_PPC64_DTPREL16_LO:
   14268 	case R_PPC64_DTPREL16_HI:
   14269 	case R_PPC64_DTPREL16_HA:
   14270 	case R_PPC64_DTPREL16_DS:
   14271 	case R_PPC64_DTPREL16_LO_DS:
   14272 	case R_PPC64_DTPREL16_HIGH:
   14273 	case R_PPC64_DTPREL16_HIGHA:
   14274 	case R_PPC64_DTPREL16_HIGHER:
   14275 	case R_PPC64_DTPREL16_HIGHERA:
   14276 	case R_PPC64_DTPREL16_HIGHEST:
   14277 	case R_PPC64_DTPREL16_HIGHESTA:
   14278 	  addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
   14279 	  break;
   14280 
   14281 	case R_PPC64_ADDR64_LOCAL:
   14282 	  addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
   14283 					      ? h->elf.other
   14284 					      : sym->st_other);
   14285 	  break;
   14286 
   14287 	case R_PPC64_DTPMOD64:
   14288 	  relocation = 1;
   14289 	  addend = 0;
   14290 	  goto dodyn;
   14291 
   14292 	case R_PPC64_TPREL64:
   14293 	  addend -= htab->elf.tls_sec->vma + TP_OFFSET;
   14294 	  goto dodyn;
   14295 
   14296 	case R_PPC64_DTPREL64:
   14297 	  addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
   14298 	  /* Fall thru */
   14299 
   14300 	  /* Relocations that may need to be propagated if this is a
   14301 	     dynamic object.  */
   14302 	case R_PPC64_REL30:
   14303 	case R_PPC64_REL32:
   14304 	case R_PPC64_REL64:
   14305 	case R_PPC64_ADDR14:
   14306 	case R_PPC64_ADDR14_BRNTAKEN:
   14307 	case R_PPC64_ADDR14_BRTAKEN:
   14308 	case R_PPC64_ADDR16:
   14309 	case R_PPC64_ADDR16_DS:
   14310 	case R_PPC64_ADDR16_HA:
   14311 	case R_PPC64_ADDR16_HI:
   14312 	case R_PPC64_ADDR16_HIGH:
   14313 	case R_PPC64_ADDR16_HIGHA:
   14314 	case R_PPC64_ADDR16_HIGHER:
   14315 	case R_PPC64_ADDR16_HIGHERA:
   14316 	case R_PPC64_ADDR16_HIGHEST:
   14317 	case R_PPC64_ADDR16_HIGHESTA:
   14318 	case R_PPC64_ADDR16_LO:
   14319 	case R_PPC64_ADDR16_LO_DS:
   14320 	case R_PPC64_ADDR24:
   14321 	case R_PPC64_ADDR32:
   14322 	case R_PPC64_ADDR64:
   14323 	case R_PPC64_UADDR16:
   14324 	case R_PPC64_UADDR32:
   14325 	case R_PPC64_UADDR64:
   14326 	dodyn:
   14327 	  if ((input_section->flags & SEC_ALLOC) == 0)
   14328 	    break;
   14329 
   14330 	  if (NO_OPD_RELOCS && is_opd)
   14331 	    break;
   14332 
   14333 	  if ((info->shared
   14334 	       && (h == NULL
   14335 		   || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
   14336 		   || h->elf.root.type != bfd_link_hash_undefweak)
   14337 	       && (must_be_dyn_reloc (info, r_type)
   14338 		   || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
   14339 	      || (ELIMINATE_COPY_RELOCS
   14340 		  && !info->shared
   14341 		  && h != NULL
   14342 		  && h->elf.dynindx != -1
   14343 		  && !h->elf.non_got_ref
   14344 		  && !h->elf.def_regular)
   14345 	      || (!info->shared
   14346 		  && (h != NULL
   14347 		      ? h->elf.type == STT_GNU_IFUNC
   14348 		      : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
   14349 	    {
   14350 	      bfd_boolean skip, relocate;
   14351 	      asection *sreloc;
   14352 	      bfd_vma out_off;
   14353 
   14354 	      /* When generating a dynamic object, these relocations
   14355 		 are copied into the output file to be resolved at run
   14356 		 time.  */
   14357 
   14358 	      skip = FALSE;
   14359 	      relocate = FALSE;
   14360 
   14361 	      out_off = _bfd_elf_section_offset (output_bfd, info,
   14362 						 input_section, rel->r_offset);
   14363 	      if (out_off == (bfd_vma) -1)
   14364 		skip = TRUE;
   14365 	      else if (out_off == (bfd_vma) -2)
   14366 		skip = TRUE, relocate = TRUE;
   14367 	      out_off += (input_section->output_section->vma
   14368 			  + input_section->output_offset);
   14369 	      outrel.r_offset = out_off;
   14370 	      outrel.r_addend = rel->r_addend;
   14371 
   14372 	      /* Optimize unaligned reloc use.  */
   14373 	      if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
   14374 		  || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
   14375 		r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
   14376 	      else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
   14377 		       || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
   14378 		r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
   14379 	      else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
   14380 		       || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
   14381 		r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
   14382 
   14383 	      if (skip)
   14384 		memset (&outrel, 0, sizeof outrel);
   14385 	      else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
   14386 		       && !is_opd
   14387 		       && r_type != R_PPC64_TOC)
   14388 		{
   14389 		  BFD_ASSERT (h->elf.dynindx != -1);
   14390 		  outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
   14391 		}
   14392 	      else
   14393 		{
   14394 		  /* This symbol is local, or marked to become local,
   14395 		     or this is an opd section reloc which must point
   14396 		     at a local function.  */
   14397 		  outrel.r_addend += relocation;
   14398 		  if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
   14399 		    {
   14400 		      if (is_opd && h != NULL)
   14401 			{
   14402 			  /* Lie about opd entries.  This case occurs
   14403 			     when building shared libraries and we
   14404 			     reference a function in another shared
   14405 			     lib.  The same thing happens for a weak
   14406 			     definition in an application that's
   14407 			     overridden by a strong definition in a
   14408 			     shared lib.  (I believe this is a generic
   14409 			     bug in binutils handling of weak syms.)
   14410 			     In these cases we won't use the opd
   14411 			     entry in this lib.  */
   14412 			  unresolved_reloc = FALSE;
   14413 			}
   14414 		      if (!is_opd
   14415 			  && r_type == R_PPC64_ADDR64
   14416 			  && (h != NULL
   14417 			      ? h->elf.type == STT_GNU_IFUNC
   14418 			      : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
   14419 			outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
   14420 		      else
   14421 			{
   14422 			  outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   14423 
   14424 			  /* We need to relocate .opd contents for ld.so.
   14425 			     Prelink also wants simple and consistent rules
   14426 			     for relocs.  This make all RELATIVE relocs have
   14427 			     *r_offset equal to r_addend.  */
   14428 			  relocate = TRUE;
   14429 			}
   14430 		    }
   14431 		  else
   14432 		    {
   14433 		      long indx = 0;
   14434 
   14435 		      if (h != NULL
   14436 			  ? h->elf.type == STT_GNU_IFUNC
   14437 			  : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
   14438 			{
   14439 			  info->callbacks->einfo
   14440 			    (_("%P: %H: %s for indirect "
   14441 			       "function `%T' unsupported\n"),
   14442 			     input_bfd, input_section, rel->r_offset,
   14443 			     ppc64_elf_howto_table[r_type]->name,
   14444 			     sym_name);
   14445 			  ret = FALSE;
   14446 			}
   14447 		      else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
   14448 			;
   14449 		      else if (sec == NULL || sec->owner == NULL)
   14450 			{
   14451 			  bfd_set_error (bfd_error_bad_value);
   14452 			  return FALSE;
   14453 			}
   14454 		      else
   14455 			{
   14456 			  asection *osec;
   14457 
   14458 			  osec = sec->output_section;
   14459 			  indx = elf_section_data (osec)->dynindx;
   14460 
   14461 			  if (indx == 0)
   14462 			    {
   14463 			      if ((osec->flags & SEC_READONLY) == 0
   14464 				  && htab->elf.data_index_section != NULL)
   14465 				osec = htab->elf.data_index_section;
   14466 			      else
   14467 				osec = htab->elf.text_index_section;
   14468 			      indx = elf_section_data (osec)->dynindx;
   14469 			    }
   14470 			  BFD_ASSERT (indx != 0);
   14471 
   14472 			  /* We are turning this relocation into one
   14473 			     against a section symbol, so subtract out
   14474 			     the output section's address but not the
   14475 			     offset of the input section in the output
   14476 			     section.  */
   14477 			  outrel.r_addend -= osec->vma;
   14478 			}
   14479 
   14480 		      outrel.r_info = ELF64_R_INFO (indx, r_type);
   14481 		    }
   14482 		}
   14483 
   14484 	      sreloc = elf_section_data (input_section)->sreloc;
   14485 	      if (h != NULL
   14486 		  ? h->elf.type == STT_GNU_IFUNC
   14487 		  : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
   14488 		sreloc = htab->elf.irelplt;
   14489 	      if (sreloc == NULL)
   14490 		abort ();
   14491 
   14492 	      if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
   14493 		  >= sreloc->size)
   14494 		abort ();
   14495 	      loc = sreloc->contents;
   14496 	      loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
   14497 	      bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
   14498 
   14499 	      /* If this reloc is against an external symbol, it will
   14500 		 be computed at runtime, so there's no need to do
   14501 		 anything now.  However, for the sake of prelink ensure
   14502 		 that the section contents are a known value.  */
   14503 	      if (! relocate)
   14504 		{
   14505 		  unresolved_reloc = FALSE;
   14506 		  /* The value chosen here is quite arbitrary as ld.so
   14507 		     ignores section contents except for the special
   14508 		     case of .opd where the contents might be accessed
   14509 		     before relocation.  Choose zero, as that won't
   14510 		     cause reloc overflow.  */
   14511 		  relocation = 0;
   14512 		  addend = 0;
   14513 		  /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
   14514 		     to improve backward compatibility with older
   14515 		     versions of ld.  */
   14516 		  if (r_type == R_PPC64_ADDR64)
   14517 		    addend = outrel.r_addend;
   14518 		  /* Adjust pc_relative relocs to have zero in *r_offset.  */
   14519 		  else if (ppc64_elf_howto_table[r_type]->pc_relative)
   14520 		    addend = (input_section->output_section->vma
   14521 			      + input_section->output_offset
   14522 			      + rel->r_offset);
   14523 		}
   14524 	    }
   14525 	  break;
   14526 
   14527 	case R_PPC64_COPY:
   14528 	case R_PPC64_GLOB_DAT:
   14529 	case R_PPC64_JMP_SLOT:
   14530 	case R_PPC64_JMP_IREL:
   14531 	case R_PPC64_RELATIVE:
   14532 	  /* We shouldn't ever see these dynamic relocs in relocatable
   14533 	     files.  */
   14534 	  /* Fall through.  */
   14535 
   14536 	case R_PPC64_PLTGOT16:
   14537 	case R_PPC64_PLTGOT16_DS:
   14538 	case R_PPC64_PLTGOT16_HA:
   14539 	case R_PPC64_PLTGOT16_HI:
   14540 	case R_PPC64_PLTGOT16_LO:
   14541 	case R_PPC64_PLTGOT16_LO_DS:
   14542 	case R_PPC64_PLTREL32:
   14543 	case R_PPC64_PLTREL64:
   14544 	  /* These ones haven't been implemented yet.  */
   14545 
   14546 	  info->callbacks->einfo
   14547 	    (_("%P: %B: %s is not supported for `%T'\n"),
   14548 	     input_bfd,
   14549 	     ppc64_elf_howto_table[r_type]->name, sym_name);
   14550 
   14551 	  bfd_set_error (bfd_error_invalid_operation);
   14552 	  ret = FALSE;
   14553 	  continue;
   14554 	}
   14555 
   14556       /* Multi-instruction sequences that access the TOC can be
   14557 	 optimized, eg. addis ra,r2,0; addi rb,ra,x;
   14558 	 to             nop;           addi rb,r2,x;  */
   14559       switch (r_type)
   14560 	{
   14561 	default:
   14562 	  break;
   14563 
   14564 	case R_PPC64_GOT_TLSLD16_HI:
   14565 	case R_PPC64_GOT_TLSGD16_HI:
   14566 	case R_PPC64_GOT_TPREL16_HI:
   14567 	case R_PPC64_GOT_DTPREL16_HI:
   14568 	case R_PPC64_GOT16_HI:
   14569 	case R_PPC64_TOC16_HI:
   14570 	  /* These relocs would only be useful if building up an
   14571 	     offset to later add to r2, perhaps in an indexed
   14572 	     addressing mode instruction.  Don't try to optimize.
   14573 	     Unfortunately, the possibility of someone building up an
   14574 	     offset like this or even with the HA relocs, means that
   14575 	     we need to check the high insn when optimizing the low
   14576 	     insn.  */
   14577 	  break;
   14578 
   14579 	case R_PPC64_GOT_TLSLD16_HA:
   14580 	case R_PPC64_GOT_TLSGD16_HA:
   14581 	case R_PPC64_GOT_TPREL16_HA:
   14582 	case R_PPC64_GOT_DTPREL16_HA:
   14583 	case R_PPC64_GOT16_HA:
   14584 	case R_PPC64_TOC16_HA:
   14585 	  if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
   14586 	      && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
   14587 	    {
   14588 	      bfd_byte *p = contents + (rel->r_offset & ~3);
   14589 	      bfd_put_32 (input_bfd, NOP, p);
   14590 	    }
   14591 	  break;
   14592 
   14593 	case R_PPC64_GOT_TLSLD16_LO:
   14594 	case R_PPC64_GOT_TLSGD16_LO:
   14595 	case R_PPC64_GOT_TPREL16_LO_DS:
   14596 	case R_PPC64_GOT_DTPREL16_LO_DS:
   14597 	case R_PPC64_GOT16_LO:
   14598 	case R_PPC64_GOT16_LO_DS:
   14599 	case R_PPC64_TOC16_LO:
   14600 	case R_PPC64_TOC16_LO_DS:
   14601 	  if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
   14602 	      && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
   14603 	    {
   14604 	      bfd_byte *p = contents + (rel->r_offset & ~3);
   14605 	      insn = bfd_get_32 (input_bfd, p);
   14606 	      if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
   14607 		{
   14608 		  /* Transform addic to addi when we change reg.  */
   14609 		  insn &= ~((0x3f << 26) | (0x1f << 16));
   14610 		  insn |= (14u << 26) | (2 << 16);
   14611 		}
   14612 	      else
   14613 		{
   14614 		  insn &= ~(0x1f << 16);
   14615 		  insn |= 2 << 16;
   14616 		}
   14617 	      bfd_put_32 (input_bfd, insn, p);
   14618 	    }
   14619 	  break;
   14620 	}
   14621 
   14622       /* Do any further special processing.  */
   14623       howto = ppc64_elf_howto_table[(int) r_type];
   14624       switch (r_type)
   14625 	{
   14626 	default:
   14627 	  break;
   14628 
   14629 	case R_PPC64_REL16_HA:
   14630 	case R_PPC64_ADDR16_HA:
   14631 	case R_PPC64_ADDR16_HIGHA:
   14632 	case R_PPC64_ADDR16_HIGHERA:
   14633 	case R_PPC64_ADDR16_HIGHESTA:
   14634 	case R_PPC64_TOC16_HA:
   14635 	case R_PPC64_SECTOFF_HA:
   14636 	case R_PPC64_TPREL16_HA:
   14637 	case R_PPC64_TPREL16_HIGHA:
   14638 	case R_PPC64_TPREL16_HIGHERA:
   14639 	case R_PPC64_TPREL16_HIGHESTA:
   14640 	case R_PPC64_DTPREL16_HA:
   14641 	case R_PPC64_DTPREL16_HIGHA:
   14642 	case R_PPC64_DTPREL16_HIGHERA:
   14643 	case R_PPC64_DTPREL16_HIGHESTA:
   14644 	  /* It's just possible that this symbol is a weak symbol
   14645 	     that's not actually defined anywhere. In that case,
   14646 	     'sec' would be NULL, and we should leave the symbol
   14647 	     alone (it will be set to zero elsewhere in the link).  */
   14648 	  if (sec == NULL)
   14649 	    break;
   14650 	  /* Fall thru */
   14651 
   14652 	case R_PPC64_GOT16_HA:
   14653 	case R_PPC64_PLTGOT16_HA:
   14654 	case R_PPC64_PLT16_HA:
   14655 	case R_PPC64_GOT_TLSGD16_HA:
   14656 	case R_PPC64_GOT_TLSLD16_HA:
   14657 	case R_PPC64_GOT_TPREL16_HA:
   14658 	case R_PPC64_GOT_DTPREL16_HA:
   14659 	  /* Add 0x10000 if sign bit in 0:15 is set.
   14660 	     Bits 0:15 are not used.  */
   14661 	  addend += 0x8000;
   14662 	  break;
   14663 
   14664 	case R_PPC64_ADDR16_DS:
   14665 	case R_PPC64_ADDR16_LO_DS:
   14666 	case R_PPC64_GOT16_DS:
   14667 	case R_PPC64_GOT16_LO_DS:
   14668 	case R_PPC64_PLT16_LO_DS:
   14669 	case R_PPC64_SECTOFF_DS:
   14670 	case R_PPC64_SECTOFF_LO_DS:
   14671 	case R_PPC64_TOC16_DS:
   14672 	case R_PPC64_TOC16_LO_DS:
   14673 	case R_PPC64_PLTGOT16_DS:
   14674 	case R_PPC64_PLTGOT16_LO_DS:
   14675 	case R_PPC64_GOT_TPREL16_DS:
   14676 	case R_PPC64_GOT_TPREL16_LO_DS:
   14677 	case R_PPC64_GOT_DTPREL16_DS:
   14678 	case R_PPC64_GOT_DTPREL16_LO_DS:
   14679 	case R_PPC64_TPREL16_DS:
   14680 	case R_PPC64_TPREL16_LO_DS:
   14681 	case R_PPC64_DTPREL16_DS:
   14682 	case R_PPC64_DTPREL16_LO_DS:
   14683 	  insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
   14684 	  mask = 3;
   14685 	  /* If this reloc is against an lq insn, then the value must be
   14686 	     a multiple of 16.  This is somewhat of a hack, but the
   14687 	     "correct" way to do this by defining _DQ forms of all the
   14688 	     _DS relocs bloats all reloc switches in this file.  It
   14689 	     doesn't seem to make much sense to use any of these relocs
   14690 	     in data, so testing the insn should be safe.  */
   14691 	  if ((insn & (0x3f << 26)) == (56u << 26))
   14692 	    mask = 15;
   14693 	  if (((relocation + addend) & mask) != 0)
   14694 	    {
   14695 	      info->callbacks->einfo
   14696 		(_("%P: %H: error: %s not a multiple of %u\n"),
   14697 		 input_bfd, input_section, rel->r_offset,
   14698 		 howto->name,
   14699 		 mask + 1);
   14700 	      bfd_set_error (bfd_error_bad_value);
   14701 	      ret = FALSE;
   14702 	      continue;
   14703 	    }
   14704 	  break;
   14705 	}
   14706 
   14707       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
   14708 	 because such sections are not SEC_ALLOC and thus ld.so will
   14709 	 not process them.  */
   14710       if (unresolved_reloc
   14711 	  && !((input_section->flags & SEC_DEBUGGING) != 0
   14712 	       && h->elf.def_dynamic)
   14713 	  && _bfd_elf_section_offset (output_bfd, info, input_section,
   14714 				      rel->r_offset) != (bfd_vma) -1)
   14715 	{
   14716 	  info->callbacks->einfo
   14717 	    (_("%P: %H: unresolvable %s against `%T'\n"),
   14718 	     input_bfd, input_section, rel->r_offset,
   14719 	     howto->name,
   14720 	     h->elf.root.root.string);
   14721 	  ret = FALSE;
   14722 	}
   14723 
   14724       /* 16-bit fields in insns mostly have signed values, but a
   14725 	 few insns have 16-bit unsigned values.  Really, we should
   14726 	 have different reloc types.  */
   14727       if (howto->complain_on_overflow != complain_overflow_dont
   14728 	  && howto->dst_mask == 0xffff
   14729 	  && (input_section->flags & SEC_CODE) != 0)
   14730 	{
   14731 	  enum complain_overflow complain = complain_overflow_signed;
   14732 
   14733 	  insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
   14734 	  if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
   14735 	    complain = complain_overflow_bitfield;
   14736 	  else if (howto->rightshift == 0
   14737 		   ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
   14738 		      || (insn & (0x3f << 26)) == 24u << 26 /* ori */
   14739 		      || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
   14740 		   : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
   14741 		      || (insn & (0x3f << 26)) == 25u << 26 /* oris */
   14742 		      || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
   14743 	    complain = complain_overflow_unsigned;
   14744 	  if (howto->complain_on_overflow != complain)
   14745 	    {
   14746 	      alt_howto = *howto;
   14747 	      alt_howto.complain_on_overflow = complain;
   14748 	      howto = &alt_howto;
   14749 	    }
   14750 	}
   14751 
   14752       r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
   14753 				    rel->r_offset, relocation, addend);
   14754 
   14755       if (r != bfd_reloc_ok)
   14756 	{
   14757 	  char *more_info = NULL;
   14758 	  const char *reloc_name = howto->name;
   14759 
   14760 	  if (reloc_dest != DEST_NORMAL)
   14761 	    {
   14762 	      more_info = bfd_malloc (strlen (reloc_name) + 8);
   14763 	      if (more_info != NULL)
   14764 		{
   14765 		  strcpy (more_info, reloc_name);
   14766 		  strcat (more_info, (reloc_dest == DEST_OPD
   14767 				      ? " (OPD)" : " (stub)"));
   14768 		  reloc_name = more_info;
   14769 		}
   14770 	    }
   14771 
   14772 	  if (r == bfd_reloc_overflow)
   14773 	    {
   14774 	      if (warned)
   14775 		continue;
   14776 	      if (h != NULL
   14777 		  && h->elf.root.type == bfd_link_hash_undefweak
   14778 		  && howto->pc_relative)
   14779 		{
   14780 		  /* Assume this is a call protected by other code that
   14781 		     detects the symbol is undefined.  If this is the case,
   14782 		     we can safely ignore the overflow.  If not, the
   14783 		     program is hosed anyway, and a little warning isn't
   14784 		     going to help.  */
   14785 
   14786 		  continue;
   14787 		}
   14788 
   14789 	      if (!((*info->callbacks->reloc_overflow)
   14790 		    (info, &h->elf.root, sym_name,
   14791 		     reloc_name, orig_rel.r_addend,
   14792 		     input_bfd, input_section, rel->r_offset)))
   14793 		return FALSE;
   14794 	    }
   14795 	  else
   14796 	    {
   14797 	      info->callbacks->einfo
   14798 		(_("%P: %H: %s against `%T': error %d\n"),
   14799 		 input_bfd, input_section, rel->r_offset,
   14800 		 reloc_name, sym_name, (int) r);
   14801 	      ret = FALSE;
   14802 	    }
   14803 	  if (more_info != NULL)
   14804 	    free (more_info);
   14805 	}
   14806     }
   14807 
   14808   /* If we're emitting relocations, then shortly after this function
   14809      returns, reloc offsets and addends for this section will be
   14810      adjusted.  Worse, reloc symbol indices will be for the output
   14811      file rather than the input.  Save a copy of the relocs for
   14812      opd_entry_value.  */
   14813   if (is_opd && (info->emitrelocations || info->relocatable))
   14814     {
   14815       bfd_size_type amt;
   14816       amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
   14817       rel = bfd_alloc (input_bfd, amt);
   14818       BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
   14819       ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
   14820       if (rel == NULL)
   14821 	return FALSE;
   14822       memcpy (rel, relocs, amt);
   14823     }
   14824   return ret;
   14825 }
   14826 
   14827 /* Adjust the value of any local symbols in opd sections.  */
   14828 
   14829 static int
   14830 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
   14831 			      const char *name ATTRIBUTE_UNUSED,
   14832 			      Elf_Internal_Sym *elfsym,
   14833 			      asection *input_sec,
   14834 			      struct elf_link_hash_entry *h)
   14835 {
   14836   struct _opd_sec_data *opd;
   14837   long adjust;
   14838   bfd_vma value;
   14839 
   14840   if (h != NULL)
   14841     return 1;
   14842 
   14843   opd = get_opd_info (input_sec);
   14844   if (opd == NULL || opd->adjust == NULL)
   14845     return 1;
   14846 
   14847   value = elfsym->st_value - input_sec->output_offset;
   14848   if (!info->relocatable)
   14849     value -= input_sec->output_section->vma;
   14850 
   14851   adjust = opd->adjust[value / 8];
   14852   if (adjust == -1)
   14853     return 2;
   14854 
   14855   elfsym->st_value += adjust;
   14856   return 1;
   14857 }
   14858 
   14859 /* Finish up dynamic symbol handling.  We set the contents of various
   14860    dynamic sections here.  */
   14861 
   14862 static bfd_boolean
   14863 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
   14864 				 struct bfd_link_info *info,
   14865 				 struct elf_link_hash_entry *h,
   14866 				 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
   14867 {
   14868   struct ppc_link_hash_table *htab;
   14869   struct plt_entry *ent;
   14870   Elf_Internal_Rela rela;
   14871   bfd_byte *loc;
   14872 
   14873   htab = ppc_hash_table (info);
   14874   if (htab == NULL)
   14875     return FALSE;
   14876 
   14877   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
   14878     if (ent->plt.offset != (bfd_vma) -1)
   14879       {
   14880 	/* This symbol has an entry in the procedure linkage
   14881 	   table.  Set it up.  */
   14882 	if (!htab->elf.dynamic_sections_created
   14883 	    || h->dynindx == -1)
   14884 	  {
   14885 	    BFD_ASSERT (h->type == STT_GNU_IFUNC
   14886 			&& h->def_regular
   14887 			&& (h->root.type == bfd_link_hash_defined
   14888 			    || h->root.type == bfd_link_hash_defweak));
   14889 	    rela.r_offset = (htab->elf.iplt->output_section->vma
   14890 			     + htab->elf.iplt->output_offset
   14891 			     + ent->plt.offset);
   14892 	    if (htab->opd_abi)
   14893 	      rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
   14894 	    else
   14895 	      rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
   14896 	    rela.r_addend = (h->root.u.def.value
   14897 			     + h->root.u.def.section->output_offset
   14898 			     + h->root.u.def.section->output_section->vma
   14899 			     + ent->addend);
   14900 	    loc = (htab->elf.irelplt->contents
   14901 		   + (htab->elf.irelplt->reloc_count++
   14902 		      * sizeof (Elf64_External_Rela)));
   14903 	  }
   14904 	else
   14905 	  {
   14906 	    rela.r_offset = (htab->elf.splt->output_section->vma
   14907 			     + htab->elf.splt->output_offset
   14908 			     + ent->plt.offset);
   14909 	    rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
   14910 	    rela.r_addend = ent->addend;
   14911 	    loc = (htab->elf.srelplt->contents
   14912 		   + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
   14913 		      / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
   14914 	  }
   14915 	bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
   14916 
   14917 	if (!htab->opd_abi)
   14918 	  {
   14919 	    if (!h->def_regular)
   14920 	      {
   14921 		/* Mark the symbol as undefined, rather than as
   14922 		   defined in glink.  Leave the value if there were
   14923 		   any relocations where pointer equality matters
   14924 		   (this is a clue for the dynamic linker, to make
   14925 		   function pointer comparisons work between an
   14926 		   application and shared library), otherwise set it
   14927 		   to zero.  */
   14928 		sym->st_shndx = SHN_UNDEF;
   14929 		if (!h->pointer_equality_needed)
   14930 		  sym->st_value = 0;
   14931 		else if (!h->ref_regular_nonweak)
   14932 		  {
   14933 		    /* This breaks function pointer comparisons, but
   14934 		       that is better than breaking tests for a NULL
   14935 		       function pointer.  */
   14936 		    sym->st_value = 0;
   14937 		  }
   14938 	      }
   14939 	  }
   14940       }
   14941 
   14942   if (h->needs_copy)
   14943     {
   14944       /* This symbol needs a copy reloc.  Set it up.  */
   14945 
   14946       if (h->dynindx == -1
   14947 	  || (h->root.type != bfd_link_hash_defined
   14948 	      && h->root.type != bfd_link_hash_defweak)
   14949 	  || htab->relbss == NULL)
   14950 	abort ();
   14951 
   14952       rela.r_offset = (h->root.u.def.value
   14953 		       + h->root.u.def.section->output_section->vma
   14954 		       + h->root.u.def.section->output_offset);
   14955       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
   14956       rela.r_addend = 0;
   14957       loc = htab->relbss->contents;
   14958       loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
   14959       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
   14960     }
   14961 
   14962   return TRUE;
   14963 }
   14964 
   14965 /* Used to decide how to sort relocs in an optimal manner for the
   14966    dynamic linker, before writing them out.  */
   14967 
   14968 static enum elf_reloc_type_class
   14969 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
   14970 			    const asection *rel_sec,
   14971 			    const Elf_Internal_Rela *rela)
   14972 {
   14973   enum elf_ppc64_reloc_type r_type;
   14974   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   14975 
   14976   if (rel_sec == htab->elf.irelplt)
   14977     return reloc_class_ifunc;
   14978 
   14979   r_type = ELF64_R_TYPE (rela->r_info);
   14980   switch (r_type)
   14981     {
   14982     case R_PPC64_RELATIVE:
   14983       return reloc_class_relative;
   14984     case R_PPC64_JMP_SLOT:
   14985       return reloc_class_plt;
   14986     case R_PPC64_COPY:
   14987       return reloc_class_copy;
   14988     default:
   14989       return reloc_class_normal;
   14990     }
   14991 }
   14992 
   14993 /* Finish up the dynamic sections.  */
   14994 
   14995 static bfd_boolean
   14996 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
   14997 				   struct bfd_link_info *info)
   14998 {
   14999   struct ppc_link_hash_table *htab;
   15000   bfd *dynobj;
   15001   asection *sdyn;
   15002 
   15003   htab = ppc_hash_table (info);
   15004   if (htab == NULL)
   15005     return FALSE;
   15006 
   15007   dynobj = htab->elf.dynobj;
   15008   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
   15009 
   15010   if (htab->elf.dynamic_sections_created)
   15011     {
   15012       Elf64_External_Dyn *dyncon, *dynconend;
   15013 
   15014       if (sdyn == NULL || htab->elf.sgot == NULL)
   15015 	abort ();
   15016 
   15017       dyncon = (Elf64_External_Dyn *) sdyn->contents;
   15018       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
   15019       for (; dyncon < dynconend; dyncon++)
   15020 	{
   15021 	  Elf_Internal_Dyn dyn;
   15022 	  asection *s;
   15023 
   15024 	  bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
   15025 
   15026 	  switch (dyn.d_tag)
   15027 	    {
   15028 	    default:
   15029 	      continue;
   15030 
   15031 	    case DT_PPC64_GLINK:
   15032 	      s = htab->glink;
   15033 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   15034 	      /* We stupidly defined DT_PPC64_GLINK to be the start
   15035 		 of glink rather than the first entry point, which is
   15036 		 what ld.so needs, and now have a bigger stub to
   15037 		 support automatic multiple TOCs.  */
   15038 	      dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 8 * 4;
   15039 	      break;
   15040 
   15041 	    case DT_PPC64_OPD:
   15042 	      s = bfd_get_section_by_name (output_bfd, ".opd");
   15043 	      if (s == NULL)
   15044 		continue;
   15045 	      dyn.d_un.d_ptr = s->vma;
   15046 	      break;
   15047 
   15048 	    case DT_PPC64_OPT:
   15049 	      if (htab->do_multi_toc && htab->multi_toc_needed)
   15050 		dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
   15051 	      break;
   15052 
   15053 	    case DT_PPC64_OPDSZ:
   15054 	      s = bfd_get_section_by_name (output_bfd, ".opd");
   15055 	      if (s == NULL)
   15056 		continue;
   15057 	      dyn.d_un.d_val = s->size;
   15058 	      break;
   15059 
   15060 	    case DT_PLTGOT:
   15061 	      s = htab->elf.splt;
   15062 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   15063 	      break;
   15064 
   15065 	    case DT_JMPREL:
   15066 	      s = htab->elf.srelplt;
   15067 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   15068 	      break;
   15069 
   15070 	    case DT_PLTRELSZ:
   15071 	      dyn.d_un.d_val = htab->elf.srelplt->size;
   15072 	      break;
   15073 
   15074 	    case DT_RELASZ:
   15075 	      /* Don't count procedure linkage table relocs in the
   15076 		 overall reloc count.  */
   15077 	      s = htab->elf.srelplt;
   15078 	      if (s == NULL)
   15079 		continue;
   15080 	      dyn.d_un.d_val -= s->size;
   15081 	      break;
   15082 
   15083 	    case DT_RELA:
   15084 	      /* We may not be using the standard ELF linker script.
   15085 		 If .rela.plt is the first .rela section, we adjust
   15086 		 DT_RELA to not include it.  */
   15087 	      s = htab->elf.srelplt;
   15088 	      if (s == NULL)
   15089 		continue;
   15090 	      if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
   15091 		continue;
   15092 	      dyn.d_un.d_ptr += s->size;
   15093 	      break;
   15094 	    }
   15095 
   15096 	  bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
   15097 	}
   15098     }
   15099 
   15100   if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0)
   15101     {
   15102       /* Fill in the first entry in the global offset table.
   15103 	 We use it to hold the link-time TOCbase.  */
   15104       bfd_put_64 (output_bfd,
   15105 		  elf_gp (output_bfd) + TOC_BASE_OFF,
   15106 		  htab->elf.sgot->contents);
   15107 
   15108       /* Set .got entry size.  */
   15109       elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
   15110     }
   15111 
   15112   if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
   15113     {
   15114       /* Set .plt entry size.  */
   15115       elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
   15116 	= PLT_ENTRY_SIZE (htab);
   15117     }
   15118 
   15119   /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
   15120      brlt ourselves if emitrelocations.  */
   15121   if (htab->brlt != NULL
   15122       && htab->brlt->reloc_count != 0
   15123       && !_bfd_elf_link_output_relocs (output_bfd,
   15124 				       htab->brlt,
   15125 				       elf_section_data (htab->brlt)->rela.hdr,
   15126 				       elf_section_data (htab->brlt)->relocs,
   15127 				       NULL))
   15128     return FALSE;
   15129 
   15130   if (htab->glink != NULL
   15131       && htab->glink->reloc_count != 0
   15132       && !_bfd_elf_link_output_relocs (output_bfd,
   15133 				       htab->glink,
   15134 				       elf_section_data (htab->glink)->rela.hdr,
   15135 				       elf_section_data (htab->glink)->relocs,
   15136 				       NULL))
   15137     return FALSE;
   15138 
   15139   if (htab->glink_eh_frame != NULL
   15140       && htab->glink_eh_frame->size != 0)
   15141     {
   15142       bfd_vma val;
   15143       bfd_byte *p;
   15144       asection *stub_sec;
   15145 
   15146       p = htab->glink_eh_frame->contents + sizeof (glink_eh_frame_cie);
   15147       for (stub_sec = htab->params->stub_bfd->sections;
   15148 	   stub_sec != NULL;
   15149 	   stub_sec = stub_sec->next)
   15150 	if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
   15151 	  {
   15152 	    /* FDE length.  */
   15153 	    p += 4;
   15154 	    /* CIE pointer.  */
   15155 	    p += 4;
   15156 	    /* Offset to stub section.  */
   15157 	    val = (stub_sec->output_section->vma
   15158 		   + stub_sec->output_offset);
   15159 	    val -= (htab->glink_eh_frame->output_section->vma
   15160 		    + htab->glink_eh_frame->output_offset
   15161 		    + (p - htab->glink_eh_frame->contents));
   15162 	    if (val + 0x80000000 > 0xffffffff)
   15163 	      {
   15164 		info->callbacks->einfo
   15165 		  (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
   15166 		   stub_sec->name);
   15167 		return FALSE;
   15168 	      }
   15169 	    bfd_put_32 (dynobj, val, p);
   15170 	    p += 4;
   15171 	    /* stub section size.  */
   15172 	    p += 4;
   15173 	    /* Augmentation.  */
   15174 	    p += 1;
   15175 	    /* Pad.  */
   15176 	    p += 7;
   15177 	  }
   15178       if (htab->glink != NULL && htab->glink->size != 0)
   15179 	{
   15180 	  /* FDE length.  */
   15181 	  p += 4;
   15182 	  /* CIE pointer.  */
   15183 	  p += 4;
   15184 	  /* Offset to .glink.  */
   15185 	  val = (htab->glink->output_section->vma
   15186 		 + htab->glink->output_offset
   15187 		 + 8);
   15188 	  val -= (htab->glink_eh_frame->output_section->vma
   15189 		  + htab->glink_eh_frame->output_offset
   15190 		  + (p - htab->glink_eh_frame->contents));
   15191 	  if (val + 0x80000000 > 0xffffffff)
   15192 	    {
   15193 	      info->callbacks->einfo
   15194 		(_("%P: %s offset too large for .eh_frame sdata4 encoding"),
   15195 		 htab->glink->name);
   15196 	      return FALSE;
   15197 	    }
   15198 	  bfd_put_32 (dynobj, val, p);
   15199 	  p += 4;
   15200 	  /* .glink size.  */
   15201 	  p += 4;
   15202 	  /* Augmentation.  */
   15203 	  p += 1;
   15204 	  /* Ops.  */
   15205 	  p += 7;
   15206 	}
   15207 
   15208       if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
   15209 	  && !_bfd_elf_write_section_eh_frame (output_bfd, info,
   15210 					       htab->glink_eh_frame,
   15211 					       htab->glink_eh_frame->contents))
   15212 	return FALSE;
   15213     }
   15214 
   15215   /* We need to handle writing out multiple GOT sections ourselves,
   15216      since we didn't add them to DYNOBJ.  We know dynobj is the first
   15217      bfd.  */
   15218   while ((dynobj = dynobj->link.next) != NULL)
   15219     {
   15220       asection *s;
   15221 
   15222       if (!is_ppc64_elf (dynobj))
   15223 	continue;
   15224 
   15225       s = ppc64_elf_tdata (dynobj)->got;
   15226       if (s != NULL
   15227 	  && s->size != 0
   15228 	  && s->output_section != bfd_abs_section_ptr
   15229 	  && !bfd_set_section_contents (output_bfd, s->output_section,
   15230 					s->contents, s->output_offset,
   15231 					s->size))
   15232 	return FALSE;
   15233       s = ppc64_elf_tdata (dynobj)->relgot;
   15234       if (s != NULL
   15235 	  && s->size != 0
   15236 	  && s->output_section != bfd_abs_section_ptr
   15237 	  && !bfd_set_section_contents (output_bfd, s->output_section,
   15238 					s->contents, s->output_offset,
   15239 					s->size))
   15240 	return FALSE;
   15241     }
   15242 
   15243   return TRUE;
   15244 }
   15245 
   15246 #include "elf64-target.h"
   15247 
   15248 /* FreeBSD support */
   15249 
   15250 #undef  TARGET_LITTLE_SYM
   15251 #undef  TARGET_LITTLE_NAME
   15252 
   15253 #undef  TARGET_BIG_SYM
   15254 #define TARGET_BIG_SYM	powerpc_elf64_fbsd_vec
   15255 #undef  TARGET_BIG_NAME
   15256 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
   15257 
   15258 #undef  ELF_OSABI
   15259 #define	ELF_OSABI       ELFOSABI_FREEBSD
   15260 
   15261 #undef  elf64_bed
   15262 #define elf64_bed	elf64_powerpc_fbsd_bed
   15263 
   15264 #include "elf64-target.h"
   15265 
   15266