Home | History | Annotate | Line # | Download | only in bfd
elf64-ppc.c revision 1.3.2.1
      1 /* PowerPC64-specific support for 64-bit ELF.
      2    Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
      3    2009, 2010, 2011, 2012 Free Software Foundation, Inc.
      4    Written by Linus Nordberg, Swox AB <info (at) swox.com>,
      5    based on elf32-ppc.c by Ian Lance Taylor.
      6    Largely rewritten by Alan Modra.
      7 
      8    This file is part of BFD, the Binary File Descriptor library.
      9 
     10    This program is free software; you can redistribute it and/or modify
     11    it under the terms of the GNU General Public License as published by
     12    the Free Software Foundation; either version 3 of the License, or
     13    (at your option) any later version.
     14 
     15    This program is distributed in the hope that it will be useful,
     16    but WITHOUT ANY WARRANTY; without even the implied warranty of
     17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     18    GNU General Public License for more details.
     19 
     20    You should have received a copy of the GNU General Public License along
     21    with this program; if not, write to the Free Software Foundation, Inc.,
     22    51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
     23 
     24 
     25 /* The 64-bit PowerPC ELF ABI may be found at
     26    http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
     27    http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html  */
     28 
     29 #include "sysdep.h"
     30 #include <stdarg.h>
     31 #include "bfd.h"
     32 #include "bfdlink.h"
     33 #include "libbfd.h"
     34 #include "elf-bfd.h"
     35 #include "elf/ppc64.h"
     36 #include "elf64-ppc.h"
     37 #include "dwarf2.h"
     38 
     39 static bfd_reloc_status_type ppc64_elf_ha_reloc
     40   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     41 static bfd_reloc_status_type ppc64_elf_branch_reloc
     42   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     43 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
     44   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     45 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
     46   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     47 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
     48   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     49 static bfd_reloc_status_type ppc64_elf_toc_reloc
     50   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     51 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
     52   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     53 static bfd_reloc_status_type ppc64_elf_toc64_reloc
     54   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     55 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
     56   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     57 static bfd_vma opd_entry_value
     58   (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
     59 
     60 #define TARGET_LITTLE_SYM	bfd_elf64_powerpcle_vec
     61 #define TARGET_LITTLE_NAME	"elf64-powerpcle"
     62 #define TARGET_BIG_SYM		bfd_elf64_powerpc_vec
     63 #define TARGET_BIG_NAME		"elf64-powerpc"
     64 #define ELF_ARCH		bfd_arch_powerpc
     65 #define ELF_TARGET_ID		PPC64_ELF_DATA
     66 #define ELF_MACHINE_CODE	EM_PPC64
     67 #define ELF_MAXPAGESIZE		0x10000
     68 #define ELF_COMMONPAGESIZE	0x1000
     69 #define elf_info_to_howto	ppc64_elf_info_to_howto
     70 
     71 #define elf_backend_want_got_sym 0
     72 #define elf_backend_want_plt_sym 0
     73 #define elf_backend_plt_alignment 3
     74 #define elf_backend_plt_not_loaded 1
     75 #define elf_backend_got_header_size 8
     76 #define elf_backend_can_gc_sections 1
     77 #define elf_backend_can_refcount 1
     78 #define elf_backend_rela_normal 1
     79 #define elf_backend_default_execstack 0
     80 
     81 #define bfd_elf64_mkobject		      ppc64_elf_mkobject
     82 #define bfd_elf64_bfd_reloc_type_lookup	      ppc64_elf_reloc_type_lookup
     83 #define bfd_elf64_bfd_reloc_name_lookup	      ppc64_elf_reloc_name_lookup
     84 #define bfd_elf64_bfd_merge_private_bfd_data  _bfd_generic_verify_endian_match
     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_bfd_link_hash_table_free    ppc64_elf_link_hash_table_free
     88 #define bfd_elf64_get_synthetic_symtab	      ppc64_elf_get_synthetic_symtab
     89 #define bfd_elf64_bfd_link_just_syms	      ppc64_elf_link_just_syms
     90 
     91 #define elf_backend_object_p		      ppc64_elf_object_p
     92 #define elf_backend_grok_prstatus	      ppc64_elf_grok_prstatus
     93 #define elf_backend_grok_psinfo		      ppc64_elf_grok_psinfo
     94 #define elf_backend_write_core_note	      ppc64_elf_write_core_note
     95 #define elf_backend_create_dynamic_sections   ppc64_elf_create_dynamic_sections
     96 #define elf_backend_copy_indirect_symbol      ppc64_elf_copy_indirect_symbol
     97 #define elf_backend_add_symbol_hook	      ppc64_elf_add_symbol_hook
     98 #define elf_backend_check_directives	      ppc64_elf_process_dot_syms
     99 #define elf_backend_as_needed_cleanup	      ppc64_elf_as_needed_cleanup
    100 #define elf_backend_archive_symbol_lookup     ppc64_elf_archive_symbol_lookup
    101 #define elf_backend_check_relocs	      ppc64_elf_check_relocs
    102 #define elf_backend_gc_keep		      ppc64_elf_gc_keep
    103 #define elf_backend_gc_mark_dynamic_ref       ppc64_elf_gc_mark_dynamic_ref
    104 #define elf_backend_gc_mark_hook	      ppc64_elf_gc_mark_hook
    105 #define elf_backend_gc_sweep_hook	      ppc64_elf_gc_sweep_hook
    106 #define elf_backend_adjust_dynamic_symbol     ppc64_elf_adjust_dynamic_symbol
    107 #define elf_backend_hide_symbol		      ppc64_elf_hide_symbol
    108 #define elf_backend_maybe_function_sym	      ppc64_elf_maybe_function_sym
    109 #define elf_backend_always_size_sections      ppc64_elf_func_desc_adjust
    110 #define elf_backend_size_dynamic_sections     ppc64_elf_size_dynamic_sections
    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_post_process_headers      _bfd_elf_set_osabi
    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 24
    127 
    128 /* The initial size of the plt reserved for the dynamic linker.  */
    129 #define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
    130 
    131 /* TOC base pointers offset from start of TOC.  */
    132 #define TOC_BASE_OFF	0x8000
    133 
    134 /* Offset of tp and dtp pointers from start of TLS block.  */
    135 #define TP_OFFSET	0x7000
    136 #define DTP_OFFSET	0x8000
    137 
    138 /* .plt call stub instructions.  The normal stub is like this, but
    139    sometimes the .plt entry crosses a 64k boundary and we need to
    140    insert an addi to adjust r12.  */
    141 #define PLT_CALL_STUB_SIZE (7*4)
    142 #define ADDIS_R12_R2	0x3d820000	/* addis %r12,%r2,xxx@ha     */
    143 #define STD_R2_40R1	0xf8410028	/* std	 %r2,40(%r1)	     */
    144 #define LD_R11_0R12	0xe96c0000	/* ld	 %r11,xxx+0@l(%r12)  */
    145 #define MTCTR_R11	0x7d6903a6	/* mtctr %r11		     */
    146 #define LD_R2_0R12	0xe84c0000	/* ld	 %r2,xxx+8@l(%r12)   */
    147 					/* ld	 %r11,xxx+16@l(%r12) */
    148 #define BCTR		0x4e800420	/* bctr			     */
    149 
    150 
    151 #define ADDIS_R12_R12	0x3d8c0000	/* addis %r12,%r12,off@ha  */
    152 #define ADDI_R12_R12	0x398c0000	/* addi %r12,%r12,off@l  */
    153 #define ADDIS_R2_R2	0x3c420000	/* addis %r2,%r2,off@ha  */
    154 #define ADDI_R2_R2	0x38420000	/* addi  %r2,%r2,off@l   */
    155 
    156 #define XOR_R11_R11_R11	0x7d6b5a78	/* xor   %r11,%r11,%r11  */
    157 #define ADD_R12_R12_R11	0x7d8c5a14	/* add   %r12,%r12,%r11  */
    158 #define ADD_R2_R2_R11	0x7c425a14	/* add   %r2,%r2,%r11    */
    159 #define CMPLDI_R2_0	0x28220000	/* cmpldi %r2,0          */
    160 #define BNECTR		0x4ca20420	/* bnectr+               */
    161 #define BNECTR_P4	0x4ce20420	/* bnectr+               */
    162 
    163 #define LD_R11_0R2	0xe9620000	/* ld	 %r11,xxx+0(%r2) */
    164 #define LD_R2_0R2	0xe8420000	/* ld	 %r2,xxx+0(%r2)  */
    165 
    166 #define LD_R2_40R1	0xe8410028	/* ld    %r2,40(%r1)     */
    167 
    168 /* glink call stub instructions.  We enter with the index in R0.  */
    169 #define GLINK_CALL_STUB_SIZE (16*4)
    170 					/* 0:				*/
    171 					/*  .quad plt0-1f		*/
    172 					/* __glink:			*/
    173 #define MFLR_R12	0x7d8802a6	/*  mflr %12			*/
    174 #define BCL_20_31	0x429f0005	/*  bcl 20,31,1f		*/
    175 					/* 1:				*/
    176 #define MFLR_R11	0x7d6802a6	/*  mflr %11			*/
    177 #define LD_R2_M16R11	0xe84bfff0	/*  ld %2,(0b-1b)(%11)		*/
    178 #define MTLR_R12	0x7d8803a6	/*  mtlr %12			*/
    179 #define ADD_R12_R2_R11	0x7d825a14	/*  add %12,%2,%11		*/
    180 					/*  ld %11,0(%12)		*/
    181 					/*  ld %2,8(%12)		*/
    182 					/*  mtctr %11			*/
    183 					/*  ld %11,16(%12)		*/
    184 					/*  bctr			*/
    185 
    186 /* Pad with this.  */
    187 #define NOP		0x60000000
    188 
    189 /* Some other nops.  */
    190 #define CROR_151515	0x4def7b82
    191 #define CROR_313131	0x4ffffb82
    192 
    193 /* .glink entries for the first 32k functions are two instructions.  */
    194 #define LI_R0_0		0x38000000	/* li    %r0,0		*/
    195 #define B_DOT		0x48000000	/* b     .		*/
    196 
    197 /* After that, we need two instructions to load the index, followed by
    198    a branch.  */
    199 #define LIS_R0_0	0x3c000000	/* lis   %r0,0		*/
    200 #define ORI_R0_R0_0	0x60000000	/* ori	 %r0,%r0,0	*/
    201 
    202 /* Instructions used by the save and restore reg functions.  */
    203 #define STD_R0_0R1	0xf8010000	/* std   %r0,0(%r1)	*/
    204 #define STD_R0_0R12	0xf80c0000	/* std   %r0,0(%r12)	*/
    205 #define LD_R0_0R1	0xe8010000	/* ld    %r0,0(%r1)	*/
    206 #define LD_R0_0R12	0xe80c0000	/* ld    %r0,0(%r12)	*/
    207 #define STFD_FR0_0R1	0xd8010000	/* stfd  %fr0,0(%r1)	*/
    208 #define LFD_FR0_0R1	0xc8010000	/* lfd   %fr0,0(%r1)	*/
    209 #define LI_R12_0	0x39800000	/* li    %r12,0		*/
    210 #define STVX_VR0_R12_R0	0x7c0c01ce	/* stvx  %v0,%r12,%r0	*/
    211 #define LVX_VR0_R12_R0	0x7c0c00ce	/* lvx   %v0,%r12,%r0	*/
    212 #define MTLR_R0		0x7c0803a6	/* mtlr  %r0		*/
    213 #define BLR		0x4e800020	/* blr			*/
    214 
    215 /* Since .opd is an array of descriptors and each entry will end up
    216    with identical R_PPC64_RELATIVE relocs, there is really no need to
    217    propagate .opd relocs;  The dynamic linker should be taught to
    218    relocate .opd without reloc entries.  */
    219 #ifndef NO_OPD_RELOCS
    220 #define NO_OPD_RELOCS 0
    221 #endif
    222 
    223 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
    225 
    226 /* Relocation HOWTO's.  */
    227 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
    228 
    229 static reloc_howto_type ppc64_elf_howto_raw[] = {
    230   /* This reloc does nothing.  */
    231   HOWTO (R_PPC64_NONE,		/* type */
    232 	 0,			/* rightshift */
    233 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    234 	 32,			/* bitsize */
    235 	 FALSE,			/* pc_relative */
    236 	 0,			/* bitpos */
    237 	 complain_overflow_dont, /* complain_on_overflow */
    238 	 bfd_elf_generic_reloc,	/* special_function */
    239 	 "R_PPC64_NONE",	/* name */
    240 	 FALSE,			/* partial_inplace */
    241 	 0,			/* src_mask */
    242 	 0,			/* dst_mask */
    243 	 FALSE),		/* pcrel_offset */
    244 
    245   /* A standard 32 bit relocation.  */
    246   HOWTO (R_PPC64_ADDR32,	/* type */
    247 	 0,			/* rightshift */
    248 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    249 	 32,			/* bitsize */
    250 	 FALSE,			/* pc_relative */
    251 	 0,			/* bitpos */
    252 	 complain_overflow_bitfield, /* complain_on_overflow */
    253 	 bfd_elf_generic_reloc,	/* special_function */
    254 	 "R_PPC64_ADDR32",	/* name */
    255 	 FALSE,			/* partial_inplace */
    256 	 0,			/* src_mask */
    257 	 0xffffffff,		/* dst_mask */
    258 	 FALSE),		/* pcrel_offset */
    259 
    260   /* An absolute 26 bit branch; the lower two bits must be zero.
    261      FIXME: we don't check that, we just clear them.  */
    262   HOWTO (R_PPC64_ADDR24,	/* type */
    263 	 0,			/* rightshift */
    264 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    265 	 26,			/* bitsize */
    266 	 FALSE,			/* pc_relative */
    267 	 0,			/* bitpos */
    268 	 complain_overflow_bitfield, /* complain_on_overflow */
    269 	 bfd_elf_generic_reloc,	/* special_function */
    270 	 "R_PPC64_ADDR24",	/* name */
    271 	 FALSE,			/* partial_inplace */
    272 	 0,			/* src_mask */
    273 	 0x03fffffc,		/* dst_mask */
    274 	 FALSE),		/* pcrel_offset */
    275 
    276   /* A standard 16 bit relocation.  */
    277   HOWTO (R_PPC64_ADDR16,	/* type */
    278 	 0,			/* rightshift */
    279 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    280 	 16,			/* bitsize */
    281 	 FALSE,			/* pc_relative */
    282 	 0,			/* bitpos */
    283 	 complain_overflow_bitfield, /* complain_on_overflow */
    284 	 bfd_elf_generic_reloc,	/* special_function */
    285 	 "R_PPC64_ADDR16",	/* name */
    286 	 FALSE,			/* partial_inplace */
    287 	 0,			/* src_mask */
    288 	 0xffff,		/* dst_mask */
    289 	 FALSE),		/* pcrel_offset */
    290 
    291   /* A 16 bit relocation without overflow.  */
    292   HOWTO (R_PPC64_ADDR16_LO,	/* type */
    293 	 0,			/* rightshift */
    294 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    295 	 16,			/* bitsize */
    296 	 FALSE,			/* pc_relative */
    297 	 0,			/* bitpos */
    298 	 complain_overflow_dont,/* complain_on_overflow */
    299 	 bfd_elf_generic_reloc,	/* special_function */
    300 	 "R_PPC64_ADDR16_LO",	/* name */
    301 	 FALSE,			/* partial_inplace */
    302 	 0,			/* src_mask */
    303 	 0xffff,		/* dst_mask */
    304 	 FALSE),		/* pcrel_offset */
    305 
    306   /* Bits 16-31 of an address.  */
    307   HOWTO (R_PPC64_ADDR16_HI,	/* type */
    308 	 16,			/* rightshift */
    309 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    310 	 16,			/* bitsize */
    311 	 FALSE,			/* pc_relative */
    312 	 0,			/* bitpos */
    313 	 complain_overflow_dont, /* complain_on_overflow */
    314 	 bfd_elf_generic_reloc,	/* special_function */
    315 	 "R_PPC64_ADDR16_HI",	/* name */
    316 	 FALSE,			/* partial_inplace */
    317 	 0,			/* src_mask */
    318 	 0xffff,		/* dst_mask */
    319 	 FALSE),		/* pcrel_offset */
    320 
    321   /* Bits 16-31 of an address, plus 1 if the contents of the low 16
    322      bits, treated as a signed number, is negative.  */
    323   HOWTO (R_PPC64_ADDR16_HA,	/* type */
    324 	 16,			/* rightshift */
    325 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    326 	 16,			/* bitsize */
    327 	 FALSE,			/* pc_relative */
    328 	 0,			/* bitpos */
    329 	 complain_overflow_dont, /* complain_on_overflow */
    330 	 ppc64_elf_ha_reloc,	/* special_function */
    331 	 "R_PPC64_ADDR16_HA",	/* name */
    332 	 FALSE,			/* partial_inplace */
    333 	 0,			/* src_mask */
    334 	 0xffff,		/* dst_mask */
    335 	 FALSE),		/* pcrel_offset */
    336 
    337   /* An absolute 16 bit branch; the lower two bits must be zero.
    338      FIXME: we don't check that, we just clear them.  */
    339   HOWTO (R_PPC64_ADDR14,	/* type */
    340 	 0,			/* rightshift */
    341 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    342 	 16,			/* bitsize */
    343 	 FALSE,			/* pc_relative */
    344 	 0,			/* bitpos */
    345 	 complain_overflow_bitfield, /* complain_on_overflow */
    346 	 ppc64_elf_branch_reloc, /* special_function */
    347 	 "R_PPC64_ADDR14",	/* name */
    348 	 FALSE,			/* partial_inplace */
    349 	 0,			/* src_mask */
    350 	 0x0000fffc,		/* dst_mask */
    351 	 FALSE),		/* pcrel_offset */
    352 
    353   /* An absolute 16 bit branch, for which bit 10 should be set to
    354      indicate that the branch is expected to be taken.  The lower two
    355      bits must be zero.  */
    356   HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
    357 	 0,			/* rightshift */
    358 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    359 	 16,			/* bitsize */
    360 	 FALSE,			/* pc_relative */
    361 	 0,			/* bitpos */
    362 	 complain_overflow_bitfield, /* complain_on_overflow */
    363 	 ppc64_elf_brtaken_reloc, /* special_function */
    364 	 "R_PPC64_ADDR14_BRTAKEN",/* name */
    365 	 FALSE,			/* partial_inplace */
    366 	 0,			/* src_mask */
    367 	 0x0000fffc,		/* dst_mask */
    368 	 FALSE),		/* pcrel_offset */
    369 
    370   /* An absolute 16 bit branch, for which bit 10 should be set to
    371      indicate that the branch is not expected to be taken.  The lower
    372      two bits must be zero.  */
    373   HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
    374 	 0,			/* rightshift */
    375 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    376 	 16,			/* bitsize */
    377 	 FALSE,			/* pc_relative */
    378 	 0,			/* bitpos */
    379 	 complain_overflow_bitfield, /* complain_on_overflow */
    380 	 ppc64_elf_brtaken_reloc, /* special_function */
    381 	 "R_PPC64_ADDR14_BRNTAKEN",/* name */
    382 	 FALSE,			/* partial_inplace */
    383 	 0,			/* src_mask */
    384 	 0x0000fffc,		/* dst_mask */
    385 	 FALSE),		/* pcrel_offset */
    386 
    387   /* A relative 26 bit branch; the lower two bits must be zero.  */
    388   HOWTO (R_PPC64_REL24,		/* type */
    389 	 0,			/* rightshift */
    390 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    391 	 26,			/* bitsize */
    392 	 TRUE,			/* pc_relative */
    393 	 0,			/* bitpos */
    394 	 complain_overflow_signed, /* complain_on_overflow */
    395 	 ppc64_elf_branch_reloc, /* special_function */
    396 	 "R_PPC64_REL24",	/* name */
    397 	 FALSE,			/* partial_inplace */
    398 	 0,			/* src_mask */
    399 	 0x03fffffc,		/* dst_mask */
    400 	 TRUE),			/* pcrel_offset */
    401 
    402   /* A relative 16 bit branch; the lower two bits must be zero.  */
    403   HOWTO (R_PPC64_REL14,		/* type */
    404 	 0,			/* rightshift */
    405 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    406 	 16,			/* bitsize */
    407 	 TRUE,			/* pc_relative */
    408 	 0,			/* bitpos */
    409 	 complain_overflow_signed, /* complain_on_overflow */
    410 	 ppc64_elf_branch_reloc, /* special_function */
    411 	 "R_PPC64_REL14",	/* name */
    412 	 FALSE,			/* partial_inplace */
    413 	 0,			/* src_mask */
    414 	 0x0000fffc,		/* dst_mask */
    415 	 TRUE),			/* pcrel_offset */
    416 
    417   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
    418      the branch is expected to be taken.  The lower two bits must be
    419      zero.  */
    420   HOWTO (R_PPC64_REL14_BRTAKEN,	/* type */
    421 	 0,			/* rightshift */
    422 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    423 	 16,			/* bitsize */
    424 	 TRUE,			/* pc_relative */
    425 	 0,			/* bitpos */
    426 	 complain_overflow_signed, /* complain_on_overflow */
    427 	 ppc64_elf_brtaken_reloc, /* special_function */
    428 	 "R_PPC64_REL14_BRTAKEN", /* name */
    429 	 FALSE,			/* partial_inplace */
    430 	 0,			/* src_mask */
    431 	 0x0000fffc,		/* dst_mask */
    432 	 TRUE),			/* pcrel_offset */
    433 
    434   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
    435      the branch is not expected to be taken.  The lower two bits must
    436      be zero.  */
    437   HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
    438 	 0,			/* rightshift */
    439 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    440 	 16,			/* bitsize */
    441 	 TRUE,			/* pc_relative */
    442 	 0,			/* bitpos */
    443 	 complain_overflow_signed, /* complain_on_overflow */
    444 	 ppc64_elf_brtaken_reloc, /* special_function */
    445 	 "R_PPC64_REL14_BRNTAKEN",/* name */
    446 	 FALSE,			/* partial_inplace */
    447 	 0,			/* src_mask */
    448 	 0x0000fffc,		/* dst_mask */
    449 	 TRUE),			/* pcrel_offset */
    450 
    451   /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
    452      symbol.  */
    453   HOWTO (R_PPC64_GOT16,		/* type */
    454 	 0,			/* rightshift */
    455 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    456 	 16,			/* bitsize */
    457 	 FALSE,			/* pc_relative */
    458 	 0,			/* bitpos */
    459 	 complain_overflow_signed, /* complain_on_overflow */
    460 	 ppc64_elf_unhandled_reloc, /* special_function */
    461 	 "R_PPC64_GOT16",	/* name */
    462 	 FALSE,			/* partial_inplace */
    463 	 0,			/* src_mask */
    464 	 0xffff,		/* dst_mask */
    465 	 FALSE),		/* pcrel_offset */
    466 
    467   /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
    468      the symbol.  */
    469   HOWTO (R_PPC64_GOT16_LO,	/* type */
    470 	 0,			/* rightshift */
    471 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    472 	 16,			/* bitsize */
    473 	 FALSE,			/* pc_relative */
    474 	 0,			/* bitpos */
    475 	 complain_overflow_dont, /* complain_on_overflow */
    476 	 ppc64_elf_unhandled_reloc, /* special_function */
    477 	 "R_PPC64_GOT16_LO",	/* name */
    478 	 FALSE,			/* partial_inplace */
    479 	 0,			/* src_mask */
    480 	 0xffff,		/* dst_mask */
    481 	 FALSE),		/* pcrel_offset */
    482 
    483   /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
    484      the symbol.  */
    485   HOWTO (R_PPC64_GOT16_HI,	/* type */
    486 	 16,			/* rightshift */
    487 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    488 	 16,			/* bitsize */
    489 	 FALSE,			/* pc_relative */
    490 	 0,			/* bitpos */
    491 	 complain_overflow_dont,/* complain_on_overflow */
    492 	 ppc64_elf_unhandled_reloc, /* special_function */
    493 	 "R_PPC64_GOT16_HI",	/* name */
    494 	 FALSE,			/* partial_inplace */
    495 	 0,			/* src_mask */
    496 	 0xffff,		/* dst_mask */
    497 	 FALSE),		/* pcrel_offset */
    498 
    499   /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
    500      the symbol.  */
    501   HOWTO (R_PPC64_GOT16_HA,	/* type */
    502 	 16,			/* rightshift */
    503 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    504 	 16,			/* bitsize */
    505 	 FALSE,			/* pc_relative */
    506 	 0,			/* bitpos */
    507 	 complain_overflow_dont,/* complain_on_overflow */
    508 	 ppc64_elf_unhandled_reloc, /* special_function */
    509 	 "R_PPC64_GOT16_HA",	/* name */
    510 	 FALSE,			/* partial_inplace */
    511 	 0,			/* src_mask */
    512 	 0xffff,		/* dst_mask */
    513 	 FALSE),		/* pcrel_offset */
    514 
    515   /* This is used only by the dynamic linker.  The symbol should exist
    516      both in the object being run and in some shared library.  The
    517      dynamic linker copies the data addressed by the symbol from the
    518      shared library into the object, because the object being
    519      run has to have the data at some particular address.  */
    520   HOWTO (R_PPC64_COPY,		/* type */
    521 	 0,			/* rightshift */
    522 	 0,			/* this one is variable size */
    523 	 0,			/* bitsize */
    524 	 FALSE,			/* pc_relative */
    525 	 0,			/* bitpos */
    526 	 complain_overflow_dont, /* complain_on_overflow */
    527 	 ppc64_elf_unhandled_reloc, /* special_function */
    528 	 "R_PPC64_COPY",	/* name */
    529 	 FALSE,			/* partial_inplace */
    530 	 0,			/* src_mask */
    531 	 0,			/* dst_mask */
    532 	 FALSE),		/* pcrel_offset */
    533 
    534   /* Like R_PPC64_ADDR64, but used when setting global offset table
    535      entries.  */
    536   HOWTO (R_PPC64_GLOB_DAT,	/* type */
    537 	 0,			/* rightshift */
    538 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    539 	 64,			/* bitsize */
    540 	 FALSE,			/* pc_relative */
    541 	 0,			/* bitpos */
    542 	 complain_overflow_dont, /* complain_on_overflow */
    543 	 ppc64_elf_unhandled_reloc,  /* special_function */
    544 	 "R_PPC64_GLOB_DAT",	/* name */
    545 	 FALSE,			/* partial_inplace */
    546 	 0,			/* src_mask */
    547 	 ONES (64),		/* dst_mask */
    548 	 FALSE),		/* pcrel_offset */
    549 
    550   /* Created by the link editor.  Marks a procedure linkage table
    551      entry for a symbol.  */
    552   HOWTO (R_PPC64_JMP_SLOT,	/* type */
    553 	 0,			/* rightshift */
    554 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
    555 	 0,			/* bitsize */
    556 	 FALSE,			/* pc_relative */
    557 	 0,			/* bitpos */
    558 	 complain_overflow_dont, /* complain_on_overflow */
    559 	 ppc64_elf_unhandled_reloc, /* special_function */
    560 	 "R_PPC64_JMP_SLOT",	/* name */
    561 	 FALSE,			/* partial_inplace */
    562 	 0,			/* src_mask */
    563 	 0,			/* dst_mask */
    564 	 FALSE),		/* pcrel_offset */
    565 
    566   /* Used only by the dynamic linker.  When the object is run, this
    567      doubleword64 is set to the load address of the object, plus the
    568      addend.  */
    569   HOWTO (R_PPC64_RELATIVE,	/* type */
    570 	 0,			/* rightshift */
    571 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    572 	 64,			/* bitsize */
    573 	 FALSE,			/* pc_relative */
    574 	 0,			/* bitpos */
    575 	 complain_overflow_dont, /* complain_on_overflow */
    576 	 bfd_elf_generic_reloc,	/* special_function */
    577 	 "R_PPC64_RELATIVE",	/* name */
    578 	 FALSE,			/* partial_inplace */
    579 	 0,			/* src_mask */
    580 	 ONES (64),		/* dst_mask */
    581 	 FALSE),		/* pcrel_offset */
    582 
    583   /* Like R_PPC64_ADDR32, but may be unaligned.  */
    584   HOWTO (R_PPC64_UADDR32,	/* type */
    585 	 0,			/* rightshift */
    586 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    587 	 32,			/* bitsize */
    588 	 FALSE,			/* pc_relative */
    589 	 0,			/* bitpos */
    590 	 complain_overflow_bitfield, /* complain_on_overflow */
    591 	 bfd_elf_generic_reloc,	/* special_function */
    592 	 "R_PPC64_UADDR32",	/* name */
    593 	 FALSE,			/* partial_inplace */
    594 	 0,			/* src_mask */
    595 	 0xffffffff,		/* dst_mask */
    596 	 FALSE),		/* pcrel_offset */
    597 
    598   /* Like R_PPC64_ADDR16, but may be unaligned.  */
    599   HOWTO (R_PPC64_UADDR16,	/* type */
    600 	 0,			/* rightshift */
    601 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    602 	 16,			/* bitsize */
    603 	 FALSE,			/* pc_relative */
    604 	 0,			/* bitpos */
    605 	 complain_overflow_bitfield, /* complain_on_overflow */
    606 	 bfd_elf_generic_reloc,	/* special_function */
    607 	 "R_PPC64_UADDR16",	/* name */
    608 	 FALSE,			/* partial_inplace */
    609 	 0,			/* src_mask */
    610 	 0xffff,		/* dst_mask */
    611 	 FALSE),		/* pcrel_offset */
    612 
    613   /* 32-bit PC relative.  */
    614   HOWTO (R_PPC64_REL32,		/* type */
    615 	 0,			/* rightshift */
    616 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    617 	 32,			/* bitsize */
    618 	 TRUE,			/* pc_relative */
    619 	 0,			/* bitpos */
    620 	 /* FIXME: Verify.  Was complain_overflow_bitfield.  */
    621 	 complain_overflow_signed, /* complain_on_overflow */
    622 	 bfd_elf_generic_reloc,	/* special_function */
    623 	 "R_PPC64_REL32",	/* name */
    624 	 FALSE,			/* partial_inplace */
    625 	 0,			/* src_mask */
    626 	 0xffffffff,		/* dst_mask */
    627 	 TRUE),			/* pcrel_offset */
    628 
    629   /* 32-bit relocation to the symbol's procedure linkage table.  */
    630   HOWTO (R_PPC64_PLT32,		/* type */
    631 	 0,			/* rightshift */
    632 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    633 	 32,			/* bitsize */
    634 	 FALSE,			/* pc_relative */
    635 	 0,			/* bitpos */
    636 	 complain_overflow_bitfield, /* complain_on_overflow */
    637 	 ppc64_elf_unhandled_reloc, /* special_function */
    638 	 "R_PPC64_PLT32",	/* name */
    639 	 FALSE,			/* partial_inplace */
    640 	 0,			/* src_mask */
    641 	 0xffffffff,		/* dst_mask */
    642 	 FALSE),		/* pcrel_offset */
    643 
    644   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
    645      FIXME: R_PPC64_PLTREL32 not supported.  */
    646   HOWTO (R_PPC64_PLTREL32,	/* type */
    647 	 0,			/* rightshift */
    648 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    649 	 32,			/* bitsize */
    650 	 TRUE,			/* pc_relative */
    651 	 0,			/* bitpos */
    652 	 complain_overflow_signed, /* complain_on_overflow */
    653 	 bfd_elf_generic_reloc,	/* special_function */
    654 	 "R_PPC64_PLTREL32",	/* name */
    655 	 FALSE,			/* partial_inplace */
    656 	 0,			/* src_mask */
    657 	 0xffffffff,		/* dst_mask */
    658 	 TRUE),			/* pcrel_offset */
    659 
    660   /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
    661      the symbol.  */
    662   HOWTO (R_PPC64_PLT16_LO,	/* type */
    663 	 0,			/* rightshift */
    664 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    665 	 16,			/* bitsize */
    666 	 FALSE,			/* pc_relative */
    667 	 0,			/* bitpos */
    668 	 complain_overflow_dont, /* complain_on_overflow */
    669 	 ppc64_elf_unhandled_reloc, /* special_function */
    670 	 "R_PPC64_PLT16_LO",	/* name */
    671 	 FALSE,			/* partial_inplace */
    672 	 0,			/* src_mask */
    673 	 0xffff,		/* dst_mask */
    674 	 FALSE),		/* pcrel_offset */
    675 
    676   /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
    677      the symbol.  */
    678   HOWTO (R_PPC64_PLT16_HI,	/* type */
    679 	 16,			/* rightshift */
    680 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    681 	 16,			/* bitsize */
    682 	 FALSE,			/* pc_relative */
    683 	 0,			/* bitpos */
    684 	 complain_overflow_dont, /* complain_on_overflow */
    685 	 ppc64_elf_unhandled_reloc, /* special_function */
    686 	 "R_PPC64_PLT16_HI",	/* name */
    687 	 FALSE,			/* partial_inplace */
    688 	 0,			/* src_mask */
    689 	 0xffff,		/* dst_mask */
    690 	 FALSE),		/* pcrel_offset */
    691 
    692   /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
    693      the symbol.  */
    694   HOWTO (R_PPC64_PLT16_HA,	/* type */
    695 	 16,			/* rightshift */
    696 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    697 	 16,			/* bitsize */
    698 	 FALSE,			/* pc_relative */
    699 	 0,			/* bitpos */
    700 	 complain_overflow_dont, /* complain_on_overflow */
    701 	 ppc64_elf_unhandled_reloc, /* special_function */
    702 	 "R_PPC64_PLT16_HA",	/* name */
    703 	 FALSE,			/* partial_inplace */
    704 	 0,			/* src_mask */
    705 	 0xffff,		/* dst_mask */
    706 	 FALSE),		/* pcrel_offset */
    707 
    708   /* 16-bit section relative relocation.  */
    709   HOWTO (R_PPC64_SECTOFF,	/* type */
    710 	 0,			/* rightshift */
    711 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    712 	 16,			/* bitsize */
    713 	 FALSE,			/* pc_relative */
    714 	 0,			/* bitpos */
    715 	 complain_overflow_bitfield, /* complain_on_overflow */
    716 	 ppc64_elf_sectoff_reloc, /* special_function */
    717 	 "R_PPC64_SECTOFF",	/* name */
    718 	 FALSE,			/* partial_inplace */
    719 	 0,			/* src_mask */
    720 	 0xffff,		/* dst_mask */
    721 	 FALSE),		/* pcrel_offset */
    722 
    723   /* Like R_PPC64_SECTOFF, but no overflow warning.  */
    724   HOWTO (R_PPC64_SECTOFF_LO,	/* type */
    725 	 0,			/* rightshift */
    726 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    727 	 16,			/* bitsize */
    728 	 FALSE,			/* pc_relative */
    729 	 0,			/* bitpos */
    730 	 complain_overflow_dont, /* complain_on_overflow */
    731 	 ppc64_elf_sectoff_reloc, /* special_function */
    732 	 "R_PPC64_SECTOFF_LO",	/* name */
    733 	 FALSE,			/* partial_inplace */
    734 	 0,			/* src_mask */
    735 	 0xffff,		/* dst_mask */
    736 	 FALSE),		/* pcrel_offset */
    737 
    738   /* 16-bit upper half section relative relocation.  */
    739   HOWTO (R_PPC64_SECTOFF_HI,	/* type */
    740 	 16,			/* rightshift */
    741 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    742 	 16,			/* bitsize */
    743 	 FALSE,			/* pc_relative */
    744 	 0,			/* bitpos */
    745 	 complain_overflow_dont, /* complain_on_overflow */
    746 	 ppc64_elf_sectoff_reloc, /* special_function */
    747 	 "R_PPC64_SECTOFF_HI",	/* name */
    748 	 FALSE,			/* partial_inplace */
    749 	 0,			/* src_mask */
    750 	 0xffff,		/* dst_mask */
    751 	 FALSE),		/* pcrel_offset */
    752 
    753   /* 16-bit upper half adjusted section relative relocation.  */
    754   HOWTO (R_PPC64_SECTOFF_HA,	/* type */
    755 	 16,			/* rightshift */
    756 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    757 	 16,			/* bitsize */
    758 	 FALSE,			/* pc_relative */
    759 	 0,			/* bitpos */
    760 	 complain_overflow_dont, /* complain_on_overflow */
    761 	 ppc64_elf_sectoff_ha_reloc, /* special_function */
    762 	 "R_PPC64_SECTOFF_HA",	/* name */
    763 	 FALSE,			/* partial_inplace */
    764 	 0,			/* src_mask */
    765 	 0xffff,		/* dst_mask */
    766 	 FALSE),		/* pcrel_offset */
    767 
    768   /* Like R_PPC64_REL24 without touching the two least significant bits.  */
    769   HOWTO (R_PPC64_REL30,		/* type */
    770 	 2,			/* rightshift */
    771 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    772 	 30,			/* bitsize */
    773 	 TRUE,			/* pc_relative */
    774 	 0,			/* bitpos */
    775 	 complain_overflow_dont, /* complain_on_overflow */
    776 	 bfd_elf_generic_reloc, /* special_function */
    777 	 "R_PPC64_REL30",	/* name */
    778 	 FALSE,			/* partial_inplace */
    779 	 0,			/* src_mask */
    780 	 0xfffffffc,		/* dst_mask */
    781 	 TRUE),			/* pcrel_offset */
    782 
    783   /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
    784 
    785   /* A standard 64-bit relocation.  */
    786   HOWTO (R_PPC64_ADDR64,	/* type */
    787 	 0,			/* rightshift */
    788 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    789 	 64,			/* bitsize */
    790 	 FALSE,			/* pc_relative */
    791 	 0,			/* bitpos */
    792 	 complain_overflow_dont, /* complain_on_overflow */
    793 	 bfd_elf_generic_reloc,	/* special_function */
    794 	 "R_PPC64_ADDR64",	/* name */
    795 	 FALSE,			/* partial_inplace */
    796 	 0,			/* src_mask */
    797 	 ONES (64),		/* dst_mask */
    798 	 FALSE),		/* pcrel_offset */
    799 
    800   /* The bits 32-47 of an address.  */
    801   HOWTO (R_PPC64_ADDR16_HIGHER,	/* type */
    802 	 32,			/* rightshift */
    803 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    804 	 16,			/* bitsize */
    805 	 FALSE,			/* pc_relative */
    806 	 0,			/* bitpos */
    807 	 complain_overflow_dont, /* complain_on_overflow */
    808 	 bfd_elf_generic_reloc,	/* special_function */
    809 	 "R_PPC64_ADDR16_HIGHER", /* name */
    810 	 FALSE,			/* partial_inplace */
    811 	 0,			/* src_mask */
    812 	 0xffff,		/* dst_mask */
    813 	 FALSE),		/* pcrel_offset */
    814 
    815   /* The bits 32-47 of an address, plus 1 if the contents of the low
    816      16 bits, treated as a signed number, is negative.  */
    817   HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
    818 	 32,			/* rightshift */
    819 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    820 	 16,			/* bitsize */
    821 	 FALSE,			/* pc_relative */
    822 	 0,			/* bitpos */
    823 	 complain_overflow_dont, /* complain_on_overflow */
    824 	 ppc64_elf_ha_reloc,	/* special_function */
    825 	 "R_PPC64_ADDR16_HIGHERA", /* name */
    826 	 FALSE,			/* partial_inplace */
    827 	 0,			/* src_mask */
    828 	 0xffff,		/* dst_mask */
    829 	 FALSE),		/* pcrel_offset */
    830 
    831   /* The bits 48-63 of an address.  */
    832   HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
    833 	 48,			/* rightshift */
    834 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    835 	 16,			/* bitsize */
    836 	 FALSE,			/* pc_relative */
    837 	 0,			/* bitpos */
    838 	 complain_overflow_dont, /* complain_on_overflow */
    839 	 bfd_elf_generic_reloc,	/* special_function */
    840 	 "R_PPC64_ADDR16_HIGHEST", /* name */
    841 	 FALSE,			/* partial_inplace */
    842 	 0,			/* src_mask */
    843 	 0xffff,		/* dst_mask */
    844 	 FALSE),		/* pcrel_offset */
    845 
    846   /* The bits 48-63 of an address, plus 1 if the contents of the low
    847      16 bits, treated as a signed number, is negative.  */
    848   HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
    849 	 48,			/* rightshift */
    850 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    851 	 16,			/* bitsize */
    852 	 FALSE,			/* pc_relative */
    853 	 0,			/* bitpos */
    854 	 complain_overflow_dont, /* complain_on_overflow */
    855 	 ppc64_elf_ha_reloc,	/* special_function */
    856 	 "R_PPC64_ADDR16_HIGHESTA", /* name */
    857 	 FALSE,			/* partial_inplace */
    858 	 0,			/* src_mask */
    859 	 0xffff,		/* dst_mask */
    860 	 FALSE),		/* pcrel_offset */
    861 
    862   /* Like ADDR64, but may be unaligned.  */
    863   HOWTO (R_PPC64_UADDR64,	/* type */
    864 	 0,			/* rightshift */
    865 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    866 	 64,			/* bitsize */
    867 	 FALSE,			/* pc_relative */
    868 	 0,			/* bitpos */
    869 	 complain_overflow_dont, /* complain_on_overflow */
    870 	 bfd_elf_generic_reloc,	/* special_function */
    871 	 "R_PPC64_UADDR64",	/* name */
    872 	 FALSE,			/* partial_inplace */
    873 	 0,			/* src_mask */
    874 	 ONES (64),		/* dst_mask */
    875 	 FALSE),		/* pcrel_offset */
    876 
    877   /* 64-bit relative relocation.  */
    878   HOWTO (R_PPC64_REL64,		/* type */
    879 	 0,			/* rightshift */
    880 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    881 	 64,			/* bitsize */
    882 	 TRUE,			/* pc_relative */
    883 	 0,			/* bitpos */
    884 	 complain_overflow_dont, /* complain_on_overflow */
    885 	 bfd_elf_generic_reloc,	/* special_function */
    886 	 "R_PPC64_REL64",	/* name */
    887 	 FALSE,			/* partial_inplace */
    888 	 0,			/* src_mask */
    889 	 ONES (64),		/* dst_mask */
    890 	 TRUE),			/* pcrel_offset */
    891 
    892   /* 64-bit relocation to the symbol's procedure linkage table.  */
    893   HOWTO (R_PPC64_PLT64,		/* type */
    894 	 0,			/* rightshift */
    895 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    896 	 64,			/* bitsize */
    897 	 FALSE,			/* pc_relative */
    898 	 0,			/* bitpos */
    899 	 complain_overflow_dont, /* complain_on_overflow */
    900 	 ppc64_elf_unhandled_reloc, /* special_function */
    901 	 "R_PPC64_PLT64",	/* name */
    902 	 FALSE,			/* partial_inplace */
    903 	 0,			/* src_mask */
    904 	 ONES (64),		/* dst_mask */
    905 	 FALSE),		/* pcrel_offset */
    906 
    907   /* 64-bit PC relative relocation to the symbol's procedure linkage
    908      table.  */
    909   /* FIXME: R_PPC64_PLTREL64 not supported.  */
    910   HOWTO (R_PPC64_PLTREL64,	/* type */
    911 	 0,			/* rightshift */
    912 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    913 	 64,			/* bitsize */
    914 	 TRUE,			/* pc_relative */
    915 	 0,			/* bitpos */
    916 	 complain_overflow_dont, /* complain_on_overflow */
    917 	 ppc64_elf_unhandled_reloc, /* special_function */
    918 	 "R_PPC64_PLTREL64",	/* name */
    919 	 FALSE,			/* partial_inplace */
    920 	 0,			/* src_mask */
    921 	 ONES (64),		/* dst_mask */
    922 	 TRUE),			/* pcrel_offset */
    923 
    924   /* 16 bit TOC-relative relocation.  */
    925 
    926   /* R_PPC64_TOC16	  47	   half16*	S + A - .TOC.  */
    927   HOWTO (R_PPC64_TOC16,		/* type */
    928 	 0,			/* rightshift */
    929 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    930 	 16,			/* bitsize */
    931 	 FALSE,			/* pc_relative */
    932 	 0,			/* bitpos */
    933 	 complain_overflow_signed, /* complain_on_overflow */
    934 	 ppc64_elf_toc_reloc,	/* special_function */
    935 	 "R_PPC64_TOC16",	/* name */
    936 	 FALSE,			/* partial_inplace */
    937 	 0,			/* src_mask */
    938 	 0xffff,		/* dst_mask */
    939 	 FALSE),		/* pcrel_offset */
    940 
    941   /* 16 bit TOC-relative relocation without overflow.  */
    942 
    943   /* R_PPC64_TOC16_LO	  48	   half16	 #lo (S + A - .TOC.)  */
    944   HOWTO (R_PPC64_TOC16_LO,	/* type */
    945 	 0,			/* rightshift */
    946 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    947 	 16,			/* bitsize */
    948 	 FALSE,			/* pc_relative */
    949 	 0,			/* bitpos */
    950 	 complain_overflow_dont, /* complain_on_overflow */
    951 	 ppc64_elf_toc_reloc,	/* special_function */
    952 	 "R_PPC64_TOC16_LO",	/* name */
    953 	 FALSE,			/* partial_inplace */
    954 	 0,			/* src_mask */
    955 	 0xffff,		/* dst_mask */
    956 	 FALSE),		/* pcrel_offset */
    957 
    958   /* 16 bit TOC-relative relocation, high 16 bits.  */
    959 
    960   /* R_PPC64_TOC16_HI	  49	   half16	 #hi (S + A - .TOC.)  */
    961   HOWTO (R_PPC64_TOC16_HI,	/* type */
    962 	 16,			/* rightshift */
    963 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    964 	 16,			/* bitsize */
    965 	 FALSE,			/* pc_relative */
    966 	 0,			/* bitpos */
    967 	 complain_overflow_dont, /* complain_on_overflow */
    968 	 ppc64_elf_toc_reloc,	/* special_function */
    969 	 "R_PPC64_TOC16_HI",	/* name */
    970 	 FALSE,			/* partial_inplace */
    971 	 0,			/* src_mask */
    972 	 0xffff,		/* dst_mask */
    973 	 FALSE),		/* pcrel_offset */
    974 
    975   /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
    976      contents of the low 16 bits, treated as a signed number, is
    977      negative.  */
    978 
    979   /* R_PPC64_TOC16_HA	  50	   half16	 #ha (S + A - .TOC.)  */
    980   HOWTO (R_PPC64_TOC16_HA,	/* type */
    981 	 16,			/* rightshift */
    982 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    983 	 16,			/* bitsize */
    984 	 FALSE,			/* pc_relative */
    985 	 0,			/* bitpos */
    986 	 complain_overflow_dont, /* complain_on_overflow */
    987 	 ppc64_elf_toc_ha_reloc, /* special_function */
    988 	 "R_PPC64_TOC16_HA",	/* name */
    989 	 FALSE,			/* partial_inplace */
    990 	 0,			/* src_mask */
    991 	 0xffff,		/* dst_mask */
    992 	 FALSE),		/* pcrel_offset */
    993 
    994   /* 64-bit relocation; insert value of TOC base (.TOC.).  */
    995 
    996   /* R_PPC64_TOC		  51	   doubleword64	 .TOC.  */
    997   HOWTO (R_PPC64_TOC,		/* type */
    998 	 0,			/* rightshift */
    999 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
   1000 	 64,			/* bitsize */
   1001 	 FALSE,			/* pc_relative */
   1002 	 0,			/* bitpos */
   1003 	 complain_overflow_bitfield, /* complain_on_overflow */
   1004 	 ppc64_elf_toc64_reloc,	/* special_function */
   1005 	 "R_PPC64_TOC",		/* name */
   1006 	 FALSE,			/* partial_inplace */
   1007 	 0,			/* src_mask */
   1008 	 ONES (64),		/* dst_mask */
   1009 	 FALSE),		/* pcrel_offset */
   1010 
   1011   /* Like R_PPC64_GOT16, but also informs the link editor that the
   1012      value to relocate may (!) refer to a PLT entry which the link
   1013      editor (a) may replace with the symbol value.  If the link editor
   1014      is unable to fully resolve the symbol, it may (b) create a PLT
   1015      entry and store the address to the new PLT entry in the GOT.
   1016      This permits lazy resolution of function symbols at run time.
   1017      The link editor may also skip all of this and just (c) emit a
   1018      R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
   1019   /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
   1020     HOWTO (R_PPC64_PLTGOT16,	/* type */
   1021 	 0,			/* rightshift */
   1022 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1023 	 16,			/* bitsize */
   1024 	 FALSE,			/* pc_relative */
   1025 	 0,			/* bitpos */
   1026 	 complain_overflow_signed, /* complain_on_overflow */
   1027 	 ppc64_elf_unhandled_reloc, /* special_function */
   1028 	 "R_PPC64_PLTGOT16",	/* name */
   1029 	 FALSE,			/* partial_inplace */
   1030 	 0,			/* src_mask */
   1031 	 0xffff,		/* dst_mask */
   1032 	 FALSE),		/* pcrel_offset */
   1033 
   1034   /* Like R_PPC64_PLTGOT16, but without overflow.  */
   1035   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
   1036   HOWTO (R_PPC64_PLTGOT16_LO,	/* type */
   1037 	 0,			/* rightshift */
   1038 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1039 	 16,			/* bitsize */
   1040 	 FALSE,			/* pc_relative */
   1041 	 0,			/* bitpos */
   1042 	 complain_overflow_dont, /* complain_on_overflow */
   1043 	 ppc64_elf_unhandled_reloc, /* special_function */
   1044 	 "R_PPC64_PLTGOT16_LO",	/* name */
   1045 	 FALSE,			/* partial_inplace */
   1046 	 0,			/* src_mask */
   1047 	 0xffff,		/* dst_mask */
   1048 	 FALSE),		/* pcrel_offset */
   1049 
   1050   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
   1051   /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
   1052   HOWTO (R_PPC64_PLTGOT16_HI,	/* type */
   1053 	 16,			/* rightshift */
   1054 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1055 	 16,			/* bitsize */
   1056 	 FALSE,			/* pc_relative */
   1057 	 0,			/* bitpos */
   1058 	 complain_overflow_dont, /* complain_on_overflow */
   1059 	 ppc64_elf_unhandled_reloc, /* special_function */
   1060 	 "R_PPC64_PLTGOT16_HI",	/* name */
   1061 	 FALSE,			/* partial_inplace */
   1062 	 0,			/* src_mask */
   1063 	 0xffff,		/* dst_mask */
   1064 	 FALSE),		/* pcrel_offset */
   1065 
   1066   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
   1067      1 if the contents of the low 16 bits, treated as a signed number,
   1068      is negative.  */
   1069   /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
   1070   HOWTO (R_PPC64_PLTGOT16_HA,	/* type */
   1071 	 16,			/* rightshift */
   1072 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1073 	 16,			/* bitsize */
   1074 	 FALSE,			/* pc_relative */
   1075 	 0,			/* bitpos */
   1076 	 complain_overflow_dont,/* complain_on_overflow */
   1077 	 ppc64_elf_unhandled_reloc, /* special_function */
   1078 	 "R_PPC64_PLTGOT16_HA",	/* name */
   1079 	 FALSE,			/* partial_inplace */
   1080 	 0,			/* src_mask */
   1081 	 0xffff,		/* dst_mask */
   1082 	 FALSE),		/* pcrel_offset */
   1083 
   1084   /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
   1085   HOWTO (R_PPC64_ADDR16_DS,	/* type */
   1086 	 0,			/* rightshift */
   1087 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1088 	 16,			/* bitsize */
   1089 	 FALSE,			/* pc_relative */
   1090 	 0,			/* bitpos */
   1091 	 complain_overflow_bitfield, /* complain_on_overflow */
   1092 	 bfd_elf_generic_reloc,	/* special_function */
   1093 	 "R_PPC64_ADDR16_DS",	/* name */
   1094 	 FALSE,			/* partial_inplace */
   1095 	 0,			/* src_mask */
   1096 	 0xfffc,		/* dst_mask */
   1097 	 FALSE),		/* pcrel_offset */
   1098 
   1099   /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
   1100   HOWTO (R_PPC64_ADDR16_LO_DS,	/* type */
   1101 	 0,			/* rightshift */
   1102 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1103 	 16,			/* bitsize */
   1104 	 FALSE,			/* pc_relative */
   1105 	 0,			/* bitpos */
   1106 	 complain_overflow_dont,/* complain_on_overflow */
   1107 	 bfd_elf_generic_reloc,	/* special_function */
   1108 	 "R_PPC64_ADDR16_LO_DS",/* name */
   1109 	 FALSE,			/* partial_inplace */
   1110 	 0,			/* src_mask */
   1111 	 0xfffc,		/* dst_mask */
   1112 	 FALSE),		/* pcrel_offset */
   1113 
   1114   /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
   1115   HOWTO (R_PPC64_GOT16_DS,	/* type */
   1116 	 0,			/* rightshift */
   1117 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1118 	 16,			/* bitsize */
   1119 	 FALSE,			/* pc_relative */
   1120 	 0,			/* bitpos */
   1121 	 complain_overflow_signed, /* complain_on_overflow */
   1122 	 ppc64_elf_unhandled_reloc, /* special_function */
   1123 	 "R_PPC64_GOT16_DS",	/* name */
   1124 	 FALSE,			/* partial_inplace */
   1125 	 0,			/* src_mask */
   1126 	 0xfffc,		/* dst_mask */
   1127 	 FALSE),		/* pcrel_offset */
   1128 
   1129   /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
   1130   HOWTO (R_PPC64_GOT16_LO_DS,	/* type */
   1131 	 0,			/* rightshift */
   1132 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1133 	 16,			/* bitsize */
   1134 	 FALSE,			/* pc_relative */
   1135 	 0,			/* bitpos */
   1136 	 complain_overflow_dont, /* complain_on_overflow */
   1137 	 ppc64_elf_unhandled_reloc, /* special_function */
   1138 	 "R_PPC64_GOT16_LO_DS",	/* name */
   1139 	 FALSE,			/* partial_inplace */
   1140 	 0,			/* src_mask */
   1141 	 0xfffc,		/* dst_mask */
   1142 	 FALSE),		/* pcrel_offset */
   1143 
   1144   /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
   1145   HOWTO (R_PPC64_PLT16_LO_DS,	/* type */
   1146 	 0,			/* rightshift */
   1147 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1148 	 16,			/* bitsize */
   1149 	 FALSE,			/* pc_relative */
   1150 	 0,			/* bitpos */
   1151 	 complain_overflow_dont, /* complain_on_overflow */
   1152 	 ppc64_elf_unhandled_reloc, /* special_function */
   1153 	 "R_PPC64_PLT16_LO_DS",	/* name */
   1154 	 FALSE,			/* partial_inplace */
   1155 	 0,			/* src_mask */
   1156 	 0xfffc,		/* dst_mask */
   1157 	 FALSE),		/* pcrel_offset */
   1158 
   1159   /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
   1160   HOWTO (R_PPC64_SECTOFF_DS,	/* type */
   1161 	 0,			/* rightshift */
   1162 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1163 	 16,			/* bitsize */
   1164 	 FALSE,			/* pc_relative */
   1165 	 0,			/* bitpos */
   1166 	 complain_overflow_bitfield, /* complain_on_overflow */
   1167 	 ppc64_elf_sectoff_reloc, /* special_function */
   1168 	 "R_PPC64_SECTOFF_DS",	/* name */
   1169 	 FALSE,			/* partial_inplace */
   1170 	 0,			/* src_mask */
   1171 	 0xfffc,		/* dst_mask */
   1172 	 FALSE),		/* pcrel_offset */
   1173 
   1174   /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
   1175   HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
   1176 	 0,			/* rightshift */
   1177 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1178 	 16,			/* bitsize */
   1179 	 FALSE,			/* pc_relative */
   1180 	 0,			/* bitpos */
   1181 	 complain_overflow_dont, /* complain_on_overflow */
   1182 	 ppc64_elf_sectoff_reloc, /* special_function */
   1183 	 "R_PPC64_SECTOFF_LO_DS",/* name */
   1184 	 FALSE,			/* partial_inplace */
   1185 	 0,			/* src_mask */
   1186 	 0xfffc,		/* dst_mask */
   1187 	 FALSE),		/* pcrel_offset */
   1188 
   1189   /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
   1190   HOWTO (R_PPC64_TOC16_DS,	/* type */
   1191 	 0,			/* rightshift */
   1192 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1193 	 16,			/* bitsize */
   1194 	 FALSE,			/* pc_relative */
   1195 	 0,			/* bitpos */
   1196 	 complain_overflow_signed, /* complain_on_overflow */
   1197 	 ppc64_elf_toc_reloc,	/* special_function */
   1198 	 "R_PPC64_TOC16_DS",	/* name */
   1199 	 FALSE,			/* partial_inplace */
   1200 	 0,			/* src_mask */
   1201 	 0xfffc,		/* dst_mask */
   1202 	 FALSE),		/* pcrel_offset */
   1203 
   1204   /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
   1205   HOWTO (R_PPC64_TOC16_LO_DS,	/* type */
   1206 	 0,			/* rightshift */
   1207 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1208 	 16,			/* bitsize */
   1209 	 FALSE,			/* pc_relative */
   1210 	 0,			/* bitpos */
   1211 	 complain_overflow_dont, /* complain_on_overflow */
   1212 	 ppc64_elf_toc_reloc,	/* special_function */
   1213 	 "R_PPC64_TOC16_LO_DS",	/* name */
   1214 	 FALSE,			/* partial_inplace */
   1215 	 0,			/* src_mask */
   1216 	 0xfffc,		/* dst_mask */
   1217 	 FALSE),		/* pcrel_offset */
   1218 
   1219   /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
   1220   /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
   1221   HOWTO (R_PPC64_PLTGOT16_DS,	/* type */
   1222 	 0,			/* rightshift */
   1223 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1224 	 16,			/* bitsize */
   1225 	 FALSE,			/* pc_relative */
   1226 	 0,			/* bitpos */
   1227 	 complain_overflow_signed, /* complain_on_overflow */
   1228 	 ppc64_elf_unhandled_reloc, /* special_function */
   1229 	 "R_PPC64_PLTGOT16_DS",	/* name */
   1230 	 FALSE,			/* partial_inplace */
   1231 	 0,			/* src_mask */
   1232 	 0xfffc,		/* dst_mask */
   1233 	 FALSE),		/* pcrel_offset */
   1234 
   1235   /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
   1236   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
   1237   HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
   1238 	 0,			/* rightshift */
   1239 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1240 	 16,			/* bitsize */
   1241 	 FALSE,			/* pc_relative */
   1242 	 0,			/* bitpos */
   1243 	 complain_overflow_dont, /* complain_on_overflow */
   1244 	 ppc64_elf_unhandled_reloc, /* special_function */
   1245 	 "R_PPC64_PLTGOT16_LO_DS",/* name */
   1246 	 FALSE,			/* partial_inplace */
   1247 	 0,			/* src_mask */
   1248 	 0xfffc,		/* dst_mask */
   1249 	 FALSE),		/* pcrel_offset */
   1250 
   1251   /* Marker relocs for TLS.  */
   1252   HOWTO (R_PPC64_TLS,
   1253 	 0,			/* rightshift */
   1254 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1255 	 32,			/* bitsize */
   1256 	 FALSE,			/* pc_relative */
   1257 	 0,			/* bitpos */
   1258 	 complain_overflow_dont, /* complain_on_overflow */
   1259 	 bfd_elf_generic_reloc,	/* special_function */
   1260 	 "R_PPC64_TLS",		/* name */
   1261 	 FALSE,			/* partial_inplace */
   1262 	 0,			/* src_mask */
   1263 	 0,			/* dst_mask */
   1264 	 FALSE),		/* pcrel_offset */
   1265 
   1266   HOWTO (R_PPC64_TLSGD,
   1267 	 0,			/* rightshift */
   1268 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1269 	 32,			/* bitsize */
   1270 	 FALSE,			/* pc_relative */
   1271 	 0,			/* bitpos */
   1272 	 complain_overflow_dont, /* complain_on_overflow */
   1273 	 bfd_elf_generic_reloc,	/* special_function */
   1274 	 "R_PPC64_TLSGD",	/* name */
   1275 	 FALSE,			/* partial_inplace */
   1276 	 0,			/* src_mask */
   1277 	 0,			/* dst_mask */
   1278 	 FALSE),		/* pcrel_offset */
   1279 
   1280   HOWTO (R_PPC64_TLSLD,
   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_TLSLD",	/* name */
   1289 	 FALSE,			/* partial_inplace */
   1290 	 0,			/* src_mask */
   1291 	 0,			/* dst_mask */
   1292 	 FALSE),		/* pcrel_offset */
   1293 
   1294   HOWTO (R_PPC64_TOCSAVE,
   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_TOCSAVE",	/* name */
   1303 	 FALSE,			/* partial_inplace */
   1304 	 0,			/* src_mask */
   1305 	 0,			/* dst_mask */
   1306 	 FALSE),		/* pcrel_offset */
   1307 
   1308   /* Computes the load module index of the load module that contains the
   1309      definition of its TLS sym.  */
   1310   HOWTO (R_PPC64_DTPMOD64,
   1311 	 0,			/* rightshift */
   1312 	 4,			/* size (0 = byte, 1 = short, 2 = long) */
   1313 	 64,			/* bitsize */
   1314 	 FALSE,			/* pc_relative */
   1315 	 0,			/* bitpos */
   1316 	 complain_overflow_dont, /* complain_on_overflow */
   1317 	 ppc64_elf_unhandled_reloc, /* special_function */
   1318 	 "R_PPC64_DTPMOD64",	/* name */
   1319 	 FALSE,			/* partial_inplace */
   1320 	 0,			/* src_mask */
   1321 	 ONES (64),		/* dst_mask */
   1322 	 FALSE),		/* pcrel_offset */
   1323 
   1324   /* Computes a dtv-relative displacement, the difference between the value
   1325      of sym+add and the base address of the thread-local storage block that
   1326      contains the definition of sym, minus 0x8000.  */
   1327   HOWTO (R_PPC64_DTPREL64,
   1328 	 0,			/* rightshift */
   1329 	 4,			/* size (0 = byte, 1 = short, 2 = long) */
   1330 	 64,			/* bitsize */
   1331 	 FALSE,			/* pc_relative */
   1332 	 0,			/* bitpos */
   1333 	 complain_overflow_dont, /* complain_on_overflow */
   1334 	 ppc64_elf_unhandled_reloc, /* special_function */
   1335 	 "R_PPC64_DTPREL64",	/* name */
   1336 	 FALSE,			/* partial_inplace */
   1337 	 0,			/* src_mask */
   1338 	 ONES (64),		/* dst_mask */
   1339 	 FALSE),		/* pcrel_offset */
   1340 
   1341   /* A 16 bit dtprel reloc.  */
   1342   HOWTO (R_PPC64_DTPREL16,
   1343 	 0,			/* rightshift */
   1344 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1345 	 16,			/* bitsize */
   1346 	 FALSE,			/* pc_relative */
   1347 	 0,			/* bitpos */
   1348 	 complain_overflow_signed, /* complain_on_overflow */
   1349 	 ppc64_elf_unhandled_reloc, /* special_function */
   1350 	 "R_PPC64_DTPREL16",	/* name */
   1351 	 FALSE,			/* partial_inplace */
   1352 	 0,			/* src_mask */
   1353 	 0xffff,		/* dst_mask */
   1354 	 FALSE),		/* pcrel_offset */
   1355 
   1356   /* Like DTPREL16, but no overflow.  */
   1357   HOWTO (R_PPC64_DTPREL16_LO,
   1358 	 0,			/* rightshift */
   1359 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1360 	 16,			/* bitsize */
   1361 	 FALSE,			/* pc_relative */
   1362 	 0,			/* bitpos */
   1363 	 complain_overflow_dont, /* complain_on_overflow */
   1364 	 ppc64_elf_unhandled_reloc, /* special_function */
   1365 	 "R_PPC64_DTPREL16_LO",	/* name */
   1366 	 FALSE,			/* partial_inplace */
   1367 	 0,			/* src_mask */
   1368 	 0xffff,		/* dst_mask */
   1369 	 FALSE),		/* pcrel_offset */
   1370 
   1371   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
   1372   HOWTO (R_PPC64_DTPREL16_HI,
   1373 	 16,			/* rightshift */
   1374 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1375 	 16,			/* bitsize */
   1376 	 FALSE,			/* pc_relative */
   1377 	 0,			/* bitpos */
   1378 	 complain_overflow_dont, /* complain_on_overflow */
   1379 	 ppc64_elf_unhandled_reloc, /* special_function */
   1380 	 "R_PPC64_DTPREL16_HI",	/* name */
   1381 	 FALSE,			/* partial_inplace */
   1382 	 0,			/* src_mask */
   1383 	 0xffff,		/* dst_mask */
   1384 	 FALSE),		/* pcrel_offset */
   1385 
   1386   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
   1387   HOWTO (R_PPC64_DTPREL16_HA,
   1388 	 16,			/* rightshift */
   1389 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1390 	 16,			/* bitsize */
   1391 	 FALSE,			/* pc_relative */
   1392 	 0,			/* bitpos */
   1393 	 complain_overflow_dont, /* complain_on_overflow */
   1394 	 ppc64_elf_unhandled_reloc, /* special_function */
   1395 	 "R_PPC64_DTPREL16_HA",	/* name */
   1396 	 FALSE,			/* partial_inplace */
   1397 	 0,			/* src_mask */
   1398 	 0xffff,		/* dst_mask */
   1399 	 FALSE),		/* pcrel_offset */
   1400 
   1401   /* Like DTPREL16_HI, but next higher group of 16 bits.  */
   1402   HOWTO (R_PPC64_DTPREL16_HIGHER,
   1403 	 32,			/* rightshift */
   1404 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1405 	 16,			/* bitsize */
   1406 	 FALSE,			/* pc_relative */
   1407 	 0,			/* bitpos */
   1408 	 complain_overflow_dont, /* complain_on_overflow */
   1409 	 ppc64_elf_unhandled_reloc, /* special_function */
   1410 	 "R_PPC64_DTPREL16_HIGHER", /* name */
   1411 	 FALSE,			/* partial_inplace */
   1412 	 0,			/* src_mask */
   1413 	 0xffff,		/* dst_mask */
   1414 	 FALSE),		/* pcrel_offset */
   1415 
   1416   /* Like DTPREL16_HIGHER, but adjust for low 16 bits.  */
   1417   HOWTO (R_PPC64_DTPREL16_HIGHERA,
   1418 	 32,			/* rightshift */
   1419 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1420 	 16,			/* bitsize */
   1421 	 FALSE,			/* pc_relative */
   1422 	 0,			/* bitpos */
   1423 	 complain_overflow_dont, /* complain_on_overflow */
   1424 	 ppc64_elf_unhandled_reloc, /* special_function */
   1425 	 "R_PPC64_DTPREL16_HIGHERA", /* name */
   1426 	 FALSE,			/* partial_inplace */
   1427 	 0,			/* src_mask */
   1428 	 0xffff,		/* dst_mask */
   1429 	 FALSE),		/* pcrel_offset */
   1430 
   1431   /* Like DTPREL16_HIGHER, but next higher group of 16 bits.  */
   1432   HOWTO (R_PPC64_DTPREL16_HIGHEST,
   1433 	 48,			/* rightshift */
   1434 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1435 	 16,			/* bitsize */
   1436 	 FALSE,			/* pc_relative */
   1437 	 0,			/* bitpos */
   1438 	 complain_overflow_dont, /* complain_on_overflow */
   1439 	 ppc64_elf_unhandled_reloc, /* special_function */
   1440 	 "R_PPC64_DTPREL16_HIGHEST", /* name */
   1441 	 FALSE,			/* partial_inplace */
   1442 	 0,			/* src_mask */
   1443 	 0xffff,		/* dst_mask */
   1444 	 FALSE),		/* pcrel_offset */
   1445 
   1446   /* Like DTPREL16_HIGHEST, but adjust for low 16 bits.  */
   1447   HOWTO (R_PPC64_DTPREL16_HIGHESTA,
   1448 	 48,			/* rightshift */
   1449 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1450 	 16,			/* bitsize */
   1451 	 FALSE,			/* pc_relative */
   1452 	 0,			/* bitpos */
   1453 	 complain_overflow_dont, /* complain_on_overflow */
   1454 	 ppc64_elf_unhandled_reloc, /* special_function */
   1455 	 "R_PPC64_DTPREL16_HIGHESTA", /* name */
   1456 	 FALSE,			/* partial_inplace */
   1457 	 0,			/* src_mask */
   1458 	 0xffff,		/* dst_mask */
   1459 	 FALSE),		/* pcrel_offset */
   1460 
   1461   /* Like DTPREL16, but for insns with a DS field.  */
   1462   HOWTO (R_PPC64_DTPREL16_DS,
   1463 	 0,			/* rightshift */
   1464 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1465 	 16,			/* bitsize */
   1466 	 FALSE,			/* pc_relative */
   1467 	 0,			/* bitpos */
   1468 	 complain_overflow_signed, /* complain_on_overflow */
   1469 	 ppc64_elf_unhandled_reloc, /* special_function */
   1470 	 "R_PPC64_DTPREL16_DS",	/* name */
   1471 	 FALSE,			/* partial_inplace */
   1472 	 0,			/* src_mask */
   1473 	 0xfffc,		/* dst_mask */
   1474 	 FALSE),		/* pcrel_offset */
   1475 
   1476   /* Like DTPREL16_DS, but no overflow.  */
   1477   HOWTO (R_PPC64_DTPREL16_LO_DS,
   1478 	 0,			/* rightshift */
   1479 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1480 	 16,			/* bitsize */
   1481 	 FALSE,			/* pc_relative */
   1482 	 0,			/* bitpos */
   1483 	 complain_overflow_dont, /* complain_on_overflow */
   1484 	 ppc64_elf_unhandled_reloc, /* special_function */
   1485 	 "R_PPC64_DTPREL16_LO_DS", /* name */
   1486 	 FALSE,			/* partial_inplace */
   1487 	 0,			/* src_mask */
   1488 	 0xfffc,		/* dst_mask */
   1489 	 FALSE),		/* pcrel_offset */
   1490 
   1491   /* Computes a tp-relative displacement, the difference between the value of
   1492      sym+add and the value of the thread pointer (r13).  */
   1493   HOWTO (R_PPC64_TPREL64,
   1494 	 0,			/* rightshift */
   1495 	 4,			/* size (0 = byte, 1 = short, 2 = long) */
   1496 	 64,			/* bitsize */
   1497 	 FALSE,			/* pc_relative */
   1498 	 0,			/* bitpos */
   1499 	 complain_overflow_dont, /* complain_on_overflow */
   1500 	 ppc64_elf_unhandled_reloc, /* special_function */
   1501 	 "R_PPC64_TPREL64",	/* name */
   1502 	 FALSE,			/* partial_inplace */
   1503 	 0,			/* src_mask */
   1504 	 ONES (64),		/* dst_mask */
   1505 	 FALSE),		/* pcrel_offset */
   1506 
   1507   /* A 16 bit tprel reloc.  */
   1508   HOWTO (R_PPC64_TPREL16,
   1509 	 0,			/* rightshift */
   1510 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1511 	 16,			/* bitsize */
   1512 	 FALSE,			/* pc_relative */
   1513 	 0,			/* bitpos */
   1514 	 complain_overflow_signed, /* complain_on_overflow */
   1515 	 ppc64_elf_unhandled_reloc, /* special_function */
   1516 	 "R_PPC64_TPREL16",	/* name */
   1517 	 FALSE,			/* partial_inplace */
   1518 	 0,			/* src_mask */
   1519 	 0xffff,		/* dst_mask */
   1520 	 FALSE),		/* pcrel_offset */
   1521 
   1522   /* Like TPREL16, but no overflow.  */
   1523   HOWTO (R_PPC64_TPREL16_LO,
   1524 	 0,			/* rightshift */
   1525 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1526 	 16,			/* bitsize */
   1527 	 FALSE,			/* pc_relative */
   1528 	 0,			/* bitpos */
   1529 	 complain_overflow_dont, /* complain_on_overflow */
   1530 	 ppc64_elf_unhandled_reloc, /* special_function */
   1531 	 "R_PPC64_TPREL16_LO",	/* name */
   1532 	 FALSE,			/* partial_inplace */
   1533 	 0,			/* src_mask */
   1534 	 0xffff,		/* dst_mask */
   1535 	 FALSE),		/* pcrel_offset */
   1536 
   1537   /* Like TPREL16_LO, but next higher group of 16 bits.  */
   1538   HOWTO (R_PPC64_TPREL16_HI,
   1539 	 16,			/* rightshift */
   1540 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1541 	 16,			/* bitsize */
   1542 	 FALSE,			/* pc_relative */
   1543 	 0,			/* bitpos */
   1544 	 complain_overflow_dont, /* complain_on_overflow */
   1545 	 ppc64_elf_unhandled_reloc, /* special_function */
   1546 	 "R_PPC64_TPREL16_HI",	/* name */
   1547 	 FALSE,			/* partial_inplace */
   1548 	 0,			/* src_mask */
   1549 	 0xffff,		/* dst_mask */
   1550 	 FALSE),		/* pcrel_offset */
   1551 
   1552   /* Like TPREL16_HI, but adjust for low 16 bits.  */
   1553   HOWTO (R_PPC64_TPREL16_HA,
   1554 	 16,			/* rightshift */
   1555 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1556 	 16,			/* bitsize */
   1557 	 FALSE,			/* pc_relative */
   1558 	 0,			/* bitpos */
   1559 	 complain_overflow_dont, /* complain_on_overflow */
   1560 	 ppc64_elf_unhandled_reloc, /* special_function */
   1561 	 "R_PPC64_TPREL16_HA",	/* name */
   1562 	 FALSE,			/* partial_inplace */
   1563 	 0,			/* src_mask */
   1564 	 0xffff,		/* dst_mask */
   1565 	 FALSE),		/* pcrel_offset */
   1566 
   1567   /* Like TPREL16_HI, but next higher group of 16 bits.  */
   1568   HOWTO (R_PPC64_TPREL16_HIGHER,
   1569 	 32,			/* rightshift */
   1570 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1571 	 16,			/* bitsize */
   1572 	 FALSE,			/* pc_relative */
   1573 	 0,			/* bitpos */
   1574 	 complain_overflow_dont, /* complain_on_overflow */
   1575 	 ppc64_elf_unhandled_reloc, /* special_function */
   1576 	 "R_PPC64_TPREL16_HIGHER",	/* name */
   1577 	 FALSE,			/* partial_inplace */
   1578 	 0,			/* src_mask */
   1579 	 0xffff,		/* dst_mask */
   1580 	 FALSE),		/* pcrel_offset */
   1581 
   1582   /* Like TPREL16_HIGHER, but adjust for low 16 bits.  */
   1583   HOWTO (R_PPC64_TPREL16_HIGHERA,
   1584 	 32,			/* rightshift */
   1585 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1586 	 16,			/* bitsize */
   1587 	 FALSE,			/* pc_relative */
   1588 	 0,			/* bitpos */
   1589 	 complain_overflow_dont, /* complain_on_overflow */
   1590 	 ppc64_elf_unhandled_reloc, /* special_function */
   1591 	 "R_PPC64_TPREL16_HIGHERA", /* name */
   1592 	 FALSE,			/* partial_inplace */
   1593 	 0,			/* src_mask */
   1594 	 0xffff,		/* dst_mask */
   1595 	 FALSE),		/* pcrel_offset */
   1596 
   1597   /* Like TPREL16_HIGHER, but next higher group of 16 bits.  */
   1598   HOWTO (R_PPC64_TPREL16_HIGHEST,
   1599 	 48,			/* rightshift */
   1600 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1601 	 16,			/* bitsize */
   1602 	 FALSE,			/* pc_relative */
   1603 	 0,			/* bitpos */
   1604 	 complain_overflow_dont, /* complain_on_overflow */
   1605 	 ppc64_elf_unhandled_reloc, /* special_function */
   1606 	 "R_PPC64_TPREL16_HIGHEST", /* name */
   1607 	 FALSE,			/* partial_inplace */
   1608 	 0,			/* src_mask */
   1609 	 0xffff,		/* dst_mask */
   1610 	 FALSE),		/* pcrel_offset */
   1611 
   1612   /* Like TPREL16_HIGHEST, but adjust for low 16 bits.  */
   1613   HOWTO (R_PPC64_TPREL16_HIGHESTA,
   1614 	 48,			/* rightshift */
   1615 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1616 	 16,			/* bitsize */
   1617 	 FALSE,			/* pc_relative */
   1618 	 0,			/* bitpos */
   1619 	 complain_overflow_dont, /* complain_on_overflow */
   1620 	 ppc64_elf_unhandled_reloc, /* special_function */
   1621 	 "R_PPC64_TPREL16_HIGHESTA", /* name */
   1622 	 FALSE,			/* partial_inplace */
   1623 	 0,			/* src_mask */
   1624 	 0xffff,		/* dst_mask */
   1625 	 FALSE),		/* pcrel_offset */
   1626 
   1627   /* Like TPREL16, but for insns with a DS field.  */
   1628   HOWTO (R_PPC64_TPREL16_DS,
   1629 	 0,			/* rightshift */
   1630 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1631 	 16,			/* bitsize */
   1632 	 FALSE,			/* pc_relative */
   1633 	 0,			/* bitpos */
   1634 	 complain_overflow_signed, /* complain_on_overflow */
   1635 	 ppc64_elf_unhandled_reloc, /* special_function */
   1636 	 "R_PPC64_TPREL16_DS",	/* name */
   1637 	 FALSE,			/* partial_inplace */
   1638 	 0,			/* src_mask */
   1639 	 0xfffc,		/* dst_mask */
   1640 	 FALSE),		/* pcrel_offset */
   1641 
   1642   /* Like TPREL16_DS, but no overflow.  */
   1643   HOWTO (R_PPC64_TPREL16_LO_DS,
   1644 	 0,			/* rightshift */
   1645 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1646 	 16,			/* bitsize */
   1647 	 FALSE,			/* pc_relative */
   1648 	 0,			/* bitpos */
   1649 	 complain_overflow_dont, /* complain_on_overflow */
   1650 	 ppc64_elf_unhandled_reloc, /* special_function */
   1651 	 "R_PPC64_TPREL16_LO_DS", /* name */
   1652 	 FALSE,			/* partial_inplace */
   1653 	 0,			/* src_mask */
   1654 	 0xfffc,		/* dst_mask */
   1655 	 FALSE),		/* pcrel_offset */
   1656 
   1657   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
   1658      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
   1659      to the first entry relative to the TOC base (r2).  */
   1660   HOWTO (R_PPC64_GOT_TLSGD16,
   1661 	 0,			/* rightshift */
   1662 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1663 	 16,			/* bitsize */
   1664 	 FALSE,			/* pc_relative */
   1665 	 0,			/* bitpos */
   1666 	 complain_overflow_signed, /* complain_on_overflow */
   1667 	 ppc64_elf_unhandled_reloc, /* special_function */
   1668 	 "R_PPC64_GOT_TLSGD16",	/* name */
   1669 	 FALSE,			/* partial_inplace */
   1670 	 0,			/* src_mask */
   1671 	 0xffff,		/* dst_mask */
   1672 	 FALSE),		/* pcrel_offset */
   1673 
   1674   /* Like GOT_TLSGD16, but no overflow.  */
   1675   HOWTO (R_PPC64_GOT_TLSGD16_LO,
   1676 	 0,			/* rightshift */
   1677 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1678 	 16,			/* bitsize */
   1679 	 FALSE,			/* pc_relative */
   1680 	 0,			/* bitpos */
   1681 	 complain_overflow_dont, /* complain_on_overflow */
   1682 	 ppc64_elf_unhandled_reloc, /* special_function */
   1683 	 "R_PPC64_GOT_TLSGD16_LO", /* name */
   1684 	 FALSE,			/* partial_inplace */
   1685 	 0,			/* src_mask */
   1686 	 0xffff,		/* dst_mask */
   1687 	 FALSE),		/* pcrel_offset */
   1688 
   1689   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
   1690   HOWTO (R_PPC64_GOT_TLSGD16_HI,
   1691 	 16,			/* rightshift */
   1692 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1693 	 16,			/* bitsize */
   1694 	 FALSE,			/* pc_relative */
   1695 	 0,			/* bitpos */
   1696 	 complain_overflow_dont, /* complain_on_overflow */
   1697 	 ppc64_elf_unhandled_reloc, /* special_function */
   1698 	 "R_PPC64_GOT_TLSGD16_HI", /* name */
   1699 	 FALSE,			/* partial_inplace */
   1700 	 0,			/* src_mask */
   1701 	 0xffff,		/* dst_mask */
   1702 	 FALSE),		/* pcrel_offset */
   1703 
   1704   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
   1705   HOWTO (R_PPC64_GOT_TLSGD16_HA,
   1706 	 16,			/* rightshift */
   1707 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1708 	 16,			/* bitsize */
   1709 	 FALSE,			/* pc_relative */
   1710 	 0,			/* bitpos */
   1711 	 complain_overflow_dont, /* complain_on_overflow */
   1712 	 ppc64_elf_unhandled_reloc, /* special_function */
   1713 	 "R_PPC64_GOT_TLSGD16_HA", /* name */
   1714 	 FALSE,			/* partial_inplace */
   1715 	 0,			/* src_mask */
   1716 	 0xffff,		/* dst_mask */
   1717 	 FALSE),		/* pcrel_offset */
   1718 
   1719   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
   1720      with values (sym+add)@dtpmod and zero, and computes the offset to the
   1721      first entry relative to the TOC base (r2).  */
   1722   HOWTO (R_PPC64_GOT_TLSLD16,
   1723 	 0,			/* rightshift */
   1724 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1725 	 16,			/* bitsize */
   1726 	 FALSE,			/* pc_relative */
   1727 	 0,			/* bitpos */
   1728 	 complain_overflow_signed, /* complain_on_overflow */
   1729 	 ppc64_elf_unhandled_reloc, /* special_function */
   1730 	 "R_PPC64_GOT_TLSLD16",	/* name */
   1731 	 FALSE,			/* partial_inplace */
   1732 	 0,			/* src_mask */
   1733 	 0xffff,		/* dst_mask */
   1734 	 FALSE),		/* pcrel_offset */
   1735 
   1736   /* Like GOT_TLSLD16, but no overflow.  */
   1737   HOWTO (R_PPC64_GOT_TLSLD16_LO,
   1738 	 0,			/* rightshift */
   1739 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1740 	 16,			/* bitsize */
   1741 	 FALSE,			/* pc_relative */
   1742 	 0,			/* bitpos */
   1743 	 complain_overflow_dont, /* complain_on_overflow */
   1744 	 ppc64_elf_unhandled_reloc, /* special_function */
   1745 	 "R_PPC64_GOT_TLSLD16_LO", /* name */
   1746 	 FALSE,			/* partial_inplace */
   1747 	 0,			/* src_mask */
   1748 	 0xffff,		/* dst_mask */
   1749 	 FALSE),		/* pcrel_offset */
   1750 
   1751   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
   1752   HOWTO (R_PPC64_GOT_TLSLD16_HI,
   1753 	 16,			/* rightshift */
   1754 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1755 	 16,			/* bitsize */
   1756 	 FALSE,			/* pc_relative */
   1757 	 0,			/* bitpos */
   1758 	 complain_overflow_dont, /* complain_on_overflow */
   1759 	 ppc64_elf_unhandled_reloc, /* special_function */
   1760 	 "R_PPC64_GOT_TLSLD16_HI", /* name */
   1761 	 FALSE,			/* partial_inplace */
   1762 	 0,			/* src_mask */
   1763 	 0xffff,		/* dst_mask */
   1764 	 FALSE),		/* pcrel_offset */
   1765 
   1766   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
   1767   HOWTO (R_PPC64_GOT_TLSLD16_HA,
   1768 	 16,			/* rightshift */
   1769 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1770 	 16,			/* bitsize */
   1771 	 FALSE,			/* pc_relative */
   1772 	 0,			/* bitpos */
   1773 	 complain_overflow_dont, /* complain_on_overflow */
   1774 	 ppc64_elf_unhandled_reloc, /* special_function */
   1775 	 "R_PPC64_GOT_TLSLD16_HA", /* name */
   1776 	 FALSE,			/* partial_inplace */
   1777 	 0,			/* src_mask */
   1778 	 0xffff,		/* dst_mask */
   1779 	 FALSE),		/* pcrel_offset */
   1780 
   1781   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
   1782      the offset to the entry relative to the TOC base (r2).  */
   1783   HOWTO (R_PPC64_GOT_DTPREL16_DS,
   1784 	 0,			/* rightshift */
   1785 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1786 	 16,			/* bitsize */
   1787 	 FALSE,			/* pc_relative */
   1788 	 0,			/* bitpos */
   1789 	 complain_overflow_signed, /* complain_on_overflow */
   1790 	 ppc64_elf_unhandled_reloc, /* special_function */
   1791 	 "R_PPC64_GOT_DTPREL16_DS", /* name */
   1792 	 FALSE,			/* partial_inplace */
   1793 	 0,			/* src_mask */
   1794 	 0xfffc,		/* dst_mask */
   1795 	 FALSE),		/* pcrel_offset */
   1796 
   1797   /* Like GOT_DTPREL16_DS, but no overflow.  */
   1798   HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
   1799 	 0,			/* rightshift */
   1800 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1801 	 16,			/* bitsize */
   1802 	 FALSE,			/* pc_relative */
   1803 	 0,			/* bitpos */
   1804 	 complain_overflow_dont, /* complain_on_overflow */
   1805 	 ppc64_elf_unhandled_reloc, /* special_function */
   1806 	 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
   1807 	 FALSE,			/* partial_inplace */
   1808 	 0,			/* src_mask */
   1809 	 0xfffc,		/* dst_mask */
   1810 	 FALSE),		/* pcrel_offset */
   1811 
   1812   /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits.  */
   1813   HOWTO (R_PPC64_GOT_DTPREL16_HI,
   1814 	 16,			/* rightshift */
   1815 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1816 	 16,			/* bitsize */
   1817 	 FALSE,			/* pc_relative */
   1818 	 0,			/* bitpos */
   1819 	 complain_overflow_dont, /* complain_on_overflow */
   1820 	 ppc64_elf_unhandled_reloc, /* special_function */
   1821 	 "R_PPC64_GOT_DTPREL16_HI", /* name */
   1822 	 FALSE,			/* partial_inplace */
   1823 	 0,			/* src_mask */
   1824 	 0xffff,		/* dst_mask */
   1825 	 FALSE),		/* pcrel_offset */
   1826 
   1827   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
   1828   HOWTO (R_PPC64_GOT_DTPREL16_HA,
   1829 	 16,			/* rightshift */
   1830 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1831 	 16,			/* bitsize */
   1832 	 FALSE,			/* pc_relative */
   1833 	 0,			/* bitpos */
   1834 	 complain_overflow_dont, /* complain_on_overflow */
   1835 	 ppc64_elf_unhandled_reloc, /* special_function */
   1836 	 "R_PPC64_GOT_DTPREL16_HA", /* name */
   1837 	 FALSE,			/* partial_inplace */
   1838 	 0,			/* src_mask */
   1839 	 0xffff,		/* dst_mask */
   1840 	 FALSE),		/* pcrel_offset */
   1841 
   1842   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
   1843      offset to the entry relative to the TOC base (r2).  */
   1844   HOWTO (R_PPC64_GOT_TPREL16_DS,
   1845 	 0,			/* rightshift */
   1846 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1847 	 16,			/* bitsize */
   1848 	 FALSE,			/* pc_relative */
   1849 	 0,			/* bitpos */
   1850 	 complain_overflow_signed, /* complain_on_overflow */
   1851 	 ppc64_elf_unhandled_reloc, /* special_function */
   1852 	 "R_PPC64_GOT_TPREL16_DS", /* name */
   1853 	 FALSE,			/* partial_inplace */
   1854 	 0,			/* src_mask */
   1855 	 0xfffc,		/* dst_mask */
   1856 	 FALSE),		/* pcrel_offset */
   1857 
   1858   /* Like GOT_TPREL16_DS, but no overflow.  */
   1859   HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
   1860 	 0,			/* rightshift */
   1861 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1862 	 16,			/* bitsize */
   1863 	 FALSE,			/* pc_relative */
   1864 	 0,			/* bitpos */
   1865 	 complain_overflow_dont, /* complain_on_overflow */
   1866 	 ppc64_elf_unhandled_reloc, /* special_function */
   1867 	 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
   1868 	 FALSE,			/* partial_inplace */
   1869 	 0,			/* src_mask */
   1870 	 0xfffc,		/* dst_mask */
   1871 	 FALSE),		/* pcrel_offset */
   1872 
   1873   /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits.  */
   1874   HOWTO (R_PPC64_GOT_TPREL16_HI,
   1875 	 16,			/* rightshift */
   1876 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1877 	 16,			/* bitsize */
   1878 	 FALSE,			/* pc_relative */
   1879 	 0,			/* bitpos */
   1880 	 complain_overflow_dont, /* complain_on_overflow */
   1881 	 ppc64_elf_unhandled_reloc, /* special_function */
   1882 	 "R_PPC64_GOT_TPREL16_HI", /* name */
   1883 	 FALSE,			/* partial_inplace */
   1884 	 0,			/* src_mask */
   1885 	 0xffff,		/* dst_mask */
   1886 	 FALSE),		/* pcrel_offset */
   1887 
   1888   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
   1889   HOWTO (R_PPC64_GOT_TPREL16_HA,
   1890 	 16,			/* rightshift */
   1891 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1892 	 16,			/* bitsize */
   1893 	 FALSE,			/* pc_relative */
   1894 	 0,			/* bitpos */
   1895 	 complain_overflow_dont, /* complain_on_overflow */
   1896 	 ppc64_elf_unhandled_reloc, /* special_function */
   1897 	 "R_PPC64_GOT_TPREL16_HA", /* name */
   1898 	 FALSE,			/* partial_inplace */
   1899 	 0,			/* src_mask */
   1900 	 0xffff,		/* dst_mask */
   1901 	 FALSE),		/* pcrel_offset */
   1902 
   1903   HOWTO (R_PPC64_JMP_IREL,	/* type */
   1904 	 0,			/* rightshift */
   1905 	 0,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
   1906 	 0,			/* bitsize */
   1907 	 FALSE,			/* pc_relative */
   1908 	 0,			/* bitpos */
   1909 	 complain_overflow_dont, /* complain_on_overflow */
   1910 	 ppc64_elf_unhandled_reloc, /* special_function */
   1911 	 "R_PPC64_JMP_IREL",	/* name */
   1912 	 FALSE,			/* partial_inplace */
   1913 	 0,			/* src_mask */
   1914 	 0,			/* dst_mask */
   1915 	 FALSE),		/* pcrel_offset */
   1916 
   1917   HOWTO (R_PPC64_IRELATIVE,	/* type */
   1918 	 0,			/* rightshift */
   1919 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
   1920 	 64,			/* bitsize */
   1921 	 FALSE,			/* pc_relative */
   1922 	 0,			/* bitpos */
   1923 	 complain_overflow_dont, /* complain_on_overflow */
   1924 	 bfd_elf_generic_reloc,	/* special_function */
   1925 	 "R_PPC64_IRELATIVE",	/* name */
   1926 	 FALSE,			/* partial_inplace */
   1927 	 0,			/* src_mask */
   1928 	 ONES (64),		/* dst_mask */
   1929 	 FALSE),		/* pcrel_offset */
   1930 
   1931   /* A 16 bit relative relocation.  */
   1932   HOWTO (R_PPC64_REL16,		/* type */
   1933 	 0,			/* rightshift */
   1934 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1935 	 16,			/* bitsize */
   1936 	 TRUE,			/* pc_relative */
   1937 	 0,			/* bitpos */
   1938 	 complain_overflow_bitfield, /* complain_on_overflow */
   1939 	 bfd_elf_generic_reloc,	/* special_function */
   1940 	 "R_PPC64_REL16",	/* name */
   1941 	 FALSE,			/* partial_inplace */
   1942 	 0,			/* src_mask */
   1943 	 0xffff,		/* dst_mask */
   1944 	 TRUE),			/* pcrel_offset */
   1945 
   1946   /* A 16 bit relative relocation without overflow.  */
   1947   HOWTO (R_PPC64_REL16_LO,	/* type */
   1948 	 0,			/* rightshift */
   1949 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1950 	 16,			/* bitsize */
   1951 	 TRUE,			/* pc_relative */
   1952 	 0,			/* bitpos */
   1953 	 complain_overflow_dont,/* complain_on_overflow */
   1954 	 bfd_elf_generic_reloc,	/* special_function */
   1955 	 "R_PPC64_REL16_LO",	/* name */
   1956 	 FALSE,			/* partial_inplace */
   1957 	 0,			/* src_mask */
   1958 	 0xffff,		/* dst_mask */
   1959 	 TRUE),			/* pcrel_offset */
   1960 
   1961   /* The high order 16 bits of a relative address.  */
   1962   HOWTO (R_PPC64_REL16_HI,	/* type */
   1963 	 16,			/* rightshift */
   1964 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1965 	 16,			/* bitsize */
   1966 	 TRUE,			/* pc_relative */
   1967 	 0,			/* bitpos */
   1968 	 complain_overflow_dont, /* complain_on_overflow */
   1969 	 bfd_elf_generic_reloc,	/* special_function */
   1970 	 "R_PPC64_REL16_HI",	/* name */
   1971 	 FALSE,			/* partial_inplace */
   1972 	 0,			/* src_mask */
   1973 	 0xffff,		/* dst_mask */
   1974 	 TRUE),			/* pcrel_offset */
   1975 
   1976   /* The high order 16 bits of a relative address, plus 1 if the contents of
   1977      the low 16 bits, treated as a signed number, is negative.  */
   1978   HOWTO (R_PPC64_REL16_HA,	/* type */
   1979 	 16,			/* rightshift */
   1980 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1981 	 16,			/* bitsize */
   1982 	 TRUE,			/* pc_relative */
   1983 	 0,			/* bitpos */
   1984 	 complain_overflow_dont, /* complain_on_overflow */
   1985 	 ppc64_elf_ha_reloc,	/* special_function */
   1986 	 "R_PPC64_REL16_HA",	/* name */
   1987 	 FALSE,			/* partial_inplace */
   1988 	 0,			/* src_mask */
   1989 	 0xffff,		/* dst_mask */
   1990 	 TRUE),			/* pcrel_offset */
   1991 
   1992   /* GNU extension to record C++ vtable hierarchy.  */
   1993   HOWTO (R_PPC64_GNU_VTINHERIT,	/* type */
   1994 	 0,			/* rightshift */
   1995 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
   1996 	 0,			/* bitsize */
   1997 	 FALSE,			/* pc_relative */
   1998 	 0,			/* bitpos */
   1999 	 complain_overflow_dont, /* complain_on_overflow */
   2000 	 NULL,			/* special_function */
   2001 	 "R_PPC64_GNU_VTINHERIT", /* name */
   2002 	 FALSE,			/* partial_inplace */
   2003 	 0,			/* src_mask */
   2004 	 0,			/* dst_mask */
   2005 	 FALSE),		/* pcrel_offset */
   2006 
   2007   /* GNU extension to record C++ vtable member usage.  */
   2008   HOWTO (R_PPC64_GNU_VTENTRY,	/* type */
   2009 	 0,			/* rightshift */
   2010 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
   2011 	 0,			/* bitsize */
   2012 	 FALSE,			/* pc_relative */
   2013 	 0,			/* bitpos */
   2014 	 complain_overflow_dont, /* complain_on_overflow */
   2015 	 NULL,			/* special_function */
   2016 	 "R_PPC64_GNU_VTENTRY",	/* name */
   2017 	 FALSE,			/* partial_inplace */
   2018 	 0,			/* src_mask */
   2019 	 0,			/* dst_mask */
   2020 	 FALSE),		/* pcrel_offset */
   2021 };
   2022 
   2023 
   2024 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
   2026    be done.  */
   2027 
   2028 static void
   2029 ppc_howto_init (void)
   2030 {
   2031   unsigned int i, type;
   2032 
   2033   for (i = 0;
   2034        i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
   2035        i++)
   2036     {
   2037       type = ppc64_elf_howto_raw[i].type;
   2038       BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
   2039 			  / sizeof (ppc64_elf_howto_table[0])));
   2040       ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
   2041     }
   2042 }
   2043 
   2044 static reloc_howto_type *
   2045 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
   2046 			     bfd_reloc_code_real_type code)
   2047 {
   2048   enum elf_ppc64_reloc_type r = R_PPC64_NONE;
   2049 
   2050   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
   2051     /* Initialize howto table if needed.  */
   2052     ppc_howto_init ();
   2053 
   2054   switch (code)
   2055     {
   2056     default:
   2057       return NULL;
   2058 
   2059     case BFD_RELOC_NONE:			r = R_PPC64_NONE;
   2060       break;
   2061     case BFD_RELOC_32:				r = R_PPC64_ADDR32;
   2062       break;
   2063     case BFD_RELOC_PPC_BA26:			r = R_PPC64_ADDR24;
   2064       break;
   2065     case BFD_RELOC_16:				r = R_PPC64_ADDR16;
   2066       break;
   2067     case BFD_RELOC_LO16:			r = R_PPC64_ADDR16_LO;
   2068       break;
   2069     case BFD_RELOC_HI16:			r = R_PPC64_ADDR16_HI;
   2070       break;
   2071     case BFD_RELOC_HI16_S:			r = R_PPC64_ADDR16_HA;
   2072       break;
   2073     case BFD_RELOC_PPC_BA16:			r = R_PPC64_ADDR14;
   2074       break;
   2075     case BFD_RELOC_PPC_BA16_BRTAKEN:		r = R_PPC64_ADDR14_BRTAKEN;
   2076       break;
   2077     case BFD_RELOC_PPC_BA16_BRNTAKEN:		r = R_PPC64_ADDR14_BRNTAKEN;
   2078       break;
   2079     case BFD_RELOC_PPC_B26:			r = R_PPC64_REL24;
   2080       break;
   2081     case BFD_RELOC_PPC_B16:			r = R_PPC64_REL14;
   2082       break;
   2083     case BFD_RELOC_PPC_B16_BRTAKEN:		r = R_PPC64_REL14_BRTAKEN;
   2084       break;
   2085     case BFD_RELOC_PPC_B16_BRNTAKEN:		r = R_PPC64_REL14_BRNTAKEN;
   2086       break;
   2087     case BFD_RELOC_16_GOTOFF:			r = R_PPC64_GOT16;
   2088       break;
   2089     case BFD_RELOC_LO16_GOTOFF:			r = R_PPC64_GOT16_LO;
   2090       break;
   2091     case BFD_RELOC_HI16_GOTOFF:			r = R_PPC64_GOT16_HI;
   2092       break;
   2093     case BFD_RELOC_HI16_S_GOTOFF:		r = R_PPC64_GOT16_HA;
   2094       break;
   2095     case BFD_RELOC_PPC_COPY:			r = R_PPC64_COPY;
   2096       break;
   2097     case BFD_RELOC_PPC_GLOB_DAT:		r = R_PPC64_GLOB_DAT;
   2098       break;
   2099     case BFD_RELOC_32_PCREL:			r = R_PPC64_REL32;
   2100       break;
   2101     case BFD_RELOC_32_PLTOFF:			r = R_PPC64_PLT32;
   2102       break;
   2103     case BFD_RELOC_32_PLT_PCREL:		r = R_PPC64_PLTREL32;
   2104       break;
   2105     case BFD_RELOC_LO16_PLTOFF:			r = R_PPC64_PLT16_LO;
   2106       break;
   2107     case BFD_RELOC_HI16_PLTOFF:			r = R_PPC64_PLT16_HI;
   2108       break;
   2109     case BFD_RELOC_HI16_S_PLTOFF:		r = R_PPC64_PLT16_HA;
   2110       break;
   2111     case BFD_RELOC_16_BASEREL:			r = R_PPC64_SECTOFF;
   2112       break;
   2113     case BFD_RELOC_LO16_BASEREL:		r = R_PPC64_SECTOFF_LO;
   2114       break;
   2115     case BFD_RELOC_HI16_BASEREL:		r = R_PPC64_SECTOFF_HI;
   2116       break;
   2117     case BFD_RELOC_HI16_S_BASEREL:		r = R_PPC64_SECTOFF_HA;
   2118       break;
   2119     case BFD_RELOC_CTOR:			r = R_PPC64_ADDR64;
   2120       break;
   2121     case BFD_RELOC_64:				r = R_PPC64_ADDR64;
   2122       break;
   2123     case BFD_RELOC_PPC64_HIGHER:		r = R_PPC64_ADDR16_HIGHER;
   2124       break;
   2125     case BFD_RELOC_PPC64_HIGHER_S:		r = R_PPC64_ADDR16_HIGHERA;
   2126       break;
   2127     case BFD_RELOC_PPC64_HIGHEST:		r = R_PPC64_ADDR16_HIGHEST;
   2128       break;
   2129     case BFD_RELOC_PPC64_HIGHEST_S:		r = R_PPC64_ADDR16_HIGHESTA;
   2130       break;
   2131     case BFD_RELOC_64_PCREL:			r = R_PPC64_REL64;
   2132       break;
   2133     case BFD_RELOC_64_PLTOFF:			r = R_PPC64_PLT64;
   2134       break;
   2135     case BFD_RELOC_64_PLT_PCREL:		r = R_PPC64_PLTREL64;
   2136       break;
   2137     case BFD_RELOC_PPC_TOC16:			r = R_PPC64_TOC16;
   2138       break;
   2139     case BFD_RELOC_PPC64_TOC16_LO:		r = R_PPC64_TOC16_LO;
   2140       break;
   2141     case BFD_RELOC_PPC64_TOC16_HI:		r = R_PPC64_TOC16_HI;
   2142       break;
   2143     case BFD_RELOC_PPC64_TOC16_HA:		r = R_PPC64_TOC16_HA;
   2144       break;
   2145     case BFD_RELOC_PPC64_TOC:			r = R_PPC64_TOC;
   2146       break;
   2147     case BFD_RELOC_PPC64_PLTGOT16:		r = R_PPC64_PLTGOT16;
   2148       break;
   2149     case BFD_RELOC_PPC64_PLTGOT16_LO:		r = R_PPC64_PLTGOT16_LO;
   2150       break;
   2151     case BFD_RELOC_PPC64_PLTGOT16_HI:		r = R_PPC64_PLTGOT16_HI;
   2152       break;
   2153     case BFD_RELOC_PPC64_PLTGOT16_HA:		r = R_PPC64_PLTGOT16_HA;
   2154       break;
   2155     case BFD_RELOC_PPC64_ADDR16_DS:		r = R_PPC64_ADDR16_DS;
   2156       break;
   2157     case BFD_RELOC_PPC64_ADDR16_LO_DS:		r = R_PPC64_ADDR16_LO_DS;
   2158       break;
   2159     case BFD_RELOC_PPC64_GOT16_DS:		r = R_PPC64_GOT16_DS;
   2160       break;
   2161     case BFD_RELOC_PPC64_GOT16_LO_DS:		r = R_PPC64_GOT16_LO_DS;
   2162       break;
   2163     case BFD_RELOC_PPC64_PLT16_LO_DS:		r = R_PPC64_PLT16_LO_DS;
   2164       break;
   2165     case BFD_RELOC_PPC64_SECTOFF_DS:		r = R_PPC64_SECTOFF_DS;
   2166       break;
   2167     case BFD_RELOC_PPC64_SECTOFF_LO_DS:		r = R_PPC64_SECTOFF_LO_DS;
   2168       break;
   2169     case BFD_RELOC_PPC64_TOC16_DS:		r = R_PPC64_TOC16_DS;
   2170       break;
   2171     case BFD_RELOC_PPC64_TOC16_LO_DS:		r = R_PPC64_TOC16_LO_DS;
   2172       break;
   2173     case BFD_RELOC_PPC64_PLTGOT16_DS:		r = R_PPC64_PLTGOT16_DS;
   2174       break;
   2175     case BFD_RELOC_PPC64_PLTGOT16_LO_DS:	r = R_PPC64_PLTGOT16_LO_DS;
   2176       break;
   2177     case BFD_RELOC_PPC_TLS:			r = R_PPC64_TLS;
   2178       break;
   2179     case BFD_RELOC_PPC_TLSGD:			r = R_PPC64_TLSGD;
   2180       break;
   2181     case BFD_RELOC_PPC_TLSLD:			r = R_PPC64_TLSLD;
   2182       break;
   2183     case BFD_RELOC_PPC_DTPMOD:			r = R_PPC64_DTPMOD64;
   2184       break;
   2185     case BFD_RELOC_PPC_TPREL16:			r = R_PPC64_TPREL16;
   2186       break;
   2187     case BFD_RELOC_PPC_TPREL16_LO:		r = R_PPC64_TPREL16_LO;
   2188       break;
   2189     case BFD_RELOC_PPC_TPREL16_HI:		r = R_PPC64_TPREL16_HI;
   2190       break;
   2191     case BFD_RELOC_PPC_TPREL16_HA:		r = R_PPC64_TPREL16_HA;
   2192       break;
   2193     case BFD_RELOC_PPC_TPREL:			r = R_PPC64_TPREL64;
   2194       break;
   2195     case BFD_RELOC_PPC_DTPREL16:		r = R_PPC64_DTPREL16;
   2196       break;
   2197     case BFD_RELOC_PPC_DTPREL16_LO:		r = R_PPC64_DTPREL16_LO;
   2198       break;
   2199     case BFD_RELOC_PPC_DTPREL16_HI:		r = R_PPC64_DTPREL16_HI;
   2200       break;
   2201     case BFD_RELOC_PPC_DTPREL16_HA:		r = R_PPC64_DTPREL16_HA;
   2202       break;
   2203     case BFD_RELOC_PPC_DTPREL:			r = R_PPC64_DTPREL64;
   2204       break;
   2205     case BFD_RELOC_PPC_GOT_TLSGD16:		r = R_PPC64_GOT_TLSGD16;
   2206       break;
   2207     case BFD_RELOC_PPC_GOT_TLSGD16_LO:		r = R_PPC64_GOT_TLSGD16_LO;
   2208       break;
   2209     case BFD_RELOC_PPC_GOT_TLSGD16_HI:		r = R_PPC64_GOT_TLSGD16_HI;
   2210       break;
   2211     case BFD_RELOC_PPC_GOT_TLSGD16_HA:		r = R_PPC64_GOT_TLSGD16_HA;
   2212       break;
   2213     case BFD_RELOC_PPC_GOT_TLSLD16:		r = R_PPC64_GOT_TLSLD16;
   2214       break;
   2215     case BFD_RELOC_PPC_GOT_TLSLD16_LO:		r = R_PPC64_GOT_TLSLD16_LO;
   2216       break;
   2217     case BFD_RELOC_PPC_GOT_TLSLD16_HI:		r = R_PPC64_GOT_TLSLD16_HI;
   2218       break;
   2219     case BFD_RELOC_PPC_GOT_TLSLD16_HA:		r = R_PPC64_GOT_TLSLD16_HA;
   2220       break;
   2221     case BFD_RELOC_PPC_GOT_TPREL16:		r = R_PPC64_GOT_TPREL16_DS;
   2222       break;
   2223     case BFD_RELOC_PPC_GOT_TPREL16_LO:		r = R_PPC64_GOT_TPREL16_LO_DS;
   2224       break;
   2225     case BFD_RELOC_PPC_GOT_TPREL16_HI:		r = R_PPC64_GOT_TPREL16_HI;
   2226       break;
   2227     case BFD_RELOC_PPC_GOT_TPREL16_HA:		r = R_PPC64_GOT_TPREL16_HA;
   2228       break;
   2229     case BFD_RELOC_PPC_GOT_DTPREL16:		r = R_PPC64_GOT_DTPREL16_DS;
   2230       break;
   2231     case BFD_RELOC_PPC_GOT_DTPREL16_LO:		r = R_PPC64_GOT_DTPREL16_LO_DS;
   2232       break;
   2233     case BFD_RELOC_PPC_GOT_DTPREL16_HI:		r = R_PPC64_GOT_DTPREL16_HI;
   2234       break;
   2235     case BFD_RELOC_PPC_GOT_DTPREL16_HA:		r = R_PPC64_GOT_DTPREL16_HA;
   2236       break;
   2237     case BFD_RELOC_PPC64_TPREL16_DS:		r = R_PPC64_TPREL16_DS;
   2238       break;
   2239     case BFD_RELOC_PPC64_TPREL16_LO_DS:		r = R_PPC64_TPREL16_LO_DS;
   2240       break;
   2241     case BFD_RELOC_PPC64_TPREL16_HIGHER:	r = R_PPC64_TPREL16_HIGHER;
   2242       break;
   2243     case BFD_RELOC_PPC64_TPREL16_HIGHERA:	r = R_PPC64_TPREL16_HIGHERA;
   2244       break;
   2245     case BFD_RELOC_PPC64_TPREL16_HIGHEST:	r = R_PPC64_TPREL16_HIGHEST;
   2246       break;
   2247     case BFD_RELOC_PPC64_TPREL16_HIGHESTA:	r = R_PPC64_TPREL16_HIGHESTA;
   2248       break;
   2249     case BFD_RELOC_PPC64_DTPREL16_DS:		r = R_PPC64_DTPREL16_DS;
   2250       break;
   2251     case BFD_RELOC_PPC64_DTPREL16_LO_DS:	r = R_PPC64_DTPREL16_LO_DS;
   2252       break;
   2253     case BFD_RELOC_PPC64_DTPREL16_HIGHER:	r = R_PPC64_DTPREL16_HIGHER;
   2254       break;
   2255     case BFD_RELOC_PPC64_DTPREL16_HIGHERA:	r = R_PPC64_DTPREL16_HIGHERA;
   2256       break;
   2257     case BFD_RELOC_PPC64_DTPREL16_HIGHEST:	r = R_PPC64_DTPREL16_HIGHEST;
   2258       break;
   2259     case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:	r = R_PPC64_DTPREL16_HIGHESTA;
   2260       break;
   2261     case BFD_RELOC_16_PCREL:			r = R_PPC64_REL16;
   2262       break;
   2263     case BFD_RELOC_LO16_PCREL:			r = R_PPC64_REL16_LO;
   2264       break;
   2265     case BFD_RELOC_HI16_PCREL:			r = R_PPC64_REL16_HI;
   2266       break;
   2267     case BFD_RELOC_HI16_S_PCREL:		r = R_PPC64_REL16_HA;
   2268       break;
   2269     case BFD_RELOC_VTABLE_INHERIT:		r = R_PPC64_GNU_VTINHERIT;
   2270       break;
   2271     case BFD_RELOC_VTABLE_ENTRY:		r = R_PPC64_GNU_VTENTRY;
   2272       break;
   2273     }
   2274 
   2275   return ppc64_elf_howto_table[r];
   2276 };
   2277 
   2278 static reloc_howto_type *
   2279 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
   2280 			     const char *r_name)
   2281 {
   2282   unsigned int i;
   2283 
   2284   for (i = 0;
   2285        i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
   2286        i++)
   2287     if (ppc64_elf_howto_raw[i].name != NULL
   2288 	&& strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
   2289       return &ppc64_elf_howto_raw[i];
   2290 
   2291   return NULL;
   2292 }
   2293 
   2294 /* Set the howto pointer for a PowerPC ELF reloc.  */
   2295 
   2296 static void
   2297 ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
   2298 			 Elf_Internal_Rela *dst)
   2299 {
   2300   unsigned int type;
   2301 
   2302   /* Initialize howto table if needed.  */
   2303   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
   2304     ppc_howto_init ();
   2305 
   2306   type = ELF64_R_TYPE (dst->r_info);
   2307   if (type >= (sizeof (ppc64_elf_howto_table)
   2308 	       / sizeof (ppc64_elf_howto_table[0])))
   2309     {
   2310       (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
   2311 			     abfd, (int) type);
   2312       type = R_PPC64_NONE;
   2313     }
   2314   cache_ptr->howto = ppc64_elf_howto_table[type];
   2315 }
   2316 
   2317 /* Handle the R_PPC64_ADDR16_HA and similar relocs.  */
   2318 
   2319 static bfd_reloc_status_type
   2320 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2321 		    void *data, asection *input_section,
   2322 		    bfd *output_bfd, char **error_message)
   2323 {
   2324   /* If this is a relocatable link (output_bfd test tells us), just
   2325      call the generic function.  Any adjustment will be done at final
   2326      link time.  */
   2327   if (output_bfd != NULL)
   2328     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2329 				  input_section, output_bfd, error_message);
   2330 
   2331   /* Adjust the addend for sign extension of the low 16 bits.
   2332      We won't actually be using the low 16 bits, so trashing them
   2333      doesn't matter.  */
   2334   reloc_entry->addend += 0x8000;
   2335   return bfd_reloc_continue;
   2336 }
   2337 
   2338 static bfd_reloc_status_type
   2339 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2340 			void *data, asection *input_section,
   2341 			bfd *output_bfd, char **error_message)
   2342 {
   2343   if (output_bfd != NULL)
   2344     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2345 				  input_section, output_bfd, error_message);
   2346 
   2347   if (strcmp (symbol->section->name, ".opd") == 0
   2348       && (symbol->section->owner->flags & DYNAMIC) == 0)
   2349     {
   2350       bfd_vma dest = opd_entry_value (symbol->section,
   2351 				      symbol->value + reloc_entry->addend,
   2352 				      NULL, NULL, FALSE);
   2353       if (dest != (bfd_vma) -1)
   2354 	reloc_entry->addend = dest - (symbol->value
   2355 				      + symbol->section->output_section->vma
   2356 				      + symbol->section->output_offset);
   2357     }
   2358   return bfd_reloc_continue;
   2359 }
   2360 
   2361 static bfd_reloc_status_type
   2362 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2363 			 void *data, asection *input_section,
   2364 			 bfd *output_bfd, char **error_message)
   2365 {
   2366   long insn;
   2367   enum elf_ppc64_reloc_type r_type;
   2368   bfd_size_type octets;
   2369   /* Assume 'at' branch hints.  */
   2370   bfd_boolean is_isa_v2 = TRUE;
   2371 
   2372   /* If this is a relocatable link (output_bfd test tells us), just
   2373      call the generic function.  Any adjustment will be done at final
   2374      link time.  */
   2375   if (output_bfd != NULL)
   2376     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2377 				  input_section, output_bfd, error_message);
   2378 
   2379   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
   2380   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
   2381   insn &= ~(0x01 << 21);
   2382   r_type = reloc_entry->howto->type;
   2383   if (r_type == R_PPC64_ADDR14_BRTAKEN
   2384       || r_type == R_PPC64_REL14_BRTAKEN)
   2385     insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
   2386 
   2387   if (is_isa_v2)
   2388     {
   2389       /* Set 'a' bit.  This is 0b00010 in BO field for branch
   2390 	 on CR(BI) insns (BO == 001at or 011at), and 0b01000
   2391 	 for branch on CTR insns (BO == 1a00t or 1a01t).  */
   2392       if ((insn & (0x14 << 21)) == (0x04 << 21))
   2393 	insn |= 0x02 << 21;
   2394       else if ((insn & (0x14 << 21)) == (0x10 << 21))
   2395 	insn |= 0x08 << 21;
   2396       else
   2397 	goto out;
   2398     }
   2399   else
   2400     {
   2401       bfd_vma target = 0;
   2402       bfd_vma from;
   2403 
   2404       if (!bfd_is_com_section (symbol->section))
   2405 	target = symbol->value;
   2406       target += symbol->section->output_section->vma;
   2407       target += symbol->section->output_offset;
   2408       target += reloc_entry->addend;
   2409 
   2410       from = (reloc_entry->address
   2411 	      + input_section->output_offset
   2412 	      + input_section->output_section->vma);
   2413 
   2414       /* Invert 'y' bit if not the default.  */
   2415       if ((bfd_signed_vma) (target - from) < 0)
   2416 	insn ^= 0x01 << 21;
   2417     }
   2418   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
   2419  out:
   2420   return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
   2421 				 input_section, output_bfd, error_message);
   2422 }
   2423 
   2424 static bfd_reloc_status_type
   2425 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2426 			 void *data, asection *input_section,
   2427 			 bfd *output_bfd, char **error_message)
   2428 {
   2429   /* If this is a relocatable link (output_bfd test tells us), just
   2430      call the generic function.  Any adjustment will be done at final
   2431      link time.  */
   2432   if (output_bfd != NULL)
   2433     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2434 				  input_section, output_bfd, error_message);
   2435 
   2436   /* Subtract the symbol section base address.  */
   2437   reloc_entry->addend -= symbol->section->output_section->vma;
   2438   return bfd_reloc_continue;
   2439 }
   2440 
   2441 static bfd_reloc_status_type
   2442 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2443 			    void *data, asection *input_section,
   2444 			    bfd *output_bfd, char **error_message)
   2445 {
   2446   /* If this is a relocatable link (output_bfd test tells us), just
   2447      call the generic function.  Any adjustment will be done at final
   2448      link time.  */
   2449   if (output_bfd != NULL)
   2450     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2451 				  input_section, output_bfd, error_message);
   2452 
   2453   /* Subtract the symbol section base address.  */
   2454   reloc_entry->addend -= symbol->section->output_section->vma;
   2455 
   2456   /* Adjust the addend for sign extension of the low 16 bits.  */
   2457   reloc_entry->addend += 0x8000;
   2458   return bfd_reloc_continue;
   2459 }
   2460 
   2461 static bfd_reloc_status_type
   2462 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2463 		     void *data, asection *input_section,
   2464 		     bfd *output_bfd, char **error_message)
   2465 {
   2466   bfd_vma TOCstart;
   2467 
   2468   /* If this is a relocatable link (output_bfd test tells us), just
   2469      call the generic function.  Any adjustment will be done at final
   2470      link time.  */
   2471   if (output_bfd != NULL)
   2472     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2473 				  input_section, output_bfd, error_message);
   2474 
   2475   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
   2476   if (TOCstart == 0)
   2477     TOCstart = ppc64_elf_toc (input_section->output_section->owner);
   2478 
   2479   /* Subtract the TOC base address.  */
   2480   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
   2481   return bfd_reloc_continue;
   2482 }
   2483 
   2484 static bfd_reloc_status_type
   2485 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2486 			void *data, asection *input_section,
   2487 			bfd *output_bfd, char **error_message)
   2488 {
   2489   bfd_vma TOCstart;
   2490 
   2491   /* If this is a relocatable link (output_bfd test tells us), just
   2492      call the generic function.  Any adjustment will be done at final
   2493      link time.  */
   2494   if (output_bfd != NULL)
   2495     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2496 				  input_section, output_bfd, error_message);
   2497 
   2498   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
   2499   if (TOCstart == 0)
   2500     TOCstart = ppc64_elf_toc (input_section->output_section->owner);
   2501 
   2502   /* Subtract the TOC base address.  */
   2503   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
   2504 
   2505   /* Adjust the addend for sign extension of the low 16 bits.  */
   2506   reloc_entry->addend += 0x8000;
   2507   return bfd_reloc_continue;
   2508 }
   2509 
   2510 static bfd_reloc_status_type
   2511 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2512 		       void *data, asection *input_section,
   2513 		       bfd *output_bfd, char **error_message)
   2514 {
   2515   bfd_vma TOCstart;
   2516   bfd_size_type octets;
   2517 
   2518   /* If this is a relocatable link (output_bfd test tells us), just
   2519      call the generic function.  Any adjustment will be done at final
   2520      link time.  */
   2521   if (output_bfd != NULL)
   2522     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2523 				  input_section, output_bfd, error_message);
   2524 
   2525   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
   2526   if (TOCstart == 0)
   2527     TOCstart = ppc64_elf_toc (input_section->output_section->owner);
   2528 
   2529   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
   2530   bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
   2531   return bfd_reloc_ok;
   2532 }
   2533 
   2534 static bfd_reloc_status_type
   2535 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2536 			   void *data, asection *input_section,
   2537 			   bfd *output_bfd, char **error_message)
   2538 {
   2539   /* If this is a relocatable link (output_bfd test tells us), just
   2540      call the generic function.  Any adjustment will be done at final
   2541      link time.  */
   2542   if (output_bfd != NULL)
   2543     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2544 				  input_section, output_bfd, error_message);
   2545 
   2546   if (error_message != NULL)
   2547     {
   2548       static char buf[60];
   2549       sprintf (buf, "generic linker can't handle %s",
   2550 	       reloc_entry->howto->name);
   2551       *error_message = buf;
   2552     }
   2553   return bfd_reloc_dangerous;
   2554 }
   2555 
   2556 /* Track GOT entries needed for a given symbol.  We might need more
   2557    than one got entry per symbol.  */
   2558 struct got_entry
   2559 {
   2560   struct got_entry *next;
   2561 
   2562   /* The symbol addend that we'll be placing in the GOT.  */
   2563   bfd_vma addend;
   2564 
   2565   /* Unlike other ELF targets, we use separate GOT entries for the same
   2566      symbol referenced from different input files.  This is to support
   2567      automatic multiple TOC/GOT sections, where the TOC base can vary
   2568      from one input file to another.  After partitioning into TOC groups
   2569      we merge entries within the group.
   2570 
   2571      Point to the BFD owning this GOT entry.  */
   2572   bfd *owner;
   2573 
   2574   /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
   2575      TLS_TPREL or TLS_DTPREL for tls entries.  */
   2576   unsigned char tls_type;
   2577 
   2578   /* Non-zero if got.ent points to real entry.  */
   2579   unsigned char is_indirect;
   2580 
   2581   /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
   2582   union
   2583     {
   2584       bfd_signed_vma refcount;
   2585       bfd_vma offset;
   2586       struct got_entry *ent;
   2587     } got;
   2588 };
   2589 
   2590 /* The same for PLT.  */
   2591 struct plt_entry
   2592 {
   2593   struct plt_entry *next;
   2594 
   2595   bfd_vma addend;
   2596 
   2597   union
   2598     {
   2599       bfd_signed_vma refcount;
   2600       bfd_vma offset;
   2601     } plt;
   2602 };
   2603 
   2604 struct ppc64_elf_obj_tdata
   2605 {
   2606   struct elf_obj_tdata elf;
   2607 
   2608   /* Shortcuts to dynamic linker sections.  */
   2609   asection *got;
   2610   asection *relgot;
   2611 
   2612   /* Used during garbage collection.  We attach global symbols defined
   2613      on removed .opd entries to this section so that the sym is removed.  */
   2614   asection *deleted_section;
   2615 
   2616   /* TLS local dynamic got entry handling.  Support for multiple GOT
   2617      sections means we potentially need one of these for each input bfd.  */
   2618   struct got_entry tlsld_got;
   2619 
   2620   /* A copy of relocs before they are modified for --emit-relocs.  */
   2621   Elf_Internal_Rela *opd_relocs;
   2622 
   2623   /* Nonzero if this bfd has small toc/got relocs, ie. that expect
   2624      the reloc to be in the range -32768 to 32767.  */
   2625   unsigned int has_small_toc_reloc : 1;
   2626 
   2627   /* Set if toc/got ha relocs detected not using r2, or lo reloc
   2628      instruction not one we handle.  */
   2629   unsigned int unexpected_toc_insn : 1;
   2630 };
   2631 
   2632 #define ppc64_elf_tdata(bfd) \
   2633   ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
   2634 
   2635 #define ppc64_tlsld_got(bfd) \
   2636   (&ppc64_elf_tdata (bfd)->tlsld_got)
   2637 
   2638 #define is_ppc64_elf(bfd) \
   2639   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
   2640    && elf_object_id (bfd) == PPC64_ELF_DATA)
   2641 
   2642 /* Override the generic function because we store some extras.  */
   2643 
   2644 static bfd_boolean
   2645 ppc64_elf_mkobject (bfd *abfd)
   2646 {
   2647   return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
   2648 				  PPC64_ELF_DATA);
   2649 }
   2650 
   2651 /* Fix bad default arch selected for a 64 bit input bfd when the
   2652    default is 32 bit.  */
   2653 
   2654 static bfd_boolean
   2655 ppc64_elf_object_p (bfd *abfd)
   2656 {
   2657   if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
   2658     {
   2659       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
   2660 
   2661       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
   2662 	{
   2663 	  /* Relies on arch after 32 bit default being 64 bit default.  */
   2664 	  abfd->arch_info = abfd->arch_info->next;
   2665 	  BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
   2666 	}
   2667     }
   2668   return TRUE;
   2669 }
   2670 
   2671 /* Support for core dump NOTE sections.  */
   2672 
   2673 static bfd_boolean
   2674 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
   2675 {
   2676   size_t offset, size;
   2677 
   2678   if (note->descsz != 504)
   2679     return FALSE;
   2680 
   2681   /* pr_cursig */
   2682   elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
   2683 
   2684   /* pr_pid */
   2685   elf_tdata (abfd)->core_lwpid = bfd_get_32 (abfd, note->descdata + 32);
   2686 
   2687   /* pr_reg */
   2688   offset = 112;
   2689   size = 384;
   2690 
   2691   /* Make a ".reg/999" section.  */
   2692   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
   2693 					  size, note->descpos + offset);
   2694 }
   2695 
   2696 static bfd_boolean
   2697 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
   2698 {
   2699   if (note->descsz != 136)
   2700     return FALSE;
   2701 
   2702   elf_tdata (abfd)->core_pid
   2703     = bfd_get_32 (abfd, note->descdata + 24);
   2704   elf_tdata (abfd)->core_program
   2705     = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
   2706   elf_tdata (abfd)->core_command
   2707     = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
   2708 
   2709   return TRUE;
   2710 }
   2711 
   2712 static char *
   2713 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
   2714 			   ...)
   2715 {
   2716   switch (note_type)
   2717     {
   2718     default:
   2719       return NULL;
   2720 
   2721     case NT_PRPSINFO:
   2722       {
   2723 	char data[136];
   2724 	va_list ap;
   2725 
   2726 	va_start (ap, note_type);
   2727 	memset (data, 0, sizeof (data));
   2728 	strncpy (data + 40, va_arg (ap, const char *), 16);
   2729 	strncpy (data + 56, va_arg (ap, const char *), 80);
   2730 	va_end (ap);
   2731 	return elfcore_write_note (abfd, buf, bufsiz,
   2732 				   "CORE", note_type, data, sizeof (data));
   2733       }
   2734 
   2735     case NT_PRSTATUS:
   2736       {
   2737 	char data[504];
   2738 	va_list ap;
   2739 	long pid;
   2740 	int cursig;
   2741 	const void *greg;
   2742 
   2743 	va_start (ap, note_type);
   2744 	memset (data, 0, 112);
   2745 	pid = va_arg (ap, long);
   2746 	bfd_put_32 (abfd, pid, data + 32);
   2747 	cursig = va_arg (ap, int);
   2748 	bfd_put_16 (abfd, cursig, data + 12);
   2749 	greg = va_arg (ap, const void *);
   2750 	memcpy (data + 112, greg, 384);
   2751 	memset (data + 496, 0, 8);
   2752 	va_end (ap);
   2753 	return elfcore_write_note (abfd, buf, bufsiz,
   2754 				   "CORE", note_type, data, sizeof (data));
   2755       }
   2756     }
   2757 }
   2758 
   2759 /* Add extra PPC sections.  */
   2760 
   2761 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
   2762 {
   2763   { STRING_COMMA_LEN (".plt"),    0, SHT_NOBITS,   0 },
   2764   { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
   2765   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   2766   { STRING_COMMA_LEN (".toc"),    0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   2767   { STRING_COMMA_LEN (".toc1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   2768   { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
   2769   { NULL,                     0,  0, 0,            0 }
   2770 };
   2771 
   2772 enum _ppc64_sec_type {
   2773   sec_normal = 0,
   2774   sec_opd = 1,
   2775   sec_toc = 2
   2776 };
   2777 
   2778 struct _ppc64_elf_section_data
   2779 {
   2780   struct bfd_elf_section_data elf;
   2781 
   2782   union
   2783   {
   2784     /* An array with one entry for each opd function descriptor.  */
   2785     struct _opd_sec_data
   2786     {
   2787       /* Points to the function code section for local opd entries.  */
   2788       asection **func_sec;
   2789 
   2790       /* After editing .opd, adjust references to opd local syms.  */
   2791       long *adjust;
   2792     } opd;
   2793 
   2794     /* An array for toc sections, indexed by offset/8.  */
   2795     struct _toc_sec_data
   2796     {
   2797       /* Specifies the relocation symbol index used at a given toc offset.  */
   2798       unsigned *symndx;
   2799 
   2800       /* And the relocation addend.  */
   2801       bfd_vma *add;
   2802     } toc;
   2803   } u;
   2804 
   2805   enum _ppc64_sec_type sec_type:2;
   2806 
   2807   /* Flag set when small branches are detected.  Used to
   2808      select suitable defaults for the stub group size.  */
   2809   unsigned int has_14bit_branch:1;
   2810 };
   2811 
   2812 #define ppc64_elf_section_data(sec) \
   2813   ((struct _ppc64_elf_section_data *) elf_section_data (sec))
   2814 
   2815 static bfd_boolean
   2816 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
   2817 {
   2818   if (!sec->used_by_bfd)
   2819     {
   2820       struct _ppc64_elf_section_data *sdata;
   2821       bfd_size_type amt = sizeof (*sdata);
   2822 
   2823       sdata = bfd_zalloc (abfd, amt);
   2824       if (sdata == NULL)
   2825 	return FALSE;
   2826       sec->used_by_bfd = sdata;
   2827     }
   2828 
   2829   return _bfd_elf_new_section_hook (abfd, sec);
   2830 }
   2831 
   2832 static struct _opd_sec_data *
   2833 get_opd_info (asection * sec)
   2834 {
   2835   if (sec != NULL
   2836       && ppc64_elf_section_data (sec) != NULL
   2837       && ppc64_elf_section_data (sec)->sec_type == sec_opd)
   2838     return &ppc64_elf_section_data (sec)->u.opd;
   2839   return NULL;
   2840 }
   2841 
   2842 /* Parameters for the qsort hook.  */
   2844 static bfd_boolean synthetic_relocatable;
   2845 
   2846 /* qsort comparison function for ppc64_elf_get_synthetic_symtab.  */
   2847 
   2848 static int
   2849 compare_symbols (const void *ap, const void *bp)
   2850 {
   2851   const asymbol *a = * (const asymbol **) ap;
   2852   const asymbol *b = * (const asymbol **) bp;
   2853 
   2854   /* Section symbols first.  */
   2855   if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
   2856     return -1;
   2857   if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
   2858     return 1;
   2859 
   2860   /* then .opd symbols.  */
   2861   if (strcmp (a->section->name, ".opd") == 0
   2862       && strcmp (b->section->name, ".opd") != 0)
   2863     return -1;
   2864   if (strcmp (a->section->name, ".opd") != 0
   2865       && strcmp (b->section->name, ".opd") == 0)
   2866     return 1;
   2867 
   2868   /* then other code symbols.  */
   2869   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   2870       == (SEC_CODE | SEC_ALLOC)
   2871       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   2872 	 != (SEC_CODE | SEC_ALLOC))
   2873     return -1;
   2874 
   2875   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   2876       != (SEC_CODE | SEC_ALLOC)
   2877       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   2878 	 == (SEC_CODE | SEC_ALLOC))
   2879     return 1;
   2880 
   2881   if (synthetic_relocatable)
   2882     {
   2883       if (a->section->id < b->section->id)
   2884 	return -1;
   2885 
   2886       if (a->section->id > b->section->id)
   2887 	return 1;
   2888     }
   2889 
   2890   if (a->value + a->section->vma < b->value + b->section->vma)
   2891     return -1;
   2892 
   2893   if (a->value + a->section->vma > b->value + b->section->vma)
   2894     return 1;
   2895 
   2896   /* For syms with the same value, prefer strong dynamic global function
   2897      syms over other syms.  */
   2898   if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
   2899     return -1;
   2900 
   2901   if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
   2902     return 1;
   2903 
   2904   if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
   2905     return -1;
   2906 
   2907   if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
   2908     return 1;
   2909 
   2910   if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
   2911     return -1;
   2912 
   2913   if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
   2914     return 1;
   2915 
   2916   if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
   2917     return -1;
   2918 
   2919   if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
   2920     return 1;
   2921 
   2922   return 0;
   2923 }
   2924 
   2925 /* Search SYMS for a symbol of the given VALUE.  */
   2926 
   2927 static asymbol *
   2928 sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
   2929 {
   2930   long mid;
   2931 
   2932   if (id == -1)
   2933     {
   2934       while (lo < hi)
   2935 	{
   2936 	  mid = (lo + hi) >> 1;
   2937 	  if (syms[mid]->value + syms[mid]->section->vma < value)
   2938 	    lo = mid + 1;
   2939 	  else if (syms[mid]->value + syms[mid]->section->vma > value)
   2940 	    hi = mid;
   2941 	  else
   2942 	    return syms[mid];
   2943 	}
   2944     }
   2945   else
   2946     {
   2947       while (lo < hi)
   2948 	{
   2949 	  mid = (lo + hi) >> 1;
   2950 	  if (syms[mid]->section->id < id)
   2951 	    lo = mid + 1;
   2952 	  else if (syms[mid]->section->id > id)
   2953 	    hi = mid;
   2954 	  else if (syms[mid]->value < value)
   2955 	    lo = mid + 1;
   2956 	  else if (syms[mid]->value > value)
   2957 	    hi = mid;
   2958 	  else
   2959 	    return syms[mid];
   2960 	}
   2961     }
   2962   return NULL;
   2963 }
   2964 
   2965 static bfd_boolean
   2966 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
   2967 {
   2968   bfd_vma vma = *(bfd_vma *) ptr;
   2969   return ((section->flags & SEC_ALLOC) != 0
   2970 	  && section->vma <= vma
   2971 	  && vma < section->vma + section->size);
   2972 }
   2973 
   2974 /* Create synthetic symbols, effectively restoring "dot-symbol" function
   2975    entry syms.  Also generate @plt symbols for the glink branch table.  */
   2976 
   2977 static long
   2978 ppc64_elf_get_synthetic_symtab (bfd *abfd,
   2979 				long static_count, asymbol **static_syms,
   2980 				long dyn_count, asymbol **dyn_syms,
   2981 				asymbol **ret)
   2982 {
   2983   asymbol *s;
   2984   long i;
   2985   long count;
   2986   char *names;
   2987   long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
   2988   asection *opd;
   2989   bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
   2990   asymbol **syms;
   2991 
   2992   *ret = NULL;
   2993 
   2994   opd = bfd_get_section_by_name (abfd, ".opd");
   2995   if (opd == NULL)
   2996     return 0;
   2997 
   2998   symcount = static_count;
   2999   if (!relocatable)
   3000     symcount += dyn_count;
   3001   if (symcount == 0)
   3002     return 0;
   3003 
   3004   syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
   3005   if (syms == NULL)
   3006     return -1;
   3007 
   3008   if (!relocatable && static_count != 0 && dyn_count != 0)
   3009     {
   3010       /* Use both symbol tables.  */
   3011       memcpy (syms, static_syms, static_count * sizeof (*syms));
   3012       memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
   3013     }
   3014   else if (!relocatable && static_count == 0)
   3015     memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
   3016   else
   3017     memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
   3018 
   3019   synthetic_relocatable = relocatable;
   3020   qsort (syms, symcount, sizeof (*syms), compare_symbols);
   3021 
   3022   if (!relocatable && symcount > 1)
   3023     {
   3024       long j;
   3025       /* Trim duplicate syms, since we may have merged the normal and
   3026 	 dynamic symbols.  Actually, we only care about syms that have
   3027 	 different values, so trim any with the same value.  */
   3028       for (i = 1, j = 1; i < symcount; ++i)
   3029 	if (syms[i - 1]->value + syms[i - 1]->section->vma
   3030 	    != syms[i]->value + syms[i]->section->vma)
   3031 	  syms[j++] = syms[i];
   3032       symcount = j;
   3033     }
   3034 
   3035   i = 0;
   3036   if (strcmp (syms[i]->section->name, ".opd") == 0)
   3037     ++i;
   3038   codesecsym = i;
   3039 
   3040   for (; i < symcount; ++i)
   3041     if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   3042 	 != (SEC_CODE | SEC_ALLOC))
   3043 	|| (syms[i]->flags & BSF_SECTION_SYM) == 0)
   3044       break;
   3045   codesecsymend = i;
   3046 
   3047   for (; i < symcount; ++i)
   3048     if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
   3049       break;
   3050   secsymend = i;
   3051 
   3052   for (; i < symcount; ++i)
   3053     if (strcmp (syms[i]->section->name, ".opd") != 0)
   3054       break;
   3055   opdsymend = i;
   3056 
   3057   for (; i < symcount; ++i)
   3058     if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   3059 	!= (SEC_CODE | SEC_ALLOC))
   3060       break;
   3061   symcount = i;
   3062 
   3063   count = 0;
   3064 
   3065   if (relocatable)
   3066     {
   3067       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
   3068       arelent *r;
   3069       size_t size;
   3070       long relcount;
   3071 
   3072       if (opdsymend == secsymend)
   3073 	goto done;
   3074 
   3075       slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   3076       relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
   3077       if (relcount == 0)
   3078 	goto done;
   3079 
   3080       if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
   3081 	{
   3082 	  count = -1;
   3083 	  goto done;
   3084 	}
   3085 
   3086       size = 0;
   3087       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
   3088 	{
   3089 	  asymbol *sym;
   3090 
   3091 	  while (r < opd->relocation + relcount
   3092 		 && r->address < syms[i]->value + opd->vma)
   3093 	    ++r;
   3094 
   3095 	  if (r == opd->relocation + relcount)
   3096 	    break;
   3097 
   3098 	  if (r->address != syms[i]->value + opd->vma)
   3099 	    continue;
   3100 
   3101 	  if (r->howto->type != R_PPC64_ADDR64)
   3102 	    continue;
   3103 
   3104 	  sym = *r->sym_ptr_ptr;
   3105 	  if (!sym_exists_at (syms, opdsymend, symcount,
   3106 			      sym->section->id, sym->value + r->addend))
   3107 	    {
   3108 	      ++count;
   3109 	      size += sizeof (asymbol);
   3110 	      size += strlen (syms[i]->name) + 2;
   3111 	    }
   3112 	}
   3113 
   3114       s = *ret = bfd_malloc (size);
   3115       if (s == NULL)
   3116 	{
   3117 	  count = -1;
   3118 	  goto done;
   3119 	}
   3120 
   3121       names = (char *) (s + count);
   3122 
   3123       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
   3124 	{
   3125 	  asymbol *sym;
   3126 
   3127 	  while (r < opd->relocation + relcount
   3128 		 && r->address < syms[i]->value + opd->vma)
   3129 	    ++r;
   3130 
   3131 	  if (r == opd->relocation + relcount)
   3132 	    break;
   3133 
   3134 	  if (r->address != syms[i]->value + opd->vma)
   3135 	    continue;
   3136 
   3137 	  if (r->howto->type != R_PPC64_ADDR64)
   3138 	    continue;
   3139 
   3140 	  sym = *r->sym_ptr_ptr;
   3141 	  if (!sym_exists_at (syms, opdsymend, symcount,
   3142 			      sym->section->id, sym->value + r->addend))
   3143 	    {
   3144 	      size_t len;
   3145 
   3146 	      *s = *syms[i];
   3147 	      s->flags |= BSF_SYNTHETIC;
   3148 	      s->section = sym->section;
   3149 	      s->value = sym->value + r->addend;
   3150 	      s->name = names;
   3151 	      *names++ = '.';
   3152 	      len = strlen (syms[i]->name);
   3153 	      memcpy (names, syms[i]->name, len + 1);
   3154 	      names += len + 1;
   3155 	      /* Have udata.p point back to the original symbol this
   3156 		 synthetic symbol was derived from.  */
   3157 	      s->udata.p = syms[i];
   3158 	      s++;
   3159 	    }
   3160 	}
   3161     }
   3162   else
   3163     {
   3164       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
   3165       bfd_byte *contents;
   3166       size_t size;
   3167       long plt_count = 0;
   3168       bfd_vma glink_vma = 0, resolv_vma = 0;
   3169       asection *dynamic, *glink = NULL, *relplt = NULL;
   3170       arelent *p;
   3171 
   3172       if (!bfd_malloc_and_get_section (abfd, opd, &contents))
   3173 	{
   3174 	  if (contents)
   3175 	    {
   3176 	    free_contents_and_exit:
   3177 	      free (contents);
   3178 	    }
   3179 	  count = -1;
   3180 	  goto done;
   3181 	}
   3182 
   3183       size = 0;
   3184       for (i = secsymend; i < opdsymend; ++i)
   3185 	{
   3186 	  bfd_vma ent;
   3187 
   3188 	  /* Ignore bogus symbols.  */
   3189 	  if (syms[i]->value > opd->size - 8)
   3190 	    continue;
   3191 
   3192 	  ent = bfd_get_64 (abfd, contents + syms[i]->value);
   3193 	  if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
   3194 	    {
   3195 	      ++count;
   3196 	      size += sizeof (asymbol);
   3197 	      size += strlen (syms[i]->name) + 2;
   3198 	    }
   3199 	}
   3200 
   3201       /* Get start of .glink stubs from DT_PPC64_GLINK.  */
   3202       if (dyn_count != 0
   3203 	  && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
   3204 	{
   3205 	  bfd_byte *dynbuf, *extdyn, *extdynend;
   3206 	  size_t extdynsize;
   3207 	  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
   3208 
   3209 	  if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
   3210 	    goto free_contents_and_exit;
   3211 
   3212 	  extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
   3213 	  swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
   3214 
   3215 	  extdyn = dynbuf;
   3216 	  extdynend = extdyn + dynamic->size;
   3217 	  for (; extdyn < extdynend; extdyn += extdynsize)
   3218 	    {
   3219 	      Elf_Internal_Dyn dyn;
   3220 	      (*swap_dyn_in) (abfd, extdyn, &dyn);
   3221 
   3222 	      if (dyn.d_tag == DT_NULL)
   3223 		break;
   3224 
   3225 	      if (dyn.d_tag == DT_PPC64_GLINK)
   3226 		{
   3227 		  /* The first glink stub starts at offset 32; see comment in
   3228 		     ppc64_elf_finish_dynamic_sections. */
   3229 		  glink_vma = dyn.d_un.d_val + 32;
   3230 		  /* The .glink section usually does not survive the final
   3231 		     link; search for the section (usually .text) where the
   3232 		     glink stubs now reside.  */
   3233 		  glink = bfd_sections_find_if (abfd, section_covers_vma,
   3234 						&glink_vma);
   3235 		  break;
   3236 		}
   3237 	    }
   3238 
   3239 	  free (dynbuf);
   3240 	}
   3241 
   3242       if (glink != NULL)
   3243 	{
   3244 	  /* Determine __glink trampoline by reading the relative branch
   3245 	     from the first glink stub.  */
   3246 	  bfd_byte buf[4];
   3247 	  if (bfd_get_section_contents (abfd, glink, buf,
   3248 					glink_vma + 4 - glink->vma, 4))
   3249 	    {
   3250 	      unsigned int insn = bfd_get_32 (abfd, buf);
   3251 	      insn ^= B_DOT;
   3252 	      if ((insn & ~0x3fffffc) == 0)
   3253 		resolv_vma = glink_vma + 4 + (insn ^ 0x2000000) - 0x2000000;
   3254 	    }
   3255 
   3256 	  if (resolv_vma)
   3257 	    size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
   3258 
   3259 	  relplt = bfd_get_section_by_name (abfd, ".rela.plt");
   3260 	  if (relplt != NULL)
   3261 	    {
   3262 	      slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   3263 	      if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
   3264 		goto free_contents_and_exit;
   3265 
   3266 	      plt_count = relplt->size / sizeof (Elf64_External_Rela);
   3267 	      size += plt_count * sizeof (asymbol);
   3268 
   3269 	      p = relplt->relocation;
   3270 	      for (i = 0; i < plt_count; i++, p++)
   3271 		{
   3272 		  size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
   3273 		  if (p->addend != 0)
   3274 		    size += sizeof ("+0x") - 1 + 16;
   3275 		}
   3276 	    }
   3277 	}
   3278 
   3279       s = *ret = bfd_malloc (size);
   3280       if (s == NULL)
   3281 	goto free_contents_and_exit;
   3282 
   3283       names = (char *) (s + count + plt_count + (resolv_vma != 0));
   3284 
   3285       for (i = secsymend; i < opdsymend; ++i)
   3286 	{
   3287 	  bfd_vma ent;
   3288 
   3289 	  if (syms[i]->value > opd->size - 8)
   3290 	    continue;
   3291 
   3292 	  ent = bfd_get_64 (abfd, contents + syms[i]->value);
   3293 	  if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
   3294 	    {
   3295 	      long lo, hi;
   3296 	      size_t len;
   3297 	      asection *sec = abfd->sections;
   3298 
   3299 	      *s = *syms[i];
   3300 	      lo = codesecsym;
   3301 	      hi = codesecsymend;
   3302 	      while (lo < hi)
   3303 		{
   3304 		  long mid = (lo + hi) >> 1;
   3305 		  if (syms[mid]->section->vma < ent)
   3306 		    lo = mid + 1;
   3307 		  else if (syms[mid]->section->vma > ent)
   3308 		    hi = mid;
   3309 		  else
   3310 		    {
   3311 		      sec = syms[mid]->section;
   3312 		      break;
   3313 		    }
   3314 		}
   3315 
   3316 	      if (lo >= hi && lo > codesecsym)
   3317 		sec = syms[lo - 1]->section;
   3318 
   3319 	      for (; sec != NULL; sec = sec->next)
   3320 		{
   3321 		  if (sec->vma > ent)
   3322 		    break;
   3323 		  /* SEC_LOAD may not be set if SEC is from a separate debug
   3324 		     info file.  */
   3325 		  if ((sec->flags & SEC_ALLOC) == 0)
   3326 		    break;
   3327 		  if ((sec->flags & SEC_CODE) != 0)
   3328 		    s->section = sec;
   3329 		}
   3330 	      s->flags |= BSF_SYNTHETIC;
   3331 	      s->value = ent - s->section->vma;
   3332 	      s->name = names;
   3333 	      *names++ = '.';
   3334 	      len = strlen (syms[i]->name);
   3335 	      memcpy (names, syms[i]->name, len + 1);
   3336 	      names += len + 1;
   3337 	      /* Have udata.p point back to the original symbol this
   3338 		 synthetic symbol was derived from.  */
   3339 	      s->udata.p = syms[i];
   3340 	      s++;
   3341 	    }
   3342 	}
   3343       free (contents);
   3344 
   3345       if (glink != NULL && relplt != NULL)
   3346 	{
   3347 	  if (resolv_vma)
   3348 	    {
   3349 	      /* Add a symbol for the main glink trampoline.  */
   3350 	      memset (s, 0, sizeof *s);
   3351 	      s->the_bfd = abfd;
   3352 	      s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
   3353 	      s->section = glink;
   3354 	      s->value = resolv_vma - glink->vma;
   3355 	      s->name = names;
   3356 	      memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
   3357 	      names += sizeof ("__glink_PLTresolve");
   3358 	      s++;
   3359 	      count++;
   3360 	    }
   3361 
   3362 	  /* FIXME: It would be very much nicer to put sym@plt on the
   3363 	     stub rather than on the glink branch table entry.  The
   3364 	     objdump disassembler would then use a sensible symbol
   3365 	     name on plt calls.  The difficulty in doing so is
   3366 	     a) finding the stubs, and,
   3367 	     b) matching stubs against plt entries, and,
   3368 	     c) there can be multiple stubs for a given plt entry.
   3369 
   3370 	     Solving (a) could be done by code scanning, but older
   3371 	     ppc64 binaries used different stubs to current code.
   3372 	     (b) is the tricky one since you need to known the toc
   3373 	     pointer for at least one function that uses a pic stub to
   3374 	     be able to calculate the plt address referenced.
   3375 	     (c) means gdb would need to set multiple breakpoints (or
   3376 	     find the glink branch itself) when setting breakpoints
   3377 	     for pending shared library loads.  */
   3378 	  p = relplt->relocation;
   3379 	  for (i = 0; i < plt_count; i++, p++)
   3380 	    {
   3381 	      size_t len;
   3382 
   3383 	      *s = **p->sym_ptr_ptr;
   3384 	      /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
   3385 		 we are defining a symbol, ensure one of them is set.  */
   3386 	      if ((s->flags & BSF_LOCAL) == 0)
   3387 		s->flags |= BSF_GLOBAL;
   3388 	      s->flags |= BSF_SYNTHETIC;
   3389 	      s->section = glink;
   3390 	      s->value = glink_vma - glink->vma;
   3391 	      s->name = names;
   3392 	      s->udata.p = NULL;
   3393 	      len = strlen ((*p->sym_ptr_ptr)->name);
   3394 	      memcpy (names, (*p->sym_ptr_ptr)->name, len);
   3395 	      names += len;
   3396 	      if (p->addend != 0)
   3397 		{
   3398 		  memcpy (names, "+0x", sizeof ("+0x") - 1);
   3399 		  names += sizeof ("+0x") - 1;
   3400 		  bfd_sprintf_vma (abfd, names, p->addend);
   3401 		  names += strlen (names);
   3402 		}
   3403 	      memcpy (names, "@plt", sizeof ("@plt"));
   3404 	      names += sizeof ("@plt");
   3405 	      s++;
   3406 	      glink_vma += 8;
   3407 	      if (i >= 0x8000)
   3408 		glink_vma += 4;
   3409 	    }
   3410 	  count += plt_count;
   3411 	}
   3412     }
   3413 
   3414  done:
   3415   free (syms);
   3416   return count;
   3417 }
   3418 
   3419 /* The following functions are specific to the ELF linker, while
   3421    functions above are used generally.  Those named ppc64_elf_* are
   3422    called by the main ELF linker code.  They appear in this file more
   3423    or less in the order in which they are called.  eg.
   3424    ppc64_elf_check_relocs is called early in the link process,
   3425    ppc64_elf_finish_dynamic_sections is one of the last functions
   3426    called.
   3427 
   3428    PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
   3429    functions have both a function code symbol and a function descriptor
   3430    symbol.  A call to foo in a relocatable object file looks like:
   3431 
   3432    .		.text
   3433    .	x:
   3434    .		bl	.foo
   3435    .		nop
   3436 
   3437    The function definition in another object file might be:
   3438 
   3439    .		.section .opd
   3440    .	foo:	.quad	.foo
   3441    .		.quad	.TOC.@tocbase
   3442    .		.quad	0
   3443    .
   3444    .		.text
   3445    .	.foo:	blr
   3446 
   3447    When the linker resolves the call during a static link, the branch
   3448    unsurprisingly just goes to .foo and the .opd information is unused.
   3449    If the function definition is in a shared library, things are a little
   3450    different:  The call goes via a plt call stub, the opd information gets
   3451    copied to the plt, and the linker patches the nop.
   3452 
   3453    .	x:
   3454    .		bl	.foo_stub
   3455    .		ld	2,40(1)
   3456    .
   3457    .
   3458    .	.foo_stub:
   3459    .		addis	12,2,Lfoo@toc@ha	# in practice, the call stub
   3460    .		addi	12,12,Lfoo@toc@l	# is slightly optimized, but
   3461    .		std	2,40(1)			# this is the general idea
   3462    .		ld	11,0(12)
   3463    .		ld	2,8(12)
   3464    .		mtctr	11
   3465    .		ld	11,16(12)
   3466    .		bctr
   3467    .
   3468    .		.section .plt
   3469    .	Lfoo:	reloc (R_PPC64_JMP_SLOT, foo)
   3470 
   3471    The "reloc ()" notation is supposed to indicate that the linker emits
   3472    an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
   3473    copying.
   3474 
   3475    What are the difficulties here?  Well, firstly, the relocations
   3476    examined by the linker in check_relocs are against the function code
   3477    sym .foo, while the dynamic relocation in the plt is emitted against
   3478    the function descriptor symbol, foo.  Somewhere along the line, we need
   3479    to carefully copy dynamic link information from one symbol to the other.
   3480    Secondly, the generic part of the elf linker will make .foo a dynamic
   3481    symbol as is normal for most other backends.  We need foo dynamic
   3482    instead, at least for an application final link.  However, when
   3483    creating a shared library containing foo, we need to have both symbols
   3484    dynamic so that references to .foo are satisfied during the early
   3485    stages of linking.  Otherwise the linker might decide to pull in a
   3486    definition from some other object, eg. a static library.
   3487 
   3488    Update: As of August 2004, we support a new convention.  Function
   3489    calls may use the function descriptor symbol, ie. "bl foo".  This
   3490    behaves exactly as "bl .foo".  */
   3491 
   3492 /* Of those relocs that might be copied as dynamic relocs, this function
   3493    selects those that must be copied when linking a shared library,
   3494    even when the symbol is local.  */
   3495 
   3496 static int
   3497 must_be_dyn_reloc (struct bfd_link_info *info,
   3498 		   enum elf_ppc64_reloc_type r_type)
   3499 {
   3500   switch (r_type)
   3501     {
   3502     default:
   3503       return 1;
   3504 
   3505     case R_PPC64_REL32:
   3506     case R_PPC64_REL64:
   3507     case R_PPC64_REL30:
   3508       return 0;
   3509 
   3510     case R_PPC64_TPREL16:
   3511     case R_PPC64_TPREL16_LO:
   3512     case R_PPC64_TPREL16_HI:
   3513     case R_PPC64_TPREL16_HA:
   3514     case R_PPC64_TPREL16_DS:
   3515     case R_PPC64_TPREL16_LO_DS:
   3516     case R_PPC64_TPREL16_HIGHER:
   3517     case R_PPC64_TPREL16_HIGHERA:
   3518     case R_PPC64_TPREL16_HIGHEST:
   3519     case R_PPC64_TPREL16_HIGHESTA:
   3520     case R_PPC64_TPREL64:
   3521       return !info->executable;
   3522     }
   3523 }
   3524 
   3525 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
   3526    copying dynamic variables from a shared lib into an app's dynbss
   3527    section, and instead use a dynamic relocation to point into the
   3528    shared lib.  With code that gcc generates, it's vital that this be
   3529    enabled;  In the PowerPC64 ABI, the address of a function is actually
   3530    the address of a function descriptor, which resides in the .opd
   3531    section.  gcc uses the descriptor directly rather than going via the
   3532    GOT as some other ABI's do, which means that initialized function
   3533    pointers must reference the descriptor.  Thus, a function pointer
   3534    initialized to the address of a function in a shared library will
   3535    either require a copy reloc, or a dynamic reloc.  Using a copy reloc
   3536    redefines the function descriptor symbol to point to the copy.  This
   3537    presents a problem as a plt entry for that function is also
   3538    initialized from the function descriptor symbol and the copy reloc
   3539    may not be initialized first.  */
   3540 #define ELIMINATE_COPY_RELOCS 1
   3541 
   3542 /* Section name for stubs is the associated section name plus this
   3543    string.  */
   3544 #define STUB_SUFFIX ".stub"
   3545 
   3546 /* Linker stubs.
   3547    ppc_stub_long_branch:
   3548    Used when a 14 bit branch (or even a 24 bit branch) can't reach its
   3549    destination, but a 24 bit branch in a stub section will reach.
   3550    .	b	dest
   3551 
   3552    ppc_stub_plt_branch:
   3553    Similar to the above, but a 24 bit branch in the stub section won't
   3554    reach its destination.
   3555    .	addis	%r12,%r2,xxx@toc@ha
   3556    .	ld	%r11,xxx@toc@l(%r12)
   3557    .	mtctr	%r11
   3558    .	bctr
   3559 
   3560    ppc_stub_plt_call:
   3561    Used to call a function in a shared library.  If it so happens that
   3562    the plt entry referenced crosses a 64k boundary, then an extra
   3563    "addi %r12,%r12,xxx@toc@l" will be inserted before the "mtctr".
   3564    .	addis	%r12,%r2,xxx@toc@ha
   3565    .	std	%r2,40(%r1)
   3566    .	ld	%r11,xxx+0@toc@l(%r12)
   3567    .	mtctr	%r11
   3568    .	ld	%r2,xxx+8@toc@l(%r12)
   3569    .	ld	%r11,xxx+16@toc@l(%r12)
   3570    .	bctr
   3571 
   3572    ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
   3573    code to adjust the value and save r2 to support multiple toc sections.
   3574    A ppc_stub_long_branch with an r2 offset looks like:
   3575    .	std	%r2,40(%r1)
   3576    .	addis	%r2,%r2,off@ha
   3577    .	addi	%r2,%r2,off@l
   3578    .	b	dest
   3579 
   3580    A ppc_stub_plt_branch with an r2 offset looks like:
   3581    .	std	%r2,40(%r1)
   3582    .	addis	%r12,%r2,xxx@toc@ha
   3583    .	ld	%r11,xxx@toc@l(%r12)
   3584    .	addis	%r2,%r2,off@ha
   3585    .	addi	%r2,%r2,off@l
   3586    .	mtctr	%r11
   3587    .	bctr
   3588 
   3589    In cases where the "addis" instruction would add zero, the "addis" is
   3590    omitted and following instructions modified slightly in some cases.
   3591 */
   3592 
   3593 enum ppc_stub_type {
   3594   ppc_stub_none,
   3595   ppc_stub_long_branch,
   3596   ppc_stub_long_branch_r2off,
   3597   ppc_stub_plt_branch,
   3598   ppc_stub_plt_branch_r2off,
   3599   ppc_stub_plt_call,
   3600   ppc_stub_plt_call_r2save
   3601 };
   3602 
   3603 struct ppc_stub_hash_entry {
   3604 
   3605   /* Base hash table entry structure.  */
   3606   struct bfd_hash_entry root;
   3607 
   3608   enum ppc_stub_type stub_type;
   3609 
   3610   /* The stub section.  */
   3611   asection *stub_sec;
   3612 
   3613   /* Offset within stub_sec of the beginning of this stub.  */
   3614   bfd_vma stub_offset;
   3615 
   3616   /* Given the symbol's value and its section we can determine its final
   3617      value when building the stubs (so the stub knows where to jump.  */
   3618   bfd_vma target_value;
   3619   asection *target_section;
   3620 
   3621   /* The symbol table entry, if any, that this was derived from.  */
   3622   struct ppc_link_hash_entry *h;
   3623   struct plt_entry *plt_ent;
   3624 
   3625   /* And the reloc addend that this was derived from.  */
   3626   bfd_vma addend;
   3627 
   3628   /* Where this stub is being called from, or, in the case of combined
   3629      stub sections, the first input section in the group.  */
   3630   asection *id_sec;
   3631 };
   3632 
   3633 struct ppc_branch_hash_entry {
   3634 
   3635   /* Base hash table entry structure.  */
   3636   struct bfd_hash_entry root;
   3637 
   3638   /* Offset within branch lookup table.  */
   3639   unsigned int offset;
   3640 
   3641   /* Generation marker.  */
   3642   unsigned int iter;
   3643 };
   3644 
   3645 struct ppc_link_hash_entry
   3646 {
   3647   struct elf_link_hash_entry elf;
   3648 
   3649   union {
   3650     /* A pointer to the most recently used stub hash entry against this
   3651        symbol.  */
   3652     struct ppc_stub_hash_entry *stub_cache;
   3653 
   3654     /* A pointer to the next symbol starting with a '.'  */
   3655     struct ppc_link_hash_entry *next_dot_sym;
   3656   } u;
   3657 
   3658   /* Track dynamic relocs copied for this symbol.  */
   3659   struct elf_dyn_relocs *dyn_relocs;
   3660 
   3661   /* Link between function code and descriptor symbols.  */
   3662   struct ppc_link_hash_entry *oh;
   3663 
   3664   /* Flag function code and descriptor symbols.  */
   3665   unsigned int is_func:1;
   3666   unsigned int is_func_descriptor:1;
   3667   unsigned int fake:1;
   3668 
   3669   /* Whether global opd/toc sym has been adjusted or not.
   3670      After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
   3671      should be set for all globals defined in any opd/toc section.  */
   3672   unsigned int adjust_done:1;
   3673 
   3674   /* Set if we twiddled this symbol to weak at some stage.  */
   3675   unsigned int was_undefined:1;
   3676 
   3677   /* Contexts in which symbol is used in the GOT (or TOC).
   3678      TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
   3679      corresponding relocs are encountered during check_relocs.
   3680      tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
   3681      indicate the corresponding GOT entry type is not needed.
   3682      tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
   3683      a TPREL one.  We use a separate flag rather than setting TPREL
   3684      just for convenience in distinguishing the two cases.  */
   3685 #define TLS_GD		 1	/* GD reloc. */
   3686 #define TLS_LD		 2	/* LD reloc. */
   3687 #define TLS_TPREL	 4	/* TPREL reloc, => IE. */
   3688 #define TLS_DTPREL	 8	/* DTPREL reloc, => LD. */
   3689 #define TLS_TLS		16	/* Any TLS reloc.  */
   3690 #define TLS_EXPLICIT	32	/* Marks TOC section TLS relocs. */
   3691 #define TLS_TPRELGD	64	/* TPREL reloc resulting from GD->IE. */
   3692 #define PLT_IFUNC      128	/* STT_GNU_IFUNC.  */
   3693   unsigned char tls_mask;
   3694 };
   3695 
   3696 /* ppc64 ELF linker hash table.  */
   3697 
   3698 struct ppc_link_hash_table
   3699 {
   3700   struct elf_link_hash_table elf;
   3701 
   3702   /* The stub hash table.  */
   3703   struct bfd_hash_table stub_hash_table;
   3704 
   3705   /* Another hash table for plt_branch stubs.  */
   3706   struct bfd_hash_table branch_hash_table;
   3707 
   3708   /* Hash table for function prologue tocsave.  */
   3709   htab_t tocsave_htab;
   3710 
   3711   /* Linker stub bfd.  */
   3712   bfd *stub_bfd;
   3713 
   3714   /* Linker call-backs.  */
   3715   asection * (*add_stub_section) (const char *, asection *);
   3716   void (*layout_sections_again) (void);
   3717 
   3718   /* Array to keep track of which stub sections have been created, and
   3719      information on stub grouping.  */
   3720   struct map_stub {
   3721     /* This is the section to which stubs in the group will be attached.  */
   3722     asection *link_sec;
   3723     /* The stub section.  */
   3724     asection *stub_sec;
   3725     /* Along with elf_gp, specifies the TOC pointer used in this group.  */
   3726     bfd_vma toc_off;
   3727   } *stub_group;
   3728 
   3729   /* Temp used when calculating TOC pointers.  */
   3730   bfd_vma toc_curr;
   3731   bfd *toc_bfd;
   3732   asection *toc_first_sec;
   3733 
   3734   /* Highest input section id.  */
   3735   int top_id;
   3736 
   3737   /* Highest output section index.  */
   3738   int top_index;
   3739 
   3740   /* Used when adding symbols.  */
   3741   struct ppc_link_hash_entry *dot_syms;
   3742 
   3743   /* List of input sections for each output section.  */
   3744   asection **input_list;
   3745 
   3746   /* Short-cuts to get to dynamic linker sections.  */
   3747   asection *got;
   3748   asection *plt;
   3749   asection *relplt;
   3750   asection *iplt;
   3751   asection *reliplt;
   3752   asection *dynbss;
   3753   asection *relbss;
   3754   asection *glink;
   3755   asection *sfpr;
   3756   asection *brlt;
   3757   asection *relbrlt;
   3758   asection *glink_eh_frame;
   3759 
   3760   /* Shortcut to .__tls_get_addr and __tls_get_addr.  */
   3761   struct ppc_link_hash_entry *tls_get_addr;
   3762   struct ppc_link_hash_entry *tls_get_addr_fd;
   3763 
   3764   /* The size of reliplt used by got entry relocs.  */
   3765   bfd_size_type got_reli_size;
   3766 
   3767   /* Statistics.  */
   3768   unsigned long stub_count[ppc_stub_plt_call_r2save];
   3769 
   3770   /* Number of stubs against global syms.  */
   3771   unsigned long stub_globals;
   3772 
   3773   /* Alignment of PLT call stubs.  */
   3774   unsigned int plt_stub_align:4;
   3775 
   3776   /* Set if PLT call stubs should load r11.  */
   3777   unsigned int plt_static_chain:1;
   3778 
   3779   /* Set if PLT call stubs need a read-read barrier.  */
   3780   unsigned int plt_thread_safe:1;
   3781 
   3782   /* Set if we should emit symbols for stubs.  */
   3783   unsigned int emit_stub_syms:1;
   3784 
   3785   /* Set if __tls_get_addr optimization should not be done.  */
   3786   unsigned int no_tls_get_addr_opt:1;
   3787 
   3788   /* Support for multiple toc sections.  */
   3789   unsigned int do_multi_toc:1;
   3790   unsigned int multi_toc_needed:1;
   3791   unsigned int second_toc_pass:1;
   3792   unsigned int do_toc_opt:1;
   3793 
   3794   /* Set on error.  */
   3795   unsigned int stub_error:1;
   3796 
   3797   /* Temp used by ppc64_elf_process_dot_syms.  */
   3798   unsigned int twiddled_syms:1;
   3799 
   3800   /* Incremented every time we size stubs.  */
   3801   unsigned int stub_iteration;
   3802 
   3803   /* Small local sym cache.  */
   3804   struct sym_cache sym_cache;
   3805 };
   3806 
   3807 /* Rename some of the generic section flags to better document how they
   3808    are used here.  */
   3809 
   3810 /* Nonzero if this section has TLS related relocations.  */
   3811 #define has_tls_reloc sec_flg0
   3812 
   3813 /* Nonzero if this section has a call to __tls_get_addr.  */
   3814 #define has_tls_get_addr_call sec_flg1
   3815 
   3816 /* Nonzero if this section has any toc or got relocs.  */
   3817 #define has_toc_reloc sec_flg2
   3818 
   3819 /* Nonzero if this section has a call to another section that uses
   3820    the toc or got.  */
   3821 #define makes_toc_func_call sec_flg3
   3822 
   3823 /* Recursion protection when determining above flag.  */
   3824 #define call_check_in_progress sec_flg4
   3825 #define call_check_done sec_flg5
   3826 
   3827 /* Get the ppc64 ELF linker hash table from a link_info structure.  */
   3828 
   3829 #define ppc_hash_table(p) \
   3830   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
   3831   == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
   3832 
   3833 #define ppc_stub_hash_lookup(table, string, create, copy) \
   3834   ((struct ppc_stub_hash_entry *) \
   3835    bfd_hash_lookup ((table), (string), (create), (copy)))
   3836 
   3837 #define ppc_branch_hash_lookup(table, string, create, copy) \
   3838   ((struct ppc_branch_hash_entry *) \
   3839    bfd_hash_lookup ((table), (string), (create), (copy)))
   3840 
   3841 /* Create an entry in the stub hash table.  */
   3842 
   3843 static struct bfd_hash_entry *
   3844 stub_hash_newfunc (struct bfd_hash_entry *entry,
   3845 		   struct bfd_hash_table *table,
   3846 		   const char *string)
   3847 {
   3848   /* Allocate the structure if it has not already been allocated by a
   3849      subclass.  */
   3850   if (entry == NULL)
   3851     {
   3852       entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
   3853       if (entry == NULL)
   3854 	return entry;
   3855     }
   3856 
   3857   /* Call the allocation method of the superclass.  */
   3858   entry = bfd_hash_newfunc (entry, table, string);
   3859   if (entry != NULL)
   3860     {
   3861       struct ppc_stub_hash_entry *eh;
   3862 
   3863       /* Initialize the local fields.  */
   3864       eh = (struct ppc_stub_hash_entry *) entry;
   3865       eh->stub_type = ppc_stub_none;
   3866       eh->stub_sec = NULL;
   3867       eh->stub_offset = 0;
   3868       eh->target_value = 0;
   3869       eh->target_section = NULL;
   3870       eh->h = NULL;
   3871       eh->id_sec = NULL;
   3872     }
   3873 
   3874   return entry;
   3875 }
   3876 
   3877 /* Create an entry in the branch hash table.  */
   3878 
   3879 static struct bfd_hash_entry *
   3880 branch_hash_newfunc (struct bfd_hash_entry *entry,
   3881 		     struct bfd_hash_table *table,
   3882 		     const char *string)
   3883 {
   3884   /* Allocate the structure if it has not already been allocated by a
   3885      subclass.  */
   3886   if (entry == NULL)
   3887     {
   3888       entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
   3889       if (entry == NULL)
   3890 	return entry;
   3891     }
   3892 
   3893   /* Call the allocation method of the superclass.  */
   3894   entry = bfd_hash_newfunc (entry, table, string);
   3895   if (entry != NULL)
   3896     {
   3897       struct ppc_branch_hash_entry *eh;
   3898 
   3899       /* Initialize the local fields.  */
   3900       eh = (struct ppc_branch_hash_entry *) entry;
   3901       eh->offset = 0;
   3902       eh->iter = 0;
   3903     }
   3904 
   3905   return entry;
   3906 }
   3907 
   3908 /* Create an entry in a ppc64 ELF linker hash table.  */
   3909 
   3910 static struct bfd_hash_entry *
   3911 link_hash_newfunc (struct bfd_hash_entry *entry,
   3912 		   struct bfd_hash_table *table,
   3913 		   const char *string)
   3914 {
   3915   /* Allocate the structure if it has not already been allocated by a
   3916      subclass.  */
   3917   if (entry == NULL)
   3918     {
   3919       entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
   3920       if (entry == NULL)
   3921 	return entry;
   3922     }
   3923 
   3924   /* Call the allocation method of the superclass.  */
   3925   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
   3926   if (entry != NULL)
   3927     {
   3928       struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
   3929 
   3930       memset (&eh->u.stub_cache, 0,
   3931 	      (sizeof (struct ppc_link_hash_entry)
   3932 	       - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
   3933 
   3934       /* When making function calls, old ABI code references function entry
   3935 	 points (dot symbols), while new ABI code references the function
   3936 	 descriptor symbol.  We need to make any combination of reference and
   3937 	 definition work together, without breaking archive linking.
   3938 
   3939 	 For a defined function "foo" and an undefined call to "bar":
   3940 	 An old object defines "foo" and ".foo", references ".bar" (possibly
   3941 	 "bar" too).
   3942 	 A new object defines "foo" and references "bar".
   3943 
   3944 	 A new object thus has no problem with its undefined symbols being
   3945 	 satisfied by definitions in an old object.  On the other hand, the
   3946 	 old object won't have ".bar" satisfied by a new object.
   3947 
   3948 	 Keep a list of newly added dot-symbols.  */
   3949 
   3950       if (string[0] == '.')
   3951 	{
   3952 	  struct ppc_link_hash_table *htab;
   3953 
   3954 	  htab = (struct ppc_link_hash_table *) table;
   3955 	  eh->u.next_dot_sym = htab->dot_syms;
   3956 	  htab->dot_syms = eh;
   3957 	}
   3958     }
   3959 
   3960   return entry;
   3961 }
   3962 
   3963 struct tocsave_entry {
   3964   asection *sec;
   3965   bfd_vma offset;
   3966 };
   3967 
   3968 static hashval_t
   3969 tocsave_htab_hash (const void *p)
   3970 {
   3971   const struct tocsave_entry *e = (const struct tocsave_entry *) p;
   3972   return ((bfd_vma)(intptr_t) e->sec ^ e->offset) >> 3;
   3973 }
   3974 
   3975 static int
   3976 tocsave_htab_eq (const void *p1, const void *p2)
   3977 {
   3978   const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
   3979   const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
   3980   return e1->sec == e2->sec && e1->offset == e2->offset;
   3981 }
   3982 
   3983 /* Create a ppc64 ELF linker hash table.  */
   3984 
   3985 static struct bfd_link_hash_table *
   3986 ppc64_elf_link_hash_table_create (bfd *abfd)
   3987 {
   3988   struct ppc_link_hash_table *htab;
   3989   bfd_size_type amt = sizeof (struct ppc_link_hash_table);
   3990 
   3991   htab = bfd_zmalloc (amt);
   3992   if (htab == NULL)
   3993     return NULL;
   3994 
   3995   if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
   3996 				      sizeof (struct ppc_link_hash_entry),
   3997 				      PPC64_ELF_DATA))
   3998     {
   3999       free (htab);
   4000       return NULL;
   4001     }
   4002 
   4003   /* Init the stub hash table too.  */
   4004   if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
   4005 			    sizeof (struct ppc_stub_hash_entry)))
   4006     return NULL;
   4007 
   4008   /* And the branch hash table.  */
   4009   if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
   4010 			    sizeof (struct ppc_branch_hash_entry)))
   4011     return NULL;
   4012 
   4013   htab->tocsave_htab = htab_try_create (1024,
   4014 					tocsave_htab_hash,
   4015 					tocsave_htab_eq,
   4016 					NULL);
   4017   if (htab->tocsave_htab == NULL)
   4018     return NULL;
   4019 
   4020   /* Initializing two fields of the union is just cosmetic.  We really
   4021      only care about glist, but when compiled on a 32-bit host the
   4022      bfd_vma fields are larger.  Setting the bfd_vma to zero makes
   4023      debugger inspection of these fields look nicer.  */
   4024   htab->elf.init_got_refcount.refcount = 0;
   4025   htab->elf.init_got_refcount.glist = NULL;
   4026   htab->elf.init_plt_refcount.refcount = 0;
   4027   htab->elf.init_plt_refcount.glist = NULL;
   4028   htab->elf.init_got_offset.offset = 0;
   4029   htab->elf.init_got_offset.glist = NULL;
   4030   htab->elf.init_plt_offset.offset = 0;
   4031   htab->elf.init_plt_offset.glist = NULL;
   4032 
   4033   return &htab->elf.root;
   4034 }
   4035 
   4036 /* Free the derived linker hash table.  */
   4037 
   4038 static void
   4039 ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
   4040 {
   4041   struct ppc_link_hash_table *htab = (struct ppc_link_hash_table *) hash;
   4042 
   4043   bfd_hash_table_free (&htab->stub_hash_table);
   4044   bfd_hash_table_free (&htab->branch_hash_table);
   4045   if (htab->tocsave_htab)
   4046     htab_delete (htab->tocsave_htab);
   4047   _bfd_generic_link_hash_table_free (hash);
   4048 }
   4049 
   4050 /* Satisfy the ELF linker by filling in some fields in our fake bfd.  */
   4051 
   4052 void
   4053 ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
   4054 {
   4055   struct ppc_link_hash_table *htab;
   4056 
   4057   elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
   4058 
   4059 /* Always hook our dynamic sections into the first bfd, which is the
   4060    linker created stub bfd.  This ensures that the GOT header is at
   4061    the start of the output TOC section.  */
   4062   htab = ppc_hash_table (info);
   4063   if (htab == NULL)
   4064     return;
   4065   htab->stub_bfd = abfd;
   4066   htab->elf.dynobj = abfd;
   4067 }
   4068 
   4069 /* Build a name for an entry in the stub hash table.  */
   4070 
   4071 static char *
   4072 ppc_stub_name (const asection *input_section,
   4073 	       const asection *sym_sec,
   4074 	       const struct ppc_link_hash_entry *h,
   4075 	       const Elf_Internal_Rela *rel)
   4076 {
   4077   char *stub_name;
   4078   bfd_size_type len;
   4079 
   4080   /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
   4081      offsets from a sym as a branch target?  In fact, we could
   4082      probably assume the addend is always zero.  */
   4083   BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
   4084 
   4085   if (h)
   4086     {
   4087       len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
   4088       stub_name = bfd_malloc (len);
   4089       if (stub_name == NULL)
   4090 	return stub_name;
   4091 
   4092       sprintf (stub_name, "%08x.%s+%x",
   4093 	       input_section->id & 0xffffffff,
   4094 	       h->elf.root.root.string,
   4095 	       (int) rel->r_addend & 0xffffffff);
   4096     }
   4097   else
   4098     {
   4099       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
   4100       stub_name = bfd_malloc (len);
   4101       if (stub_name == NULL)
   4102 	return stub_name;
   4103 
   4104       sprintf (stub_name, "%08x.%x:%x+%x",
   4105 	       input_section->id & 0xffffffff,
   4106 	       sym_sec->id & 0xffffffff,
   4107 	       (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
   4108 	       (int) rel->r_addend & 0xffffffff);
   4109     }
   4110   if (stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
   4111     stub_name[len - 2] = 0;
   4112   return stub_name;
   4113 }
   4114 
   4115 /* Look up an entry in the stub hash.  Stub entries are cached because
   4116    creating the stub name takes a bit of time.  */
   4117 
   4118 static struct ppc_stub_hash_entry *
   4119 ppc_get_stub_entry (const asection *input_section,
   4120 		    const asection *sym_sec,
   4121 		    struct ppc_link_hash_entry *h,
   4122 		    const Elf_Internal_Rela *rel,
   4123 		    struct ppc_link_hash_table *htab)
   4124 {
   4125   struct ppc_stub_hash_entry *stub_entry;
   4126   const asection *id_sec;
   4127 
   4128   /* If this input section is part of a group of sections sharing one
   4129      stub section, then use the id of the first section in the group.
   4130      Stub names need to include a section id, as there may well be
   4131      more than one stub used to reach say, printf, and we need to
   4132      distinguish between them.  */
   4133   id_sec = htab->stub_group[input_section->id].link_sec;
   4134 
   4135   if (h != NULL && h->u.stub_cache != NULL
   4136       && h->u.stub_cache->h == h
   4137       && h->u.stub_cache->id_sec == id_sec)
   4138     {
   4139       stub_entry = h->u.stub_cache;
   4140     }
   4141   else
   4142     {
   4143       char *stub_name;
   4144 
   4145       stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
   4146       if (stub_name == NULL)
   4147 	return NULL;
   4148 
   4149       stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
   4150 					 stub_name, FALSE, FALSE);
   4151       if (h != NULL)
   4152 	h->u.stub_cache = stub_entry;
   4153 
   4154       free (stub_name);
   4155     }
   4156 
   4157   return stub_entry;
   4158 }
   4159 
   4160 /* Add a new stub entry to the stub hash.  Not all fields of the new
   4161    stub entry are initialised.  */
   4162 
   4163 static struct ppc_stub_hash_entry *
   4164 ppc_add_stub (const char *stub_name,
   4165 	      asection *section,
   4166 	      struct bfd_link_info *info)
   4167 {
   4168   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   4169   asection *link_sec;
   4170   asection *stub_sec;
   4171   struct ppc_stub_hash_entry *stub_entry;
   4172 
   4173   link_sec = htab->stub_group[section->id].link_sec;
   4174   stub_sec = htab->stub_group[section->id].stub_sec;
   4175   if (stub_sec == NULL)
   4176     {
   4177       stub_sec = htab->stub_group[link_sec->id].stub_sec;
   4178       if (stub_sec == NULL)
   4179 	{
   4180 	  size_t namelen;
   4181 	  bfd_size_type len;
   4182 	  char *s_name;
   4183 
   4184 	  namelen = strlen (link_sec->name);
   4185 	  len = namelen + sizeof (STUB_SUFFIX);
   4186 	  s_name = bfd_alloc (htab->stub_bfd, len);
   4187 	  if (s_name == NULL)
   4188 	    return NULL;
   4189 
   4190 	  memcpy (s_name, link_sec->name, namelen);
   4191 	  memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
   4192 	  stub_sec = (*htab->add_stub_section) (s_name, link_sec);
   4193 	  if (stub_sec == NULL)
   4194 	    return NULL;
   4195 	  htab->stub_group[link_sec->id].stub_sec = stub_sec;
   4196 	}
   4197       htab->stub_group[section->id].stub_sec = stub_sec;
   4198     }
   4199 
   4200   /* Enter this entry into the linker stub hash table.  */
   4201   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
   4202 				     TRUE, FALSE);
   4203   if (stub_entry == NULL)
   4204     {
   4205       info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
   4206 			      section->owner, stub_name);
   4207       return NULL;
   4208     }
   4209 
   4210   stub_entry->stub_sec = stub_sec;
   4211   stub_entry->stub_offset = 0;
   4212   stub_entry->id_sec = link_sec;
   4213   return stub_entry;
   4214 }
   4215 
   4216 /* Create sections for linker generated code.  */
   4217 
   4218 static bfd_boolean
   4219 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
   4220 {
   4221   struct ppc_link_hash_table *htab;
   4222   flagword flags;
   4223 
   4224   htab = ppc_hash_table (info);
   4225   if (htab == NULL)
   4226     return FALSE;
   4227 
   4228   /* Create .sfpr for code to save and restore fp regs.  */
   4229   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
   4230 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   4231   htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
   4232 						   flags);
   4233   if (htab->sfpr == NULL
   4234       || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
   4235     return FALSE;
   4236 
   4237   /* Create .glink for lazy dynamic linking support.  */
   4238   htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
   4239 						    flags);
   4240   if (htab->glink == NULL
   4241       || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
   4242     return FALSE;
   4243 
   4244   if (!info->no_ld_generated_unwind_info)
   4245     {
   4246       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
   4247 	       | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   4248       htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
   4249 								 ".eh_frame",
   4250 								 flags);
   4251       if (htab->glink_eh_frame == NULL
   4252 	  || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
   4253 	return FALSE;
   4254     }
   4255 
   4256   flags = SEC_ALLOC | SEC_LINKER_CREATED;
   4257   htab->iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
   4258   if (htab->iplt == NULL
   4259       || ! bfd_set_section_alignment (dynobj, htab->iplt, 3))
   4260     return FALSE;
   4261 
   4262   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
   4263 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   4264   htab->reliplt = bfd_make_section_anyway_with_flags (dynobj,
   4265 						      ".rela.iplt",
   4266 						      flags);
   4267   if (htab->reliplt == NULL
   4268       || ! bfd_set_section_alignment (dynobj, htab->reliplt, 3))
   4269     return FALSE;
   4270 
   4271   /* Create branch lookup table for plt_branch stubs.  */
   4272   flags = (SEC_ALLOC | SEC_LOAD
   4273 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   4274   htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
   4275 						   flags);
   4276   if (htab->brlt == NULL
   4277       || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
   4278     return FALSE;
   4279 
   4280   if (!info->shared)
   4281     return TRUE;
   4282 
   4283   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
   4284 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   4285   htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
   4286 						      ".rela.branch_lt",
   4287 						      flags);
   4288   if (htab->relbrlt == NULL
   4289       || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
   4290     return FALSE;
   4291 
   4292   return TRUE;
   4293 }
   4294 
   4295 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
   4296    not already done.  */
   4297 
   4298 static bfd_boolean
   4299 create_got_section (bfd *abfd, struct bfd_link_info *info)
   4300 {
   4301   asection *got, *relgot;
   4302   flagword flags;
   4303   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   4304 
   4305   if (!is_ppc64_elf (abfd))
   4306     return FALSE;
   4307   if (htab == NULL)
   4308     return FALSE;
   4309 
   4310   if (!htab->got)
   4311     {
   4312       if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
   4313 	return FALSE;
   4314 
   4315       htab->got = bfd_get_linker_section (htab->elf.dynobj, ".got");
   4316       if (!htab->got)
   4317 	abort ();
   4318     }
   4319 
   4320   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
   4321 	   | SEC_LINKER_CREATED);
   4322 
   4323   got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
   4324   if (!got
   4325       || !bfd_set_section_alignment (abfd, got, 3))
   4326     return FALSE;
   4327 
   4328   relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
   4329 					       flags | SEC_READONLY);
   4330   if (!relgot
   4331       || ! bfd_set_section_alignment (abfd, relgot, 3))
   4332     return FALSE;
   4333 
   4334   ppc64_elf_tdata (abfd)->got = got;
   4335   ppc64_elf_tdata (abfd)->relgot = relgot;
   4336   return TRUE;
   4337 }
   4338 
   4339 /* Create the dynamic sections, and set up shortcuts.  */
   4340 
   4341 static bfd_boolean
   4342 ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
   4343 {
   4344   struct ppc_link_hash_table *htab;
   4345 
   4346   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
   4347     return FALSE;
   4348 
   4349   htab = ppc_hash_table (info);
   4350   if (htab == NULL)
   4351     return FALSE;
   4352 
   4353   if (!htab->got)
   4354     htab->got = bfd_get_linker_section (dynobj, ".got");
   4355   htab->plt = bfd_get_linker_section (dynobj, ".plt");
   4356   htab->relplt = bfd_get_linker_section (dynobj, ".rela.plt");
   4357   htab->dynbss = bfd_get_linker_section (dynobj, ".dynbss");
   4358   if (!info->shared)
   4359     htab->relbss = bfd_get_linker_section (dynobj, ".rela.bss");
   4360 
   4361   if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
   4362       || (!info->shared && !htab->relbss))
   4363     abort ();
   4364 
   4365   return TRUE;
   4366 }
   4367 
   4368 /* Follow indirect and warning symbol links.  */
   4369 
   4370 static inline struct bfd_link_hash_entry *
   4371 follow_link (struct bfd_link_hash_entry *h)
   4372 {
   4373   while (h->type == bfd_link_hash_indirect
   4374 	 || h->type == bfd_link_hash_warning)
   4375     h = h->u.i.link;
   4376   return h;
   4377 }
   4378 
   4379 static inline struct elf_link_hash_entry *
   4380 elf_follow_link (struct elf_link_hash_entry *h)
   4381 {
   4382   return (struct elf_link_hash_entry *) follow_link (&h->root);
   4383 }
   4384 
   4385 static inline struct ppc_link_hash_entry *
   4386 ppc_follow_link (struct ppc_link_hash_entry *h)
   4387 {
   4388   return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
   4389 }
   4390 
   4391 /* Merge PLT info on FROM with that on TO.  */
   4392 
   4393 static void
   4394 move_plt_plist (struct ppc_link_hash_entry *from,
   4395 		struct ppc_link_hash_entry *to)
   4396 {
   4397   if (from->elf.plt.plist != NULL)
   4398     {
   4399       if (to->elf.plt.plist != NULL)
   4400 	{
   4401 	  struct plt_entry **entp;
   4402 	  struct plt_entry *ent;
   4403 
   4404 	  for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
   4405 	    {
   4406 	      struct plt_entry *dent;
   4407 
   4408 	      for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
   4409 		if (dent->addend == ent->addend)
   4410 		  {
   4411 		    dent->plt.refcount += ent->plt.refcount;
   4412 		    *entp = ent->next;
   4413 		    break;
   4414 		  }
   4415 	      if (dent == NULL)
   4416 		entp = &ent->next;
   4417 	    }
   4418 	  *entp = to->elf.plt.plist;
   4419 	}
   4420 
   4421       to->elf.plt.plist = from->elf.plt.plist;
   4422       from->elf.plt.plist = NULL;
   4423     }
   4424 }
   4425 
   4426 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
   4427 
   4428 static void
   4429 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
   4430 				struct elf_link_hash_entry *dir,
   4431 				struct elf_link_hash_entry *ind)
   4432 {
   4433   struct ppc_link_hash_entry *edir, *eind;
   4434 
   4435   edir = (struct ppc_link_hash_entry *) dir;
   4436   eind = (struct ppc_link_hash_entry *) ind;
   4437 
   4438   edir->is_func |= eind->is_func;
   4439   edir->is_func_descriptor |= eind->is_func_descriptor;
   4440   edir->tls_mask |= eind->tls_mask;
   4441   if (eind->oh != NULL)
   4442     edir->oh = ppc_follow_link (eind->oh);
   4443 
   4444   /* If called to transfer flags for a weakdef during processing
   4445      of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
   4446      We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
   4447   if (!(ELIMINATE_COPY_RELOCS
   4448 	&& eind->elf.root.type != bfd_link_hash_indirect
   4449 	&& edir->elf.dynamic_adjusted))
   4450     edir->elf.non_got_ref |= eind->elf.non_got_ref;
   4451 
   4452   edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
   4453   edir->elf.ref_regular |= eind->elf.ref_regular;
   4454   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
   4455   edir->elf.needs_plt |= eind->elf.needs_plt;
   4456 
   4457   /* Copy over any dynamic relocs we may have on the indirect sym.  */
   4458   if (eind->dyn_relocs != NULL)
   4459     {
   4460       if (edir->dyn_relocs != NULL)
   4461 	{
   4462 	  struct elf_dyn_relocs **pp;
   4463 	  struct elf_dyn_relocs *p;
   4464 
   4465 	  /* Add reloc counts against the indirect sym to the direct sym
   4466 	     list.  Merge any entries against the same section.  */
   4467 	  for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
   4468 	    {
   4469 	      struct elf_dyn_relocs *q;
   4470 
   4471 	      for (q = edir->dyn_relocs; q != NULL; q = q->next)
   4472 		if (q->sec == p->sec)
   4473 		  {
   4474 		    q->pc_count += p->pc_count;
   4475 		    q->count += p->count;
   4476 		    *pp = p->next;
   4477 		    break;
   4478 		  }
   4479 	      if (q == NULL)
   4480 		pp = &p->next;
   4481 	    }
   4482 	  *pp = edir->dyn_relocs;
   4483 	}
   4484 
   4485       edir->dyn_relocs = eind->dyn_relocs;
   4486       eind->dyn_relocs = NULL;
   4487     }
   4488 
   4489   /* If we were called to copy over info for a weak sym, that's all.
   4490      You might think dyn_relocs need not be copied over;  After all,
   4491      both syms will be dynamic or both non-dynamic so we're just
   4492      moving reloc accounting around.  However, ELIMINATE_COPY_RELOCS
   4493      code in ppc64_elf_adjust_dynamic_symbol needs to check for
   4494      dyn_relocs in read-only sections, and it does so on what is the
   4495      DIR sym here.  */
   4496   if (eind->elf.root.type != bfd_link_hash_indirect)
   4497     return;
   4498 
   4499   /* Copy over got entries that we may have already seen to the
   4500      symbol which just became indirect.  */
   4501   if (eind->elf.got.glist != NULL)
   4502     {
   4503       if (edir->elf.got.glist != NULL)
   4504 	{
   4505 	  struct got_entry **entp;
   4506 	  struct got_entry *ent;
   4507 
   4508 	  for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
   4509 	    {
   4510 	      struct got_entry *dent;
   4511 
   4512 	      for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
   4513 		if (dent->addend == ent->addend
   4514 		    && dent->owner == ent->owner
   4515 		    && dent->tls_type == ent->tls_type)
   4516 		  {
   4517 		    dent->got.refcount += ent->got.refcount;
   4518 		    *entp = ent->next;
   4519 		    break;
   4520 		  }
   4521 	      if (dent == NULL)
   4522 		entp = &ent->next;
   4523 	    }
   4524 	  *entp = edir->elf.got.glist;
   4525 	}
   4526 
   4527       edir->elf.got.glist = eind->elf.got.glist;
   4528       eind->elf.got.glist = NULL;
   4529     }
   4530 
   4531   /* And plt entries.  */
   4532   move_plt_plist (eind, edir);
   4533 
   4534   if (eind->elf.dynindx != -1)
   4535     {
   4536       if (edir->elf.dynindx != -1)
   4537 	_bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
   4538 				edir->elf.dynstr_index);
   4539       edir->elf.dynindx = eind->elf.dynindx;
   4540       edir->elf.dynstr_index = eind->elf.dynstr_index;
   4541       eind->elf.dynindx = -1;
   4542       eind->elf.dynstr_index = 0;
   4543     }
   4544 }
   4545 
   4546 /* Find the function descriptor hash entry from the given function code
   4547    hash entry FH.  Link the entries via their OH fields.  */
   4548 
   4549 static struct ppc_link_hash_entry *
   4550 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
   4551 {
   4552   struct ppc_link_hash_entry *fdh = fh->oh;
   4553 
   4554   if (fdh == NULL)
   4555     {
   4556       const char *fd_name = fh->elf.root.root.string + 1;
   4557 
   4558       fdh = (struct ppc_link_hash_entry *)
   4559 	elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
   4560       if (fdh == NULL)
   4561 	return fdh;
   4562 
   4563       fdh->is_func_descriptor = 1;
   4564       fdh->oh = fh;
   4565       fh->is_func = 1;
   4566       fh->oh = fdh;
   4567     }
   4568 
   4569   return ppc_follow_link (fdh);
   4570 }
   4571 
   4572 /* Make a fake function descriptor sym for the code sym FH.  */
   4573 
   4574 static struct ppc_link_hash_entry *
   4575 make_fdh (struct bfd_link_info *info,
   4576 	  struct ppc_link_hash_entry *fh)
   4577 {
   4578   bfd *abfd;
   4579   asymbol *newsym;
   4580   struct bfd_link_hash_entry *bh;
   4581   struct ppc_link_hash_entry *fdh;
   4582 
   4583   abfd = fh->elf.root.u.undef.abfd;
   4584   newsym = bfd_make_empty_symbol (abfd);
   4585   newsym->name = fh->elf.root.root.string + 1;
   4586   newsym->section = bfd_und_section_ptr;
   4587   newsym->value = 0;
   4588   newsym->flags = BSF_WEAK;
   4589 
   4590   bh = NULL;
   4591   if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
   4592 					 newsym->flags, newsym->section,
   4593 					 newsym->value, NULL, FALSE, FALSE,
   4594 					 &bh))
   4595     return NULL;
   4596 
   4597   fdh = (struct ppc_link_hash_entry *) bh;
   4598   fdh->elf.non_elf = 0;
   4599   fdh->fake = 1;
   4600   fdh->is_func_descriptor = 1;
   4601   fdh->oh = fh;
   4602   fh->is_func = 1;
   4603   fh->oh = fdh;
   4604   return fdh;
   4605 }
   4606 
   4607 /* Fix function descriptor symbols defined in .opd sections to be
   4608    function type.  */
   4609 
   4610 static bfd_boolean
   4611 ppc64_elf_add_symbol_hook (bfd *ibfd,
   4612 			   struct bfd_link_info *info,
   4613 			   Elf_Internal_Sym *isym,
   4614 			   const char **name ATTRIBUTE_UNUSED,
   4615 			   flagword *flags ATTRIBUTE_UNUSED,
   4616 			   asection **sec,
   4617 			   bfd_vma *value ATTRIBUTE_UNUSED)
   4618 {
   4619   if ((ibfd->flags & DYNAMIC) == 0
   4620       && ELF_ST_BIND (isym->st_info) == STB_GNU_UNIQUE)
   4621     elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
   4622 
   4623   if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
   4624     {
   4625       if ((ibfd->flags & DYNAMIC) == 0)
   4626 	elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
   4627     }
   4628   else if (ELF_ST_TYPE (isym->st_info) == STT_FUNC)
   4629     ;
   4630   else if (*sec != NULL
   4631 	   && strcmp ((*sec)->name, ".opd") == 0)
   4632     isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
   4633 
   4634   return TRUE;
   4635 }
   4636 
   4637 /* This function makes an old ABI object reference to ".bar" cause the
   4638    inclusion of a new ABI object archive that defines "bar".
   4639    NAME is a symbol defined in an archive.  Return a symbol in the hash
   4640    table that might be satisfied by the archive symbols.  */
   4641 
   4642 static struct elf_link_hash_entry *
   4643 ppc64_elf_archive_symbol_lookup (bfd *abfd,
   4644 				 struct bfd_link_info *info,
   4645 				 const char *name)
   4646 {
   4647   struct elf_link_hash_entry *h;
   4648   char *dot_name;
   4649   size_t len;
   4650 
   4651   h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
   4652   if (h != NULL
   4653       /* Don't return this sym if it is a fake function descriptor
   4654 	 created by add_symbol_adjust.  */
   4655       && !(h->root.type == bfd_link_hash_undefweak
   4656 	   && ((struct ppc_link_hash_entry *) h)->fake))
   4657     return h;
   4658 
   4659   if (name[0] == '.')
   4660     return h;
   4661 
   4662   len = strlen (name);
   4663   dot_name = bfd_alloc (abfd, len + 2);
   4664   if (dot_name == NULL)
   4665     return (struct elf_link_hash_entry *) 0 - 1;
   4666   dot_name[0] = '.';
   4667   memcpy (dot_name + 1, name, len + 1);
   4668   h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
   4669   bfd_release (abfd, dot_name);
   4670   return h;
   4671 }
   4672 
   4673 /* This function satisfies all old ABI object references to ".bar" if a
   4674    new ABI object defines "bar".  Well, at least, undefined dot symbols
   4675    are made weak.  This stops later archive searches from including an
   4676    object if we already have a function descriptor definition.  It also
   4677    prevents the linker complaining about undefined symbols.
   4678    We also check and correct mismatched symbol visibility here.  The
   4679    most restrictive visibility of the function descriptor and the
   4680    function entry symbol is used.  */
   4681 
   4682 static bfd_boolean
   4683 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
   4684 {
   4685   struct ppc_link_hash_table *htab;
   4686   struct ppc_link_hash_entry *fdh;
   4687 
   4688   if (eh->elf.root.type == bfd_link_hash_indirect)
   4689     return TRUE;
   4690 
   4691   if (eh->elf.root.type == bfd_link_hash_warning)
   4692     eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
   4693 
   4694   if (eh->elf.root.root.string[0] != '.')
   4695     abort ();
   4696 
   4697   htab = ppc_hash_table (info);
   4698   if (htab == NULL)
   4699     return FALSE;
   4700 
   4701   fdh = lookup_fdh (eh, htab);
   4702   if (fdh == NULL)
   4703     {
   4704       if (!info->relocatable
   4705 	  && (eh->elf.root.type == bfd_link_hash_undefined
   4706 	      || eh->elf.root.type == bfd_link_hash_undefweak)
   4707 	  && eh->elf.ref_regular)
   4708 	{
   4709 	  /* Make an undefweak function descriptor sym, which is enough to
   4710 	     pull in an --as-needed shared lib, but won't cause link
   4711 	     errors.  Archives are handled elsewhere.  */
   4712 	  fdh = make_fdh (info, eh);
   4713 	  if (fdh == NULL)
   4714 	    return FALSE;
   4715 	  fdh->elf.ref_regular = 1;
   4716 	}
   4717     }
   4718   else
   4719     {
   4720       unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
   4721       unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
   4722       if (entry_vis < descr_vis)
   4723 	fdh->elf.other += entry_vis - descr_vis;
   4724       else if (entry_vis > descr_vis)
   4725 	eh->elf.other += descr_vis - entry_vis;
   4726 
   4727       if ((fdh->elf.root.type == bfd_link_hash_defined
   4728 	   || fdh->elf.root.type == bfd_link_hash_defweak)
   4729 	  && eh->elf.root.type == bfd_link_hash_undefined)
   4730 	{
   4731 	  eh->elf.root.type = bfd_link_hash_undefweak;
   4732 	  eh->was_undefined = 1;
   4733 	  htab->twiddled_syms = 1;
   4734 	}
   4735     }
   4736 
   4737   return TRUE;
   4738 }
   4739 
   4740 /* Process list of dot-symbols we made in link_hash_newfunc.  */
   4741 
   4742 static bfd_boolean
   4743 ppc64_elf_process_dot_syms (bfd *ibfd, struct bfd_link_info *info)
   4744 {
   4745   struct ppc_link_hash_table *htab;
   4746   struct ppc_link_hash_entry **p, *eh;
   4747 
   4748   if (!is_ppc64_elf (info->output_bfd))
   4749     return TRUE;
   4750   htab = ppc_hash_table (info);
   4751   if (htab == NULL)
   4752     return FALSE;
   4753 
   4754   if (is_ppc64_elf (ibfd))
   4755     {
   4756       p = &htab->dot_syms;
   4757       while ((eh = *p) != NULL)
   4758 	{
   4759 	  *p = NULL;
   4760 	  if (!add_symbol_adjust (eh, info))
   4761 	    return FALSE;
   4762 	  p = &eh->u.next_dot_sym;
   4763 	}
   4764     }
   4765 
   4766   /* Clear the list for non-ppc64 input files.  */
   4767   p = &htab->dot_syms;
   4768   while ((eh = *p) != NULL)
   4769     {
   4770       *p = NULL;
   4771       p = &eh->u.next_dot_sym;
   4772     }
   4773 
   4774   /* We need to fix the undefs list for any syms we have twiddled to
   4775      undef_weak.  */
   4776   if (htab->twiddled_syms)
   4777     {
   4778       bfd_link_repair_undef_list (&htab->elf.root);
   4779       htab->twiddled_syms = 0;
   4780     }
   4781   return TRUE;
   4782 }
   4783 
   4784 /* Undo hash table changes when an --as-needed input file is determined
   4785    not to be needed.  */
   4786 
   4787 static bfd_boolean
   4788 ppc64_elf_as_needed_cleanup (bfd *ibfd ATTRIBUTE_UNUSED,
   4789 			     struct bfd_link_info *info)
   4790 {
   4791   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   4792 
   4793   if (htab == NULL)
   4794     return FALSE;
   4795 
   4796   htab->dot_syms = NULL;
   4797   return TRUE;
   4798 }
   4799 
   4800 /* If --just-symbols against a final linked binary, then assume we need
   4801    toc adjusting stubs when calling functions defined there.  */
   4802 
   4803 static void
   4804 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
   4805 {
   4806   if ((sec->flags & SEC_CODE) != 0
   4807       && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
   4808       && is_ppc64_elf (sec->owner))
   4809     {
   4810       asection *got = bfd_get_section_by_name (sec->owner, ".got");
   4811       if (got != NULL
   4812 	  && got->size >= elf_backend_got_header_size
   4813 	  && bfd_get_section_by_name (sec->owner, ".opd") != NULL)
   4814 	sec->has_toc_reloc = 1;
   4815     }
   4816   _bfd_elf_link_just_syms (sec, info);
   4817 }
   4818 
   4819 static struct plt_entry **
   4820 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
   4821 		       unsigned long r_symndx, bfd_vma r_addend, int tls_type)
   4822 {
   4823   struct got_entry **local_got_ents = elf_local_got_ents (abfd);
   4824   struct plt_entry **local_plt;
   4825   unsigned char *local_got_tls_masks;
   4826 
   4827   if (local_got_ents == NULL)
   4828     {
   4829       bfd_size_type size = symtab_hdr->sh_info;
   4830 
   4831       size *= (sizeof (*local_got_ents)
   4832 	       + sizeof (*local_plt)
   4833 	       + sizeof (*local_got_tls_masks));
   4834       local_got_ents = bfd_zalloc (abfd, size);
   4835       if (local_got_ents == NULL)
   4836 	return NULL;
   4837       elf_local_got_ents (abfd) = local_got_ents;
   4838     }
   4839 
   4840   if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
   4841     {
   4842       struct got_entry *ent;
   4843 
   4844       for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
   4845 	if (ent->addend == r_addend
   4846 	    && ent->owner == abfd
   4847 	    && ent->tls_type == tls_type)
   4848 	  break;
   4849       if (ent == NULL)
   4850 	{
   4851 	  bfd_size_type amt = sizeof (*ent);
   4852 	  ent = bfd_alloc (abfd, amt);
   4853 	  if (ent == NULL)
   4854 	    return FALSE;
   4855 	  ent->next = local_got_ents[r_symndx];
   4856 	  ent->addend = r_addend;
   4857 	  ent->owner = abfd;
   4858 	  ent->tls_type = tls_type;
   4859 	  ent->is_indirect = FALSE;
   4860 	  ent->got.refcount = 0;
   4861 	  local_got_ents[r_symndx] = ent;
   4862 	}
   4863       ent->got.refcount += 1;
   4864     }
   4865 
   4866   local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
   4867   local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
   4868   local_got_tls_masks[r_symndx] |= tls_type;
   4869 
   4870   return local_plt + r_symndx;
   4871 }
   4872 
   4873 static bfd_boolean
   4874 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
   4875 {
   4876   struct plt_entry *ent;
   4877 
   4878   for (ent = *plist; ent != NULL; ent = ent->next)
   4879     if (ent->addend == addend)
   4880       break;
   4881   if (ent == NULL)
   4882     {
   4883       bfd_size_type amt = sizeof (*ent);
   4884       ent = bfd_alloc (abfd, amt);
   4885       if (ent == NULL)
   4886 	return FALSE;
   4887       ent->next = *plist;
   4888       ent->addend = addend;
   4889       ent->plt.refcount = 0;
   4890       *plist = ent;
   4891     }
   4892   ent->plt.refcount += 1;
   4893   return TRUE;
   4894 }
   4895 
   4896 static bfd_boolean
   4897 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
   4898 {
   4899   return (r_type == R_PPC64_REL24
   4900 	  || r_type == R_PPC64_REL14
   4901 	  || r_type == R_PPC64_REL14_BRTAKEN
   4902 	  || r_type == R_PPC64_REL14_BRNTAKEN
   4903 	  || r_type == R_PPC64_ADDR24
   4904 	  || r_type == R_PPC64_ADDR14
   4905 	  || r_type == R_PPC64_ADDR14_BRTAKEN
   4906 	  || r_type == R_PPC64_ADDR14_BRNTAKEN);
   4907 }
   4908 
   4909 /* Look through the relocs for a section during the first phase, and
   4910    calculate needed space in the global offset table, procedure
   4911    linkage table, and dynamic reloc sections.  */
   4912 
   4913 static bfd_boolean
   4914 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
   4915 			asection *sec, const Elf_Internal_Rela *relocs)
   4916 {
   4917   struct ppc_link_hash_table *htab;
   4918   Elf_Internal_Shdr *symtab_hdr;
   4919   struct elf_link_hash_entry **sym_hashes;
   4920   const Elf_Internal_Rela *rel;
   4921   const Elf_Internal_Rela *rel_end;
   4922   asection *sreloc;
   4923   asection **opd_sym_map;
   4924   struct elf_link_hash_entry *tga, *dottga;
   4925 
   4926   if (info->relocatable)
   4927     return TRUE;
   4928 
   4929   /* Don't do anything special with non-loaded, non-alloced sections.
   4930      In particular, any relocs in such sections should not affect GOT
   4931      and PLT reference counting (ie. we don't allow them to create GOT
   4932      or PLT entries), there's no possibility or desire to optimize TLS
   4933      relocs, and there's not much point in propagating relocs to shared
   4934      libs that the dynamic linker won't relocate.  */
   4935   if ((sec->flags & SEC_ALLOC) == 0)
   4936     return TRUE;
   4937 
   4938   BFD_ASSERT (is_ppc64_elf (abfd));
   4939 
   4940   htab = ppc_hash_table (info);
   4941   if (htab == NULL)
   4942     return FALSE;
   4943 
   4944   tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
   4945 			      FALSE, FALSE, TRUE);
   4946   dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
   4947 				 FALSE, FALSE, TRUE);
   4948   symtab_hdr = &elf_symtab_hdr (abfd);
   4949   sym_hashes = elf_sym_hashes (abfd);
   4950   sreloc = NULL;
   4951   opd_sym_map = NULL;
   4952   if (strcmp (sec->name, ".opd") == 0)
   4953     {
   4954       /* Garbage collection needs some extra help with .opd sections.
   4955 	 We don't want to necessarily keep everything referenced by
   4956 	 relocs in .opd, as that would keep all functions.  Instead,
   4957 	 if we reference an .opd symbol (a function descriptor), we
   4958 	 want to keep the function code symbol's section.  This is
   4959 	 easy for global symbols, but for local syms we need to keep
   4960 	 information about the associated function section.  */
   4961       bfd_size_type amt;
   4962 
   4963       amt = sec->size * sizeof (*opd_sym_map) / 8;
   4964       opd_sym_map = bfd_zalloc (abfd, amt);
   4965       if (opd_sym_map == NULL)
   4966 	return FALSE;
   4967       ppc64_elf_section_data (sec)->u.opd.func_sec = opd_sym_map;
   4968       BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
   4969       ppc64_elf_section_data (sec)->sec_type = sec_opd;
   4970     }
   4971 
   4972   if (htab->sfpr == NULL
   4973       && !create_linkage_sections (htab->elf.dynobj, info))
   4974     return FALSE;
   4975 
   4976   rel_end = relocs + sec->reloc_count;
   4977   for (rel = relocs; rel < rel_end; rel++)
   4978     {
   4979       unsigned long r_symndx;
   4980       struct elf_link_hash_entry *h;
   4981       enum elf_ppc64_reloc_type r_type;
   4982       int tls_type;
   4983       struct _ppc64_elf_section_data *ppc64_sec;
   4984       struct plt_entry **ifunc;
   4985 
   4986       r_symndx = ELF64_R_SYM (rel->r_info);
   4987       if (r_symndx < symtab_hdr->sh_info)
   4988 	h = NULL;
   4989       else
   4990 	{
   4991 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   4992 	  h = elf_follow_link (h);
   4993 	}
   4994 
   4995       tls_type = 0;
   4996       ifunc = NULL;
   4997       if (h != NULL)
   4998 	{
   4999 	  if (h->type == STT_GNU_IFUNC)
   5000 	    {
   5001 	      h->needs_plt = 1;
   5002 	      ifunc = &h->plt.plist;
   5003 	    }
   5004 	}
   5005       else
   5006 	{
   5007 	  Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
   5008 							  abfd, r_symndx);
   5009 	  if (isym == NULL)
   5010 	    return FALSE;
   5011 
   5012 	  if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
   5013 	    {
   5014 	      ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
   5015 					     rel->r_addend, PLT_IFUNC);
   5016 	      if (ifunc == NULL)
   5017 		return FALSE;
   5018 	    }
   5019 	}
   5020       r_type = ELF64_R_TYPE (rel->r_info);
   5021       if (is_branch_reloc (r_type))
   5022 	{
   5023 	  if (h != NULL && (h == tga || h == dottga))
   5024 	    {
   5025 	      if (rel != relocs
   5026 		  && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
   5027 		      || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
   5028 		/* We have a new-style __tls_get_addr call with a marker
   5029 		   reloc.  */
   5030 		;
   5031 	      else
   5032 		/* Mark this section as having an old-style call.  */
   5033 		sec->has_tls_get_addr_call = 1;
   5034 	    }
   5035 
   5036 	  /* STT_GNU_IFUNC symbols must have a PLT entry.  */
   5037 	  if (ifunc != NULL
   5038 	      && !update_plt_info (abfd, ifunc, rel->r_addend))
   5039 	    return FALSE;
   5040 	}
   5041 
   5042       switch (r_type)
   5043 	{
   5044 	case R_PPC64_TLSGD:
   5045 	case R_PPC64_TLSLD:
   5046 	  /* These special tls relocs tie a call to __tls_get_addr with
   5047 	     its parameter symbol.  */
   5048 	  break;
   5049 
   5050 	case R_PPC64_GOT_TLSLD16:
   5051 	case R_PPC64_GOT_TLSLD16_LO:
   5052 	case R_PPC64_GOT_TLSLD16_HI:
   5053 	case R_PPC64_GOT_TLSLD16_HA:
   5054 	  tls_type = TLS_TLS | TLS_LD;
   5055 	  goto dogottls;
   5056 
   5057 	case R_PPC64_GOT_TLSGD16:
   5058 	case R_PPC64_GOT_TLSGD16_LO:
   5059 	case R_PPC64_GOT_TLSGD16_HI:
   5060 	case R_PPC64_GOT_TLSGD16_HA:
   5061 	  tls_type = TLS_TLS | TLS_GD;
   5062 	  goto dogottls;
   5063 
   5064 	case R_PPC64_GOT_TPREL16_DS:
   5065 	case R_PPC64_GOT_TPREL16_LO_DS:
   5066 	case R_PPC64_GOT_TPREL16_HI:
   5067 	case R_PPC64_GOT_TPREL16_HA:
   5068 	  if (!info->executable)
   5069 	    info->flags |= DF_STATIC_TLS;
   5070 	  tls_type = TLS_TLS | TLS_TPREL;
   5071 	  goto dogottls;
   5072 
   5073 	case R_PPC64_GOT_DTPREL16_DS:
   5074 	case R_PPC64_GOT_DTPREL16_LO_DS:
   5075 	case R_PPC64_GOT_DTPREL16_HI:
   5076 	case R_PPC64_GOT_DTPREL16_HA:
   5077 	  tls_type = TLS_TLS | TLS_DTPREL;
   5078 	dogottls:
   5079 	  sec->has_tls_reloc = 1;
   5080 	  /* Fall thru */
   5081 
   5082 	case R_PPC64_GOT16:
   5083 	case R_PPC64_GOT16_DS:
   5084 	case R_PPC64_GOT16_HA:
   5085 	case R_PPC64_GOT16_HI:
   5086 	case R_PPC64_GOT16_LO:
   5087 	case R_PPC64_GOT16_LO_DS:
   5088 	  /* This symbol requires a global offset table entry.  */
   5089 	  sec->has_toc_reloc = 1;
   5090 	  if (r_type == R_PPC64_GOT_TLSLD16
   5091 	      || r_type == R_PPC64_GOT_TLSGD16
   5092 	      || r_type == R_PPC64_GOT_TPREL16_DS
   5093 	      || r_type == R_PPC64_GOT_DTPREL16_DS
   5094 	      || r_type == R_PPC64_GOT16
   5095 	      || r_type == R_PPC64_GOT16_DS)
   5096 	    {
   5097 	      htab->do_multi_toc = 1;
   5098 	      ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
   5099 	    }
   5100 
   5101 	  if (ppc64_elf_tdata (abfd)->got == NULL
   5102 	      && !create_got_section (abfd, info))
   5103 	    return FALSE;
   5104 
   5105 	  if (h != NULL)
   5106 	    {
   5107 	      struct ppc_link_hash_entry *eh;
   5108 	      struct got_entry *ent;
   5109 
   5110 	      eh = (struct ppc_link_hash_entry *) h;
   5111 	      for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
   5112 		if (ent->addend == rel->r_addend
   5113 		    && ent->owner == abfd
   5114 		    && ent->tls_type == tls_type)
   5115 		  break;
   5116 	      if (ent == NULL)
   5117 		{
   5118 		  bfd_size_type amt = sizeof (*ent);
   5119 		  ent = bfd_alloc (abfd, amt);
   5120 		  if (ent == NULL)
   5121 		    return FALSE;
   5122 		  ent->next = eh->elf.got.glist;
   5123 		  ent->addend = rel->r_addend;
   5124 		  ent->owner = abfd;
   5125 		  ent->tls_type = tls_type;
   5126 		  ent->is_indirect = FALSE;
   5127 		  ent->got.refcount = 0;
   5128 		  eh->elf.got.glist = ent;
   5129 		}
   5130 	      ent->got.refcount += 1;
   5131 	      eh->tls_mask |= tls_type;
   5132 	    }
   5133 	  else
   5134 	    /* This is a global offset table entry for a local symbol.  */
   5135 	    if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
   5136 					rel->r_addend, tls_type))
   5137 	      return FALSE;
   5138 	  break;
   5139 
   5140 	case R_PPC64_PLT16_HA:
   5141 	case R_PPC64_PLT16_HI:
   5142 	case R_PPC64_PLT16_LO:
   5143 	case R_PPC64_PLT32:
   5144 	case R_PPC64_PLT64:
   5145 	  /* This symbol requires a procedure linkage table entry.  We
   5146 	     actually build the entry in adjust_dynamic_symbol,
   5147 	     because this might be a case of linking PIC code without
   5148 	     linking in any dynamic objects, in which case we don't
   5149 	     need to generate a procedure linkage table after all.  */
   5150 	  if (h == NULL)
   5151 	    {
   5152 	      /* It does not make sense to have a procedure linkage
   5153 		 table entry for a local symbol.  */
   5154 	      bfd_set_error (bfd_error_bad_value);
   5155 	      return FALSE;
   5156 	    }
   5157 	  else
   5158 	    {
   5159 	      if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
   5160 		return FALSE;
   5161 	      h->needs_plt = 1;
   5162 	      if (h->root.root.string[0] == '.'
   5163 		  && h->root.root.string[1] != '\0')
   5164 		((struct ppc_link_hash_entry *) h)->is_func = 1;
   5165 	    }
   5166 	  break;
   5167 
   5168 	  /* The following relocations don't need to propagate the
   5169 	     relocation if linking a shared object since they are
   5170 	     section relative.  */
   5171 	case R_PPC64_SECTOFF:
   5172 	case R_PPC64_SECTOFF_LO:
   5173 	case R_PPC64_SECTOFF_HI:
   5174 	case R_PPC64_SECTOFF_HA:
   5175 	case R_PPC64_SECTOFF_DS:
   5176 	case R_PPC64_SECTOFF_LO_DS:
   5177 	case R_PPC64_DTPREL16:
   5178 	case R_PPC64_DTPREL16_LO:
   5179 	case R_PPC64_DTPREL16_HI:
   5180 	case R_PPC64_DTPREL16_HA:
   5181 	case R_PPC64_DTPREL16_DS:
   5182 	case R_PPC64_DTPREL16_LO_DS:
   5183 	case R_PPC64_DTPREL16_HIGHER:
   5184 	case R_PPC64_DTPREL16_HIGHERA:
   5185 	case R_PPC64_DTPREL16_HIGHEST:
   5186 	case R_PPC64_DTPREL16_HIGHESTA:
   5187 	  break;
   5188 
   5189 	  /* Nor do these.  */
   5190 	case R_PPC64_REL16:
   5191 	case R_PPC64_REL16_LO:
   5192 	case R_PPC64_REL16_HI:
   5193 	case R_PPC64_REL16_HA:
   5194 	  break;
   5195 
   5196 	case R_PPC64_TOC16:
   5197 	case R_PPC64_TOC16_DS:
   5198 	  htab->do_multi_toc = 1;
   5199 	  ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
   5200 	case R_PPC64_TOC16_LO:
   5201 	case R_PPC64_TOC16_HI:
   5202 	case R_PPC64_TOC16_HA:
   5203 	case R_PPC64_TOC16_LO_DS:
   5204 	  sec->has_toc_reloc = 1;
   5205 	  break;
   5206 
   5207 	  /* This relocation describes the C++ object vtable hierarchy.
   5208 	     Reconstruct it for later use during GC.  */
   5209 	case R_PPC64_GNU_VTINHERIT:
   5210 	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
   5211 	    return FALSE;
   5212 	  break;
   5213 
   5214 	  /* This relocation describes which C++ vtable entries are actually
   5215 	     used.  Record for later use during GC.  */
   5216 	case R_PPC64_GNU_VTENTRY:
   5217 	  BFD_ASSERT (h != NULL);
   5218 	  if (h != NULL
   5219 	      && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
   5220 	    return FALSE;
   5221 	  break;
   5222 
   5223 	case R_PPC64_REL14:
   5224 	case R_PPC64_REL14_BRTAKEN:
   5225 	case R_PPC64_REL14_BRNTAKEN:
   5226 	  {
   5227 	    asection *dest = NULL;
   5228 
   5229 	    /* Heuristic: If jumping outside our section, chances are
   5230 	       we are going to need a stub.  */
   5231 	    if (h != NULL)
   5232 	      {
   5233 		/* If the sym is weak it may be overridden later, so
   5234 		   don't assume we know where a weak sym lives.  */
   5235 		if (h->root.type == bfd_link_hash_defined)
   5236 		  dest = h->root.u.def.section;
   5237 	      }
   5238 	    else
   5239 	      {
   5240 		Elf_Internal_Sym *isym;
   5241 
   5242 		isym = bfd_sym_from_r_symndx (&htab->sym_cache,
   5243 					      abfd, r_symndx);
   5244 		if (isym == NULL)
   5245 		  return FALSE;
   5246 
   5247 		dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
   5248 	      }
   5249 
   5250 	    if (dest != sec)
   5251 	      ppc64_elf_section_data (sec)->has_14bit_branch = 1;
   5252 	  }
   5253 	  /* Fall through.  */
   5254 
   5255 	case R_PPC64_REL24:
   5256 	  if (h != NULL && ifunc == NULL)
   5257 	    {
   5258 	      /* We may need a .plt entry if the function this reloc
   5259 		 refers to is in a shared lib.  */
   5260 	      if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
   5261 		return FALSE;
   5262 	      h->needs_plt = 1;
   5263 	      if (h->root.root.string[0] == '.'
   5264 		  && h->root.root.string[1] != '\0')
   5265 		((struct ppc_link_hash_entry *) h)->is_func = 1;
   5266 	      if (h == tga || h == dottga)
   5267 		sec->has_tls_reloc = 1;
   5268 	    }
   5269 	  break;
   5270 
   5271 	case R_PPC64_TPREL64:
   5272 	  tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
   5273 	  if (!info->executable)
   5274 	    info->flags |= DF_STATIC_TLS;
   5275 	  goto dotlstoc;
   5276 
   5277 	case R_PPC64_DTPMOD64:
   5278 	  if (rel + 1 < rel_end
   5279 	      && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
   5280 	      && rel[1].r_offset == rel->r_offset + 8)
   5281 	    tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
   5282 	  else
   5283 	    tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
   5284 	  goto dotlstoc;
   5285 
   5286 	case R_PPC64_DTPREL64:
   5287 	  tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
   5288 	  if (rel != relocs
   5289 	      && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
   5290 	      && rel[-1].r_offset == rel->r_offset - 8)
   5291 	    /* This is the second reloc of a dtpmod, dtprel pair.
   5292 	       Don't mark with TLS_DTPREL.  */
   5293 	    goto dodyn;
   5294 
   5295 	dotlstoc:
   5296 	  sec->has_tls_reloc = 1;
   5297 	  if (h != NULL)
   5298 	    {
   5299 	      struct ppc_link_hash_entry *eh;
   5300 	      eh = (struct ppc_link_hash_entry *) h;
   5301 	      eh->tls_mask |= tls_type;
   5302 	    }
   5303 	  else
   5304 	    if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
   5305 					rel->r_addend, tls_type))
   5306 	      return FALSE;
   5307 
   5308 	  ppc64_sec = ppc64_elf_section_data (sec);
   5309 	  if (ppc64_sec->sec_type != sec_toc)
   5310 	    {
   5311 	      bfd_size_type amt;
   5312 
   5313 	      /* One extra to simplify get_tls_mask.  */
   5314 	      amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
   5315 	      ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
   5316 	      if (ppc64_sec->u.toc.symndx == NULL)
   5317 		return FALSE;
   5318 	      amt = sec->size * sizeof (bfd_vma) / 8;
   5319 	      ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
   5320 	      if (ppc64_sec->u.toc.add == NULL)
   5321 		return FALSE;
   5322 	      BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
   5323 	      ppc64_sec->sec_type = sec_toc;
   5324 	    }
   5325 	  BFD_ASSERT (rel->r_offset % 8 == 0);
   5326 	  ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
   5327 	  ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
   5328 
   5329 	  /* Mark the second slot of a GD or LD entry.
   5330 	     -1 to indicate GD and -2 to indicate LD.  */
   5331 	  if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
   5332 	    ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
   5333 	  else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
   5334 	    ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
   5335 	  goto dodyn;
   5336 
   5337 	case R_PPC64_TPREL16:
   5338 	case R_PPC64_TPREL16_LO:
   5339 	case R_PPC64_TPREL16_HI:
   5340 	case R_PPC64_TPREL16_HA:
   5341 	case R_PPC64_TPREL16_DS:
   5342 	case R_PPC64_TPREL16_LO_DS:
   5343 	case R_PPC64_TPREL16_HIGHER:
   5344 	case R_PPC64_TPREL16_HIGHERA:
   5345 	case R_PPC64_TPREL16_HIGHEST:
   5346 	case R_PPC64_TPREL16_HIGHESTA:
   5347 	  if (info->shared)
   5348 	    {
   5349 	      if (!info->executable)
   5350 		info->flags |= DF_STATIC_TLS;
   5351 	      goto dodyn;
   5352 	    }
   5353 	  break;
   5354 
   5355 	case R_PPC64_ADDR64:
   5356 	  if (opd_sym_map != NULL
   5357 	      && rel + 1 < rel_end
   5358 	      && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
   5359 	    {
   5360 	      if (h != NULL)
   5361 		{
   5362 		  if (h->root.root.string[0] == '.'
   5363 		      && h->root.root.string[1] != 0
   5364 		      && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
   5365 		    ;
   5366 		  else
   5367 		    ((struct ppc_link_hash_entry *) h)->is_func = 1;
   5368 		}
   5369 	      else
   5370 		{
   5371 		  asection *s;
   5372 		  Elf_Internal_Sym *isym;
   5373 
   5374 		  isym = bfd_sym_from_r_symndx (&htab->sym_cache,
   5375 						abfd, r_symndx);
   5376 		  if (isym == NULL)
   5377 		    return FALSE;
   5378 
   5379 		  s = bfd_section_from_elf_index (abfd, isym->st_shndx);
   5380 		  if (s != NULL && s != sec)
   5381 		    opd_sym_map[rel->r_offset / 8] = s;
   5382 		}
   5383 	    }
   5384 	  /* Fall through.  */
   5385 
   5386 	case R_PPC64_REL30:
   5387 	case R_PPC64_REL32:
   5388 	case R_PPC64_REL64:
   5389 	case R_PPC64_ADDR14:
   5390 	case R_PPC64_ADDR14_BRNTAKEN:
   5391 	case R_PPC64_ADDR14_BRTAKEN:
   5392 	case R_PPC64_ADDR16:
   5393 	case R_PPC64_ADDR16_DS:
   5394 	case R_PPC64_ADDR16_HA:
   5395 	case R_PPC64_ADDR16_HI:
   5396 	case R_PPC64_ADDR16_HIGHER:
   5397 	case R_PPC64_ADDR16_HIGHERA:
   5398 	case R_PPC64_ADDR16_HIGHEST:
   5399 	case R_PPC64_ADDR16_HIGHESTA:
   5400 	case R_PPC64_ADDR16_LO:
   5401 	case R_PPC64_ADDR16_LO_DS:
   5402 	case R_PPC64_ADDR24:
   5403 	case R_PPC64_ADDR32:
   5404 	case R_PPC64_UADDR16:
   5405 	case R_PPC64_UADDR32:
   5406 	case R_PPC64_UADDR64:
   5407 	case R_PPC64_TOC:
   5408 	  if (h != NULL && !info->shared)
   5409 	    /* We may need a copy reloc.  */
   5410 	    h->non_got_ref = 1;
   5411 
   5412 	  /* Don't propagate .opd relocs.  */
   5413 	  if (NO_OPD_RELOCS && opd_sym_map != NULL)
   5414 	    break;
   5415 
   5416 	  /* If we are creating a shared library, and this is a reloc
   5417 	     against a global symbol, or a non PC relative reloc
   5418 	     against a local symbol, then we need to copy the reloc
   5419 	     into the shared library.  However, if we are linking with
   5420 	     -Bsymbolic, we do not need to copy a reloc against a
   5421 	     global symbol which is defined in an object we are
   5422 	     including in the link (i.e., DEF_REGULAR is set).  At
   5423 	     this point we have not seen all the input files, so it is
   5424 	     possible that DEF_REGULAR is not set now but will be set
   5425 	     later (it is never cleared).  In case of a weak definition,
   5426 	     DEF_REGULAR may be cleared later by a strong definition in
   5427 	     a shared library.  We account for that possibility below by
   5428 	     storing information in the dyn_relocs field of the hash
   5429 	     table entry.  A similar situation occurs when creating
   5430 	     shared libraries and symbol visibility changes render the
   5431 	     symbol local.
   5432 
   5433 	     If on the other hand, we are creating an executable, we
   5434 	     may need to keep relocations for symbols satisfied by a
   5435 	     dynamic library if we manage to avoid copy relocs for the
   5436 	     symbol.  */
   5437 	dodyn:
   5438 	  if ((info->shared
   5439 	       && (must_be_dyn_reloc (info, r_type)
   5440 		   || (h != NULL
   5441 		       && (! info->symbolic
   5442 			   || h->root.type == bfd_link_hash_defweak
   5443 			   || !h->def_regular))))
   5444 	      || (ELIMINATE_COPY_RELOCS
   5445 		  && !info->shared
   5446 		  && h != NULL
   5447 		  && (h->root.type == bfd_link_hash_defweak
   5448 		      || !h->def_regular))
   5449 	      || (!info->shared
   5450 		  && ifunc != NULL))
   5451 	    {
   5452 	      struct elf_dyn_relocs *p;
   5453 	      struct elf_dyn_relocs **head;
   5454 
   5455 	      /* We must copy these reloc types into the output file.
   5456 		 Create a reloc section in dynobj and make room for
   5457 		 this reloc.  */
   5458 	      if (sreloc == NULL)
   5459 		{
   5460 		  sreloc = _bfd_elf_make_dynamic_reloc_section
   5461 		    (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
   5462 
   5463 		  if (sreloc == NULL)
   5464 		    return FALSE;
   5465 		}
   5466 
   5467 	      /* If this is a global symbol, we count the number of
   5468 		 relocations we need for this symbol.  */
   5469 	      if (h != NULL)
   5470 		{
   5471 		  head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
   5472 		}
   5473 	      else
   5474 		{
   5475 		  /* Track dynamic relocs needed for local syms too.
   5476 		     We really need local syms available to do this
   5477 		     easily.  Oh well.  */
   5478 		  asection *s;
   5479 		  void *vpp;
   5480 		  Elf_Internal_Sym *isym;
   5481 
   5482 		  isym = bfd_sym_from_r_symndx (&htab->sym_cache,
   5483 						abfd, r_symndx);
   5484 		  if (isym == NULL)
   5485 		    return FALSE;
   5486 
   5487 		  s = bfd_section_from_elf_index (abfd, isym->st_shndx);
   5488 		  if (s == NULL)
   5489 		    s = sec;
   5490 
   5491 		  vpp = &elf_section_data (s)->local_dynrel;
   5492 		  head = (struct elf_dyn_relocs **) vpp;
   5493 		}
   5494 
   5495 	      p = *head;
   5496 	      if (p == NULL || p->sec != sec)
   5497 		{
   5498 		  p = bfd_alloc (htab->elf.dynobj, sizeof *p);
   5499 		  if (p == NULL)
   5500 		    return FALSE;
   5501 		  p->next = *head;
   5502 		  *head = p;
   5503 		  p->sec = sec;
   5504 		  p->count = 0;
   5505 		  p->pc_count = 0;
   5506 		}
   5507 
   5508 	      p->count += 1;
   5509 	      if (!must_be_dyn_reloc (info, r_type))
   5510 		p->pc_count += 1;
   5511 	    }
   5512 	  break;
   5513 
   5514 	default:
   5515 	  break;
   5516 	}
   5517     }
   5518 
   5519   return TRUE;
   5520 }
   5521 
   5522 /* OFFSET in OPD_SEC specifies a function descriptor.  Return the address
   5523    of the code entry point, and its section.  */
   5524 
   5525 static bfd_vma
   5526 opd_entry_value (asection *opd_sec,
   5527 		 bfd_vma offset,
   5528 		 asection **code_sec,
   5529 		 bfd_vma *code_off,
   5530 		 bfd_boolean in_code_sec)
   5531 {
   5532   bfd *opd_bfd = opd_sec->owner;
   5533   Elf_Internal_Rela *relocs;
   5534   Elf_Internal_Rela *lo, *hi, *look;
   5535   bfd_vma val;
   5536 
   5537   /* No relocs implies we are linking a --just-symbols object, or looking
   5538      at a final linked executable with addr2line or somesuch.  */
   5539   if (opd_sec->reloc_count == 0)
   5540     {
   5541       char buf[8];
   5542 
   5543       if (!bfd_get_section_contents (opd_bfd, opd_sec, buf, offset, 8))
   5544 	return (bfd_vma) -1;
   5545 
   5546       val = bfd_get_64 (opd_bfd, buf);
   5547       if (code_sec != NULL)
   5548 	{
   5549 	  asection *sec, *likely = NULL;
   5550 
   5551 	  if (in_code_sec)
   5552 	    {
   5553 	      sec = *code_sec;
   5554 	      if (sec->vma <= val
   5555 		  && val < sec->vma + sec->size)
   5556 		likely = sec;
   5557 	      else
   5558 		val = -1;
   5559 	    }
   5560 	  else
   5561 	    for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
   5562 	      if (sec->vma <= val
   5563 		  && (sec->flags & SEC_LOAD) != 0
   5564 		  && (sec->flags & SEC_ALLOC) != 0)
   5565 		likely = sec;
   5566 	  if (likely != NULL)
   5567 	    {
   5568 	      *code_sec = likely;
   5569 	      if (code_off != NULL)
   5570 		*code_off = val - likely->vma;
   5571 	    }
   5572 	}
   5573       return val;
   5574     }
   5575 
   5576   BFD_ASSERT (is_ppc64_elf (opd_bfd));
   5577 
   5578   relocs = ppc64_elf_tdata (opd_bfd)->opd_relocs;
   5579   if (relocs == NULL)
   5580     relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
   5581 
   5582   /* Go find the opd reloc at the sym address.  */
   5583   lo = relocs;
   5584   BFD_ASSERT (lo != NULL);
   5585   hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
   5586   val = (bfd_vma) -1;
   5587   while (lo < hi)
   5588     {
   5589       look = lo + (hi - lo) / 2;
   5590       if (look->r_offset < offset)
   5591 	lo = look + 1;
   5592       else if (look->r_offset > offset)
   5593 	hi = look;
   5594       else
   5595 	{
   5596 	  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
   5597 
   5598 	  if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
   5599 	      && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
   5600 	    {
   5601 	      unsigned long symndx = ELF64_R_SYM (look->r_info);
   5602 	      asection *sec;
   5603 
   5604 	      if (symndx < symtab_hdr->sh_info
   5605 		  || elf_sym_hashes (opd_bfd) == NULL)
   5606 		{
   5607 		  Elf_Internal_Sym *sym;
   5608 
   5609 		  sym = (Elf_Internal_Sym *) symtab_hdr->contents;
   5610 		  if (sym == NULL)
   5611 		    {
   5612 		      size_t symcnt = symtab_hdr->sh_info;
   5613 		      if (elf_sym_hashes (opd_bfd) == NULL)
   5614 			symcnt = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
   5615 		      sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr, symcnt,
   5616 						  0, NULL, NULL, NULL);
   5617 		      if (sym == NULL)
   5618 			break;
   5619 		      symtab_hdr->contents = (bfd_byte *) sym;
   5620 		    }
   5621 
   5622 		  sym += symndx;
   5623 		  val = sym->st_value;
   5624 		  sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
   5625 		  BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
   5626 		}
   5627 	      else
   5628 		{
   5629 		  struct elf_link_hash_entry **sym_hashes;
   5630 		  struct elf_link_hash_entry *rh;
   5631 
   5632 		  sym_hashes = elf_sym_hashes (opd_bfd);
   5633 		  rh = sym_hashes[symndx - symtab_hdr->sh_info];
   5634 		  rh = elf_follow_link (rh);
   5635 		  BFD_ASSERT (rh->root.type == bfd_link_hash_defined
   5636 			      || rh->root.type == bfd_link_hash_defweak);
   5637 		  val = rh->root.u.def.value;
   5638 		  sec = rh->root.u.def.section;
   5639 		}
   5640 	      val += look->r_addend;
   5641 	      if (code_off != NULL)
   5642 		*code_off = val;
   5643 	      if (code_sec != NULL)
   5644 		{
   5645 		  if (in_code_sec && *code_sec != sec)
   5646 		    return -1;
   5647 		  else
   5648 		    *code_sec = sec;
   5649 		}
   5650 	      if (sec != NULL && sec->output_section != NULL)
   5651 		val += sec->output_section->vma + sec->output_offset;
   5652 	    }
   5653 	  break;
   5654 	}
   5655     }
   5656 
   5657   return val;
   5658 }
   5659 
   5660 /* If the ELF symbol SYM might be a function in SEC, return the
   5661    function size and set *CODE_OFF to the function's entry point,
   5662    otherwise return zero.  */
   5663 
   5664 static bfd_size_type
   5665 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
   5666 			      bfd_vma *code_off)
   5667 {
   5668   bfd_size_type size;
   5669 
   5670   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
   5671 		     | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
   5672     return 0;
   5673 
   5674   size = 0;
   5675   if (!(sym->flags & BSF_SYNTHETIC))
   5676     size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
   5677 
   5678   if (strcmp (sym->section->name, ".opd") == 0)
   5679     {
   5680       if (opd_entry_value (sym->section, sym->value,
   5681 			   &sec, code_off, TRUE) == (bfd_vma) -1)
   5682 	return 0;
   5683       /* An old ABI binary with dot-syms has a size of 24 on the .opd
   5684 	 symbol.  This size has nothing to do with the code size of the
   5685 	 function, which is what we're supposed to return, but the
   5686 	 code size isn't available without looking up the dot-sym.
   5687 	 However, doing that would be a waste of time particularly
   5688 	 since elf_find_function will look at the dot-sym anyway.
   5689 	 Now, elf_find_function will keep the largest size of any
   5690 	 function sym found at the code address of interest, so return
   5691 	 1 here to avoid it incorrectly caching a larger function size
   5692 	 for a small function.  This does mean we return the wrong
   5693 	 size for a new-ABI function of size 24, but all that does is
   5694 	 disable caching for such functions.  */
   5695       if (size == 24)
   5696 	size = 1;
   5697     }
   5698   else
   5699     {
   5700       if (sym->section != sec)
   5701 	return 0;
   5702       *code_off = sym->value;
   5703     }
   5704   if (size == 0)
   5705     size = 1;
   5706   return size;
   5707 }
   5708 
   5709 /* Return true if symbol is defined in a regular object file.  */
   5710 
   5711 static bfd_boolean
   5712 is_static_defined (struct elf_link_hash_entry *h)
   5713 {
   5714   return ((h->root.type == bfd_link_hash_defined
   5715 	   || h->root.type == bfd_link_hash_defweak)
   5716 	  && h->root.u.def.section != NULL
   5717 	  && h->root.u.def.section->output_section != NULL);
   5718 }
   5719 
   5720 /* If FDH is a function descriptor symbol, return the associated code
   5721    entry symbol if it is defined.  Return NULL otherwise.  */
   5722 
   5723 static struct ppc_link_hash_entry *
   5724 defined_code_entry (struct ppc_link_hash_entry *fdh)
   5725 {
   5726   if (fdh->is_func_descriptor)
   5727     {
   5728       struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
   5729       if (fh->elf.root.type == bfd_link_hash_defined
   5730 	  || fh->elf.root.type == bfd_link_hash_defweak)
   5731 	return fh;
   5732     }
   5733   return NULL;
   5734 }
   5735 
   5736 /* If FH is a function code entry symbol, return the associated
   5737    function descriptor symbol if it is defined.  Return NULL otherwise.  */
   5738 
   5739 static struct ppc_link_hash_entry *
   5740 defined_func_desc (struct ppc_link_hash_entry *fh)
   5741 {
   5742   if (fh->oh != NULL
   5743       && fh->oh->is_func_descriptor)
   5744     {
   5745       struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
   5746       if (fdh->elf.root.type == bfd_link_hash_defined
   5747 	  || fdh->elf.root.type == bfd_link_hash_defweak)
   5748 	return fdh;
   5749     }
   5750   return NULL;
   5751 }
   5752 
   5753 /* Mark all our entry sym sections, both opd and code section.  */
   5754 
   5755 static void
   5756 ppc64_elf_gc_keep (struct bfd_link_info *info)
   5757 {
   5758   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   5759   struct bfd_sym_chain *sym;
   5760 
   5761   if (htab == NULL)
   5762     return;
   5763 
   5764   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
   5765     {
   5766       struct ppc_link_hash_entry *eh, *fh;
   5767       asection *sec;
   5768 
   5769       eh = (struct ppc_link_hash_entry *)
   5770 	elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
   5771       if (eh == NULL)
   5772 	continue;
   5773       if (eh->elf.root.type != bfd_link_hash_defined
   5774 	  && eh->elf.root.type != bfd_link_hash_defweak)
   5775 	continue;
   5776 
   5777       fh = defined_code_entry (eh);
   5778       if (fh != NULL)
   5779 	{
   5780 	  sec = fh->elf.root.u.def.section;
   5781 	  sec->flags |= SEC_KEEP;
   5782 	}
   5783       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
   5784 	       && opd_entry_value (eh->elf.root.u.def.section,
   5785 				   eh->elf.root.u.def.value,
   5786 				   &sec, NULL, FALSE) != (bfd_vma) -1)
   5787 	sec->flags |= SEC_KEEP;
   5788 
   5789       sec = eh->elf.root.u.def.section;
   5790       sec->flags |= SEC_KEEP;
   5791     }
   5792 }
   5793 
   5794 /* Mark sections containing dynamically referenced symbols.  When
   5795    building shared libraries, we must assume that any visible symbol is
   5796    referenced.  */
   5797 
   5798 static bfd_boolean
   5799 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
   5800 {
   5801   struct bfd_link_info *info = (struct bfd_link_info *) inf;
   5802   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
   5803   struct ppc_link_hash_entry *fdh;
   5804 
   5805   /* Dynamic linking info is on the func descriptor sym.  */
   5806   fdh = defined_func_desc (eh);
   5807   if (fdh != NULL)
   5808     eh = fdh;
   5809 
   5810   if ((eh->elf.root.type == bfd_link_hash_defined
   5811        || eh->elf.root.type == bfd_link_hash_defweak)
   5812       && (eh->elf.ref_dynamic
   5813 	  || (!info->executable
   5814 	      && eh->elf.def_regular
   5815 	      && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
   5816 	      && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
   5817 	      && (strchr (eh->elf.root.root.string, ELF_VER_CHR) != NULL
   5818 		  || !bfd_hide_sym_by_version (info->version_info,
   5819 					       eh->elf.root.root.string)))))
   5820     {
   5821       asection *code_sec;
   5822       struct ppc_link_hash_entry *fh;
   5823 
   5824       eh->elf.root.u.def.section->flags |= SEC_KEEP;
   5825 
   5826       /* Function descriptor syms cause the associated
   5827 	 function code sym section to be marked.  */
   5828       fh = defined_code_entry (eh);
   5829       if (fh != NULL)
   5830 	{
   5831 	  code_sec = fh->elf.root.u.def.section;
   5832 	  code_sec->flags |= SEC_KEEP;
   5833 	}
   5834       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
   5835 	       && opd_entry_value (eh->elf.root.u.def.section,
   5836 				   eh->elf.root.u.def.value,
   5837 				   &code_sec, NULL, FALSE) != (bfd_vma) -1)
   5838 	code_sec->flags |= SEC_KEEP;
   5839     }
   5840 
   5841   return TRUE;
   5842 }
   5843 
   5844 /* Return the section that should be marked against GC for a given
   5845    relocation.  */
   5846 
   5847 static asection *
   5848 ppc64_elf_gc_mark_hook (asection *sec,
   5849 			struct bfd_link_info *info,
   5850 			Elf_Internal_Rela *rel,
   5851 			struct elf_link_hash_entry *h,
   5852 			Elf_Internal_Sym *sym)
   5853 {
   5854   asection *rsec;
   5855 
   5856   /* Syms return NULL if we're marking .opd, so we avoid marking all
   5857      function sections, as all functions are referenced in .opd.  */
   5858   rsec = NULL;
   5859   if (get_opd_info (sec) != NULL)
   5860     return rsec;
   5861 
   5862   if (h != NULL)
   5863     {
   5864       enum elf_ppc64_reloc_type r_type;
   5865       struct ppc_link_hash_entry *eh, *fh, *fdh;
   5866 
   5867       r_type = ELF64_R_TYPE (rel->r_info);
   5868       switch (r_type)
   5869 	{
   5870 	case R_PPC64_GNU_VTINHERIT:
   5871 	case R_PPC64_GNU_VTENTRY:
   5872 	  break;
   5873 
   5874 	default:
   5875 	  switch (h->root.type)
   5876 	    {
   5877 	    case bfd_link_hash_defined:
   5878 	    case bfd_link_hash_defweak:
   5879 	      eh = (struct ppc_link_hash_entry *) h;
   5880 	      fdh = defined_func_desc (eh);
   5881 	      if (fdh != NULL)
   5882 		eh = fdh;
   5883 
   5884 	      /* Function descriptor syms cause the associated
   5885 		 function code sym section to be marked.  */
   5886 	      fh = defined_code_entry (eh);
   5887 	      if (fh != NULL)
   5888 		{
   5889 		  /* They also mark their opd section.  */
   5890 		  eh->elf.root.u.def.section->gc_mark = 1;
   5891 
   5892 		  rsec = fh->elf.root.u.def.section;
   5893 		}
   5894 	      else if (get_opd_info (eh->elf.root.u.def.section) != NULL
   5895 		       && opd_entry_value (eh->elf.root.u.def.section,
   5896 					   eh->elf.root.u.def.value,
   5897 					   &rsec, NULL, FALSE) != (bfd_vma) -1)
   5898 		eh->elf.root.u.def.section->gc_mark = 1;
   5899 	      else
   5900 		rsec = h->root.u.def.section;
   5901 	      break;
   5902 
   5903 	    case bfd_link_hash_common:
   5904 	      rsec = h->root.u.c.p->section;
   5905 	      break;
   5906 
   5907 	    default:
   5908 	      return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
   5909 	    }
   5910 	}
   5911     }
   5912   else
   5913     {
   5914       struct _opd_sec_data *opd;
   5915 
   5916       rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
   5917       opd = get_opd_info (rsec);
   5918       if (opd != NULL && opd->func_sec != NULL)
   5919 	{
   5920 	  rsec->gc_mark = 1;
   5921 
   5922 	  rsec = opd->func_sec[(sym->st_value + rel->r_addend) / 8];
   5923 	}
   5924     }
   5925 
   5926   return rsec;
   5927 }
   5928 
   5929 /* Update the .got, .plt. and dynamic reloc reference counts for the
   5930    section being removed.  */
   5931 
   5932 static bfd_boolean
   5933 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
   5934 			 asection *sec, const Elf_Internal_Rela *relocs)
   5935 {
   5936   struct ppc_link_hash_table *htab;
   5937   Elf_Internal_Shdr *symtab_hdr;
   5938   struct elf_link_hash_entry **sym_hashes;
   5939   struct got_entry **local_got_ents;
   5940   const Elf_Internal_Rela *rel, *relend;
   5941 
   5942   if (info->relocatable)
   5943     return TRUE;
   5944 
   5945   if ((sec->flags & SEC_ALLOC) == 0)
   5946     return TRUE;
   5947 
   5948   elf_section_data (sec)->local_dynrel = NULL;
   5949 
   5950   htab = ppc_hash_table (info);
   5951   if (htab == NULL)
   5952     return FALSE;
   5953 
   5954   symtab_hdr = &elf_symtab_hdr (abfd);
   5955   sym_hashes = elf_sym_hashes (abfd);
   5956   local_got_ents = elf_local_got_ents (abfd);
   5957 
   5958   relend = relocs + sec->reloc_count;
   5959   for (rel = relocs; rel < relend; rel++)
   5960     {
   5961       unsigned long r_symndx;
   5962       enum elf_ppc64_reloc_type r_type;
   5963       struct elf_link_hash_entry *h = NULL;
   5964       unsigned char tls_type = 0;
   5965 
   5966       r_symndx = ELF64_R_SYM (rel->r_info);
   5967       r_type = ELF64_R_TYPE (rel->r_info);
   5968       if (r_symndx >= symtab_hdr->sh_info)
   5969 	{
   5970 	  struct ppc_link_hash_entry *eh;
   5971 	  struct elf_dyn_relocs **pp;
   5972 	  struct elf_dyn_relocs *p;
   5973 
   5974 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   5975 	  h = elf_follow_link (h);
   5976 	  eh = (struct ppc_link_hash_entry *) h;
   5977 
   5978 	  for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
   5979 	    if (p->sec == sec)
   5980 	      {
   5981 		/* Everything must go for SEC.  */
   5982 		*pp = p->next;
   5983 		break;
   5984 	      }
   5985 	}
   5986 
   5987       if (is_branch_reloc (r_type))
   5988 	{
   5989 	  struct plt_entry **ifunc = NULL;
   5990 	  if (h != NULL)
   5991 	    {
   5992 	      if (h->type == STT_GNU_IFUNC)
   5993 		ifunc = &h->plt.plist;
   5994 	    }
   5995 	  else if (local_got_ents != NULL)
   5996 	    {
   5997 	      struct plt_entry **local_plt = (struct plt_entry **)
   5998 		(local_got_ents + symtab_hdr->sh_info);
   5999 	      unsigned char *local_got_tls_masks = (unsigned char *)
   6000 		(local_plt + symtab_hdr->sh_info);
   6001 	      if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
   6002 		ifunc = local_plt + r_symndx;
   6003 	    }
   6004 	  if (ifunc != NULL)
   6005 	    {
   6006 	      struct plt_entry *ent;
   6007 
   6008 	      for (ent = *ifunc; ent != NULL; ent = ent->next)
   6009 		if (ent->addend == rel->r_addend)
   6010 		  break;
   6011 	      if (ent == NULL)
   6012 		abort ();
   6013 	      if (ent->plt.refcount > 0)
   6014 		ent->plt.refcount -= 1;
   6015 	      continue;
   6016 	    }
   6017 	}
   6018 
   6019       switch (r_type)
   6020 	{
   6021 	case R_PPC64_GOT_TLSLD16:
   6022 	case R_PPC64_GOT_TLSLD16_LO:
   6023 	case R_PPC64_GOT_TLSLD16_HI:
   6024 	case R_PPC64_GOT_TLSLD16_HA:
   6025 	  tls_type = TLS_TLS | TLS_LD;
   6026 	  goto dogot;
   6027 
   6028 	case R_PPC64_GOT_TLSGD16:
   6029 	case R_PPC64_GOT_TLSGD16_LO:
   6030 	case R_PPC64_GOT_TLSGD16_HI:
   6031 	case R_PPC64_GOT_TLSGD16_HA:
   6032 	  tls_type = TLS_TLS | TLS_GD;
   6033 	  goto dogot;
   6034 
   6035 	case R_PPC64_GOT_TPREL16_DS:
   6036 	case R_PPC64_GOT_TPREL16_LO_DS:
   6037 	case R_PPC64_GOT_TPREL16_HI:
   6038 	case R_PPC64_GOT_TPREL16_HA:
   6039 	  tls_type = TLS_TLS | TLS_TPREL;
   6040 	  goto dogot;
   6041 
   6042 	case R_PPC64_GOT_DTPREL16_DS:
   6043 	case R_PPC64_GOT_DTPREL16_LO_DS:
   6044 	case R_PPC64_GOT_DTPREL16_HI:
   6045 	case R_PPC64_GOT_DTPREL16_HA:
   6046 	  tls_type = TLS_TLS | TLS_DTPREL;
   6047 	  goto dogot;
   6048 
   6049 	case R_PPC64_GOT16:
   6050 	case R_PPC64_GOT16_DS:
   6051 	case R_PPC64_GOT16_HA:
   6052 	case R_PPC64_GOT16_HI:
   6053 	case R_PPC64_GOT16_LO:
   6054 	case R_PPC64_GOT16_LO_DS:
   6055 	dogot:
   6056 	  {
   6057 	    struct got_entry *ent;
   6058 
   6059 	    if (h != NULL)
   6060 	      ent = h->got.glist;
   6061 	    else
   6062 	      ent = local_got_ents[r_symndx];
   6063 
   6064 	    for (; ent != NULL; ent = ent->next)
   6065 	      if (ent->addend == rel->r_addend
   6066 		  && ent->owner == abfd
   6067 		  && ent->tls_type == tls_type)
   6068 		break;
   6069 	    if (ent == NULL)
   6070 	      abort ();
   6071 	    if (ent->got.refcount > 0)
   6072 	      ent->got.refcount -= 1;
   6073 	  }
   6074 	  break;
   6075 
   6076 	case R_PPC64_PLT16_HA:
   6077 	case R_PPC64_PLT16_HI:
   6078 	case R_PPC64_PLT16_LO:
   6079 	case R_PPC64_PLT32:
   6080 	case R_PPC64_PLT64:
   6081 	case R_PPC64_REL14:
   6082 	case R_PPC64_REL14_BRNTAKEN:
   6083 	case R_PPC64_REL14_BRTAKEN:
   6084 	case R_PPC64_REL24:
   6085 	  if (h != NULL)
   6086 	    {
   6087 	      struct plt_entry *ent;
   6088 
   6089 	      for (ent = h->plt.plist; ent != NULL; ent = ent->next)
   6090 		if (ent->addend == rel->r_addend)
   6091 		  break;
   6092 	      if (ent != NULL && ent->plt.refcount > 0)
   6093 		ent->plt.refcount -= 1;
   6094 	    }
   6095 	  break;
   6096 
   6097 	default:
   6098 	  break;
   6099 	}
   6100     }
   6101   return TRUE;
   6102 }
   6103 
   6104 /* The maximum size of .sfpr.  */
   6105 #define SFPR_MAX (218*4)
   6106 
   6107 struct sfpr_def_parms
   6108 {
   6109   const char name[12];
   6110   unsigned char lo, hi;
   6111   bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
   6112   bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
   6113 };
   6114 
   6115 /* Auto-generate _save*, _rest* functions in .sfpr.  */
   6116 
   6117 static bfd_boolean
   6118 sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
   6119 {
   6120   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   6121   unsigned int i;
   6122   size_t len = strlen (parm->name);
   6123   bfd_boolean writing = FALSE;
   6124   char sym[16];
   6125 
   6126   if (htab == NULL)
   6127     return FALSE;
   6128 
   6129   memcpy (sym, parm->name, len);
   6130   sym[len + 2] = 0;
   6131 
   6132   for (i = parm->lo; i <= parm->hi; i++)
   6133     {
   6134       struct elf_link_hash_entry *h;
   6135 
   6136       sym[len + 0] = i / 10 + '0';
   6137       sym[len + 1] = i % 10 + '0';
   6138       h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
   6139       if (h != NULL
   6140 	  && !h->def_regular)
   6141 	{
   6142 	  h->root.type = bfd_link_hash_defined;
   6143 	  h->root.u.def.section = htab->sfpr;
   6144 	  h->root.u.def.value = htab->sfpr->size;
   6145 	  h->type = STT_FUNC;
   6146 	  h->def_regular = 1;
   6147 	  _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
   6148 	  writing = TRUE;
   6149 	  if (htab->sfpr->contents == NULL)
   6150 	    {
   6151 	      htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
   6152 	      if (htab->sfpr->contents == NULL)
   6153 		return FALSE;
   6154 	    }
   6155 	}
   6156       if (writing)
   6157 	{
   6158 	  bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
   6159 	  if (i != parm->hi)
   6160 	    p = (*parm->write_ent) (htab->elf.dynobj, p, i);
   6161 	  else
   6162 	    p = (*parm->write_tail) (htab->elf.dynobj, p, i);
   6163 	  htab->sfpr->size = p - htab->sfpr->contents;
   6164 	}
   6165     }
   6166 
   6167   return TRUE;
   6168 }
   6169 
   6170 static bfd_byte *
   6171 savegpr0 (bfd *abfd, bfd_byte *p, int r)
   6172 {
   6173   bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6174   return p + 4;
   6175 }
   6176 
   6177 static bfd_byte *
   6178 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
   6179 {
   6180   p = savegpr0 (abfd, p, r);
   6181   bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
   6182   p = p + 4;
   6183   bfd_put_32 (abfd, BLR, p);
   6184   return p + 4;
   6185 }
   6186 
   6187 static bfd_byte *
   6188 restgpr0 (bfd *abfd, bfd_byte *p, int r)
   6189 {
   6190   bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6191   return p + 4;
   6192 }
   6193 
   6194 static bfd_byte *
   6195 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
   6196 {
   6197   bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
   6198   p = p + 4;
   6199   p = restgpr0 (abfd, p, r);
   6200   bfd_put_32 (abfd, MTLR_R0, p);
   6201   p = p + 4;
   6202   if (r == 29)
   6203     {
   6204       p = restgpr0 (abfd, p, 30);
   6205       p = restgpr0 (abfd, p, 31);
   6206     }
   6207   bfd_put_32 (abfd, BLR, p);
   6208   return p + 4;
   6209 }
   6210 
   6211 static bfd_byte *
   6212 savegpr1 (bfd *abfd, bfd_byte *p, int r)
   6213 {
   6214   bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6215   return p + 4;
   6216 }
   6217 
   6218 static bfd_byte *
   6219 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
   6220 {
   6221   p = savegpr1 (abfd, p, r);
   6222   bfd_put_32 (abfd, BLR, p);
   6223   return p + 4;
   6224 }
   6225 
   6226 static bfd_byte *
   6227 restgpr1 (bfd *abfd, bfd_byte *p, int r)
   6228 {
   6229   bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6230   return p + 4;
   6231 }
   6232 
   6233 static bfd_byte *
   6234 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
   6235 {
   6236   p = restgpr1 (abfd, p, r);
   6237   bfd_put_32 (abfd, BLR, p);
   6238   return p + 4;
   6239 }
   6240 
   6241 static bfd_byte *
   6242 savefpr (bfd *abfd, bfd_byte *p, int r)
   6243 {
   6244   bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6245   return p + 4;
   6246 }
   6247 
   6248 static bfd_byte *
   6249 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
   6250 {
   6251   p = savefpr (abfd, p, r);
   6252   bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
   6253   p = p + 4;
   6254   bfd_put_32 (abfd, BLR, p);
   6255   return p + 4;
   6256 }
   6257 
   6258 static bfd_byte *
   6259 restfpr (bfd *abfd, bfd_byte *p, int r)
   6260 {
   6261   bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6262   return p + 4;
   6263 }
   6264 
   6265 static bfd_byte *
   6266 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
   6267 {
   6268   bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
   6269   p = p + 4;
   6270   p = restfpr (abfd, p, r);
   6271   bfd_put_32 (abfd, MTLR_R0, p);
   6272   p = p + 4;
   6273   if (r == 29)
   6274     {
   6275       p = restfpr (abfd, p, 30);
   6276       p = restfpr (abfd, p, 31);
   6277     }
   6278   bfd_put_32 (abfd, BLR, p);
   6279   return p + 4;
   6280 }
   6281 
   6282 static bfd_byte *
   6283 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
   6284 {
   6285   p = savefpr (abfd, p, r);
   6286   bfd_put_32 (abfd, BLR, p);
   6287   return p + 4;
   6288 }
   6289 
   6290 static bfd_byte *
   6291 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
   6292 {
   6293   p = restfpr (abfd, p, r);
   6294   bfd_put_32 (abfd, BLR, p);
   6295   return p + 4;
   6296 }
   6297 
   6298 static bfd_byte *
   6299 savevr (bfd *abfd, bfd_byte *p, int r)
   6300 {
   6301   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
   6302   p = p + 4;
   6303   bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
   6304   return p + 4;
   6305 }
   6306 
   6307 static bfd_byte *
   6308 savevr_tail (bfd *abfd, bfd_byte *p, int r)
   6309 {
   6310   p = savevr (abfd, p, r);
   6311   bfd_put_32 (abfd, BLR, p);
   6312   return p + 4;
   6313 }
   6314 
   6315 static bfd_byte *
   6316 restvr (bfd *abfd, bfd_byte *p, int r)
   6317 {
   6318   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
   6319   p = p + 4;
   6320   bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
   6321   return p + 4;
   6322 }
   6323 
   6324 static bfd_byte *
   6325 restvr_tail (bfd *abfd, bfd_byte *p, int r)
   6326 {
   6327   p = restvr (abfd, p, r);
   6328   bfd_put_32 (abfd, BLR, p);
   6329   return p + 4;
   6330 }
   6331 
   6332 /* Called via elf_link_hash_traverse to transfer dynamic linking
   6333    information on function code symbol entries to their corresponding
   6334    function descriptor symbol entries.  */
   6335 
   6336 static bfd_boolean
   6337 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
   6338 {
   6339   struct bfd_link_info *info;
   6340   struct ppc_link_hash_table *htab;
   6341   struct plt_entry *ent;
   6342   struct ppc_link_hash_entry *fh;
   6343   struct ppc_link_hash_entry *fdh;
   6344   bfd_boolean force_local;
   6345 
   6346   fh = (struct ppc_link_hash_entry *) h;
   6347   if (fh->elf.root.type == bfd_link_hash_indirect)
   6348     return TRUE;
   6349 
   6350   info = inf;
   6351   htab = ppc_hash_table (info);
   6352   if (htab == NULL)
   6353     return FALSE;
   6354 
   6355   /* Resolve undefined references to dot-symbols as the value
   6356      in the function descriptor, if we have one in a regular object.
   6357      This is to satisfy cases like ".quad .foo".  Calls to functions
   6358      in dynamic objects are handled elsewhere.  */
   6359   if (fh->elf.root.type == bfd_link_hash_undefweak
   6360       && fh->was_undefined
   6361       && (fdh = defined_func_desc (fh)) != NULL
   6362       && get_opd_info (fdh->elf.root.u.def.section) != NULL
   6363       && opd_entry_value (fdh->elf.root.u.def.section,
   6364 			  fdh->elf.root.u.def.value,
   6365 			  &fh->elf.root.u.def.section,
   6366 			  &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
   6367     {
   6368       fh->elf.root.type = fdh->elf.root.type;
   6369       fh->elf.forced_local = 1;
   6370       fh->elf.def_regular = fdh->elf.def_regular;
   6371       fh->elf.def_dynamic = fdh->elf.def_dynamic;
   6372     }
   6373 
   6374   /* If this is a function code symbol, transfer dynamic linking
   6375      information to the function descriptor symbol.  */
   6376   if (!fh->is_func)
   6377     return TRUE;
   6378 
   6379   for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
   6380     if (ent->plt.refcount > 0)
   6381       break;
   6382   if (ent == NULL
   6383       || fh->elf.root.root.string[0] != '.'
   6384       || fh->elf.root.root.string[1] == '\0')
   6385     return TRUE;
   6386 
   6387   /* Find the corresponding function descriptor symbol.  Create it
   6388      as undefined if necessary.  */
   6389 
   6390   fdh = lookup_fdh (fh, htab);
   6391   if (fdh == NULL
   6392       && !info->executable
   6393       && (fh->elf.root.type == bfd_link_hash_undefined
   6394 	  || fh->elf.root.type == bfd_link_hash_undefweak))
   6395     {
   6396       fdh = make_fdh (info, fh);
   6397       if (fdh == NULL)
   6398 	return FALSE;
   6399     }
   6400 
   6401   /* Fake function descriptors are made undefweak.  If the function
   6402      code symbol is strong undefined, make the fake sym the same.
   6403      If the function code symbol is defined, then force the fake
   6404      descriptor local;  We can't support overriding of symbols in a
   6405      shared library on a fake descriptor.  */
   6406 
   6407   if (fdh != NULL
   6408       && fdh->fake
   6409       && fdh->elf.root.type == bfd_link_hash_undefweak)
   6410     {
   6411       if (fh->elf.root.type == bfd_link_hash_undefined)
   6412 	{
   6413 	  fdh->elf.root.type = bfd_link_hash_undefined;
   6414 	  bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
   6415 	}
   6416       else if (fh->elf.root.type == bfd_link_hash_defined
   6417 	       || fh->elf.root.type == bfd_link_hash_defweak)
   6418 	{
   6419 	  _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
   6420 	}
   6421     }
   6422 
   6423   if (fdh != NULL
   6424       && !fdh->elf.forced_local
   6425       && (!info->executable
   6426 	  || fdh->elf.def_dynamic
   6427 	  || fdh->elf.ref_dynamic
   6428 	  || (fdh->elf.root.type == bfd_link_hash_undefweak
   6429 	      && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
   6430     {
   6431       if (fdh->elf.dynindx == -1)
   6432 	if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
   6433 	  return FALSE;
   6434       fdh->elf.ref_regular |= fh->elf.ref_regular;
   6435       fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
   6436       fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
   6437       fdh->elf.non_got_ref |= fh->elf.non_got_ref;
   6438       if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
   6439 	{
   6440 	  move_plt_plist (fh, fdh);
   6441 	  fdh->elf.needs_plt = 1;
   6442 	}
   6443       fdh->is_func_descriptor = 1;
   6444       fdh->oh = fh;
   6445       fh->oh = fdh;
   6446     }
   6447 
   6448   /* Now that the info is on the function descriptor, clear the
   6449      function code sym info.  Any function code syms for which we
   6450      don't have a definition in a regular file, we force local.
   6451      This prevents a shared library from exporting syms that have
   6452      been imported from another library.  Function code syms that
   6453      are really in the library we must leave global to prevent the
   6454      linker dragging in a definition from a static library.  */
   6455   force_local = (!fh->elf.def_regular
   6456 		 || fdh == NULL
   6457 		 || !fdh->elf.def_regular
   6458 		 || fdh->elf.forced_local);
   6459   _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
   6460 
   6461   return TRUE;
   6462 }
   6463 
   6464 /* Called near the start of bfd_elf_size_dynamic_sections.  We use
   6465    this hook to a) provide some gcc support functions, and b) transfer
   6466    dynamic linking information gathered so far on function code symbol
   6467    entries, to their corresponding function descriptor symbol entries.  */
   6468 
   6469 static bfd_boolean
   6470 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
   6471 			    struct bfd_link_info *info)
   6472 {
   6473   struct ppc_link_hash_table *htab;
   6474   unsigned int i;
   6475   static const struct sfpr_def_parms funcs[] =
   6476     {
   6477       { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
   6478       { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
   6479       { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
   6480       { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
   6481       { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
   6482       { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
   6483       { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
   6484       { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
   6485       { "._savef", 14, 31, savefpr, savefpr1_tail },
   6486       { "._restf", 14, 31, restfpr, restfpr1_tail },
   6487       { "_savevr_", 20, 31, savevr, savevr_tail },
   6488       { "_restvr_", 20, 31, restvr, restvr_tail }
   6489     };
   6490 
   6491   htab = ppc_hash_table (info);
   6492   if (htab == NULL)
   6493     return FALSE;
   6494 
   6495   if (htab->sfpr == NULL)
   6496     /* We don't have any relocs.  */
   6497     return TRUE;
   6498 
   6499   /* Provide any missing _save* and _rest* functions.  */
   6500   htab->sfpr->size = 0;
   6501   if (!info->relocatable)
   6502     for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
   6503       if (!sfpr_define (info, &funcs[i]))
   6504 	return FALSE;
   6505 
   6506   elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
   6507 
   6508   if (htab->sfpr->size == 0)
   6509     htab->sfpr->flags |= SEC_EXCLUDE;
   6510 
   6511   return TRUE;
   6512 }
   6513 
   6514 /* Adjust a symbol defined by a dynamic object and referenced by a
   6515    regular object.  The current definition is in some section of the
   6516    dynamic object, but we're not including those sections.  We have to
   6517    change the definition to something the rest of the link can
   6518    understand.  */
   6519 
   6520 static bfd_boolean
   6521 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
   6522 				 struct elf_link_hash_entry *h)
   6523 {
   6524   struct ppc_link_hash_table *htab;
   6525   asection *s;
   6526 
   6527   htab = ppc_hash_table (info);
   6528   if (htab == NULL)
   6529     return FALSE;
   6530 
   6531   /* Deal with function syms.  */
   6532   if (h->type == STT_FUNC
   6533       || h->type == STT_GNU_IFUNC
   6534       || h->needs_plt)
   6535     {
   6536       /* Clear procedure linkage table information for any symbol that
   6537 	 won't need a .plt entry.  */
   6538       struct plt_entry *ent;
   6539       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
   6540 	if (ent->plt.refcount > 0)
   6541 	  break;
   6542       if (ent == NULL
   6543 	  || (h->type != STT_GNU_IFUNC
   6544 	      && (SYMBOL_CALLS_LOCAL (info, h)
   6545 		  || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
   6546 		      && h->root.type == bfd_link_hash_undefweak))))
   6547 	{
   6548 	  h->plt.plist = NULL;
   6549 	  h->needs_plt = 0;
   6550 	}
   6551     }
   6552   else
   6553     h->plt.plist = NULL;
   6554 
   6555   /* If this is a weak symbol, and there is a real definition, the
   6556      processor independent code will have arranged for us to see the
   6557      real definition first, and we can just use the same value.  */
   6558   if (h->u.weakdef != NULL)
   6559     {
   6560       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
   6561 		  || h->u.weakdef->root.type == bfd_link_hash_defweak);
   6562       h->root.u.def.section = h->u.weakdef->root.u.def.section;
   6563       h->root.u.def.value = h->u.weakdef->root.u.def.value;
   6564       if (ELIMINATE_COPY_RELOCS)
   6565 	h->non_got_ref = h->u.weakdef->non_got_ref;
   6566       return TRUE;
   6567     }
   6568 
   6569   /* If we are creating a shared library, we must presume that the
   6570      only references to the symbol are via the global offset table.
   6571      For such cases we need not do anything here; the relocations will
   6572      be handled correctly by relocate_section.  */
   6573   if (info->shared)
   6574     return TRUE;
   6575 
   6576   /* If there are no references to this symbol that do not use the
   6577      GOT, we don't need to generate a copy reloc.  */
   6578   if (!h->non_got_ref)
   6579     return TRUE;
   6580 
   6581   /* Don't generate a copy reloc for symbols defined in the executable.  */
   6582   if (!h->def_dynamic || !h->ref_regular || h->def_regular)
   6583     return TRUE;
   6584 
   6585   if (ELIMINATE_COPY_RELOCS)
   6586     {
   6587       struct ppc_link_hash_entry * eh;
   6588       struct elf_dyn_relocs *p;
   6589 
   6590       eh = (struct ppc_link_hash_entry *) h;
   6591       for (p = eh->dyn_relocs; p != NULL; p = p->next)
   6592 	{
   6593 	  s = p->sec->output_section;
   6594 	  if (s != NULL && (s->flags & SEC_READONLY) != 0)
   6595 	    break;
   6596 	}
   6597 
   6598       /* If we didn't find any dynamic relocs in read-only sections, then
   6599 	 we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
   6600       if (p == NULL)
   6601 	{
   6602 	  h->non_got_ref = 0;
   6603 	  return TRUE;
   6604 	}
   6605     }
   6606 
   6607   if (h->plt.plist != NULL)
   6608     {
   6609       /* We should never get here, but unfortunately there are versions
   6610 	 of gcc out there that improperly (for this ABI) put initialized
   6611 	 function pointers, vtable refs and suchlike in read-only
   6612 	 sections.  Allow them to proceed, but warn that this might
   6613 	 break at runtime.  */
   6614       info->callbacks->einfo
   6615 	(_("%P: copy reloc against `%T' requires lazy plt linking; "
   6616 	   "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
   6617 	 h->root.root.string);
   6618     }
   6619 
   6620   /* This is a reference to a symbol defined by a dynamic object which
   6621      is not a function.  */
   6622 
   6623   /* We must allocate the symbol in our .dynbss section, which will
   6624      become part of the .bss section of the executable.  There will be
   6625      an entry for this symbol in the .dynsym section.  The dynamic
   6626      object will contain position independent code, so all references
   6627      from the dynamic object to this symbol will go through the global
   6628      offset table.  The dynamic linker will use the .dynsym entry to
   6629      determine the address it must put in the global offset table, so
   6630      both the dynamic object and the regular object will refer to the
   6631      same memory location for the variable.  */
   6632 
   6633   /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
   6634      to copy the initial value out of the dynamic object and into the
   6635      runtime process image.  We need to remember the offset into the
   6636      .rela.bss section we are going to use.  */
   6637   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
   6638     {
   6639       htab->relbss->size += sizeof (Elf64_External_Rela);
   6640       h->needs_copy = 1;
   6641     }
   6642 
   6643   s = htab->dynbss;
   6644 
   6645   return _bfd_elf_adjust_dynamic_copy (h, s);
   6646 }
   6647 
   6648 /* If given a function descriptor symbol, hide both the function code
   6649    sym and the descriptor.  */
   6650 static void
   6651 ppc64_elf_hide_symbol (struct bfd_link_info *info,
   6652 		       struct elf_link_hash_entry *h,
   6653 		       bfd_boolean force_local)
   6654 {
   6655   struct ppc_link_hash_entry *eh;
   6656   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
   6657 
   6658   eh = (struct ppc_link_hash_entry *) h;
   6659   if (eh->is_func_descriptor)
   6660     {
   6661       struct ppc_link_hash_entry *fh = eh->oh;
   6662 
   6663       if (fh == NULL)
   6664 	{
   6665 	  const char *p, *q;
   6666 	  struct ppc_link_hash_table *htab;
   6667 	  char save;
   6668 
   6669 	  /* We aren't supposed to use alloca in BFD because on
   6670 	     systems which do not have alloca the version in libiberty
   6671 	     calls xmalloc, which might cause the program to crash
   6672 	     when it runs out of memory.  This function doesn't have a
   6673 	     return status, so there's no way to gracefully return an
   6674 	     error.  So cheat.  We know that string[-1] can be safely
   6675 	     accessed;  It's either a string in an ELF string table,
   6676 	     or allocated in an objalloc structure.  */
   6677 
   6678 	  p = eh->elf.root.root.string - 1;
   6679 	  save = *p;
   6680 	  *(char *) p = '.';
   6681 	  htab = ppc_hash_table (info);
   6682 	  if (htab == NULL)
   6683 	    return;
   6684 
   6685 	  fh = (struct ppc_link_hash_entry *)
   6686 	    elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
   6687 	  *(char *) p = save;
   6688 
   6689 	  /* Unfortunately, if it so happens that the string we were
   6690 	     looking for was allocated immediately before this string,
   6691 	     then we overwrote the string terminator.  That's the only
   6692 	     reason the lookup should fail.  */
   6693 	  if (fh == NULL)
   6694 	    {
   6695 	      q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
   6696 	      while (q >= eh->elf.root.root.string && *q == *p)
   6697 		--q, --p;
   6698 	      if (q < eh->elf.root.root.string && *p == '.')
   6699 		fh = (struct ppc_link_hash_entry *)
   6700 		  elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
   6701 	    }
   6702 	  if (fh != NULL)
   6703 	    {
   6704 	      eh->oh = fh;
   6705 	      fh->oh = eh;
   6706 	    }
   6707 	}
   6708       if (fh != NULL)
   6709 	_bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
   6710     }
   6711 }
   6712 
   6713 static bfd_boolean
   6714 get_sym_h (struct elf_link_hash_entry **hp,
   6715 	   Elf_Internal_Sym **symp,
   6716 	   asection **symsecp,
   6717 	   unsigned char **tls_maskp,
   6718 	   Elf_Internal_Sym **locsymsp,
   6719 	   unsigned long r_symndx,
   6720 	   bfd *ibfd)
   6721 {
   6722   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
   6723 
   6724   if (r_symndx >= symtab_hdr->sh_info)
   6725     {
   6726       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
   6727       struct elf_link_hash_entry *h;
   6728 
   6729       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   6730       h = elf_follow_link (h);
   6731 
   6732       if (hp != NULL)
   6733 	*hp = h;
   6734 
   6735       if (symp != NULL)
   6736 	*symp = NULL;
   6737 
   6738       if (symsecp != NULL)
   6739 	{
   6740 	  asection *symsec = NULL;
   6741 	  if (h->root.type == bfd_link_hash_defined
   6742 	      || h->root.type == bfd_link_hash_defweak)
   6743 	    symsec = h->root.u.def.section;
   6744 	  *symsecp = symsec;
   6745 	}
   6746 
   6747       if (tls_maskp != NULL)
   6748 	{
   6749 	  struct ppc_link_hash_entry *eh;
   6750 
   6751 	  eh = (struct ppc_link_hash_entry *) h;
   6752 	  *tls_maskp = &eh->tls_mask;
   6753 	}
   6754     }
   6755   else
   6756     {
   6757       Elf_Internal_Sym *sym;
   6758       Elf_Internal_Sym *locsyms = *locsymsp;
   6759 
   6760       if (locsyms == NULL)
   6761 	{
   6762 	  locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
   6763 	  if (locsyms == NULL)
   6764 	    locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
   6765 					    symtab_hdr->sh_info,
   6766 					    0, NULL, NULL, NULL);
   6767 	  if (locsyms == NULL)
   6768 	    return FALSE;
   6769 	  *locsymsp = locsyms;
   6770 	}
   6771       sym = locsyms + r_symndx;
   6772 
   6773       if (hp != NULL)
   6774 	*hp = NULL;
   6775 
   6776       if (symp != NULL)
   6777 	*symp = sym;
   6778 
   6779       if (symsecp != NULL)
   6780 	*symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
   6781 
   6782       if (tls_maskp != NULL)
   6783 	{
   6784 	  struct got_entry **lgot_ents;
   6785 	  unsigned char *tls_mask;
   6786 
   6787 	  tls_mask = NULL;
   6788 	  lgot_ents = elf_local_got_ents (ibfd);
   6789 	  if (lgot_ents != NULL)
   6790 	    {
   6791 	      struct plt_entry **local_plt = (struct plt_entry **)
   6792 		(lgot_ents + symtab_hdr->sh_info);
   6793 	      unsigned char *lgot_masks = (unsigned char *)
   6794 		(local_plt + symtab_hdr->sh_info);
   6795 	      tls_mask = &lgot_masks[r_symndx];
   6796 	    }
   6797 	  *tls_maskp = tls_mask;
   6798 	}
   6799     }
   6800   return TRUE;
   6801 }
   6802 
   6803 /* Returns TLS_MASKP for the given REL symbol.  Function return is 0 on
   6804    error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
   6805    type suitable for optimization, and 1 otherwise.  */
   6806 
   6807 static int
   6808 get_tls_mask (unsigned char **tls_maskp,
   6809 	      unsigned long *toc_symndx,
   6810 	      bfd_vma *toc_addend,
   6811 	      Elf_Internal_Sym **locsymsp,
   6812 	      const Elf_Internal_Rela *rel,
   6813 	      bfd *ibfd)
   6814 {
   6815   unsigned long r_symndx;
   6816   int next_r;
   6817   struct elf_link_hash_entry *h;
   6818   Elf_Internal_Sym *sym;
   6819   asection *sec;
   6820   bfd_vma off;
   6821 
   6822   r_symndx = ELF64_R_SYM (rel->r_info);
   6823   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
   6824     return 0;
   6825 
   6826   if ((*tls_maskp != NULL && **tls_maskp != 0)
   6827       || sec == NULL
   6828       || ppc64_elf_section_data (sec) == NULL
   6829       || ppc64_elf_section_data (sec)->sec_type != sec_toc)
   6830     return 1;
   6831 
   6832   /* Look inside a TOC section too.  */
   6833   if (h != NULL)
   6834     {
   6835       BFD_ASSERT (h->root.type == bfd_link_hash_defined);
   6836       off = h->root.u.def.value;
   6837     }
   6838   else
   6839     off = sym->st_value;
   6840   off += rel->r_addend;
   6841   BFD_ASSERT (off % 8 == 0);
   6842   r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
   6843   next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
   6844   if (toc_symndx != NULL)
   6845     *toc_symndx = r_symndx;
   6846   if (toc_addend != NULL)
   6847     *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
   6848   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
   6849     return 0;
   6850   if ((h == NULL || is_static_defined (h))
   6851       && (next_r == -1 || next_r == -2))
   6852     return 1 - next_r;
   6853   return 1;
   6854 }
   6855 
   6856 /* Find (or create) an entry in the tocsave hash table.  */
   6857 
   6858 static struct tocsave_entry *
   6859 tocsave_find (struct ppc_link_hash_table *htab,
   6860 	      enum insert_option insert,
   6861 	      Elf_Internal_Sym **local_syms,
   6862 	      const Elf_Internal_Rela *irela,
   6863 	      bfd *ibfd)
   6864 {
   6865   unsigned long r_indx;
   6866   struct elf_link_hash_entry *h;
   6867   Elf_Internal_Sym *sym;
   6868   struct tocsave_entry ent, *p;
   6869   hashval_t hash;
   6870   struct tocsave_entry **slot;
   6871 
   6872   r_indx = ELF64_R_SYM (irela->r_info);
   6873   if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
   6874     return NULL;
   6875   if (ent.sec == NULL || ent.sec->output_section == NULL)
   6876     {
   6877       (*_bfd_error_handler)
   6878 	(_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"));
   6879       return NULL;
   6880     }
   6881 
   6882   if (h != NULL)
   6883     ent.offset = h->root.u.def.value;
   6884   else
   6885     ent.offset = sym->st_value;
   6886   ent.offset += irela->r_addend;
   6887 
   6888   hash = tocsave_htab_hash (&ent);
   6889   slot = ((struct tocsave_entry **)
   6890 	  htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
   6891   if (slot == NULL)
   6892     return NULL;
   6893 
   6894   if (*slot == NULL)
   6895     {
   6896       p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
   6897       if (p == NULL)
   6898 	return NULL;
   6899       *p = ent;
   6900       *slot = p;
   6901     }
   6902   return *slot;
   6903 }
   6904 
   6905 /* Adjust all global syms defined in opd sections.  In gcc generated
   6906    code for the old ABI, these will already have been done.  */
   6907 
   6908 static bfd_boolean
   6909 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
   6910 {
   6911   struct ppc_link_hash_entry *eh;
   6912   asection *sym_sec;
   6913   struct _opd_sec_data *opd;
   6914 
   6915   if (h->root.type == bfd_link_hash_indirect)
   6916     return TRUE;
   6917 
   6918   if (h->root.type != bfd_link_hash_defined
   6919       && h->root.type != bfd_link_hash_defweak)
   6920     return TRUE;
   6921 
   6922   eh = (struct ppc_link_hash_entry *) h;
   6923   if (eh->adjust_done)
   6924     return TRUE;
   6925 
   6926   sym_sec = eh->elf.root.u.def.section;
   6927   opd = get_opd_info (sym_sec);
   6928   if (opd != NULL && opd->adjust != NULL)
   6929     {
   6930       long adjust = opd->adjust[eh->elf.root.u.def.value / 8];
   6931       if (adjust == -1)
   6932 	{
   6933 	  /* This entry has been deleted.  */
   6934 	  asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
   6935 	  if (dsec == NULL)
   6936 	    {
   6937 	      for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
   6938 		if (discarded_section (dsec))
   6939 		  {
   6940 		    ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
   6941 		    break;
   6942 		  }
   6943 	    }
   6944 	  eh->elf.root.u.def.value = 0;
   6945 	  eh->elf.root.u.def.section = dsec;
   6946 	}
   6947       else
   6948 	eh->elf.root.u.def.value += adjust;
   6949       eh->adjust_done = 1;
   6950     }
   6951   return TRUE;
   6952 }
   6953 
   6954 /* Handles decrementing dynamic reloc counts for the reloc specified by
   6955    R_INFO in section SEC.  If LOCAL_SYMS is NULL, then H and SYM_SEC
   6956    have already been determined.  */
   6957 
   6958 static bfd_boolean
   6959 dec_dynrel_count (bfd_vma r_info,
   6960 		  asection *sec,
   6961 		  struct bfd_link_info *info,
   6962 		  Elf_Internal_Sym **local_syms,
   6963 		  struct elf_link_hash_entry *h,
   6964 		  asection *sym_sec)
   6965 {
   6966   enum elf_ppc64_reloc_type r_type;
   6967   struct elf_dyn_relocs *p;
   6968   struct elf_dyn_relocs **pp;
   6969 
   6970   /* Can this reloc be dynamic?  This switch, and later tests here
   6971      should be kept in sync with the code in check_relocs.  */
   6972   r_type = ELF64_R_TYPE (r_info);
   6973   switch (r_type)
   6974     {
   6975     default:
   6976       return TRUE;
   6977 
   6978     case R_PPC64_TPREL16:
   6979     case R_PPC64_TPREL16_LO:
   6980     case R_PPC64_TPREL16_HI:
   6981     case R_PPC64_TPREL16_HA:
   6982     case R_PPC64_TPREL16_DS:
   6983     case R_PPC64_TPREL16_LO_DS:
   6984     case R_PPC64_TPREL16_HIGHER:
   6985     case R_PPC64_TPREL16_HIGHERA:
   6986     case R_PPC64_TPREL16_HIGHEST:
   6987     case R_PPC64_TPREL16_HIGHESTA:
   6988       if (!info->shared)
   6989 	return TRUE;
   6990 
   6991     case R_PPC64_TPREL64:
   6992     case R_PPC64_DTPMOD64:
   6993     case R_PPC64_DTPREL64:
   6994     case R_PPC64_ADDR64:
   6995     case R_PPC64_REL30:
   6996     case R_PPC64_REL32:
   6997     case R_PPC64_REL64:
   6998     case R_PPC64_ADDR14:
   6999     case R_PPC64_ADDR14_BRNTAKEN:
   7000     case R_PPC64_ADDR14_BRTAKEN:
   7001     case R_PPC64_ADDR16:
   7002     case R_PPC64_ADDR16_DS:
   7003     case R_PPC64_ADDR16_HA:
   7004     case R_PPC64_ADDR16_HI:
   7005     case R_PPC64_ADDR16_HIGHER:
   7006     case R_PPC64_ADDR16_HIGHERA:
   7007     case R_PPC64_ADDR16_HIGHEST:
   7008     case R_PPC64_ADDR16_HIGHESTA:
   7009     case R_PPC64_ADDR16_LO:
   7010     case R_PPC64_ADDR16_LO_DS:
   7011     case R_PPC64_ADDR24:
   7012     case R_PPC64_ADDR32:
   7013     case R_PPC64_UADDR16:
   7014     case R_PPC64_UADDR32:
   7015     case R_PPC64_UADDR64:
   7016     case R_PPC64_TOC:
   7017       break;
   7018     }
   7019 
   7020   if (local_syms != NULL)
   7021     {
   7022       unsigned long r_symndx;
   7023       Elf_Internal_Sym *sym;
   7024       bfd *ibfd = sec->owner;
   7025 
   7026       r_symndx = ELF64_R_SYM (r_info);
   7027       if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
   7028 	return FALSE;
   7029     }
   7030 
   7031   if ((info->shared
   7032        && (must_be_dyn_reloc (info, r_type)
   7033 	   || (h != NULL
   7034 	       && (!info->symbolic
   7035 		   || h->root.type == bfd_link_hash_defweak
   7036 		   || !h->def_regular))))
   7037       || (ELIMINATE_COPY_RELOCS
   7038 	  && !info->shared
   7039 	  && h != NULL
   7040 	  && (h->root.type == bfd_link_hash_defweak
   7041 	      || !h->def_regular)))
   7042     ;
   7043   else
   7044     return TRUE;
   7045 
   7046   if (h != NULL)
   7047     pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
   7048   else
   7049     {
   7050       if (sym_sec != NULL)
   7051 	{
   7052 	  void *vpp = &elf_section_data (sym_sec)->local_dynrel;
   7053 	  pp = (struct elf_dyn_relocs **) vpp;
   7054 	}
   7055       else
   7056 	{
   7057 	  void *vpp = &elf_section_data (sec)->local_dynrel;
   7058 	  pp = (struct elf_dyn_relocs **) vpp;
   7059 	}
   7060     }
   7061 
   7062   /* elf_gc_sweep may have already removed all dyn relocs associated
   7063      with local syms for a given section.  Also, symbol flags are
   7064      changed by elf_gc_sweep_symbol, confusing the test above.  Don't
   7065      report a dynreloc miscount.  */
   7066   if (*pp == NULL && info->gc_sections)
   7067     return TRUE;
   7068 
   7069   while ((p = *pp) != NULL)
   7070     {
   7071       if (p->sec == sec)
   7072 	{
   7073 	  if (!must_be_dyn_reloc (info, r_type))
   7074 	    p->pc_count -= 1;
   7075 	  p->count -= 1;
   7076 	  if (p->count == 0)
   7077 	    *pp = p->next;
   7078 	  return TRUE;
   7079 	}
   7080       pp = &p->next;
   7081     }
   7082 
   7083   info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
   7084 			  sec->owner, sec);
   7085   bfd_set_error (bfd_error_bad_value);
   7086   return FALSE;
   7087 }
   7088 
   7089 /* Remove unused Official Procedure Descriptor entries.  Currently we
   7090    only remove those associated with functions in discarded link-once
   7091    sections, or weakly defined functions that have been overridden.  It
   7092    would be possible to remove many more entries for statically linked
   7093    applications.  */
   7094 
   7095 bfd_boolean
   7096 ppc64_elf_edit_opd (struct bfd_link_info *info, bfd_boolean non_overlapping)
   7097 {
   7098   bfd *ibfd;
   7099   bfd_boolean some_edited = FALSE;
   7100   asection *need_pad = NULL;
   7101 
   7102   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   7103     {
   7104       asection *sec;
   7105       Elf_Internal_Rela *relstart, *rel, *relend;
   7106       Elf_Internal_Shdr *symtab_hdr;
   7107       Elf_Internal_Sym *local_syms;
   7108       bfd_vma offset;
   7109       struct _opd_sec_data *opd;
   7110       bfd_boolean need_edit, add_aux_fields;
   7111       bfd_size_type cnt_16b = 0;
   7112 
   7113       if (!is_ppc64_elf (ibfd))
   7114 	continue;
   7115 
   7116       sec = bfd_get_section_by_name (ibfd, ".opd");
   7117       if (sec == NULL || sec->size == 0)
   7118 	continue;
   7119 
   7120       if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   7121 	continue;
   7122 
   7123       if (sec->output_section == bfd_abs_section_ptr)
   7124 	continue;
   7125 
   7126       /* Look through the section relocs.  */
   7127       if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
   7128 	continue;
   7129 
   7130       local_syms = NULL;
   7131       symtab_hdr = &elf_symtab_hdr (ibfd);
   7132 
   7133       /* Read the relocations.  */
   7134       relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   7135 					    info->keep_memory);
   7136       if (relstart == NULL)
   7137 	return FALSE;
   7138 
   7139       /* First run through the relocs to check they are sane, and to
   7140 	 determine whether we need to edit this opd section.  */
   7141       need_edit = FALSE;
   7142       need_pad = sec;
   7143       offset = 0;
   7144       relend = relstart + sec->reloc_count;
   7145       for (rel = relstart; rel < relend; )
   7146 	{
   7147 	  enum elf_ppc64_reloc_type r_type;
   7148 	  unsigned long r_symndx;
   7149 	  asection *sym_sec;
   7150 	  struct elf_link_hash_entry *h;
   7151 	  Elf_Internal_Sym *sym;
   7152 
   7153 	  /* .opd contains a regular array of 16 or 24 byte entries.  We're
   7154 	     only interested in the reloc pointing to a function entry
   7155 	     point.  */
   7156 	  if (rel->r_offset != offset
   7157 	      || rel + 1 >= relend
   7158 	      || (rel + 1)->r_offset != offset + 8)
   7159 	    {
   7160 	      /* If someone messes with .opd alignment then after a
   7161 		 "ld -r" we might have padding in the middle of .opd.
   7162 		 Also, there's nothing to prevent someone putting
   7163 		 something silly in .opd with the assembler.  No .opd
   7164 		 optimization for them!  */
   7165 	    broken_opd:
   7166 	      (*_bfd_error_handler)
   7167 		(_("%B: .opd is not a regular array of opd entries"), ibfd);
   7168 	      need_edit = FALSE;
   7169 	      break;
   7170 	    }
   7171 
   7172 	  if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
   7173 	      || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
   7174 	    {
   7175 	      (*_bfd_error_handler)
   7176 		(_("%B: unexpected reloc type %u in .opd section"),
   7177 		 ibfd, r_type);
   7178 	      need_edit = FALSE;
   7179 	      break;
   7180 	    }
   7181 
   7182 	  r_symndx = ELF64_R_SYM (rel->r_info);
   7183 	  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   7184 			  r_symndx, ibfd))
   7185 	    goto error_ret;
   7186 
   7187 	  if (sym_sec == NULL || sym_sec->owner == NULL)
   7188 	    {
   7189 	      const char *sym_name;
   7190 	      if (h != NULL)
   7191 		sym_name = h->root.root.string;
   7192 	      else
   7193 		sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
   7194 					     sym_sec);
   7195 
   7196 	      (*_bfd_error_handler)
   7197 		(_("%B: undefined sym `%s' in .opd section"),
   7198 		 ibfd, sym_name);
   7199 	      need_edit = FALSE;
   7200 	      break;
   7201 	    }
   7202 
   7203 	  /* opd entries are always for functions defined in the
   7204 	     current input bfd.  If the symbol isn't defined in the
   7205 	     input bfd, then we won't be using the function in this
   7206 	     bfd;  It must be defined in a linkonce section in another
   7207 	     bfd, or is weak.  It's also possible that we are
   7208 	     discarding the function due to a linker script /DISCARD/,
   7209 	     which we test for via the output_section.  */
   7210 	  if (sym_sec->owner != ibfd
   7211 	      || sym_sec->output_section == bfd_abs_section_ptr)
   7212 	    need_edit = TRUE;
   7213 
   7214 	  rel += 2;
   7215 	  if (rel == relend
   7216 	      || (rel + 1 == relend && rel->r_offset == offset + 16))
   7217 	    {
   7218 	      if (sec->size == offset + 24)
   7219 		{
   7220 		  need_pad = NULL;
   7221 		  break;
   7222 		}
   7223 	      if (rel == relend && sec->size == offset + 16)
   7224 		{
   7225 		  cnt_16b++;
   7226 		  break;
   7227 		}
   7228 	      goto broken_opd;
   7229 	    }
   7230 
   7231 	  if (rel->r_offset == offset + 24)
   7232 	    offset += 24;
   7233 	  else if (rel->r_offset != offset + 16)
   7234 	    goto broken_opd;
   7235 	  else if (rel + 1 < relend
   7236 		   && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
   7237 		   && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
   7238 	    {
   7239 	      offset += 16;
   7240 	      cnt_16b++;
   7241 	    }
   7242 	  else if (rel + 2 < relend
   7243 		   && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
   7244 		   && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
   7245 	    {
   7246 	      offset += 24;
   7247 	      rel += 1;
   7248 	    }
   7249 	  else
   7250 	    goto broken_opd;
   7251 	}
   7252 
   7253       add_aux_fields = non_overlapping && cnt_16b > 0;
   7254 
   7255       if (need_edit || add_aux_fields)
   7256 	{
   7257 	  Elf_Internal_Rela *write_rel;
   7258 	  Elf_Internal_Shdr *rel_hdr;
   7259 	  bfd_byte *rptr, *wptr;
   7260 	  bfd_byte *new_contents;
   7261 	  bfd_boolean skip;
   7262 	  long opd_ent_size;
   7263 	  bfd_size_type amt;
   7264 
   7265 	  new_contents = NULL;
   7266 	  amt = sec->size * sizeof (long) / 8;
   7267 	  opd = &ppc64_elf_section_data (sec)->u.opd;
   7268 	  opd->adjust = bfd_zalloc (sec->owner, amt);
   7269 	  if (opd->adjust == NULL)
   7270 	    return FALSE;
   7271 	  ppc64_elf_section_data (sec)->sec_type = sec_opd;
   7272 
   7273 	  /* This seems a waste of time as input .opd sections are all
   7274 	     zeros as generated by gcc, but I suppose there's no reason
   7275 	     this will always be so.  We might start putting something in
   7276 	     the third word of .opd entries.  */
   7277 	  if ((sec->flags & SEC_IN_MEMORY) == 0)
   7278 	    {
   7279 	      bfd_byte *loc;
   7280 	      if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
   7281 		{
   7282 		  if (loc != NULL)
   7283 		    free (loc);
   7284 		error_ret:
   7285 		  if (local_syms != NULL
   7286 		      && symtab_hdr->contents != (unsigned char *) local_syms)
   7287 		    free (local_syms);
   7288 		  if (elf_section_data (sec)->relocs != relstart)
   7289 		    free (relstart);
   7290 		  return FALSE;
   7291 		}
   7292 	      sec->contents = loc;
   7293 	      sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
   7294 	    }
   7295 
   7296 	  elf_section_data (sec)->relocs = relstart;
   7297 
   7298 	  new_contents = sec->contents;
   7299 	  if (add_aux_fields)
   7300 	    {
   7301 	      new_contents = bfd_malloc (sec->size + cnt_16b * 8);
   7302 	      if (new_contents == NULL)
   7303 		return FALSE;
   7304 	      need_pad = FALSE;
   7305 	    }
   7306 	  wptr = new_contents;
   7307 	  rptr = sec->contents;
   7308 
   7309 	  write_rel = relstart;
   7310 	  skip = FALSE;
   7311 	  offset = 0;
   7312 	  opd_ent_size = 0;
   7313 	  for (rel = relstart; rel < relend; rel++)
   7314 	    {
   7315 	      unsigned long r_symndx;
   7316 	      asection *sym_sec;
   7317 	      struct elf_link_hash_entry *h;
   7318 	      Elf_Internal_Sym *sym;
   7319 
   7320 	      r_symndx = ELF64_R_SYM (rel->r_info);
   7321 	      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   7322 			      r_symndx, ibfd))
   7323 		goto error_ret;
   7324 
   7325 	      if (rel->r_offset == offset)
   7326 		{
   7327 		  struct ppc_link_hash_entry *fdh = NULL;
   7328 
   7329 		  /* See if the .opd entry is full 24 byte or
   7330 		     16 byte (with fd_aux entry overlapped with next
   7331 		     fd_func).  */
   7332 		  opd_ent_size = 24;
   7333 		  if ((rel + 2 == relend && sec->size == offset + 16)
   7334 		      || (rel + 3 < relend
   7335 			  && rel[2].r_offset == offset + 16
   7336 			  && rel[3].r_offset == offset + 24
   7337 			  && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
   7338 			  && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
   7339 		    opd_ent_size = 16;
   7340 
   7341 		  if (h != NULL
   7342 		      && h->root.root.string[0] == '.')
   7343 		    {
   7344 		      struct ppc_link_hash_table *htab;
   7345 
   7346 		      htab = ppc_hash_table (info);
   7347 		      if (htab != NULL)
   7348 			fdh = lookup_fdh ((struct ppc_link_hash_entry *) h,
   7349 					  htab);
   7350 		      if (fdh != NULL
   7351 			  && fdh->elf.root.type != bfd_link_hash_defined
   7352 			  && fdh->elf.root.type != bfd_link_hash_defweak)
   7353 			fdh = NULL;
   7354 		    }
   7355 
   7356 		  skip = (sym_sec->owner != ibfd
   7357 			  || sym_sec->output_section == bfd_abs_section_ptr);
   7358 		  if (skip)
   7359 		    {
   7360 		      if (fdh != NULL && sym_sec->owner == ibfd)
   7361 			{
   7362 			  /* Arrange for the function descriptor sym
   7363 			     to be dropped.  */
   7364 			  fdh->elf.root.u.def.value = 0;
   7365 			  fdh->elf.root.u.def.section = sym_sec;
   7366 			}
   7367 		      opd->adjust[rel->r_offset / 8] = -1;
   7368 		    }
   7369 		  else
   7370 		    {
   7371 		      /* We'll be keeping this opd entry.  */
   7372 
   7373 		      if (fdh != NULL)
   7374 			{
   7375 			  /* Redefine the function descriptor symbol to
   7376 			     this location in the opd section.  It is
   7377 			     necessary to update the value here rather
   7378 			     than using an array of adjustments as we do
   7379 			     for local symbols, because various places
   7380 			     in the generic ELF code use the value
   7381 			     stored in u.def.value.  */
   7382 			  fdh->elf.root.u.def.value = wptr - new_contents;
   7383 			  fdh->adjust_done = 1;
   7384 			}
   7385 
   7386 		      /* Local syms are a bit tricky.  We could
   7387 			 tweak them as they can be cached, but
   7388 			 we'd need to look through the local syms
   7389 			 for the function descriptor sym which we
   7390 			 don't have at the moment.  So keep an
   7391 			 array of adjustments.  */
   7392 		      opd->adjust[rel->r_offset / 8]
   7393 			= (wptr - new_contents) - (rptr - sec->contents);
   7394 
   7395 		      if (wptr != rptr)
   7396 			memcpy (wptr, rptr, opd_ent_size);
   7397 		      wptr += opd_ent_size;
   7398 		      if (add_aux_fields && opd_ent_size == 16)
   7399 			{
   7400 			  memset (wptr, '\0', 8);
   7401 			  wptr += 8;
   7402 			}
   7403 		    }
   7404 		  rptr += opd_ent_size;
   7405 		  offset += opd_ent_size;
   7406 		}
   7407 
   7408 	      if (skip)
   7409 		{
   7410 		  if (!NO_OPD_RELOCS
   7411 		      && !info->relocatable
   7412 		      && !dec_dynrel_count (rel->r_info, sec, info,
   7413 					    NULL, h, sym_sec))
   7414 		    goto error_ret;
   7415 		}
   7416 	      else
   7417 		{
   7418 		  /* We need to adjust any reloc offsets to point to the
   7419 		     new opd entries.  While we're at it, we may as well
   7420 		     remove redundant relocs.  */
   7421 		  rel->r_offset += opd->adjust[(offset - opd_ent_size) / 8];
   7422 		  if (write_rel != rel)
   7423 		    memcpy (write_rel, rel, sizeof (*rel));
   7424 		  ++write_rel;
   7425 		}
   7426 	    }
   7427 
   7428 	  sec->size = wptr - new_contents;
   7429 	  sec->reloc_count = write_rel - relstart;
   7430 	  if (add_aux_fields)
   7431 	    {
   7432 	      free (sec->contents);
   7433 	      sec->contents = new_contents;
   7434 	    }
   7435 
   7436 	  /* Fudge the header size too, as this is used later in
   7437 	     elf_bfd_final_link if we are emitting relocs.  */
   7438 	  rel_hdr = _bfd_elf_single_rel_hdr (sec);
   7439 	  rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
   7440 	  some_edited = TRUE;
   7441 	}
   7442       else if (elf_section_data (sec)->relocs != relstart)
   7443 	free (relstart);
   7444 
   7445       if (local_syms != NULL
   7446 	  && symtab_hdr->contents != (unsigned char *) local_syms)
   7447 	{
   7448 	  if (!info->keep_memory)
   7449 	    free (local_syms);
   7450 	  else
   7451 	    symtab_hdr->contents = (unsigned char *) local_syms;
   7452 	}
   7453     }
   7454 
   7455   if (some_edited)
   7456     elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
   7457 
   7458   /* If we are doing a final link and the last .opd entry is just 16 byte
   7459      long, add a 8 byte padding after it.  */
   7460   if (need_pad != NULL && !info->relocatable)
   7461     {
   7462       bfd_byte *p;
   7463 
   7464       if ((need_pad->flags & SEC_IN_MEMORY) == 0)
   7465 	{
   7466 	  BFD_ASSERT (need_pad->size > 0);
   7467 
   7468 	  p = bfd_malloc (need_pad->size + 8);
   7469 	  if (p == NULL)
   7470 	    return FALSE;
   7471 
   7472 	  if (! bfd_get_section_contents (need_pad->owner, need_pad,
   7473 					  p, 0, need_pad->size))
   7474 	    return FALSE;
   7475 
   7476 	  need_pad->contents = p;
   7477 	  need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
   7478 	}
   7479       else
   7480 	{
   7481 	  p = bfd_realloc (need_pad->contents, need_pad->size + 8);
   7482 	  if (p == NULL)
   7483 	    return FALSE;
   7484 
   7485 	  need_pad->contents = p;
   7486 	}
   7487 
   7488       memset (need_pad->contents + need_pad->size, 0, 8);
   7489       need_pad->size += 8;
   7490     }
   7491 
   7492   return TRUE;
   7493 }
   7494 
   7495 /* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
   7496 
   7497 asection *
   7498 ppc64_elf_tls_setup (struct bfd_link_info *info,
   7499 		     int no_tls_get_addr_opt,
   7500 		     int *no_multi_toc)
   7501 {
   7502   struct ppc_link_hash_table *htab;
   7503 
   7504   htab = ppc_hash_table (info);
   7505   if (htab == NULL)
   7506     return NULL;
   7507 
   7508   if (*no_multi_toc)
   7509     htab->do_multi_toc = 0;
   7510   else if (!htab->do_multi_toc)
   7511     *no_multi_toc = 1;
   7512 
   7513   htab->tls_get_addr = ((struct ppc_link_hash_entry *)
   7514 			elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
   7515 					      FALSE, FALSE, TRUE));
   7516   /* Move dynamic linking info to the function descriptor sym.  */
   7517   if (htab->tls_get_addr != NULL)
   7518     func_desc_adjust (&htab->tls_get_addr->elf, info);
   7519   htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
   7520 			   elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
   7521 						 FALSE, FALSE, TRUE));
   7522   if (!no_tls_get_addr_opt)
   7523     {
   7524       struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
   7525 
   7526       opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
   7527 				  FALSE, FALSE, TRUE);
   7528       if (opt != NULL)
   7529 	func_desc_adjust (opt, info);
   7530       opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
   7531 				     FALSE, FALSE, TRUE);
   7532       if (opt_fd != NULL
   7533 	  && (opt_fd->root.type == bfd_link_hash_defined
   7534 	      || opt_fd->root.type == bfd_link_hash_defweak))
   7535 	{
   7536 	  /* If glibc supports an optimized __tls_get_addr call stub,
   7537 	     signalled by the presence of __tls_get_addr_opt, and we'll
   7538 	     be calling __tls_get_addr via a plt call stub, then
   7539 	     make __tls_get_addr point to __tls_get_addr_opt.  */
   7540 	  tga_fd = &htab->tls_get_addr_fd->elf;
   7541 	  if (htab->elf.dynamic_sections_created
   7542 	      && tga_fd != NULL
   7543 	      && (tga_fd->type == STT_FUNC
   7544 		  || tga_fd->needs_plt)
   7545 	      && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
   7546 		   || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
   7547 		       && tga_fd->root.type == bfd_link_hash_undefweak)))
   7548 	    {
   7549 	      struct plt_entry *ent;
   7550 
   7551 	      for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
   7552 		if (ent->plt.refcount > 0)
   7553 		  break;
   7554 	      if (ent != NULL)
   7555 		{
   7556 		  tga_fd->root.type = bfd_link_hash_indirect;
   7557 		  tga_fd->root.u.i.link = &opt_fd->root;
   7558 		  ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
   7559 		  if (opt_fd->dynindx != -1)
   7560 		    {
   7561 		      /* Use __tls_get_addr_opt in dynamic relocations.  */
   7562 		      opt_fd->dynindx = -1;
   7563 		      _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
   7564 					      opt_fd->dynstr_index);
   7565 		      if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
   7566 			return NULL;
   7567 		    }
   7568 		  htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
   7569 		  tga = &htab->tls_get_addr->elf;
   7570 		  if (opt != NULL && tga != NULL)
   7571 		    {
   7572 		      tga->root.type = bfd_link_hash_indirect;
   7573 		      tga->root.u.i.link = &opt->root;
   7574 		      ppc64_elf_copy_indirect_symbol (info, opt, tga);
   7575 		      _bfd_elf_link_hash_hide_symbol (info, opt,
   7576 						      tga->forced_local);
   7577 		      htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
   7578 		    }
   7579 		  htab->tls_get_addr_fd->oh = htab->tls_get_addr;
   7580 		  htab->tls_get_addr_fd->is_func_descriptor = 1;
   7581 		  if (htab->tls_get_addr != NULL)
   7582 		    {
   7583 		      htab->tls_get_addr->oh = htab->tls_get_addr_fd;
   7584 		      htab->tls_get_addr->is_func = 1;
   7585 		    }
   7586 		}
   7587 	    }
   7588 	}
   7589       else
   7590 	no_tls_get_addr_opt = TRUE;
   7591     }
   7592   htab->no_tls_get_addr_opt = no_tls_get_addr_opt;
   7593   return _bfd_elf_tls_setup (info->output_bfd, info);
   7594 }
   7595 
   7596 /* Return TRUE iff REL is a branch reloc with a global symbol matching
   7597    HASH1 or HASH2.  */
   7598 
   7599 static bfd_boolean
   7600 branch_reloc_hash_match (const bfd *ibfd,
   7601 			 const Elf_Internal_Rela *rel,
   7602 			 const struct ppc_link_hash_entry *hash1,
   7603 			 const struct ppc_link_hash_entry *hash2)
   7604 {
   7605   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
   7606   enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
   7607   unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
   7608 
   7609   if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
   7610     {
   7611       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
   7612       struct elf_link_hash_entry *h;
   7613 
   7614       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   7615       h = elf_follow_link (h);
   7616       if (h == &hash1->elf || h == &hash2->elf)
   7617 	return TRUE;
   7618     }
   7619   return FALSE;
   7620 }
   7621 
   7622 /* Run through all the TLS relocs looking for optimization
   7623    opportunities.  The linker has been hacked (see ppc64elf.em) to do
   7624    a preliminary section layout so that we know the TLS segment
   7625    offsets.  We can't optimize earlier because some optimizations need
   7626    to know the tp offset, and we need to optimize before allocating
   7627    dynamic relocations.  */
   7628 
   7629 bfd_boolean
   7630 ppc64_elf_tls_optimize (struct bfd_link_info *info)
   7631 {
   7632   bfd *ibfd;
   7633   asection *sec;
   7634   struct ppc_link_hash_table *htab;
   7635   unsigned char *toc_ref;
   7636   int pass;
   7637 
   7638   if (info->relocatable || !info->executable)
   7639     return TRUE;
   7640 
   7641   htab = ppc_hash_table (info);
   7642   if (htab == NULL)
   7643     return FALSE;
   7644 
   7645   /* Make two passes over the relocs.  On the first pass, mark toc
   7646      entries involved with tls relocs, and check that tls relocs
   7647      involved in setting up a tls_get_addr call are indeed followed by
   7648      such a call.  If they are not, we can't do any tls optimization.
   7649      On the second pass twiddle tls_mask flags to notify
   7650      relocate_section that optimization can be done, and adjust got
   7651      and plt refcounts.  */
   7652   toc_ref = NULL;
   7653   for (pass = 0; pass < 2; ++pass)
   7654     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   7655       {
   7656 	Elf_Internal_Sym *locsyms = NULL;
   7657 	asection *toc = bfd_get_section_by_name (ibfd, ".toc");
   7658 
   7659 	for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   7660 	  if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
   7661 	    {
   7662 	      Elf_Internal_Rela *relstart, *rel, *relend;
   7663 	      bfd_boolean found_tls_get_addr_arg = 0;
   7664 
   7665 	      /* Read the relocations.  */
   7666 	      relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   7667 						    info->keep_memory);
   7668 	      if (relstart == NULL)
   7669 		return FALSE;
   7670 
   7671 	      relend = relstart + sec->reloc_count;
   7672 	      for (rel = relstart; rel < relend; rel++)
   7673 		{
   7674 		  enum elf_ppc64_reloc_type r_type;
   7675 		  unsigned long r_symndx;
   7676 		  struct elf_link_hash_entry *h;
   7677 		  Elf_Internal_Sym *sym;
   7678 		  asection *sym_sec;
   7679 		  unsigned char *tls_mask;
   7680 		  unsigned char tls_set, tls_clear, tls_type = 0;
   7681 		  bfd_vma value;
   7682 		  bfd_boolean ok_tprel, is_local;
   7683 		  long toc_ref_index = 0;
   7684 		  int expecting_tls_get_addr = 0;
   7685 		  bfd_boolean ret = FALSE;
   7686 
   7687 		  r_symndx = ELF64_R_SYM (rel->r_info);
   7688 		  if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
   7689 				  r_symndx, ibfd))
   7690 		    {
   7691 		    err_free_rel:
   7692 		      if (elf_section_data (sec)->relocs != relstart)
   7693 			free (relstart);
   7694 		      if (toc_ref != NULL)
   7695 			free (toc_ref);
   7696 		      if (locsyms != NULL
   7697 			  && (elf_symtab_hdr (ibfd).contents
   7698 			      != (unsigned char *) locsyms))
   7699 			free (locsyms);
   7700 		      return ret;
   7701 		    }
   7702 
   7703 		  if (h != NULL)
   7704 		    {
   7705 		      if (h->root.type == bfd_link_hash_defined
   7706 			  || h->root.type == bfd_link_hash_defweak)
   7707 			value = h->root.u.def.value;
   7708 		      else if (h->root.type == bfd_link_hash_undefweak)
   7709 			value = 0;
   7710 		      else
   7711 			{
   7712 			  found_tls_get_addr_arg = 0;
   7713 			  continue;
   7714 			}
   7715 		    }
   7716 		  else
   7717 		    /* Symbols referenced by TLS relocs must be of type
   7718 		       STT_TLS.  So no need for .opd local sym adjust.  */
   7719 		    value = sym->st_value;
   7720 
   7721 		  ok_tprel = FALSE;
   7722 		  is_local = FALSE;
   7723 		  if (h == NULL
   7724 		      || !h->def_dynamic)
   7725 		    {
   7726 		      is_local = TRUE;
   7727 		      if (h != NULL
   7728 			  && h->root.type == bfd_link_hash_undefweak)
   7729 			ok_tprel = TRUE;
   7730 		      else
   7731 			{
   7732 			  value += sym_sec->output_offset;
   7733 			  value += sym_sec->output_section->vma;
   7734 			  value -= htab->elf.tls_sec->vma;
   7735 			  ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
   7736 				      < (bfd_vma) 1 << 32);
   7737 			}
   7738 		    }
   7739 
   7740 		  r_type = ELF64_R_TYPE (rel->r_info);
   7741 		  /* If this section has old-style __tls_get_addr calls
   7742 		     without marker relocs, then check that each
   7743 		     __tls_get_addr call reloc is preceded by a reloc
   7744 		     that conceivably belongs to the __tls_get_addr arg
   7745 		     setup insn.  If we don't find matching arg setup
   7746 		     relocs, don't do any tls optimization.  */
   7747 		  if (pass == 0
   7748 		      && sec->has_tls_get_addr_call
   7749 		      && h != NULL
   7750 		      && (h == &htab->tls_get_addr->elf
   7751 			  || h == &htab->tls_get_addr_fd->elf)
   7752 		      && !found_tls_get_addr_arg
   7753 		      && is_branch_reloc (r_type))
   7754 		    {
   7755 		      info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
   7756 						"TLS optimization disabled\n"),
   7757 					      ibfd, sec, rel->r_offset);
   7758 		      ret = TRUE;
   7759 		      goto err_free_rel;
   7760 		    }
   7761 
   7762 		  found_tls_get_addr_arg = 0;
   7763 		  switch (r_type)
   7764 		    {
   7765 		    case R_PPC64_GOT_TLSLD16:
   7766 		    case R_PPC64_GOT_TLSLD16_LO:
   7767 		      expecting_tls_get_addr = 1;
   7768 		      found_tls_get_addr_arg = 1;
   7769 		      /* Fall thru */
   7770 
   7771 		    case R_PPC64_GOT_TLSLD16_HI:
   7772 		    case R_PPC64_GOT_TLSLD16_HA:
   7773 		      /* These relocs should never be against a symbol
   7774 			 defined in a shared lib.  Leave them alone if
   7775 			 that turns out to be the case.  */
   7776 		      if (!is_local)
   7777 			continue;
   7778 
   7779 		      /* LD -> LE */
   7780 		      tls_set = 0;
   7781 		      tls_clear = TLS_LD;
   7782 		      tls_type = TLS_TLS | TLS_LD;
   7783 		      break;
   7784 
   7785 		    case R_PPC64_GOT_TLSGD16:
   7786 		    case R_PPC64_GOT_TLSGD16_LO:
   7787 		      expecting_tls_get_addr = 1;
   7788 		      found_tls_get_addr_arg = 1;
   7789 		      /* Fall thru */
   7790 
   7791 		    case R_PPC64_GOT_TLSGD16_HI:
   7792 		    case R_PPC64_GOT_TLSGD16_HA:
   7793 		      if (ok_tprel)
   7794 			/* GD -> LE */
   7795 			tls_set = 0;
   7796 		      else
   7797 			/* GD -> IE */
   7798 			tls_set = TLS_TLS | TLS_TPRELGD;
   7799 		      tls_clear = TLS_GD;
   7800 		      tls_type = TLS_TLS | TLS_GD;
   7801 		      break;
   7802 
   7803 		    case R_PPC64_GOT_TPREL16_DS:
   7804 		    case R_PPC64_GOT_TPREL16_LO_DS:
   7805 		    case R_PPC64_GOT_TPREL16_HI:
   7806 		    case R_PPC64_GOT_TPREL16_HA:
   7807 		      if (ok_tprel)
   7808 			{
   7809 			  /* IE -> LE */
   7810 			  tls_set = 0;
   7811 			  tls_clear = TLS_TPREL;
   7812 			  tls_type = TLS_TLS | TLS_TPREL;
   7813 			  break;
   7814 			}
   7815 		      continue;
   7816 
   7817 		    case R_PPC64_TLSGD:
   7818 		    case R_PPC64_TLSLD:
   7819 		      found_tls_get_addr_arg = 1;
   7820 		      /* Fall thru */
   7821 
   7822 		    case R_PPC64_TLS:
   7823 		    case R_PPC64_TOC16:
   7824 		    case R_PPC64_TOC16_LO:
   7825 		      if (sym_sec == NULL || sym_sec != toc)
   7826 			continue;
   7827 
   7828 		      /* Mark this toc entry as referenced by a TLS
   7829 			 code sequence.  We can do that now in the
   7830 			 case of R_PPC64_TLS, and after checking for
   7831 			 tls_get_addr for the TOC16 relocs.  */
   7832 		      if (toc_ref == NULL)
   7833 			toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
   7834 		      if (toc_ref == NULL)
   7835 			goto err_free_rel;
   7836 
   7837 		      if (h != NULL)
   7838 			value = h->root.u.def.value;
   7839 		      else
   7840 			value = sym->st_value;
   7841 		      value += rel->r_addend;
   7842 		      BFD_ASSERT (value < toc->size && value % 8 == 0);
   7843 		      toc_ref_index = (value + toc->output_offset) / 8;
   7844 		      if (r_type == R_PPC64_TLS
   7845 			  || r_type == R_PPC64_TLSGD
   7846 			  || r_type == R_PPC64_TLSLD)
   7847 			{
   7848 			  toc_ref[toc_ref_index] = 1;
   7849 			  continue;
   7850 			}
   7851 
   7852 		      if (pass != 0 && toc_ref[toc_ref_index] == 0)
   7853 			continue;
   7854 
   7855 		      tls_set = 0;
   7856 		      tls_clear = 0;
   7857 		      expecting_tls_get_addr = 2;
   7858 		      break;
   7859 
   7860 		    case R_PPC64_TPREL64:
   7861 		      if (pass == 0
   7862 			  || sec != toc
   7863 			  || toc_ref == NULL
   7864 			  || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
   7865 			continue;
   7866 		      if (ok_tprel)
   7867 			{
   7868 			  /* IE -> LE */
   7869 			  tls_set = TLS_EXPLICIT;
   7870 			  tls_clear = TLS_TPREL;
   7871 			  break;
   7872 			}
   7873 		      continue;
   7874 
   7875 		    case R_PPC64_DTPMOD64:
   7876 		      if (pass == 0
   7877 			  || sec != toc
   7878 			  || toc_ref == NULL
   7879 			  || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
   7880 			continue;
   7881 		      if (rel + 1 < relend
   7882 			  && (rel[1].r_info
   7883 			      == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
   7884 			  && rel[1].r_offset == rel->r_offset + 8)
   7885 			{
   7886 			  if (ok_tprel)
   7887 			    /* GD -> LE */
   7888 			    tls_set = TLS_EXPLICIT | TLS_GD;
   7889 			  else
   7890 			    /* GD -> IE */
   7891 			    tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
   7892 			  tls_clear = TLS_GD;
   7893 			}
   7894 		      else
   7895 			{
   7896 			  if (!is_local)
   7897 			    continue;
   7898 
   7899 			  /* LD -> LE */
   7900 			  tls_set = TLS_EXPLICIT;
   7901 			  tls_clear = TLS_LD;
   7902 			}
   7903 		      break;
   7904 
   7905 		    default:
   7906 		      continue;
   7907 		    }
   7908 
   7909 		  if (pass == 0)
   7910 		    {
   7911 		      if (!expecting_tls_get_addr
   7912 			  || !sec->has_tls_get_addr_call)
   7913 			continue;
   7914 
   7915 		      if (rel + 1 < relend
   7916 			  && branch_reloc_hash_match (ibfd, rel + 1,
   7917 						      htab->tls_get_addr,
   7918 						      htab->tls_get_addr_fd))
   7919 			{
   7920 			  if (expecting_tls_get_addr == 2)
   7921 			    {
   7922 			      /* Check for toc tls entries.  */
   7923 			      unsigned char *toc_tls;
   7924 			      int retval;
   7925 
   7926 			      retval = get_tls_mask (&toc_tls, NULL, NULL,
   7927 						     &locsyms,
   7928 						     rel, ibfd);
   7929 			      if (retval == 0)
   7930 				goto err_free_rel;
   7931 			      if (toc_tls != NULL)
   7932 				{
   7933 				  if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
   7934 				    found_tls_get_addr_arg = 1;
   7935 				  if (retval > 1)
   7936 				    toc_ref[toc_ref_index] = 1;
   7937 				}
   7938 			    }
   7939 			  continue;
   7940 			}
   7941 
   7942 		      if (expecting_tls_get_addr != 1)
   7943 			continue;
   7944 
   7945 		      /* Uh oh, we didn't find the expected call.  We
   7946 			 could just mark this symbol to exclude it
   7947 			 from tls optimization but it's safer to skip
   7948 			 the entire optimization.  */
   7949 		      info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
   7950 						"TLS optimization disabled\n"),
   7951 					      ibfd, sec, rel->r_offset);
   7952 		      ret = TRUE;
   7953 		      goto err_free_rel;
   7954 		    }
   7955 
   7956 		  if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
   7957 		    {
   7958 		      struct plt_entry *ent;
   7959 		      for (ent = htab->tls_get_addr->elf.plt.plist;
   7960 			   ent != NULL;
   7961 			   ent = ent->next)
   7962 			if (ent->addend == 0)
   7963 			  {
   7964 			    if (ent->plt.refcount > 0)
   7965 			      {
   7966 				ent->plt.refcount -= 1;
   7967 				expecting_tls_get_addr = 0;
   7968 			      }
   7969 			    break;
   7970 			  }
   7971 		    }
   7972 
   7973 		  if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
   7974 		    {
   7975 		      struct plt_entry *ent;
   7976 		      for (ent = htab->tls_get_addr_fd->elf.plt.plist;
   7977 			   ent != NULL;
   7978 			   ent = ent->next)
   7979 			if (ent->addend == 0)
   7980 			  {
   7981 			    if (ent->plt.refcount > 0)
   7982 			      ent->plt.refcount -= 1;
   7983 			    break;
   7984 			  }
   7985 		    }
   7986 
   7987 		  if (tls_clear == 0)
   7988 		    continue;
   7989 
   7990 		  if ((tls_set & TLS_EXPLICIT) == 0)
   7991 		    {
   7992 		      struct got_entry *ent;
   7993 
   7994 		      /* Adjust got entry for this reloc.  */
   7995 		      if (h != NULL)
   7996 			ent = h->got.glist;
   7997 		      else
   7998 			ent = elf_local_got_ents (ibfd)[r_symndx];
   7999 
   8000 		      for (; ent != NULL; ent = ent->next)
   8001 			if (ent->addend == rel->r_addend
   8002 			    && ent->owner == ibfd
   8003 			    && ent->tls_type == tls_type)
   8004 			  break;
   8005 		      if (ent == NULL)
   8006 			abort ();
   8007 
   8008 		      if (tls_set == 0)
   8009 			{
   8010 			  /* We managed to get rid of a got entry.  */
   8011 			  if (ent->got.refcount > 0)
   8012 			    ent->got.refcount -= 1;
   8013 			}
   8014 		    }
   8015 		  else
   8016 		    {
   8017 		      /* If we got rid of a DTPMOD/DTPREL reloc pair then
   8018 			 we'll lose one or two dyn relocs.  */
   8019 		      if (!dec_dynrel_count (rel->r_info, sec, info,
   8020 					     NULL, h, sym_sec))
   8021 			return FALSE;
   8022 
   8023 		      if (tls_set == (TLS_EXPLICIT | TLS_GD))
   8024 			{
   8025 			  if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
   8026 						 NULL, h, sym_sec))
   8027 			    return FALSE;
   8028 			}
   8029 		    }
   8030 
   8031 		  *tls_mask |= tls_set;
   8032 		  *tls_mask &= ~tls_clear;
   8033 		}
   8034 
   8035 	      if (elf_section_data (sec)->relocs != relstart)
   8036 		free (relstart);
   8037 	    }
   8038 
   8039 	if (locsyms != NULL
   8040 	    && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
   8041 	  {
   8042 	    if (!info->keep_memory)
   8043 	      free (locsyms);
   8044 	    else
   8045 	      elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
   8046 	  }
   8047       }
   8048 
   8049   if (toc_ref != NULL)
   8050     free (toc_ref);
   8051   return TRUE;
   8052 }
   8053 
   8054 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
   8055    the values of any global symbols in a toc section that has been
   8056    edited.  Globals in toc sections should be a rarity, so this function
   8057    sets a flag if any are found in toc sections other than the one just
   8058    edited, so that futher hash table traversals can be avoided.  */
   8059 
   8060 struct adjust_toc_info
   8061 {
   8062   asection *toc;
   8063   unsigned long *skip;
   8064   bfd_boolean global_toc_syms;
   8065 };
   8066 
   8067 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
   8068 
   8069 static bfd_boolean
   8070 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
   8071 {
   8072   struct ppc_link_hash_entry *eh;
   8073   struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
   8074   unsigned long i;
   8075 
   8076   if (h->root.type != bfd_link_hash_defined
   8077       && h->root.type != bfd_link_hash_defweak)
   8078     return TRUE;
   8079 
   8080   eh = (struct ppc_link_hash_entry *) h;
   8081   if (eh->adjust_done)
   8082     return TRUE;
   8083 
   8084   if (eh->elf.root.u.def.section == toc_inf->toc)
   8085     {
   8086       if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
   8087 	i = toc_inf->toc->rawsize >> 3;
   8088       else
   8089 	i = eh->elf.root.u.def.value >> 3;
   8090 
   8091       if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
   8092 	{
   8093 	  (*_bfd_error_handler)
   8094 	    (_("%s defined on removed toc entry"), eh->elf.root.root.string);
   8095 	  do
   8096 	    ++i;
   8097 	  while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
   8098 	  eh->elf.root.u.def.value = (bfd_vma) i << 3;
   8099 	}
   8100 
   8101       eh->elf.root.u.def.value -= toc_inf->skip[i];
   8102       eh->adjust_done = 1;
   8103     }
   8104   else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
   8105     toc_inf->global_toc_syms = TRUE;
   8106 
   8107   return TRUE;
   8108 }
   8109 
   8110 /* Return TRUE iff INSN is one we expect on a _LO variety toc/got reloc.  */
   8111 
   8112 static bfd_boolean
   8113 ok_lo_toc_insn (unsigned int insn)
   8114 {
   8115   return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
   8116 	  || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
   8117 	  || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
   8118 	  || (insn & (0x3f << 26)) == 36u << 26 /* stw */
   8119 	  || (insn & (0x3f << 26)) == 38u << 26 /* stb */
   8120 	  || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
   8121 	  || (insn & (0x3f << 26)) == 42u << 26 /* lha */
   8122 	  || (insn & (0x3f << 26)) == 44u << 26 /* sth */
   8123 	  || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
   8124 	  || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
   8125 	  || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
   8126 	  || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
   8127 	  || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
   8128 	  || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
   8129 	  || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
   8130 	      && (insn & 3) != 1)
   8131 	  || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
   8132 	      && ((insn & 3) == 0 || (insn & 3) == 3))
   8133 	  || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
   8134 }
   8135 
   8136 /* Examine all relocs referencing .toc sections in order to remove
   8137    unused .toc entries.  */
   8138 
   8139 bfd_boolean
   8140 ppc64_elf_edit_toc (struct bfd_link_info *info)
   8141 {
   8142   bfd *ibfd;
   8143   struct adjust_toc_info toc_inf;
   8144   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   8145 
   8146   htab->do_toc_opt = 1;
   8147   toc_inf.global_toc_syms = TRUE;
   8148   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   8149     {
   8150       asection *toc, *sec;
   8151       Elf_Internal_Shdr *symtab_hdr;
   8152       Elf_Internal_Sym *local_syms;
   8153       Elf_Internal_Rela *relstart, *rel, *toc_relocs;
   8154       unsigned long *skip, *drop;
   8155       unsigned char *used;
   8156       unsigned char *keep, last, some_unused;
   8157 
   8158       if (!is_ppc64_elf (ibfd))
   8159 	continue;
   8160 
   8161       toc = bfd_get_section_by_name (ibfd, ".toc");
   8162       if (toc == NULL
   8163 	  || toc->size == 0
   8164 	  || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
   8165 	  || discarded_section (toc))
   8166 	continue;
   8167 
   8168       toc_relocs = NULL;
   8169       local_syms = NULL;
   8170       symtab_hdr = &elf_symtab_hdr (ibfd);
   8171 
   8172       /* Look at sections dropped from the final link.  */
   8173       skip = NULL;
   8174       relstart = NULL;
   8175       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   8176 	{
   8177 	  if (sec->reloc_count == 0
   8178 	      || !discarded_section (sec)
   8179 	      || get_opd_info (sec)
   8180 	      || (sec->flags & SEC_ALLOC) == 0
   8181 	      || (sec->flags & SEC_DEBUGGING) != 0)
   8182 	    continue;
   8183 
   8184 	  relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
   8185 	  if (relstart == NULL)
   8186 	    goto error_ret;
   8187 
   8188 	  /* Run through the relocs to see which toc entries might be
   8189 	     unused.  */
   8190 	  for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
   8191 	    {
   8192 	      enum elf_ppc64_reloc_type r_type;
   8193 	      unsigned long r_symndx;
   8194 	      asection *sym_sec;
   8195 	      struct elf_link_hash_entry *h;
   8196 	      Elf_Internal_Sym *sym;
   8197 	      bfd_vma val;
   8198 
   8199 	      r_type = ELF64_R_TYPE (rel->r_info);
   8200 	      switch (r_type)
   8201 		{
   8202 		default:
   8203 		  continue;
   8204 
   8205 		case R_PPC64_TOC16:
   8206 		case R_PPC64_TOC16_LO:
   8207 		case R_PPC64_TOC16_HI:
   8208 		case R_PPC64_TOC16_HA:
   8209 		case R_PPC64_TOC16_DS:
   8210 		case R_PPC64_TOC16_LO_DS:
   8211 		  break;
   8212 		}
   8213 
   8214 	      r_symndx = ELF64_R_SYM (rel->r_info);
   8215 	      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   8216 			      r_symndx, ibfd))
   8217 		goto error_ret;
   8218 
   8219 	      if (sym_sec != toc)
   8220 		continue;
   8221 
   8222 	      if (h != NULL)
   8223 		val = h->root.u.def.value;
   8224 	      else
   8225 		val = sym->st_value;
   8226 	      val += rel->r_addend;
   8227 
   8228 	      if (val >= toc->size)
   8229 		continue;
   8230 
   8231 	      /* Anything in the toc ought to be aligned to 8 bytes.
   8232 		 If not, don't mark as unused.  */
   8233 	      if (val & 7)
   8234 		continue;
   8235 
   8236 	      if (skip == NULL)
   8237 		{
   8238 		  skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
   8239 		  if (skip == NULL)
   8240 		    goto error_ret;
   8241 		}
   8242 
   8243 	      skip[val >> 3] = ref_from_discarded;
   8244 	    }
   8245 
   8246 	  if (elf_section_data (sec)->relocs != relstart)
   8247 	    free (relstart);
   8248 	}
   8249 
   8250       /* For largetoc loads of address constants, we can convert
   8251 	 .  addis rx,2,addr@got@ha
   8252 	 .  ld ry,addr@got@l(rx)
   8253 	 to
   8254 	 .  addis rx,2,addr@toc@ha
   8255 	 .  addi ry,rx,addr@toc@l
   8256 	 when addr is within 2G of the toc pointer.  This then means
   8257 	 that the word storing "addr" in the toc is no longer needed.  */
   8258 
   8259       if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
   8260 	  && toc->output_section->rawsize < (bfd_vma) 1 << 31
   8261 	  && toc->reloc_count != 0)
   8262 	{
   8263 	  /* Read toc relocs.  */
   8264 	  toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
   8265 						  info->keep_memory);
   8266 	  if (toc_relocs == NULL)
   8267 	    goto error_ret;
   8268 
   8269 	  for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
   8270 	    {
   8271 	      enum elf_ppc64_reloc_type r_type;
   8272 	      unsigned long r_symndx;
   8273 	      asection *sym_sec;
   8274 	      struct elf_link_hash_entry *h;
   8275 	      Elf_Internal_Sym *sym;
   8276 	      bfd_vma val, addr;
   8277 
   8278 	      r_type = ELF64_R_TYPE (rel->r_info);
   8279 	      if (r_type != R_PPC64_ADDR64)
   8280 		continue;
   8281 
   8282 	      r_symndx = ELF64_R_SYM (rel->r_info);
   8283 	      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   8284 			      r_symndx, ibfd))
   8285 		goto error_ret;
   8286 
   8287 	      if (sym_sec == NULL
   8288 		  || discarded_section (sym_sec))
   8289 		continue;
   8290 
   8291 	      if (!SYMBOL_CALLS_LOCAL (info, h))
   8292 		continue;
   8293 
   8294 	      if (h != NULL)
   8295 		{
   8296 		  if (h->type == STT_GNU_IFUNC)
   8297 		    continue;
   8298 		  val = h->root.u.def.value;
   8299 		}
   8300 	      else
   8301 		{
   8302 		  if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
   8303 		    continue;
   8304 		  val = sym->st_value;
   8305 		}
   8306 	      val += rel->r_addend;
   8307 	      val += sym_sec->output_section->vma + sym_sec->output_offset;
   8308 
   8309 	      /* We don't yet know the exact toc pointer value, but we
   8310 		 know it will be somewhere in the toc section.  Don't
   8311 		 optimize if the difference from any possible toc
   8312 		 pointer is outside [ff..f80008000, 7fff7fff].  */
   8313 	      addr = toc->output_section->vma + TOC_BASE_OFF;
   8314 	      if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
   8315 		continue;
   8316 
   8317 	      addr = toc->output_section->vma + toc->output_section->rawsize;
   8318 	      if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
   8319 		continue;
   8320 
   8321 	      if (skip == NULL)
   8322 		{
   8323 		  skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
   8324 		  if (skip == NULL)
   8325 		    goto error_ret;
   8326 		}
   8327 
   8328 	      skip[rel->r_offset >> 3]
   8329 		|= can_optimize | ((rel - toc_relocs) << 2);
   8330 	    }
   8331 	}
   8332 
   8333       if (skip == NULL)
   8334 	continue;
   8335 
   8336       used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
   8337       if (used == NULL)
   8338 	{
   8339 	error_ret:
   8340 	  if (local_syms != NULL
   8341 	      && symtab_hdr->contents != (unsigned char *) local_syms)
   8342 	    free (local_syms);
   8343 	  if (sec != NULL
   8344 	      && relstart != NULL
   8345 	      && elf_section_data (sec)->relocs != relstart)
   8346 	    free (relstart);
   8347 	  if (toc_relocs != NULL
   8348 	      && elf_section_data (toc)->relocs != toc_relocs)
   8349 	    free (toc_relocs);
   8350 	  if (skip != NULL)
   8351 	    free (skip);
   8352 	  return FALSE;
   8353 	}
   8354 
   8355       /* Now check all kept sections that might reference the toc.
   8356 	 Check the toc itself last.  */
   8357       for (sec = (ibfd->sections == toc && toc->next ? toc->next
   8358 		  : ibfd->sections);
   8359 	   sec != NULL;
   8360 	   sec = (sec == toc ? NULL
   8361 		  : sec->next == NULL ? toc
   8362 		  : sec->next == toc && toc->next ? toc->next
   8363 		  : sec->next))
   8364 	{
   8365 	  int repeat;
   8366 
   8367 	  if (sec->reloc_count == 0
   8368 	      || discarded_section (sec)
   8369 	      || get_opd_info (sec)
   8370 	      || (sec->flags & SEC_ALLOC) == 0
   8371 	      || (sec->flags & SEC_DEBUGGING) != 0)
   8372 	    continue;
   8373 
   8374 	  relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   8375 						info->keep_memory);
   8376 	  if (relstart == NULL)
   8377 	    goto error_ret;
   8378 
   8379 	  /* Mark toc entries referenced as used.  */
   8380 	  do
   8381 	    {
   8382 	      repeat = 0;
   8383 	      for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
   8384 		{
   8385 		  enum elf_ppc64_reloc_type r_type;
   8386 		  unsigned long r_symndx;
   8387 		  asection *sym_sec;
   8388 		  struct elf_link_hash_entry *h;
   8389 		  Elf_Internal_Sym *sym;
   8390 		  bfd_vma val;
   8391 		  enum {no_check, check_lo, check_ha} insn_check;
   8392 
   8393 		  r_type = ELF64_R_TYPE (rel->r_info);
   8394 		  switch (r_type)
   8395 		    {
   8396 		    default:
   8397 		      insn_check = no_check;
   8398 		      break;
   8399 
   8400 		    case R_PPC64_GOT_TLSLD16_HA:
   8401 		    case R_PPC64_GOT_TLSGD16_HA:
   8402 		    case R_PPC64_GOT_TPREL16_HA:
   8403 		    case R_PPC64_GOT_DTPREL16_HA:
   8404 		    case R_PPC64_GOT16_HA:
   8405 		    case R_PPC64_TOC16_HA:
   8406 		      insn_check = check_ha;
   8407 		      break;
   8408 
   8409 		    case R_PPC64_GOT_TLSLD16_LO:
   8410 		    case R_PPC64_GOT_TLSGD16_LO:
   8411 		    case R_PPC64_GOT_TPREL16_LO_DS:
   8412 		    case R_PPC64_GOT_DTPREL16_LO_DS:
   8413 		    case R_PPC64_GOT16_LO:
   8414 		    case R_PPC64_GOT16_LO_DS:
   8415 		    case R_PPC64_TOC16_LO:
   8416 		    case R_PPC64_TOC16_LO_DS:
   8417 		      insn_check = check_lo;
   8418 		      break;
   8419 		    }
   8420 
   8421 		  if (insn_check != no_check)
   8422 		    {
   8423 		      bfd_vma off = rel->r_offset & ~3;
   8424 		      unsigned char buf[4];
   8425 		      unsigned int insn;
   8426 
   8427 		      if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
   8428 			{
   8429 			  free (used);
   8430 			  goto error_ret;
   8431 			}
   8432 		      insn = bfd_get_32 (ibfd, buf);
   8433 		      if (insn_check == check_lo
   8434 			  ? !ok_lo_toc_insn (insn)
   8435 			  : ((insn & ((0x3f << 26) | 0x1f << 16))
   8436 			     != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
   8437 			{
   8438 			  char str[12];
   8439 
   8440 			  ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
   8441 			  sprintf (str, "%#08x", insn);
   8442 			  info->callbacks->einfo
   8443 			    (_("%P: %H: toc optimization is not supported for"
   8444 			       " %s instruction.\n"),
   8445 			     ibfd, sec, rel->r_offset & ~3, str);
   8446 			}
   8447 		    }
   8448 
   8449 		  switch (r_type)
   8450 		    {
   8451 		    case R_PPC64_TOC16:
   8452 		    case R_PPC64_TOC16_LO:
   8453 		    case R_PPC64_TOC16_HI:
   8454 		    case R_PPC64_TOC16_HA:
   8455 		    case R_PPC64_TOC16_DS:
   8456 		    case R_PPC64_TOC16_LO_DS:
   8457 		      /* In case we're taking addresses of toc entries.  */
   8458 		    case R_PPC64_ADDR64:
   8459 		      break;
   8460 
   8461 		    default:
   8462 		      continue;
   8463 		    }
   8464 
   8465 		  r_symndx = ELF64_R_SYM (rel->r_info);
   8466 		  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   8467 				  r_symndx, ibfd))
   8468 		    {
   8469 		      free (used);
   8470 		      goto error_ret;
   8471 		    }
   8472 
   8473 		  if (sym_sec != toc)
   8474 		    continue;
   8475 
   8476 		  if (h != NULL)
   8477 		    val = h->root.u.def.value;
   8478 		  else
   8479 		    val = sym->st_value;
   8480 		  val += rel->r_addend;
   8481 
   8482 		  if (val >= toc->size)
   8483 		    continue;
   8484 
   8485 		  if ((skip[val >> 3] & can_optimize) != 0)
   8486 		    {
   8487 		      bfd_vma off;
   8488 		      unsigned char opc;
   8489 
   8490 		      switch (r_type)
   8491 			{
   8492 			case R_PPC64_TOC16_HA:
   8493 			  break;
   8494 
   8495 			case R_PPC64_TOC16_LO_DS:
   8496 			  off = rel->r_offset;
   8497 			  off += (bfd_big_endian (ibfd) ? -2 : 3);
   8498 			  if (!bfd_get_section_contents (ibfd, sec, &opc,
   8499 							 off, 1))
   8500 			    {
   8501 			      free (used);
   8502 			      goto error_ret;
   8503 			    }
   8504 			  if ((opc & (0x3f << 2)) == (58u << 2))
   8505 			    break;
   8506 			  /* Fall thru */
   8507 
   8508 			default:
   8509 			  /* Wrong sort of reloc, or not a ld.  We may
   8510 			     as well clear ref_from_discarded too.  */
   8511 			  skip[val >> 3] = 0;
   8512 			}
   8513 		    }
   8514 
   8515 		  if (sec != toc)
   8516 		    used[val >> 3] = 1;
   8517 		  /* For the toc section, we only mark as used if this
   8518 		     entry itself isn't unused.  */
   8519 		  else if ((used[rel->r_offset >> 3]
   8520 			    || !(skip[rel->r_offset >> 3] & ref_from_discarded))
   8521 			   && !used[val >> 3])
   8522 		    {
   8523 		      /* Do all the relocs again, to catch reference
   8524 			 chains.  */
   8525 		      repeat = 1;
   8526 		      used[val >> 3] = 1;
   8527 		    }
   8528 		}
   8529 	    }
   8530 	  while (repeat);
   8531 
   8532 	  if (elf_section_data (sec)->relocs != relstart)
   8533 	    free (relstart);
   8534 	}
   8535 
   8536       /* Merge the used and skip arrays.  Assume that TOC
   8537 	 doublewords not appearing as either used or unused belong
   8538 	 to to an entry more than one doubleword in size.  */
   8539       for (drop = skip, keep = used, last = 0, some_unused = 0;
   8540 	   drop < skip + (toc->size + 7) / 8;
   8541 	   ++drop, ++keep)
   8542 	{
   8543 	  if (*keep)
   8544 	    {
   8545 	      *drop &= ~ref_from_discarded;
   8546 	      if ((*drop & can_optimize) != 0)
   8547 		some_unused = 1;
   8548 	      last = 0;
   8549 	    }
   8550 	  else if ((*drop & ref_from_discarded) != 0)
   8551 	    {
   8552 	      some_unused = 1;
   8553 	      last = ref_from_discarded;
   8554 	    }
   8555 	  else
   8556 	    *drop = last;
   8557 	}
   8558 
   8559       free (used);
   8560 
   8561       if (some_unused)
   8562 	{
   8563 	  bfd_byte *contents, *src;
   8564 	  unsigned long off;
   8565 	  Elf_Internal_Sym *sym;
   8566 	  bfd_boolean local_toc_syms = FALSE;
   8567 
   8568 	  /* Shuffle the toc contents, and at the same time convert the
   8569 	     skip array from booleans into offsets.  */
   8570 	  if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
   8571 	    goto error_ret;
   8572 
   8573 	  elf_section_data (toc)->this_hdr.contents = contents;
   8574 
   8575 	  for (src = contents, off = 0, drop = skip;
   8576 	       src < contents + toc->size;
   8577 	       src += 8, ++drop)
   8578 	    {
   8579 	      if ((*drop & (can_optimize | ref_from_discarded)) != 0)
   8580 		off += 8;
   8581 	      else if (off != 0)
   8582 		{
   8583 		  *drop = off;
   8584 		  memcpy (src - off, src, 8);
   8585 		}
   8586 	    }
   8587 	  *drop = off;
   8588 	  toc->rawsize = toc->size;
   8589 	  toc->size = src - contents - off;
   8590 
   8591 	  /* Adjust addends for relocs against the toc section sym,
   8592 	     and optimize any accesses we can.  */
   8593 	  for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   8594 	    {
   8595 	      if (sec->reloc_count == 0
   8596 		  || discarded_section (sec))
   8597 		continue;
   8598 
   8599 	      relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   8600 						    info->keep_memory);
   8601 	      if (relstart == NULL)
   8602 		goto error_ret;
   8603 
   8604 	      for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
   8605 		{
   8606 		  enum elf_ppc64_reloc_type r_type;
   8607 		  unsigned long r_symndx;
   8608 		  asection *sym_sec;
   8609 		  struct elf_link_hash_entry *h;
   8610 		  bfd_vma val;
   8611 
   8612 		  r_type = ELF64_R_TYPE (rel->r_info);
   8613 		  switch (r_type)
   8614 		    {
   8615 		    default:
   8616 		      continue;
   8617 
   8618 		    case R_PPC64_TOC16:
   8619 		    case R_PPC64_TOC16_LO:
   8620 		    case R_PPC64_TOC16_HI:
   8621 		    case R_PPC64_TOC16_HA:
   8622 		    case R_PPC64_TOC16_DS:
   8623 		    case R_PPC64_TOC16_LO_DS:
   8624 		    case R_PPC64_ADDR64:
   8625 		      break;
   8626 		    }
   8627 
   8628 		  r_symndx = ELF64_R_SYM (rel->r_info);
   8629 		  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   8630 				  r_symndx, ibfd))
   8631 		    goto error_ret;
   8632 
   8633 		  if (sym_sec != toc)
   8634 		    continue;
   8635 
   8636 		  if (h != NULL)
   8637 		    val = h->root.u.def.value;
   8638 		  else
   8639 		    {
   8640 		      val = sym->st_value;
   8641 		      if (val != 0)
   8642 			local_toc_syms = TRUE;
   8643 		    }
   8644 
   8645 		  val += rel->r_addend;
   8646 
   8647 		  if (val > toc->rawsize)
   8648 		    val = toc->rawsize;
   8649 		  else if ((skip[val >> 3] & ref_from_discarded) != 0)
   8650 		    continue;
   8651 		  else if ((skip[val >> 3] & can_optimize) != 0)
   8652 		    {
   8653 		      Elf_Internal_Rela *tocrel
   8654 			= toc_relocs + (skip[val >> 3] >> 2);
   8655 		      unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
   8656 
   8657 		      switch (r_type)
   8658 			{
   8659 			case R_PPC64_TOC16_HA:
   8660 			  rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
   8661 			  break;
   8662 
   8663 			case R_PPC64_TOC16_LO_DS:
   8664 			  rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
   8665 			  break;
   8666 
   8667 			default:
   8668 			  if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
   8669 			    ppc_howto_init ();
   8670 			  info->callbacks->einfo
   8671 			    (_("%P: %H: %s references "
   8672 			       "optimized away TOC entry\n"),
   8673 			     ibfd, sec, rel->r_offset,
   8674 			     ppc64_elf_howto_table[r_type]->name);
   8675 			  bfd_set_error (bfd_error_bad_value);
   8676 			  goto error_ret;
   8677 			}
   8678 		      rel->r_addend = tocrel->r_addend;
   8679 		      elf_section_data (sec)->relocs = relstart;
   8680 		      continue;
   8681 		    }
   8682 
   8683 		  if (h != NULL || sym->st_value != 0)
   8684 		    continue;
   8685 
   8686 		  rel->r_addend -= skip[val >> 3];
   8687 		  elf_section_data (sec)->relocs = relstart;
   8688 		}
   8689 
   8690 	      if (elf_section_data (sec)->relocs != relstart)
   8691 		free (relstart);
   8692 	    }
   8693 
   8694 	  /* We shouldn't have local or global symbols defined in the TOC,
   8695 	     but handle them anyway.  */
   8696 	  if (local_syms != NULL)
   8697 	    for (sym = local_syms;
   8698 		 sym < local_syms + symtab_hdr->sh_info;
   8699 		 ++sym)
   8700 	      if (sym->st_value != 0
   8701 		  && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
   8702 		{
   8703 		  unsigned long i;
   8704 
   8705 		  if (sym->st_value > toc->rawsize)
   8706 		    i = toc->rawsize >> 3;
   8707 		  else
   8708 		    i = sym->st_value >> 3;
   8709 
   8710 		  if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
   8711 		    {
   8712 		      if (local_toc_syms)
   8713 			(*_bfd_error_handler)
   8714 			  (_("%s defined on removed toc entry"),
   8715 			   bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
   8716 		      do
   8717 			++i;
   8718 		      while ((skip[i] & (ref_from_discarded | can_optimize)));
   8719 		      sym->st_value = (bfd_vma) i << 3;
   8720 		    }
   8721 
   8722 		  sym->st_value -= skip[i];
   8723 		  symtab_hdr->contents = (unsigned char *) local_syms;
   8724 		}
   8725 
   8726 	  /* Adjust any global syms defined in this toc input section.  */
   8727 	  if (toc_inf.global_toc_syms)
   8728 	    {
   8729 	      toc_inf.toc = toc;
   8730 	      toc_inf.skip = skip;
   8731 	      toc_inf.global_toc_syms = FALSE;
   8732 	      elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
   8733 				      &toc_inf);
   8734 	    }
   8735 
   8736 	  if (toc->reloc_count != 0)
   8737 	    {
   8738 	      Elf_Internal_Shdr *rel_hdr;
   8739 	      Elf_Internal_Rela *wrel;
   8740 	      bfd_size_type sz;
   8741 
   8742 	      /* Remove unused toc relocs, and adjust those we keep.  */
   8743 	      if (toc_relocs == NULL)
   8744 		toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
   8745 							info->keep_memory);
   8746 	      if (toc_relocs == NULL)
   8747 		goto error_ret;
   8748 
   8749 	      wrel = toc_relocs;
   8750 	      for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
   8751 		if ((skip[rel->r_offset >> 3]
   8752 		     & (ref_from_discarded | can_optimize)) == 0)
   8753 		  {
   8754 		    wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
   8755 		    wrel->r_info = rel->r_info;
   8756 		    wrel->r_addend = rel->r_addend;
   8757 		    ++wrel;
   8758 		  }
   8759 		else if (!dec_dynrel_count (rel->r_info, toc, info,
   8760 					    &local_syms, NULL, NULL))
   8761 		  goto error_ret;
   8762 
   8763 	      elf_section_data (toc)->relocs = toc_relocs;
   8764 	      toc->reloc_count = wrel - toc_relocs;
   8765 	      rel_hdr = _bfd_elf_single_rel_hdr (toc);
   8766 	      sz = rel_hdr->sh_entsize;
   8767 	      rel_hdr->sh_size = toc->reloc_count * sz;
   8768 	    }
   8769 	}
   8770       else if (toc_relocs != NULL
   8771 	       && elf_section_data (toc)->relocs != toc_relocs)
   8772 	free (toc_relocs);
   8773 
   8774       if (local_syms != NULL
   8775 	  && symtab_hdr->contents != (unsigned char *) local_syms)
   8776 	{
   8777 	  if (!info->keep_memory)
   8778 	    free (local_syms);
   8779 	  else
   8780 	    symtab_hdr->contents = (unsigned char *) local_syms;
   8781 	}
   8782       free (skip);
   8783     }
   8784 
   8785   return TRUE;
   8786 }
   8787 
   8788 /* Return true iff input section I references the TOC using
   8789    instructions limited to +/-32k offsets.  */
   8790 
   8791 bfd_boolean
   8792 ppc64_elf_has_small_toc_reloc (asection *i)
   8793 {
   8794   return (is_ppc64_elf (i->owner)
   8795 	  && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
   8796 }
   8797 
   8798 /* Allocate space for one GOT entry.  */
   8799 
   8800 static void
   8801 allocate_got (struct elf_link_hash_entry *h,
   8802 	      struct bfd_link_info *info,
   8803 	      struct got_entry *gent)
   8804 {
   8805   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   8806   bfd_boolean dyn;
   8807   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
   8808   int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
   8809 		 ? 16 : 8);
   8810   int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
   8811 		  ? 2 : 1) * sizeof (Elf64_External_Rela);
   8812   asection *got = ppc64_elf_tdata (gent->owner)->got;
   8813 
   8814   gent->got.offset = got->size;
   8815   got->size += entsize;
   8816 
   8817   dyn = htab->elf.dynamic_sections_created;
   8818   if ((info->shared
   8819        || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
   8820 	    && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   8821 		|| h->root.type != bfd_link_hash_undefweak))
   8822     {
   8823       asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
   8824       relgot->size += rentsize;
   8825     }
   8826   else if (h->type == STT_GNU_IFUNC)
   8827     {
   8828       asection *relgot = htab->reliplt;
   8829       relgot->size += rentsize;
   8830       htab->got_reli_size += rentsize;
   8831     }
   8832 }
   8833 
   8834 /* This function merges got entries in the same toc group.  */
   8835 
   8836 static void
   8837 merge_got_entries (struct got_entry **pent)
   8838 {
   8839   struct got_entry *ent, *ent2;
   8840 
   8841   for (ent = *pent; ent != NULL; ent = ent->next)
   8842     if (!ent->is_indirect)
   8843       for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
   8844 	if (!ent2->is_indirect
   8845 	    && ent2->addend == ent->addend
   8846 	    && ent2->tls_type == ent->tls_type
   8847 	    && elf_gp (ent2->owner) == elf_gp (ent->owner))
   8848 	  {
   8849 	    ent2->is_indirect = TRUE;
   8850 	    ent2->got.ent = ent;
   8851 	  }
   8852 }
   8853 
   8854 /* Allocate space in .plt, .got and associated reloc sections for
   8855    dynamic relocs.  */
   8856 
   8857 static bfd_boolean
   8858 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
   8859 {
   8860   struct bfd_link_info *info;
   8861   struct ppc_link_hash_table *htab;
   8862   asection *s;
   8863   struct ppc_link_hash_entry *eh;
   8864   struct elf_dyn_relocs *p;
   8865   struct got_entry **pgent, *gent;
   8866 
   8867   if (h->root.type == bfd_link_hash_indirect)
   8868     return TRUE;
   8869 
   8870   info = (struct bfd_link_info *) inf;
   8871   htab = ppc_hash_table (info);
   8872   if (htab == NULL)
   8873     return FALSE;
   8874 
   8875   if ((htab->elf.dynamic_sections_created
   8876        && h->dynindx != -1
   8877        && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
   8878       || h->type == STT_GNU_IFUNC)
   8879     {
   8880       struct plt_entry *pent;
   8881       bfd_boolean doneone = FALSE;
   8882       for (pent = h->plt.plist; pent != NULL; pent = pent->next)
   8883 	if (pent->plt.refcount > 0)
   8884 	  {
   8885 	    if (!htab->elf.dynamic_sections_created
   8886 		|| h->dynindx == -1)
   8887 	      {
   8888 		s = htab->iplt;
   8889 		pent->plt.offset = s->size;
   8890 		s->size += PLT_ENTRY_SIZE;
   8891 		s = htab->reliplt;
   8892 	      }
   8893 	    else
   8894 	      {
   8895 		/* If this is the first .plt entry, make room for the special
   8896 		   first entry.  */
   8897 		s = htab->plt;
   8898 		if (s->size == 0)
   8899 		  s->size += PLT_INITIAL_ENTRY_SIZE;
   8900 
   8901 		pent->plt.offset = s->size;
   8902 
   8903 		/* Make room for this entry.  */
   8904 		s->size += PLT_ENTRY_SIZE;
   8905 
   8906 		/* Make room for the .glink code.  */
   8907 		s = htab->glink;
   8908 		if (s->size == 0)
   8909 		  s->size += GLINK_CALL_STUB_SIZE;
   8910 		/* We need bigger stubs past index 32767.  */
   8911 		if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
   8912 		  s->size += 4;
   8913 		s->size += 2*4;
   8914 
   8915 		/* We also need to make an entry in the .rela.plt section.  */
   8916 		s = htab->relplt;
   8917 	      }
   8918 	    s->size += sizeof (Elf64_External_Rela);
   8919 	    doneone = TRUE;
   8920 	  }
   8921 	else
   8922 	  pent->plt.offset = (bfd_vma) -1;
   8923       if (!doneone)
   8924 	{
   8925 	  h->plt.plist = NULL;
   8926 	  h->needs_plt = 0;
   8927 	}
   8928     }
   8929   else
   8930     {
   8931       h->plt.plist = NULL;
   8932       h->needs_plt = 0;
   8933     }
   8934 
   8935   eh = (struct ppc_link_hash_entry *) h;
   8936   /* Run through the TLS GD got entries first if we're changing them
   8937      to TPREL.  */
   8938   if ((eh->tls_mask & TLS_TPRELGD) != 0)
   8939     for (gent = h->got.glist; gent != NULL; gent = gent->next)
   8940       if (gent->got.refcount > 0
   8941 	  && (gent->tls_type & TLS_GD) != 0)
   8942 	{
   8943 	  /* This was a GD entry that has been converted to TPREL.  If
   8944 	     there happens to be a TPREL entry we can use that one.  */
   8945 	  struct got_entry *ent;
   8946 	  for (ent = h->got.glist; ent != NULL; ent = ent->next)
   8947 	    if (ent->got.refcount > 0
   8948 		&& (ent->tls_type & TLS_TPREL) != 0
   8949 		&& ent->addend == gent->addend
   8950 		&& ent->owner == gent->owner)
   8951 	      {
   8952 		gent->got.refcount = 0;
   8953 		break;
   8954 	      }
   8955 
   8956 	  /* If not, then we'll be using our own TPREL entry.  */
   8957 	  if (gent->got.refcount != 0)
   8958 	    gent->tls_type = TLS_TLS | TLS_TPREL;
   8959 	}
   8960 
   8961   /* Remove any list entry that won't generate a word in the GOT before
   8962      we call merge_got_entries.  Otherwise we risk merging to empty
   8963      entries.  */
   8964   pgent = &h->got.glist;
   8965   while ((gent = *pgent) != NULL)
   8966     if (gent->got.refcount > 0)
   8967       {
   8968 	if ((gent->tls_type & TLS_LD) != 0
   8969 	    && !h->def_dynamic)
   8970 	  {
   8971 	    ppc64_tlsld_got (gent->owner)->got.refcount += 1;
   8972 	    *pgent = gent->next;
   8973 	  }
   8974 	else
   8975 	  pgent = &gent->next;
   8976       }
   8977     else
   8978       *pgent = gent->next;
   8979 
   8980   if (!htab->do_multi_toc)
   8981     merge_got_entries (&h->got.glist);
   8982 
   8983   for (gent = h->got.glist; gent != NULL; gent = gent->next)
   8984     if (!gent->is_indirect)
   8985       {
   8986 	/* Make sure this symbol is output as a dynamic symbol.
   8987 	   Undefined weak syms won't yet be marked as dynamic,
   8988 	   nor will all TLS symbols.  */
   8989 	if (h->dynindx == -1
   8990 	    && !h->forced_local
   8991 	    && h->type != STT_GNU_IFUNC
   8992 	    && htab->elf.dynamic_sections_created)
   8993 	  {
   8994 	    if (! bfd_elf_link_record_dynamic_symbol (info, h))
   8995 	      return FALSE;
   8996 	  }
   8997 
   8998 	if (!is_ppc64_elf (gent->owner))
   8999 	  abort ();
   9000 
   9001 	allocate_got (h, info, gent);
   9002       }
   9003 
   9004   if (eh->dyn_relocs == NULL
   9005       || (!htab->elf.dynamic_sections_created
   9006 	  && h->type != STT_GNU_IFUNC))
   9007     return TRUE;
   9008 
   9009   /* In the shared -Bsymbolic case, discard space allocated for
   9010      dynamic pc-relative relocs against symbols which turn out to be
   9011      defined in regular objects.  For the normal shared case, discard
   9012      space for relocs that have become local due to symbol visibility
   9013      changes.  */
   9014 
   9015   if (info->shared)
   9016     {
   9017       /* Relocs that use pc_count are those that appear on a call insn,
   9018 	 or certain REL relocs (see must_be_dyn_reloc) that can be
   9019 	 generated via assembly.  We want calls to protected symbols to
   9020 	 resolve directly to the function rather than going via the plt.
   9021 	 If people want function pointer comparisons to work as expected
   9022 	 then they should avoid writing weird assembly.  */
   9023       if (SYMBOL_CALLS_LOCAL (info, h))
   9024 	{
   9025 	  struct elf_dyn_relocs **pp;
   9026 
   9027 	  for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
   9028 	    {
   9029 	      p->count -= p->pc_count;
   9030 	      p->pc_count = 0;
   9031 	      if (p->count == 0)
   9032 		*pp = p->next;
   9033 	      else
   9034 		pp = &p->next;
   9035 	    }
   9036 	}
   9037 
   9038       /* Also discard relocs on undefined weak syms with non-default
   9039 	 visibility.  */
   9040       if (eh->dyn_relocs != NULL
   9041 	  && h->root.type == bfd_link_hash_undefweak)
   9042 	{
   9043 	  if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
   9044 	    eh->dyn_relocs = NULL;
   9045 
   9046 	  /* Make sure this symbol is output as a dynamic symbol.
   9047 	     Undefined weak syms won't yet be marked as dynamic.  */
   9048 	  else if (h->dynindx == -1
   9049 		   && !h->forced_local)
   9050 	    {
   9051 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   9052 		return FALSE;
   9053 	    }
   9054 	}
   9055     }
   9056   else if (h->type == STT_GNU_IFUNC)
   9057     {
   9058       if (!h->non_got_ref)
   9059 	eh->dyn_relocs = NULL;
   9060     }
   9061   else if (ELIMINATE_COPY_RELOCS)
   9062     {
   9063       /* For the non-shared case, discard space for relocs against
   9064 	 symbols which turn out to need copy relocs or are not
   9065 	 dynamic.  */
   9066 
   9067       if (!h->non_got_ref
   9068 	  && !h->def_regular)
   9069 	{
   9070 	  /* Make sure this symbol is output as a dynamic symbol.
   9071 	     Undefined weak syms won't yet be marked as dynamic.  */
   9072 	  if (h->dynindx == -1
   9073 	      && !h->forced_local)
   9074 	    {
   9075 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   9076 		return FALSE;
   9077 	    }
   9078 
   9079 	  /* If that succeeded, we know we'll be keeping all the
   9080 	     relocs.  */
   9081 	  if (h->dynindx != -1)
   9082 	    goto keep;
   9083 	}
   9084 
   9085       eh->dyn_relocs = NULL;
   9086 
   9087     keep: ;
   9088     }
   9089 
   9090   /* Finally, allocate space.  */
   9091   for (p = eh->dyn_relocs; p != NULL; p = p->next)
   9092     {
   9093       asection *sreloc = elf_section_data (p->sec)->sreloc;
   9094       if (!htab->elf.dynamic_sections_created)
   9095 	sreloc = htab->reliplt;
   9096       sreloc->size += p->count * sizeof (Elf64_External_Rela);
   9097     }
   9098 
   9099   return TRUE;
   9100 }
   9101 
   9102 /* Find any dynamic relocs that apply to read-only sections.  */
   9103 
   9104 static bfd_boolean
   9105 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
   9106 {
   9107   struct ppc_link_hash_entry *eh;
   9108   struct elf_dyn_relocs *p;
   9109 
   9110   eh = (struct ppc_link_hash_entry *) h;
   9111   for (p = eh->dyn_relocs; p != NULL; p = p->next)
   9112     {
   9113       asection *s = p->sec->output_section;
   9114 
   9115       if (s != NULL && (s->flags & SEC_READONLY) != 0)
   9116 	{
   9117 	  struct bfd_link_info *info = inf;
   9118 
   9119           if (info->warn_shared_textrel)
   9120             (*_bfd_error_handler)
   9121               (_("warning: dynamic relocation in readonly section `%s'"),
   9122               h->root.root.string);
   9123 	  info->flags |= DF_TEXTREL;
   9124 
   9125 	  /* Not an error, just cut short the traversal.  */
   9126 	  return FALSE;
   9127 	}
   9128     }
   9129   return TRUE;
   9130 }
   9131 
   9132 /* Set the sizes of the dynamic sections.  */
   9133 
   9134 static bfd_boolean
   9135 ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
   9136 				 struct bfd_link_info *info)
   9137 {
   9138   struct ppc_link_hash_table *htab;
   9139   bfd *dynobj;
   9140   asection *s;
   9141   bfd_boolean relocs;
   9142   bfd *ibfd;
   9143   struct got_entry *first_tlsld;
   9144 
   9145   htab = ppc_hash_table (info);
   9146   if (htab == NULL)
   9147     return FALSE;
   9148 
   9149   dynobj = htab->elf.dynobj;
   9150   if (dynobj == NULL)
   9151     abort ();
   9152 
   9153   if (htab->elf.dynamic_sections_created)
   9154     {
   9155       /* Set the contents of the .interp section to the interpreter.  */
   9156       if (info->executable)
   9157 	{
   9158 	  s = bfd_get_linker_section (dynobj, ".interp");
   9159 	  if (s == NULL)
   9160 	    abort ();
   9161 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
   9162 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
   9163 	}
   9164     }
   9165 
   9166   /* Set up .got offsets for local syms, and space for local dynamic
   9167      relocs.  */
   9168   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   9169     {
   9170       struct got_entry **lgot_ents;
   9171       struct got_entry **end_lgot_ents;
   9172       struct plt_entry **local_plt;
   9173       struct plt_entry **end_local_plt;
   9174       unsigned char *lgot_masks;
   9175       bfd_size_type locsymcount;
   9176       Elf_Internal_Shdr *symtab_hdr;
   9177       asection *srel;
   9178 
   9179       if (!is_ppc64_elf (ibfd))
   9180 	continue;
   9181 
   9182       for (s = ibfd->sections; s != NULL; s = s->next)
   9183 	{
   9184 	  struct elf_dyn_relocs *p;
   9185 
   9186 	  for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
   9187 	    {
   9188 	      if (!bfd_is_abs_section (p->sec)
   9189 		  && bfd_is_abs_section (p->sec->output_section))
   9190 		{
   9191 		  /* Input section has been discarded, either because
   9192 		     it is a copy of a linkonce section or due to
   9193 		     linker script /DISCARD/, so we'll be discarding
   9194 		     the relocs too.  */
   9195 		}
   9196 	      else if (p->count != 0)
   9197 		{
   9198 		  srel = elf_section_data (p->sec)->sreloc;
   9199 		  if (!htab->elf.dynamic_sections_created)
   9200 		    srel = htab->reliplt;
   9201 		  srel->size += p->count * sizeof (Elf64_External_Rela);
   9202 		  if ((p->sec->output_section->flags & SEC_READONLY) != 0)
   9203 		    {
   9204 		      if (info->warn_shared_textrel)
   9205 			(*_bfd_error_handler)
   9206 			  (_("warning: dynamic relocation in readonly section `%s'"),
   9207 			  p->sec->output_section->name);
   9208 		      info->flags |= DF_TEXTREL;
   9209 		    }
   9210 		}
   9211 	    }
   9212 	}
   9213 
   9214       lgot_ents = elf_local_got_ents (ibfd);
   9215       if (!lgot_ents)
   9216 	continue;
   9217 
   9218       symtab_hdr = &elf_symtab_hdr (ibfd);
   9219       locsymcount = symtab_hdr->sh_info;
   9220       end_lgot_ents = lgot_ents + locsymcount;
   9221       local_plt = (struct plt_entry **) end_lgot_ents;
   9222       end_local_plt = local_plt + locsymcount;
   9223       lgot_masks = (unsigned char *) end_local_plt;
   9224       s = ppc64_elf_tdata (ibfd)->got;
   9225       srel = ppc64_elf_tdata (ibfd)->relgot;
   9226       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
   9227 	{
   9228 	  struct got_entry **pent, *ent;
   9229 
   9230 	  pent = lgot_ents;
   9231 	  while ((ent = *pent) != NULL)
   9232 	    if (ent->got.refcount > 0)
   9233 	      {
   9234 		if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
   9235 		  {
   9236 		    ppc64_tlsld_got (ibfd)->got.refcount += 1;
   9237 		    *pent = ent->next;
   9238 		  }
   9239 		else
   9240 		  {
   9241 		    unsigned int num = 1;
   9242 		    ent->got.offset = s->size;
   9243 		    if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
   9244 		      num = 2;
   9245 		    s->size += num * 8;
   9246 		    if (info->shared)
   9247 		      srel->size += num * sizeof (Elf64_External_Rela);
   9248 		    else if ((*lgot_masks & PLT_IFUNC) != 0)
   9249 		      {
   9250 			htab->reliplt->size
   9251 			  += num * sizeof (Elf64_External_Rela);
   9252 			htab->got_reli_size
   9253 			  += num * sizeof (Elf64_External_Rela);
   9254 		      }
   9255 		    pent = &ent->next;
   9256 		  }
   9257 	      }
   9258 	    else
   9259 	      *pent = ent->next;
   9260 	}
   9261 
   9262       /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt.  */
   9263       for (; local_plt < end_local_plt; ++local_plt)
   9264 	{
   9265 	  struct plt_entry *ent;
   9266 
   9267 	  for (ent = *local_plt; ent != NULL; ent = ent->next)
   9268 	    if (ent->plt.refcount > 0)
   9269 	      {
   9270 		s = htab->iplt;
   9271 		ent->plt.offset = s->size;
   9272 		s->size += PLT_ENTRY_SIZE;
   9273 
   9274 		htab->reliplt->size += sizeof (Elf64_External_Rela);
   9275 	      }
   9276 	    else
   9277 	      ent->plt.offset = (bfd_vma) -1;
   9278 	}
   9279     }
   9280 
   9281   /* Allocate global sym .plt and .got entries, and space for global
   9282      sym dynamic relocs.  */
   9283   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
   9284 
   9285   first_tlsld = NULL;
   9286   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   9287     {
   9288       struct got_entry *ent;
   9289 
   9290       if (!is_ppc64_elf (ibfd))
   9291 	continue;
   9292 
   9293       ent = ppc64_tlsld_got (ibfd);
   9294       if (ent->got.refcount > 0)
   9295 	{
   9296 	  if (!htab->do_multi_toc && first_tlsld != NULL)
   9297 	    {
   9298 	      ent->is_indirect = TRUE;
   9299 	      ent->got.ent = first_tlsld;
   9300 	    }
   9301 	  else
   9302 	    {
   9303 	      if (first_tlsld == NULL)
   9304 		first_tlsld = ent;
   9305 	      s = ppc64_elf_tdata (ibfd)->got;
   9306 	      ent->got.offset = s->size;
   9307 	      ent->owner = ibfd;
   9308 	      s->size += 16;
   9309 	      if (info->shared)
   9310 		{
   9311 		  asection *srel = ppc64_elf_tdata (ibfd)->relgot;
   9312 		  srel->size += sizeof (Elf64_External_Rela);
   9313 		}
   9314 	    }
   9315 	}
   9316       else
   9317 	ent->got.offset = (bfd_vma) -1;
   9318     }
   9319 
   9320   /* We now have determined the sizes of the various dynamic sections.
   9321      Allocate memory for them.  */
   9322   relocs = FALSE;
   9323   for (s = dynobj->sections; s != NULL; s = s->next)
   9324     {
   9325       if ((s->flags & SEC_LINKER_CREATED) == 0)
   9326 	continue;
   9327 
   9328       if (s == htab->brlt || s == htab->relbrlt)
   9329 	/* These haven't been allocated yet;  don't strip.  */
   9330 	continue;
   9331       else if (s == htab->got
   9332 	       || s == htab->plt
   9333 	       || s == htab->iplt
   9334 	       || s == htab->glink
   9335 	       || s == htab->dynbss)
   9336 	{
   9337 	  /* Strip this section if we don't need it; see the
   9338 	     comment below.  */
   9339 	}
   9340       else if (s == htab->glink_eh_frame)
   9341 	{
   9342 	  if (!bfd_is_abs_section (s->output_section))
   9343 	    /* Not sized yet.  */
   9344 	    continue;
   9345 	}
   9346       else if (CONST_STRNEQ (s->name, ".rela"))
   9347 	{
   9348 	  if (s->size != 0)
   9349 	    {
   9350 	      if (s != htab->relplt)
   9351 		relocs = TRUE;
   9352 
   9353 	      /* We use the reloc_count field as a counter if we need
   9354 		 to copy relocs into the output file.  */
   9355 	      s->reloc_count = 0;
   9356 	    }
   9357 	}
   9358       else
   9359 	{
   9360 	  /* It's not one of our sections, so don't allocate space.  */
   9361 	  continue;
   9362 	}
   9363 
   9364       if (s->size == 0)
   9365 	{
   9366 	  /* If we don't need this section, strip it from the
   9367 	     output file.  This is mostly to handle .rela.bss and
   9368 	     .rela.plt.  We must create both sections in
   9369 	     create_dynamic_sections, because they must be created
   9370 	     before the linker maps input sections to output
   9371 	     sections.  The linker does that before
   9372 	     adjust_dynamic_symbol is called, and it is that
   9373 	     function which decides whether anything needs to go
   9374 	     into these sections.  */
   9375 	  s->flags |= SEC_EXCLUDE;
   9376 	  continue;
   9377 	}
   9378 
   9379       if ((s->flags & SEC_HAS_CONTENTS) == 0)
   9380 	continue;
   9381 
   9382       /* Allocate memory for the section contents.  We use bfd_zalloc
   9383 	 here in case unused entries are not reclaimed before the
   9384 	 section's contents are written out.  This should not happen,
   9385 	 but this way if it does we get a R_PPC64_NONE reloc in .rela
   9386 	 sections instead of garbage.
   9387 	 We also rely on the section contents being zero when writing
   9388 	 the GOT.  */
   9389       s->contents = bfd_zalloc (dynobj, s->size);
   9390       if (s->contents == NULL)
   9391 	return FALSE;
   9392     }
   9393 
   9394   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   9395     {
   9396       if (!is_ppc64_elf (ibfd))
   9397 	continue;
   9398 
   9399       s = ppc64_elf_tdata (ibfd)->got;
   9400       if (s != NULL && s != htab->got)
   9401 	{
   9402 	  if (s->size == 0)
   9403 	    s->flags |= SEC_EXCLUDE;
   9404 	  else
   9405 	    {
   9406 	      s->contents = bfd_zalloc (ibfd, s->size);
   9407 	      if (s->contents == NULL)
   9408 		return FALSE;
   9409 	    }
   9410 	}
   9411       s = ppc64_elf_tdata (ibfd)->relgot;
   9412       if (s != NULL)
   9413 	{
   9414 	  if (s->size == 0)
   9415 	    s->flags |= SEC_EXCLUDE;
   9416 	  else
   9417 	    {
   9418 	      s->contents = bfd_zalloc (ibfd, s->size);
   9419 	      if (s->contents == NULL)
   9420 		return FALSE;
   9421 	      relocs = TRUE;
   9422 	      s->reloc_count = 0;
   9423 	    }
   9424 	}
   9425     }
   9426 
   9427   if (htab->elf.dynamic_sections_created)
   9428     {
   9429       /* Add some entries to the .dynamic section.  We fill in the
   9430 	 values later, in ppc64_elf_finish_dynamic_sections, but we
   9431 	 must add the entries now so that we get the correct size for
   9432 	 the .dynamic section.  The DT_DEBUG entry is filled in by the
   9433 	 dynamic linker and used by the debugger.  */
   9434 #define add_dynamic_entry(TAG, VAL) \
   9435   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
   9436 
   9437       if (info->executable)
   9438 	{
   9439 	  if (!add_dynamic_entry (DT_DEBUG, 0))
   9440 	    return FALSE;
   9441 	}
   9442 
   9443       if (htab->plt != NULL && htab->plt->size != 0)
   9444 	{
   9445 	  if (!add_dynamic_entry (DT_PLTGOT, 0)
   9446 	      || !add_dynamic_entry (DT_PLTRELSZ, 0)
   9447 	      || !add_dynamic_entry (DT_PLTREL, DT_RELA)
   9448 	      || !add_dynamic_entry (DT_JMPREL, 0)
   9449 	      || !add_dynamic_entry (DT_PPC64_GLINK, 0))
   9450 	    return FALSE;
   9451 	}
   9452 
   9453       if (NO_OPD_RELOCS)
   9454 	{
   9455 	  if (!add_dynamic_entry (DT_PPC64_OPD, 0)
   9456 	      || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
   9457 	    return FALSE;
   9458 	}
   9459 
   9460       if (!htab->no_tls_get_addr_opt
   9461 	  && htab->tls_get_addr_fd != NULL
   9462 	  && htab->tls_get_addr_fd->elf.plt.plist != NULL
   9463 	  && !add_dynamic_entry (DT_PPC64_TLSOPT, 0))
   9464 	return FALSE;
   9465 
   9466       if (relocs)
   9467 	{
   9468 	  if (!add_dynamic_entry (DT_RELA, 0)
   9469 	      || !add_dynamic_entry (DT_RELASZ, 0)
   9470 	      || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
   9471 	    return FALSE;
   9472 
   9473 	  /* If any dynamic relocs apply to a read-only section,
   9474 	     then we need a DT_TEXTREL entry.  */
   9475 	  if ((info->flags & DF_TEXTREL) == 0)
   9476 	    elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
   9477 
   9478 	  if ((info->flags & DF_TEXTREL) != 0)
   9479 	    {
   9480 	      if (!add_dynamic_entry (DT_TEXTREL, 0))
   9481 		return FALSE;
   9482 	    }
   9483 	}
   9484     }
   9485 #undef add_dynamic_entry
   9486 
   9487   return TRUE;
   9488 }
   9489 
   9490 /* Determine the type of stub needed, if any, for a call.  */
   9491 
   9492 static inline enum ppc_stub_type
   9493 ppc_type_of_stub (asection *input_sec,
   9494 		  const Elf_Internal_Rela *rel,
   9495 		  struct ppc_link_hash_entry **hash,
   9496 		  struct plt_entry **plt_ent,
   9497 		  bfd_vma destination)
   9498 {
   9499   struct ppc_link_hash_entry *h = *hash;
   9500   bfd_vma location;
   9501   bfd_vma branch_offset;
   9502   bfd_vma max_branch_offset;
   9503   enum elf_ppc64_reloc_type r_type;
   9504 
   9505   if (h != NULL)
   9506     {
   9507       struct plt_entry *ent;
   9508       struct ppc_link_hash_entry *fdh = h;
   9509       if (h->oh != NULL
   9510 	  && h->oh->is_func_descriptor)
   9511 	{
   9512 	  fdh = ppc_follow_link (h->oh);
   9513 	  *hash = fdh;
   9514 	}
   9515 
   9516       for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
   9517 	if (ent->addend == rel->r_addend
   9518 	    && ent->plt.offset != (bfd_vma) -1)
   9519 	  {
   9520 	    *plt_ent = ent;
   9521 	    return ppc_stub_plt_call;
   9522 	  }
   9523 
   9524       /* Here, we know we don't have a plt entry.  If we don't have a
   9525 	 either a defined function descriptor or a defined entry symbol
   9526 	 in a regular object file, then it is pointless trying to make
   9527 	 any other type of stub.  */
   9528       if (!is_static_defined (&fdh->elf)
   9529 	  && !is_static_defined (&h->elf))
   9530 	return ppc_stub_none;
   9531     }
   9532   else if (elf_local_got_ents (input_sec->owner) != NULL)
   9533     {
   9534       Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
   9535       struct plt_entry **local_plt = (struct plt_entry **)
   9536 	elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
   9537       unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
   9538 
   9539       if (local_plt[r_symndx] != NULL)
   9540 	{
   9541 	  struct plt_entry *ent;
   9542 
   9543 	  for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
   9544 	    if (ent->addend == rel->r_addend
   9545 		&& ent->plt.offset != (bfd_vma) -1)
   9546 	      {
   9547 		*plt_ent = ent;
   9548 		return ppc_stub_plt_call;
   9549 	      }
   9550 	}
   9551     }
   9552 
   9553   /* Determine where the call point is.  */
   9554   location = (input_sec->output_offset
   9555 	      + input_sec->output_section->vma
   9556 	      + rel->r_offset);
   9557 
   9558   branch_offset = destination - location;
   9559   r_type = ELF64_R_TYPE (rel->r_info);
   9560 
   9561   /* Determine if a long branch stub is needed.  */
   9562   max_branch_offset = 1 << 25;
   9563   if (r_type != R_PPC64_REL24)
   9564     max_branch_offset = 1 << 15;
   9565 
   9566   if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
   9567     /* We need a stub.  Figure out whether a long_branch or plt_branch
   9568        is needed later.  */
   9569     return ppc_stub_long_branch;
   9570 
   9571   return ppc_stub_none;
   9572 }
   9573 
   9574 /* With power7 weakly ordered memory model, it is possible for ld.so
   9575    to update a plt entry in one thread and have another thread see a
   9576    stale zero toc entry.  To avoid this we need some sort of acquire
   9577    barrier in the call stub.  One solution is to make the load of the
   9578    toc word seem to appear to depend on the load of the function entry
   9579    word.  Another solution is to test for r2 being zero, and branch to
   9580    the appropriate glink entry if so.
   9581 
   9582    .	fake dep barrier	compare
   9583    .	ld 11,xxx(2)		ld 11,xxx(2)
   9584    .	mtctr 11		mtctr 11
   9585    .	xor 11,11,11		ld 2,xxx+8(2)
   9586    .	add 2,2,11		cmpldi 2,0
   9587    .	ld 2,xxx+8(2)		bnectr+
   9588    .	bctr			b <glink_entry>
   9589 
   9590    The solution involving the compare turns out to be faster, so
   9591    that's what we use unless the branch won't reach.  */
   9592 
   9593 #define ALWAYS_USE_FAKE_DEP 0
   9594 #define ALWAYS_EMIT_R2SAVE 0
   9595 
   9596 #define PPC_LO(v) ((v) & 0xffff)
   9597 #define PPC_HI(v) (((v) >> 16) & 0xffff)
   9598 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
   9599 
   9600 static inline unsigned int
   9601 plt_stub_size (struct ppc_link_hash_table *htab,
   9602 	       struct ppc_stub_hash_entry *stub_entry,
   9603 	       bfd_vma off)
   9604 {
   9605   unsigned size = PLT_CALL_STUB_SIZE;
   9606 
   9607   if (!(ALWAYS_EMIT_R2SAVE
   9608 	|| stub_entry->stub_type == ppc_stub_plt_call_r2save))
   9609     size -= 4;
   9610   if (!htab->plt_static_chain)
   9611     size -= 4;
   9612   if (htab->plt_thread_safe)
   9613     size += 8;
   9614   if (PPC_HA (off) == 0)
   9615     size -= 4;
   9616   if (PPC_HA (off + 8 + 8 * htab->plt_static_chain) != PPC_HA (off))
   9617     size += 4;
   9618   if (stub_entry->h != NULL
   9619       && (stub_entry->h == htab->tls_get_addr_fd
   9620 	  || stub_entry->h == htab->tls_get_addr)
   9621       && !htab->no_tls_get_addr_opt)
   9622     size += 13 * 4;
   9623   return size;
   9624 }
   9625 
   9626 /* If this stub would cross fewer 2**plt_stub_align boundaries if we align,
   9627    then return the padding needed to do so.  */
   9628 static inline unsigned int
   9629 plt_stub_pad (struct ppc_link_hash_table *htab,
   9630 	      struct ppc_stub_hash_entry *stub_entry,
   9631 	      bfd_vma plt_off)
   9632 {
   9633   int stub_align = 1 << htab->plt_stub_align;
   9634   unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
   9635   bfd_vma stub_off = stub_entry->stub_sec->size;
   9636 
   9637   if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
   9638       > (stub_size & -stub_align))
   9639     return stub_align - (stub_off & (stub_align - 1));
   9640   return 0;
   9641 }
   9642 
   9643 /* Build a .plt call stub.  */
   9644 
   9645 static inline bfd_byte *
   9646 build_plt_stub (struct ppc_link_hash_table *htab,
   9647 		struct ppc_stub_hash_entry *stub_entry,
   9648 		bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
   9649 {
   9650   bfd *obfd = htab->stub_bfd;
   9651   bfd_boolean plt_static_chain = htab->plt_static_chain;
   9652   bfd_boolean plt_thread_safe = htab->plt_thread_safe;
   9653   bfd_boolean use_fake_dep = plt_thread_safe;
   9654   bfd_vma cmp_branch_off = 0;
   9655 
   9656   if (!ALWAYS_USE_FAKE_DEP
   9657       && plt_thread_safe
   9658       && !(stub_entry->h != NULL
   9659 	   && (stub_entry->h == htab->tls_get_addr_fd
   9660 	       || stub_entry->h == htab->tls_get_addr)
   9661 	   && !htab->no_tls_get_addr_opt))
   9662     {
   9663       bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
   9664       bfd_vma pltindex = (pltoff - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE;
   9665       bfd_vma glinkoff = GLINK_CALL_STUB_SIZE + pltindex * 8;
   9666       bfd_vma to, from;
   9667 
   9668       if (pltindex > 32768)
   9669 	glinkoff += (pltindex - 32768) * 4;
   9670       to = (glinkoff
   9671 	    + htab->glink->output_offset
   9672 	    + htab->glink->output_section->vma);
   9673       from = (p - stub_entry->stub_sec->contents
   9674 	      + 4 * (ALWAYS_EMIT_R2SAVE
   9675 		     || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   9676 	      + 4 * (PPC_HA (offset) != 0)
   9677 	      + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
   9678 		     != PPC_HA (offset))
   9679 	      + 4 * (plt_static_chain != 0)
   9680 	      + 20
   9681 	      + stub_entry->stub_sec->output_offset
   9682 	      + stub_entry->stub_sec->output_section->vma);
   9683       cmp_branch_off = to - from;
   9684       use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
   9685     }
   9686 
   9687   if (PPC_HA (offset) != 0)
   9688     {
   9689       if (r != NULL)
   9690 	{
   9691 	  if (ALWAYS_EMIT_R2SAVE
   9692 	      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   9693 	    r[0].r_offset += 4;
   9694 	  r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
   9695 	  r[1].r_offset = r[0].r_offset + 4;
   9696 	  r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
   9697 	  r[1].r_addend = r[0].r_addend;
   9698 	  if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
   9699 	    {
   9700 	      r[2].r_offset = r[1].r_offset + 4;
   9701 	      r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
   9702 	      r[2].r_addend = r[0].r_addend;
   9703 	    }
   9704 	  else
   9705 	    {
   9706 	      r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
   9707 	      r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
   9708 	      r[2].r_addend = r[0].r_addend + 8;
   9709 	      if (plt_static_chain)
   9710 		{
   9711 		  r[3].r_offset = r[2].r_offset + 4;
   9712 		  r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
   9713 		  r[3].r_addend = r[0].r_addend + 16;
   9714 		}
   9715 	    }
   9716 	}
   9717       if (ALWAYS_EMIT_R2SAVE
   9718 	  || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   9719 	bfd_put_32 (obfd, STD_R2_40R1, p),			p += 4;
   9720       bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p),	p += 4;
   9721       bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p),	p += 4;
   9722       if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
   9723 	{
   9724 	  bfd_put_32 (obfd, ADDI_R12_R12 | PPC_LO (offset), p),	p += 4;
   9725 	  offset = 0;
   9726 	}
   9727       bfd_put_32 (obfd, MTCTR_R11, p),				p += 4;
   9728       if (use_fake_dep)
   9729 	{
   9730 	  bfd_put_32 (obfd, XOR_R11_R11_R11, p),		p += 4;
   9731 	  bfd_put_32 (obfd, ADD_R12_R12_R11, p),		p += 4;
   9732 	}
   9733       bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset + 8), p),	p += 4;
   9734       if (plt_static_chain)
   9735 	bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset + 16), p), p += 4;
   9736     }
   9737   else
   9738     {
   9739       if (r != NULL)
   9740 	{
   9741 	  if (ALWAYS_EMIT_R2SAVE
   9742 	      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   9743 	    r[0].r_offset += 4;
   9744 	  r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
   9745 	  if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
   9746 	    {
   9747 	      r[1].r_offset = r[0].r_offset + 4;
   9748 	      r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
   9749 	      r[1].r_addend = r[0].r_addend;
   9750 	    }
   9751 	  else
   9752 	    {
   9753 	      r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
   9754 	      r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
   9755 	      r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
   9756 	      if (plt_static_chain)
   9757 		{
   9758 		  r[2].r_offset = r[1].r_offset + 4;
   9759 		  r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
   9760 		  r[2].r_addend = r[0].r_addend + 8;
   9761 		}
   9762 	    }
   9763 	}
   9764       if (ALWAYS_EMIT_R2SAVE
   9765 	  || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   9766 	bfd_put_32 (obfd, STD_R2_40R1, p),			p += 4;
   9767       bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset), p),	p += 4;
   9768       if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
   9769 	{
   9770 	  bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p),	p += 4;
   9771 	  offset = 0;
   9772 	}
   9773       bfd_put_32 (obfd, MTCTR_R11, p),				p += 4;
   9774       if (use_fake_dep)
   9775 	{
   9776 	  bfd_put_32 (obfd, XOR_R11_R11_R11, p),		p += 4;
   9777 	  bfd_put_32 (obfd, ADD_R2_R2_R11, p),			p += 4;
   9778 	}
   9779       if (plt_static_chain)
   9780 	bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
   9781       bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p),	p += 4;
   9782     }
   9783   if (plt_thread_safe && !use_fake_dep)
   9784     {
   9785       bfd_put_32 (obfd, CMPLDI_R2_0, p),			p += 4;
   9786       bfd_put_32 (obfd, BNECTR_P4, p),				p += 4;
   9787       bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
   9788     }
   9789   else
   9790     bfd_put_32 (obfd, BCTR, p),					p += 4;
   9791   return p;
   9792 }
   9793 
   9794 /* Build a special .plt call stub for __tls_get_addr.  */
   9795 
   9796 #define LD_R11_0R3	0xe9630000
   9797 #define LD_R12_0R3	0xe9830000
   9798 #define MR_R0_R3	0x7c601b78
   9799 #define CMPDI_R11_0	0x2c2b0000
   9800 #define ADD_R3_R12_R13	0x7c6c6a14
   9801 #define BEQLR		0x4d820020
   9802 #define MR_R3_R0	0x7c030378
   9803 #define MFLR_R11	0x7d6802a6
   9804 #define STD_R11_0R1	0xf9610000
   9805 #define BCTRL		0x4e800421
   9806 #define LD_R11_0R1	0xe9610000
   9807 #define LD_R2_0R1	0xe8410000
   9808 #define MTLR_R11	0x7d6803a6
   9809 
   9810 static inline bfd_byte *
   9811 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
   9812 			 struct ppc_stub_hash_entry *stub_entry,
   9813 			 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
   9814 {
   9815   bfd *obfd = htab->stub_bfd;
   9816 
   9817   bfd_put_32 (obfd, LD_R11_0R3 + 0, p),		p += 4;
   9818   bfd_put_32 (obfd, LD_R12_0R3 + 8, p),		p += 4;
   9819   bfd_put_32 (obfd, MR_R0_R3, p),		p += 4;
   9820   bfd_put_32 (obfd, CMPDI_R11_0, p),		p += 4;
   9821   bfd_put_32 (obfd, ADD_R3_R12_R13, p),		p += 4;
   9822   bfd_put_32 (obfd, BEQLR, p),			p += 4;
   9823   bfd_put_32 (obfd, MR_R3_R0, p),		p += 4;
   9824   bfd_put_32 (obfd, MFLR_R11, p),		p += 4;
   9825   bfd_put_32 (obfd, STD_R11_0R1 + 32, p),	p += 4;
   9826 
   9827   if (r != NULL)
   9828     r[0].r_offset += 9 * 4;
   9829   p = build_plt_stub (htab, stub_entry, p, offset, r);
   9830   bfd_put_32 (obfd, BCTRL, p - 4);
   9831 
   9832   bfd_put_32 (obfd, LD_R11_0R1 + 32, p),	p += 4;
   9833   bfd_put_32 (obfd, LD_R2_0R1 + 40, p),		p += 4;
   9834   bfd_put_32 (obfd, MTLR_R11, p),		p += 4;
   9835   bfd_put_32 (obfd, BLR, p),			p += 4;
   9836 
   9837   return p;
   9838 }
   9839 
   9840 static Elf_Internal_Rela *
   9841 get_relocs (asection *sec, int count)
   9842 {
   9843   Elf_Internal_Rela *relocs;
   9844   struct bfd_elf_section_data *elfsec_data;
   9845 
   9846   elfsec_data = elf_section_data (sec);
   9847   relocs = elfsec_data->relocs;
   9848   if (relocs == NULL)
   9849     {
   9850       bfd_size_type relsize;
   9851       relsize = sec->reloc_count * sizeof (*relocs);
   9852       relocs = bfd_alloc (sec->owner, relsize);
   9853       if (relocs == NULL)
   9854 	return NULL;
   9855       elfsec_data->relocs = relocs;
   9856       elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
   9857 					  sizeof (Elf_Internal_Shdr));
   9858       if (elfsec_data->rela.hdr == NULL)
   9859 	return NULL;
   9860       elfsec_data->rela.hdr->sh_size = (sec->reloc_count
   9861 					* sizeof (Elf64_External_Rela));
   9862       elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
   9863       sec->reloc_count = 0;
   9864     }
   9865   relocs += sec->reloc_count;
   9866   sec->reloc_count += count;
   9867   return relocs;
   9868 }
   9869 
   9870 static bfd_vma
   9871 get_r2off (struct bfd_link_info *info,
   9872 	   struct ppc_stub_hash_entry *stub_entry)
   9873 {
   9874   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   9875   bfd_vma r2off = htab->stub_group[stub_entry->target_section->id].toc_off;
   9876 
   9877   if (r2off == 0)
   9878     {
   9879       /* Support linking -R objects.  Get the toc pointer from the
   9880 	 opd entry.  */
   9881       char buf[8];
   9882       asection *opd = stub_entry->h->elf.root.u.def.section;
   9883       bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
   9884 
   9885       if (strcmp (opd->name, ".opd") != 0
   9886 	  || opd->reloc_count != 0)
   9887 	{
   9888 	  info->callbacks->einfo (_("%P: cannot find opd entry toc for `%T'\n"),
   9889 				  stub_entry->h->elf.root.root.string);
   9890 	  bfd_set_error (bfd_error_bad_value);
   9891 	  return 0;
   9892 	}
   9893       if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
   9894 	return 0;
   9895       r2off = bfd_get_64 (opd->owner, buf);
   9896       r2off -= elf_gp (info->output_bfd);
   9897     }
   9898   r2off -= htab->stub_group[stub_entry->id_sec->id].toc_off;
   9899   return r2off;
   9900 }
   9901 
   9902 static bfd_boolean
   9903 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
   9904 {
   9905   struct ppc_stub_hash_entry *stub_entry;
   9906   struct ppc_branch_hash_entry *br_entry;
   9907   struct bfd_link_info *info;
   9908   struct ppc_link_hash_table *htab;
   9909   bfd_byte *loc;
   9910   bfd_byte *p;
   9911   bfd_vma dest, off;
   9912   int size;
   9913   Elf_Internal_Rela *r;
   9914   asection *plt;
   9915 
   9916   /* Massage our args to the form they really have.  */
   9917   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
   9918   info = in_arg;
   9919 
   9920   htab = ppc_hash_table (info);
   9921   if (htab == NULL)
   9922     return FALSE;
   9923 
   9924   /* Make a note of the offset within the stubs for this entry.  */
   9925   stub_entry->stub_offset = stub_entry->stub_sec->size;
   9926   loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
   9927 
   9928   htab->stub_count[stub_entry->stub_type - 1] += 1;
   9929   switch (stub_entry->stub_type)
   9930     {
   9931     case ppc_stub_long_branch:
   9932     case ppc_stub_long_branch_r2off:
   9933       /* Branches are relative.  This is where we are going to.  */
   9934       off = dest = (stub_entry->target_value
   9935 		    + stub_entry->target_section->output_offset
   9936 		    + stub_entry->target_section->output_section->vma);
   9937 
   9938       /* And this is where we are coming from.  */
   9939       off -= (stub_entry->stub_offset
   9940 	      + stub_entry->stub_sec->output_offset
   9941 	      + stub_entry->stub_sec->output_section->vma);
   9942 
   9943       size = 4;
   9944       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
   9945 	{
   9946 	  bfd_vma r2off = get_r2off (info, stub_entry);
   9947 
   9948 	  if (r2off == 0)
   9949 	    {
   9950 	      htab->stub_error = TRUE;
   9951 	      return FALSE;
   9952 	    }
   9953 	  bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
   9954 	  loc += 4;
   9955 	  size = 12;
   9956 	  if (PPC_HA (r2off) != 0)
   9957 	    {
   9958 	      size = 16;
   9959 	      bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
   9960 	      loc += 4;
   9961 	    }
   9962 	  bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
   9963 	  loc += 4;
   9964 	  off -= size - 4;
   9965 	}
   9966       bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
   9967 
   9968       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
   9969 	{
   9970 	  info->callbacks->einfo
   9971 	    (_("%P: long branch stub `%s' offset overflow\n"),
   9972 	     stub_entry->root.string);
   9973 	  htab->stub_error = TRUE;
   9974 	  return FALSE;
   9975 	}
   9976 
   9977       if (info->emitrelocations)
   9978 	{
   9979 	  r = get_relocs (stub_entry->stub_sec, 1);
   9980 	  if (r == NULL)
   9981 	    return FALSE;
   9982 	  r->r_offset = loc - stub_entry->stub_sec->contents;
   9983 	  r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
   9984 	  r->r_addend = dest;
   9985 	  if (stub_entry->h != NULL)
   9986 	    {
   9987 	      struct elf_link_hash_entry **hashes;
   9988 	      unsigned long symndx;
   9989 	      struct ppc_link_hash_entry *h;
   9990 
   9991 	      hashes = elf_sym_hashes (htab->stub_bfd);
   9992 	      if (hashes == NULL)
   9993 		{
   9994 		  bfd_size_type hsize;
   9995 
   9996 		  hsize = (htab->stub_globals + 1) * sizeof (*hashes);
   9997 		  hashes = bfd_zalloc (htab->stub_bfd, hsize);
   9998 		  if (hashes == NULL)
   9999 		    return FALSE;
   10000 		  elf_sym_hashes (htab->stub_bfd) = hashes;
   10001 		  htab->stub_globals = 1;
   10002 		}
   10003 	      symndx = htab->stub_globals++;
   10004 	      h = stub_entry->h;
   10005 	      hashes[symndx] = &h->elf;
   10006 	      r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
   10007 	      if (h->oh != NULL && h->oh->is_func)
   10008 		h = ppc_follow_link (h->oh);
   10009 	      if (h->elf.root.u.def.section != stub_entry->target_section)
   10010 		/* H is an opd symbol.  The addend must be zero.  */
   10011 		r->r_addend = 0;
   10012 	      else
   10013 		{
   10014 		  off = (h->elf.root.u.def.value
   10015 			 + h->elf.root.u.def.section->output_offset
   10016 			 + h->elf.root.u.def.section->output_section->vma);
   10017 		  r->r_addend -= off;
   10018 		}
   10019 	    }
   10020 	}
   10021       break;
   10022 
   10023     case ppc_stub_plt_branch:
   10024     case ppc_stub_plt_branch_r2off:
   10025       br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
   10026 					 stub_entry->root.string + 9,
   10027 					 FALSE, FALSE);
   10028       if (br_entry == NULL)
   10029 	{
   10030 	  info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
   10031 				  stub_entry->root.string);
   10032 	  htab->stub_error = TRUE;
   10033 	  return FALSE;
   10034 	}
   10035 
   10036       dest = (stub_entry->target_value
   10037 	      + stub_entry->target_section->output_offset
   10038 	      + stub_entry->target_section->output_section->vma);
   10039 
   10040       bfd_put_64 (htab->brlt->owner, dest,
   10041 		  htab->brlt->contents + br_entry->offset);
   10042 
   10043       if (br_entry->iter == htab->stub_iteration)
   10044 	{
   10045 	  br_entry->iter = 0;
   10046 
   10047 	  if (htab->relbrlt != NULL)
   10048 	    {
   10049 	      /* Create a reloc for the branch lookup table entry.  */
   10050 	      Elf_Internal_Rela rela;
   10051 	      bfd_byte *rl;
   10052 
   10053 	      rela.r_offset = (br_entry->offset
   10054 			       + htab->brlt->output_offset
   10055 			       + htab->brlt->output_section->vma);
   10056 	      rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   10057 	      rela.r_addend = dest;
   10058 
   10059 	      rl = htab->relbrlt->contents;
   10060 	      rl += (htab->relbrlt->reloc_count++
   10061 		     * sizeof (Elf64_External_Rela));
   10062 	      bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
   10063 	    }
   10064 	  else if (info->emitrelocations)
   10065 	    {
   10066 	      r = get_relocs (htab->brlt, 1);
   10067 	      if (r == NULL)
   10068 		return FALSE;
   10069 	      /* brlt, being SEC_LINKER_CREATED does not go through the
   10070 		 normal reloc processing.  Symbols and offsets are not
   10071 		 translated from input file to output file form, so
   10072 		 set up the offset per the output file.  */
   10073 	      r->r_offset = (br_entry->offset
   10074 			     + htab->brlt->output_offset
   10075 			     + htab->brlt->output_section->vma);
   10076 	      r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   10077 	      r->r_addend = dest;
   10078 	    }
   10079 	}
   10080 
   10081       dest = (br_entry->offset
   10082 	      + htab->brlt->output_offset
   10083 	      + htab->brlt->output_section->vma);
   10084 
   10085       off = (dest
   10086 	     - elf_gp (htab->brlt->output_section->owner)
   10087 	     - htab->stub_group[stub_entry->id_sec->id].toc_off);
   10088 
   10089       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
   10090 	{
   10091 	  info->callbacks->einfo
   10092 	    (_("%P: linkage table error against `%T'\n"),
   10093 	     stub_entry->root.string);
   10094 	  bfd_set_error (bfd_error_bad_value);
   10095 	  htab->stub_error = TRUE;
   10096 	  return FALSE;
   10097 	}
   10098 
   10099       if (info->emitrelocations)
   10100 	{
   10101 	  r = get_relocs (stub_entry->stub_sec, 1 + (PPC_HA (off) != 0));
   10102 	  if (r == NULL)
   10103 	    return FALSE;
   10104 	  r[0].r_offset = loc - stub_entry->stub_sec->contents;
   10105 	  if (bfd_big_endian (info->output_bfd))
   10106 	    r[0].r_offset += 2;
   10107 	  if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
   10108 	    r[0].r_offset += 4;
   10109 	  r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
   10110 	  r[0].r_addend = dest;
   10111 	  if (PPC_HA (off) != 0)
   10112 	    {
   10113 	      r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
   10114 	      r[1].r_offset = r[0].r_offset + 4;
   10115 	      r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
   10116 	      r[1].r_addend = r[0].r_addend;
   10117 	    }
   10118 	}
   10119 
   10120       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
   10121 	{
   10122 	  if (PPC_HA (off) != 0)
   10123 	    {
   10124 	      size = 16;
   10125 	      bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
   10126 	      loc += 4;
   10127 	      bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
   10128 	    }
   10129 	  else
   10130 	    {
   10131 	      size = 12;
   10132 	      bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
   10133 	    }
   10134 	}
   10135       else
   10136 	{
   10137 	  bfd_vma r2off = get_r2off (info, stub_entry);
   10138 
   10139 	  if (r2off == 0)
   10140 	    {
   10141 	      htab->stub_error = TRUE;
   10142 	      return FALSE;
   10143 	    }
   10144 
   10145 	  bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
   10146 	  loc += 4;
   10147 	  size = 20;
   10148 	  if (PPC_HA (off) != 0)
   10149 	    {
   10150 	      size += 4;
   10151 	      bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
   10152 	      loc += 4;
   10153 	      bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
   10154 	      loc += 4;
   10155 	    }
   10156 	  else
   10157 	    {
   10158 	      bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
   10159 	      loc += 4;
   10160 	    }
   10161 
   10162 	  if (PPC_HA (r2off) != 0)
   10163 	    {
   10164 	      size += 4;
   10165 	      bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
   10166 	      loc += 4;
   10167 	    }
   10168 	  bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
   10169 	}
   10170       loc += 4;
   10171       bfd_put_32 (htab->stub_bfd, MTCTR_R11, loc);
   10172       loc += 4;
   10173       bfd_put_32 (htab->stub_bfd, BCTR, loc);
   10174       break;
   10175 
   10176     case ppc_stub_plt_call:
   10177     case ppc_stub_plt_call_r2save:
   10178       if (stub_entry->h != NULL
   10179 	  && stub_entry->h->is_func_descriptor
   10180 	  && stub_entry->h->oh != NULL)
   10181 	{
   10182 	  struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
   10183 
   10184 	  /* If the old-ABI "dot-symbol" is undefined make it weak so
   10185 	     we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.
   10186 	     FIXME: We used to define the symbol on one of the call
   10187 	     stubs instead, which is why we test symbol section id
   10188 	     against htab->top_id in various places.  Likely all
   10189 	     these checks could now disappear.  */
   10190 	  if (fh->elf.root.type == bfd_link_hash_undefined)
   10191 	    fh->elf.root.type = bfd_link_hash_undefweak;
   10192 	  /* Stop undo_symbol_twiddle changing it back to undefined.  */
   10193 	  fh->was_undefined = 0;
   10194 	}
   10195 
   10196       /* Now build the stub.  */
   10197       dest = stub_entry->plt_ent->plt.offset & ~1;
   10198       if (dest >= (bfd_vma) -2)
   10199 	abort ();
   10200 
   10201       plt = htab->plt;
   10202       if (!htab->elf.dynamic_sections_created
   10203 	  || stub_entry->h == NULL
   10204 	  || stub_entry->h->elf.dynindx == -1)
   10205 	plt = htab->iplt;
   10206 
   10207       dest += plt->output_offset + plt->output_section->vma;
   10208 
   10209       if (stub_entry->h == NULL
   10210 	  && (stub_entry->plt_ent->plt.offset & 1) == 0)
   10211 	{
   10212 	  Elf_Internal_Rela rela;
   10213 	  bfd_byte *rl;
   10214 
   10215 	  rela.r_offset = dest;
   10216 	  rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
   10217 	  rela.r_addend = (stub_entry->target_value
   10218 			   + stub_entry->target_section->output_offset
   10219 			   + stub_entry->target_section->output_section->vma);
   10220 
   10221 	  rl = (htab->reliplt->contents
   10222 		+ (htab->reliplt->reloc_count++
   10223 		   * sizeof (Elf64_External_Rela)));
   10224 	  bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
   10225 	  stub_entry->plt_ent->plt.offset |= 1;
   10226 	}
   10227 
   10228       off = (dest
   10229 	     - elf_gp (plt->output_section->owner)
   10230 	     - htab->stub_group[stub_entry->id_sec->id].toc_off);
   10231 
   10232       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
   10233 	{
   10234 	  info->callbacks->einfo
   10235 	    (_("%P: linkage table error against `%T'\n"),
   10236 	     stub_entry->h != NULL
   10237 	     ? stub_entry->h->elf.root.root.string
   10238 	     : "<local sym>");
   10239 	  bfd_set_error (bfd_error_bad_value);
   10240 	  htab->stub_error = TRUE;
   10241 	  return FALSE;
   10242 	}
   10243 
   10244       if (htab->plt_stub_align != 0)
   10245 	{
   10246 	  unsigned pad = plt_stub_pad (htab, stub_entry, off);
   10247 
   10248 	  stub_entry->stub_sec->size += pad;
   10249 	  stub_entry->stub_offset = stub_entry->stub_sec->size;
   10250 	  loc += pad;
   10251 	}
   10252 
   10253       r = NULL;
   10254       if (info->emitrelocations)
   10255 	{
   10256 	  r = get_relocs (stub_entry->stub_sec,
   10257 			  (2
   10258 			   + (PPC_HA (off) != 0)
   10259 			   + (htab->plt_static_chain
   10260 			      && PPC_HA (off + 16) == PPC_HA (off))));
   10261 	  if (r == NULL)
   10262 	    return FALSE;
   10263 	  r[0].r_offset = loc - stub_entry->stub_sec->contents;
   10264 	  if (bfd_big_endian (info->output_bfd))
   10265 	    r[0].r_offset += 2;
   10266 	  r[0].r_addend = dest;
   10267 	}
   10268       if (stub_entry->h != NULL
   10269 	  && (stub_entry->h == htab->tls_get_addr_fd
   10270 	      || stub_entry->h == htab->tls_get_addr)
   10271 	  && !htab->no_tls_get_addr_opt)
   10272 	p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
   10273       else
   10274 	p = build_plt_stub (htab, stub_entry, loc, off, r);
   10275       size = p - loc;
   10276       break;
   10277 
   10278     default:
   10279       BFD_FAIL ();
   10280       return FALSE;
   10281     }
   10282 
   10283   stub_entry->stub_sec->size += size;
   10284 
   10285   if (htab->emit_stub_syms)
   10286     {
   10287       struct elf_link_hash_entry *h;
   10288       size_t len1, len2;
   10289       char *name;
   10290       const char *const stub_str[] = { "long_branch",
   10291 				       "long_branch_r2off",
   10292 				       "plt_branch",
   10293 				       "plt_branch_r2off",
   10294 				       "plt_call",
   10295 				       "plt_call" };
   10296 
   10297       len1 = strlen (stub_str[stub_entry->stub_type - 1]);
   10298       len2 = strlen (stub_entry->root.string);
   10299       name = bfd_malloc (len1 + len2 + 2);
   10300       if (name == NULL)
   10301 	return FALSE;
   10302       memcpy (name, stub_entry->root.string, 9);
   10303       memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
   10304       memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
   10305       h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
   10306       if (h == NULL)
   10307 	return FALSE;
   10308       if (h->root.type == bfd_link_hash_new)
   10309 	{
   10310 	  h->root.type = bfd_link_hash_defined;
   10311 	  h->root.u.def.section = stub_entry->stub_sec;
   10312 	  h->root.u.def.value = stub_entry->stub_offset;
   10313 	  h->ref_regular = 1;
   10314 	  h->def_regular = 1;
   10315 	  h->ref_regular_nonweak = 1;
   10316 	  h->forced_local = 1;
   10317 	  h->non_elf = 0;
   10318 	}
   10319     }
   10320 
   10321   return TRUE;
   10322 }
   10323 
   10324 /* As above, but don't actually build the stub.  Just bump offset so
   10325    we know stub section sizes, and select plt_branch stubs where
   10326    long_branch stubs won't do.  */
   10327 
   10328 static bfd_boolean
   10329 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
   10330 {
   10331   struct ppc_stub_hash_entry *stub_entry;
   10332   struct bfd_link_info *info;
   10333   struct ppc_link_hash_table *htab;
   10334   bfd_vma off;
   10335   int size;
   10336 
   10337   /* Massage our args to the form they really have.  */
   10338   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
   10339   info = in_arg;
   10340 
   10341   htab = ppc_hash_table (info);
   10342   if (htab == NULL)
   10343     return FALSE;
   10344 
   10345   if (stub_entry->stub_type == ppc_stub_plt_call
   10346       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   10347     {
   10348       asection *plt;
   10349       off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
   10350       if (off >= (bfd_vma) -2)
   10351 	abort ();
   10352       plt = htab->plt;
   10353       if (!htab->elf.dynamic_sections_created
   10354 	  || stub_entry->h == NULL
   10355 	  || stub_entry->h->elf.dynindx == -1)
   10356 	plt = htab->iplt;
   10357       off += (plt->output_offset
   10358 	      + plt->output_section->vma
   10359 	      - elf_gp (plt->output_section->owner)
   10360 	      - htab->stub_group[stub_entry->id_sec->id].toc_off);
   10361 
   10362       size = plt_stub_size (htab, stub_entry, off);
   10363       if (htab->plt_stub_align)
   10364 	size += plt_stub_pad (htab, stub_entry, off);
   10365       if (info->emitrelocations)
   10366 	{
   10367 	  stub_entry->stub_sec->reloc_count
   10368 	    += (2
   10369 		+ (PPC_HA (off) != 0)
   10370 		+ (htab->plt_static_chain
   10371 		   && PPC_HA (off + 16) == PPC_HA (off)));
   10372 	  stub_entry->stub_sec->flags |= SEC_RELOC;
   10373 	}
   10374     }
   10375   else
   10376     {
   10377       /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
   10378 	 variants.  */
   10379       bfd_vma r2off = 0;
   10380 
   10381       off = (stub_entry->target_value
   10382 	     + stub_entry->target_section->output_offset
   10383 	     + stub_entry->target_section->output_section->vma);
   10384       off -= (stub_entry->stub_sec->size
   10385 	      + stub_entry->stub_sec->output_offset
   10386 	      + stub_entry->stub_sec->output_section->vma);
   10387 
   10388       /* Reset the stub type from the plt variant in case we now
   10389 	 can reach with a shorter stub.  */
   10390       if (stub_entry->stub_type >= ppc_stub_plt_branch)
   10391 	stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
   10392 
   10393       size = 4;
   10394       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
   10395 	{
   10396 	  r2off = get_r2off (info, stub_entry);
   10397 	  if (r2off == 0)
   10398 	    {
   10399 	      htab->stub_error = TRUE;
   10400 	      return FALSE;
   10401 	    }
   10402 	  size = 12;
   10403 	  if (PPC_HA (r2off) != 0)
   10404 	    size = 16;
   10405 	  off -= size - 4;
   10406 	}
   10407 
   10408       /* If the branch offset if too big, use a ppc_stub_plt_branch.  */
   10409       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
   10410 	{
   10411 	  struct ppc_branch_hash_entry *br_entry;
   10412 
   10413 	  br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
   10414 					     stub_entry->root.string + 9,
   10415 					     TRUE, FALSE);
   10416 	  if (br_entry == NULL)
   10417 	    {
   10418 	      info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
   10419 				      stub_entry->root.string);
   10420 	      htab->stub_error = TRUE;
   10421 	      return FALSE;
   10422 	    }
   10423 
   10424 	  if (br_entry->iter != htab->stub_iteration)
   10425 	    {
   10426 	      br_entry->iter = htab->stub_iteration;
   10427 	      br_entry->offset = htab->brlt->size;
   10428 	      htab->brlt->size += 8;
   10429 
   10430 	      if (htab->relbrlt != NULL)
   10431 		htab->relbrlt->size += sizeof (Elf64_External_Rela);
   10432 	      else if (info->emitrelocations)
   10433 		{
   10434 		  htab->brlt->reloc_count += 1;
   10435 		  htab->brlt->flags |= SEC_RELOC;
   10436 		}
   10437 	    }
   10438 
   10439 	  stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
   10440 	  off = (br_entry->offset
   10441 		 + htab->brlt->output_offset
   10442 		 + htab->brlt->output_section->vma
   10443 		 - elf_gp (htab->brlt->output_section->owner)
   10444 		 - htab->stub_group[stub_entry->id_sec->id].toc_off);
   10445 
   10446 	  if (info->emitrelocations)
   10447 	    {
   10448 	      stub_entry->stub_sec->reloc_count += 1 + (PPC_HA (off) != 0);
   10449 	      stub_entry->stub_sec->flags |= SEC_RELOC;
   10450 	    }
   10451 
   10452 	  if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
   10453 	    {
   10454 	      size = 12;
   10455 	      if (PPC_HA (off) != 0)
   10456 		size = 16;
   10457 	    }
   10458 	  else
   10459 	    {
   10460 	      size = 20;
   10461 	      if (PPC_HA (off) != 0)
   10462 		size += 4;
   10463 
   10464 	      if (PPC_HA (r2off) != 0)
   10465 		size += 4;
   10466 	    }
   10467 	}
   10468       else if (info->emitrelocations)
   10469 	{
   10470 	  stub_entry->stub_sec->reloc_count += 1;
   10471 	  stub_entry->stub_sec->flags |= SEC_RELOC;
   10472 	}
   10473     }
   10474 
   10475   stub_entry->stub_sec->size += size;
   10476   return TRUE;
   10477 }
   10478 
   10479 /* Set up various things so that we can make a list of input sections
   10480    for each output section included in the link.  Returns -1 on error,
   10481    0 when no stubs will be needed, and 1 on success.  */
   10482 
   10483 int
   10484 ppc64_elf_setup_section_lists
   10485   (struct bfd_link_info *info,
   10486    asection *(*add_stub_section) (const char *, asection *),
   10487    void (*layout_sections_again) (void))
   10488 {
   10489   bfd *input_bfd;
   10490   int top_id, top_index, id;
   10491   asection *section;
   10492   asection **input_list;
   10493   bfd_size_type amt;
   10494   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   10495 
   10496   if (htab == NULL)
   10497     return -1;
   10498   /* Stash our params away.  */
   10499   htab->add_stub_section = add_stub_section;
   10500   htab->layout_sections_again = layout_sections_again;
   10501 
   10502   if (htab->brlt == NULL)
   10503     return 0;
   10504 
   10505   /* Find the top input section id.  */
   10506   for (input_bfd = info->input_bfds, top_id = 3;
   10507        input_bfd != NULL;
   10508        input_bfd = input_bfd->link_next)
   10509     {
   10510       for (section = input_bfd->sections;
   10511 	   section != NULL;
   10512 	   section = section->next)
   10513 	{
   10514 	  if (top_id < section->id)
   10515 	    top_id = section->id;
   10516 	}
   10517     }
   10518 
   10519   htab->top_id = top_id;
   10520   amt = sizeof (struct map_stub) * (top_id + 1);
   10521   htab->stub_group = bfd_zmalloc (amt);
   10522   if (htab->stub_group == NULL)
   10523     return -1;
   10524 
   10525   /* Set toc_off for com, und, abs and ind sections.  */
   10526   for (id = 0; id < 3; id++)
   10527     htab->stub_group[id].toc_off = TOC_BASE_OFF;
   10528 
   10529   /* We can't use output_bfd->section_count here to find the top output
   10530      section index as some sections may have been removed, and
   10531      strip_excluded_output_sections doesn't renumber the indices.  */
   10532   for (section = info->output_bfd->sections, top_index = 0;
   10533        section != NULL;
   10534        section = section->next)
   10535     {
   10536       if (top_index < section->index)
   10537 	top_index = section->index;
   10538     }
   10539 
   10540   htab->top_index = top_index;
   10541   amt = sizeof (asection *) * (top_index + 1);
   10542   input_list = bfd_zmalloc (amt);
   10543   htab->input_list = input_list;
   10544   if (input_list == NULL)
   10545     return -1;
   10546 
   10547   return 1;
   10548 }
   10549 
   10550 /* Set up for first pass at multitoc partitioning.  */
   10551 
   10552 void
   10553 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
   10554 {
   10555   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   10556 
   10557   elf_gp (info->output_bfd) = ppc64_elf_toc (info->output_bfd);
   10558   htab->toc_curr = elf_gp (info->output_bfd);
   10559   htab->toc_bfd = NULL;
   10560   htab->toc_first_sec = NULL;
   10561 }
   10562 
   10563 /* The linker repeatedly calls this function for each TOC input section
   10564    and linker generated GOT section.  Group input bfds such that the toc
   10565    within a group is less than 64k in size.  */
   10566 
   10567 bfd_boolean
   10568 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
   10569 {
   10570   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   10571   bfd_vma addr, off, limit;
   10572 
   10573   if (htab == NULL)
   10574     return FALSE;
   10575 
   10576   if (!htab->second_toc_pass)
   10577     {
   10578       /* Keep track of the first .toc or .got section for this input bfd.  */
   10579       bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
   10580 
   10581       if (new_bfd)
   10582 	{
   10583 	  htab->toc_bfd = isec->owner;
   10584 	  htab->toc_first_sec = isec;
   10585 	}
   10586 
   10587       addr = isec->output_offset + isec->output_section->vma;
   10588       off = addr - htab->toc_curr;
   10589       limit = 0x80008000;
   10590       if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
   10591 	limit = 0x10000;
   10592       if (off + isec->size > limit)
   10593 	{
   10594 	  addr = (htab->toc_first_sec->output_offset
   10595 		  + htab->toc_first_sec->output_section->vma);
   10596 	  htab->toc_curr = addr;
   10597 	}
   10598 
   10599       /* toc_curr is the base address of this toc group.  Set elf_gp
   10600 	 for the input section to be the offset relative to the
   10601 	 output toc base plus 0x8000.  Making the input elf_gp an
   10602 	 offset allows us to move the toc as a whole without
   10603 	 recalculating input elf_gp.  */
   10604       off = htab->toc_curr - elf_gp (isec->output_section->owner);
   10605       off += TOC_BASE_OFF;
   10606 
   10607       /* Die if someone uses a linker script that doesn't keep input
   10608 	 file .toc and .got together.  */
   10609       if (new_bfd
   10610 	  && elf_gp (isec->owner) != 0
   10611 	  && elf_gp (isec->owner) != off)
   10612 	return FALSE;
   10613 
   10614       elf_gp (isec->owner) = off;
   10615       return TRUE;
   10616     }
   10617 
   10618   /* During the second pass toc_first_sec points to the start of
   10619      a toc group, and toc_curr is used to track the old elf_gp.
   10620      We use toc_bfd to ensure we only look at each bfd once.  */
   10621   if (htab->toc_bfd == isec->owner)
   10622     return TRUE;
   10623   htab->toc_bfd = isec->owner;
   10624 
   10625   if (htab->toc_first_sec == NULL
   10626       || htab->toc_curr != elf_gp (isec->owner))
   10627     {
   10628       htab->toc_curr = elf_gp (isec->owner);
   10629       htab->toc_first_sec = isec;
   10630     }
   10631   addr = (htab->toc_first_sec->output_offset
   10632 	  + htab->toc_first_sec->output_section->vma);
   10633   off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
   10634   elf_gp (isec->owner) = off;
   10635 
   10636   return TRUE;
   10637 }
   10638 
   10639 /* Called via elf_link_hash_traverse to merge GOT entries for global
   10640    symbol H.  */
   10641 
   10642 static bfd_boolean
   10643 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
   10644 {
   10645   if (h->root.type == bfd_link_hash_indirect)
   10646     return TRUE;
   10647 
   10648   merge_got_entries (&h->got.glist);
   10649 
   10650   return TRUE;
   10651 }
   10652 
   10653 /* Called via elf_link_hash_traverse to allocate GOT entries for global
   10654    symbol H.  */
   10655 
   10656 static bfd_boolean
   10657 reallocate_got (struct elf_link_hash_entry *h, void *inf)
   10658 {
   10659   struct got_entry *gent;
   10660 
   10661   if (h->root.type == bfd_link_hash_indirect)
   10662     return TRUE;
   10663 
   10664   for (gent = h->got.glist; gent != NULL; gent = gent->next)
   10665     if (!gent->is_indirect)
   10666       allocate_got (h, (struct bfd_link_info *) inf, gent);
   10667   return TRUE;
   10668 }
   10669 
   10670 /* Called on the first multitoc pass after the last call to
   10671    ppc64_elf_next_toc_section.  This function removes duplicate GOT
   10672    entries.  */
   10673 
   10674 bfd_boolean
   10675 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
   10676 {
   10677   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   10678   struct bfd *ibfd, *ibfd2;
   10679   bfd_boolean done_something;
   10680 
   10681   htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
   10682 
   10683   if (!htab->do_multi_toc)
   10684     return FALSE;
   10685 
   10686   /* Merge global sym got entries within a toc group.  */
   10687   elf_link_hash_traverse (&htab->elf, merge_global_got, info);
   10688 
   10689   /* And tlsld_got.  */
   10690   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   10691     {
   10692       struct got_entry *ent, *ent2;
   10693 
   10694       if (!is_ppc64_elf (ibfd))
   10695 	continue;
   10696 
   10697       ent = ppc64_tlsld_got (ibfd);
   10698       if (!ent->is_indirect
   10699 	  && ent->got.offset != (bfd_vma) -1)
   10700 	{
   10701 	  for (ibfd2 = ibfd->link_next; ibfd2 != NULL; ibfd2 = ibfd2->link_next)
   10702 	    {
   10703 	      if (!is_ppc64_elf (ibfd2))
   10704 		continue;
   10705 
   10706 	      ent2 = ppc64_tlsld_got (ibfd2);
   10707 	      if (!ent2->is_indirect
   10708 		  && ent2->got.offset != (bfd_vma) -1
   10709 		  && elf_gp (ibfd2) == elf_gp (ibfd))
   10710 		{
   10711 		  ent2->is_indirect = TRUE;
   10712 		  ent2->got.ent = ent;
   10713 		}
   10714 	    }
   10715 	}
   10716     }
   10717 
   10718   /* Zap sizes of got sections.  */
   10719   htab->reliplt->rawsize = htab->reliplt->size;
   10720   htab->reliplt->size -= htab->got_reli_size;
   10721   htab->got_reli_size = 0;
   10722 
   10723   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   10724     {
   10725       asection *got, *relgot;
   10726 
   10727       if (!is_ppc64_elf (ibfd))
   10728 	continue;
   10729 
   10730       got = ppc64_elf_tdata (ibfd)->got;
   10731       if (got != NULL)
   10732 	{
   10733 	  got->rawsize = got->size;
   10734 	  got->size = 0;
   10735 	  relgot = ppc64_elf_tdata (ibfd)->relgot;
   10736 	  relgot->rawsize = relgot->size;
   10737 	  relgot->size = 0;
   10738 	}
   10739     }
   10740 
   10741   /* Now reallocate the got, local syms first.  We don't need to
   10742      allocate section contents again since we never increase size.  */
   10743   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   10744     {
   10745       struct got_entry **lgot_ents;
   10746       struct got_entry **end_lgot_ents;
   10747       struct plt_entry **local_plt;
   10748       struct plt_entry **end_local_plt;
   10749       unsigned char *lgot_masks;
   10750       bfd_size_type locsymcount;
   10751       Elf_Internal_Shdr *symtab_hdr;
   10752       asection *s, *srel;
   10753 
   10754       if (!is_ppc64_elf (ibfd))
   10755 	continue;
   10756 
   10757       lgot_ents = elf_local_got_ents (ibfd);
   10758       if (!lgot_ents)
   10759 	continue;
   10760 
   10761       symtab_hdr = &elf_symtab_hdr (ibfd);
   10762       locsymcount = symtab_hdr->sh_info;
   10763       end_lgot_ents = lgot_ents + locsymcount;
   10764       local_plt = (struct plt_entry **) end_lgot_ents;
   10765       end_local_plt = local_plt + locsymcount;
   10766       lgot_masks = (unsigned char *) end_local_plt;
   10767       s = ppc64_elf_tdata (ibfd)->got;
   10768       srel = ppc64_elf_tdata (ibfd)->relgot;
   10769       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
   10770 	{
   10771 	  struct got_entry *ent;
   10772 
   10773 	  for (ent = *lgot_ents; ent != NULL; ent = ent->next)
   10774 	    {
   10775 	      unsigned int num = 1;
   10776 	      ent->got.offset = s->size;
   10777 	      if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
   10778 		num = 2;
   10779 	      s->size += num * 8;
   10780 	      if (info->shared)
   10781 		srel->size += num * sizeof (Elf64_External_Rela);
   10782 	      else if ((*lgot_masks & PLT_IFUNC) != 0)
   10783 		{
   10784 		  htab->reliplt->size
   10785 		    += num * sizeof (Elf64_External_Rela);
   10786 		  htab->got_reli_size
   10787 		    += num * sizeof (Elf64_External_Rela);
   10788 		}
   10789 	    }
   10790 	}
   10791     }
   10792 
   10793   elf_link_hash_traverse (&htab->elf, reallocate_got, info);
   10794 
   10795   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   10796     {
   10797       struct got_entry *ent;
   10798 
   10799       if (!is_ppc64_elf (ibfd))
   10800 	continue;
   10801 
   10802       ent = ppc64_tlsld_got (ibfd);
   10803       if (!ent->is_indirect
   10804 	  && ent->got.offset != (bfd_vma) -1)
   10805 	{
   10806 	  asection *s = ppc64_elf_tdata (ibfd)->got;
   10807 	  ent->got.offset = s->size;
   10808 	  s->size += 16;
   10809 	  if (info->shared)
   10810 	    {
   10811 	      asection *srel = ppc64_elf_tdata (ibfd)->relgot;
   10812 	      srel->size += sizeof (Elf64_External_Rela);
   10813 	    }
   10814 	}
   10815     }
   10816 
   10817   done_something = htab->reliplt->rawsize != htab->reliplt->size;
   10818   if (!done_something)
   10819     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   10820       {
   10821 	asection *got;
   10822 
   10823 	if (!is_ppc64_elf (ibfd))
   10824 	  continue;
   10825 
   10826 	got = ppc64_elf_tdata (ibfd)->got;
   10827 	if (got != NULL)
   10828 	  {
   10829 	    done_something = got->rawsize != got->size;
   10830 	    if (done_something)
   10831 	      break;
   10832 	  }
   10833       }
   10834 
   10835   if (done_something)
   10836     (*htab->layout_sections_again) ();
   10837 
   10838   /* Set up for second pass over toc sections to recalculate elf_gp
   10839      on input sections.  */
   10840   htab->toc_bfd = NULL;
   10841   htab->toc_first_sec = NULL;
   10842   htab->second_toc_pass = TRUE;
   10843   return done_something;
   10844 }
   10845 
   10846 /* Called after second pass of multitoc partitioning.  */
   10847 
   10848 void
   10849 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
   10850 {
   10851   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   10852 
   10853   /* After the second pass, toc_curr tracks the TOC offset used
   10854      for code sections below in ppc64_elf_next_input_section.  */
   10855   htab->toc_curr = TOC_BASE_OFF;
   10856 }
   10857 
   10858 /* No toc references were found in ISEC.  If the code in ISEC makes no
   10859    calls, then there's no need to use toc adjusting stubs when branching
   10860    into ISEC.  Actually, indirect calls from ISEC are OK as they will
   10861    load r2.  Returns -1 on error, 0 for no stub needed, 1 for stub
   10862    needed, and 2 if a cyclical call-graph was found but no other reason
   10863    for a stub was detected.  If called from the top level, a return of
   10864    2 means the same as a return of 0.  */
   10865 
   10866 static int
   10867 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
   10868 {
   10869   int ret;
   10870 
   10871   /* Mark this section as checked.  */
   10872   isec->call_check_done = 1;
   10873 
   10874   /* We know none of our code bearing sections will need toc stubs.  */
   10875   if ((isec->flags & SEC_LINKER_CREATED) != 0)
   10876     return 0;
   10877 
   10878   if (isec->size == 0)
   10879     return 0;
   10880 
   10881   if (isec->output_section == NULL)
   10882     return 0;
   10883 
   10884   ret = 0;
   10885   if (isec->reloc_count != 0)
   10886     {
   10887       Elf_Internal_Rela *relstart, *rel;
   10888       Elf_Internal_Sym *local_syms;
   10889       struct ppc_link_hash_table *htab;
   10890 
   10891       relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
   10892 					    info->keep_memory);
   10893       if (relstart == NULL)
   10894 	return -1;
   10895 
   10896       /* Look for branches to outside of this section.  */
   10897       local_syms = NULL;
   10898       htab = ppc_hash_table (info);
   10899       if (htab == NULL)
   10900 	return -1;
   10901 
   10902       for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
   10903 	{
   10904 	  enum elf_ppc64_reloc_type r_type;
   10905 	  unsigned long r_symndx;
   10906 	  struct elf_link_hash_entry *h;
   10907 	  struct ppc_link_hash_entry *eh;
   10908 	  Elf_Internal_Sym *sym;
   10909 	  asection *sym_sec;
   10910 	  struct _opd_sec_data *opd;
   10911 	  bfd_vma sym_value;
   10912 	  bfd_vma dest;
   10913 
   10914 	  r_type = ELF64_R_TYPE (rel->r_info);
   10915 	  if (r_type != R_PPC64_REL24
   10916 	      && r_type != R_PPC64_REL14
   10917 	      && r_type != R_PPC64_REL14_BRTAKEN
   10918 	      && r_type != R_PPC64_REL14_BRNTAKEN)
   10919 	    continue;
   10920 
   10921 	  r_symndx = ELF64_R_SYM (rel->r_info);
   10922 	  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
   10923 			  isec->owner))
   10924 	    {
   10925 	      ret = -1;
   10926 	      break;
   10927 	    }
   10928 
   10929 	  /* Calls to dynamic lib functions go through a plt call stub
   10930 	     that uses r2.  */
   10931 	  eh = (struct ppc_link_hash_entry *) h;
   10932 	  if (eh != NULL
   10933 	      && (eh->elf.plt.plist != NULL
   10934 		  || (eh->oh != NULL
   10935 		      && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
   10936 	    {
   10937 	      ret = 1;
   10938 	      break;
   10939 	    }
   10940 
   10941 	  if (sym_sec == NULL)
   10942 	    /* Ignore other undefined symbols.  */
   10943 	    continue;
   10944 
   10945 	  /* Assume branches to other sections not included in the
   10946 	     link need stubs too, to cover -R and absolute syms.  */
   10947 	  if (sym_sec->output_section == NULL)
   10948 	    {
   10949 	      ret = 1;
   10950 	      break;
   10951 	    }
   10952 
   10953 	  if (h == NULL)
   10954 	    sym_value = sym->st_value;
   10955 	  else
   10956 	    {
   10957 	      if (h->root.type != bfd_link_hash_defined
   10958 		  && h->root.type != bfd_link_hash_defweak)
   10959 		abort ();
   10960 	      sym_value = h->root.u.def.value;
   10961 	    }
   10962 	  sym_value += rel->r_addend;
   10963 
   10964 	  /* If this branch reloc uses an opd sym, find the code section.  */
   10965 	  opd = get_opd_info (sym_sec);
   10966 	  if (opd != NULL)
   10967 	    {
   10968 	      if (h == NULL && opd->adjust != NULL)
   10969 		{
   10970 		  long adjust;
   10971 
   10972 		  adjust = opd->adjust[sym->st_value / 8];
   10973 		  if (adjust == -1)
   10974 		    /* Assume deleted functions won't ever be called.  */
   10975 		    continue;
   10976 		  sym_value += adjust;
   10977 		}
   10978 
   10979 	      dest = opd_entry_value (sym_sec, sym_value,
   10980 				      &sym_sec, NULL, FALSE);
   10981 	      if (dest == (bfd_vma) -1)
   10982 		continue;
   10983 	    }
   10984 	  else
   10985 	    dest = (sym_value
   10986 		    + sym_sec->output_offset
   10987 		    + sym_sec->output_section->vma);
   10988 
   10989 	  /* Ignore branch to self.  */
   10990 	  if (sym_sec == isec)
   10991 	    continue;
   10992 
   10993 	  /* If the called function uses the toc, we need a stub.  */
   10994 	  if (sym_sec->has_toc_reloc
   10995 	      || sym_sec->makes_toc_func_call)
   10996 	    {
   10997 	      ret = 1;
   10998 	      break;
   10999 	    }
   11000 
   11001 	  /* Assume any branch that needs a long branch stub might in fact
   11002 	     need a plt_branch stub.  A plt_branch stub uses r2.  */
   11003 	  else if (dest - (isec->output_offset
   11004 			   + isec->output_section->vma
   11005 			   + rel->r_offset) + (1 << 25) >= (2 << 25))
   11006 	    {
   11007 	      ret = 1;
   11008 	      break;
   11009 	    }
   11010 
   11011 	  /* If calling back to a section in the process of being
   11012 	     tested, we can't say for sure that no toc adjusting stubs
   11013 	     are needed, so don't return zero.  */
   11014 	  else if (sym_sec->call_check_in_progress)
   11015 	    ret = 2;
   11016 
   11017 	  /* Branches to another section that itself doesn't have any TOC
   11018 	     references are OK.  Recursively call ourselves to check.  */
   11019 	  else if (!sym_sec->call_check_done)
   11020 	    {
   11021 	      int recur;
   11022 
   11023 	      /* Mark current section as indeterminate, so that other
   11024 		 sections that call back to current won't be marked as
   11025 		 known.  */
   11026 	      isec->call_check_in_progress = 1;
   11027 	      recur = toc_adjusting_stub_needed (info, sym_sec);
   11028 	      isec->call_check_in_progress = 0;
   11029 
   11030 	      if (recur != 0)
   11031 		{
   11032 		  ret = recur;
   11033 		  if (recur != 2)
   11034 		    break;
   11035 		}
   11036 	    }
   11037 	}
   11038 
   11039       if (local_syms != NULL
   11040 	  && (elf_symtab_hdr (isec->owner).contents
   11041 	      != (unsigned char *) local_syms))
   11042 	free (local_syms);
   11043       if (elf_section_data (isec)->relocs != relstart)
   11044 	free (relstart);
   11045     }
   11046 
   11047   if ((ret & 1) == 0
   11048       && isec->map_head.s != NULL
   11049       && (strcmp (isec->output_section->name, ".init") == 0
   11050 	  || strcmp (isec->output_section->name, ".fini") == 0))
   11051     {
   11052       if (isec->map_head.s->has_toc_reloc
   11053 	  || isec->map_head.s->makes_toc_func_call)
   11054 	ret = 1;
   11055       else if (!isec->map_head.s->call_check_done)
   11056 	{
   11057 	  int recur;
   11058 	  isec->call_check_in_progress = 1;
   11059 	  recur = toc_adjusting_stub_needed (info, isec->map_head.s);
   11060 	  isec->call_check_in_progress = 0;
   11061 	  if (recur != 0)
   11062 	    ret = recur;
   11063 	}
   11064     }
   11065 
   11066   if (ret == 1)
   11067     isec->makes_toc_func_call = 1;
   11068 
   11069   return ret;
   11070 }
   11071 
   11072 /* The linker repeatedly calls this function for each input section,
   11073    in the order that input sections are linked into output sections.
   11074    Build lists of input sections to determine groupings between which
   11075    we may insert linker stubs.  */
   11076 
   11077 bfd_boolean
   11078 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
   11079 {
   11080   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   11081 
   11082   if (htab == NULL)
   11083     return FALSE;
   11084 
   11085   if ((isec->output_section->flags & SEC_CODE) != 0
   11086       && isec->output_section->index <= htab->top_index)
   11087     {
   11088       asection **list = htab->input_list + isec->output_section->index;
   11089       /* Steal the link_sec pointer for our list.  */
   11090 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
   11091       /* This happens to make the list in reverse order,
   11092 	 which is what we want.  */
   11093       PREV_SEC (isec) = *list;
   11094       *list = isec;
   11095     }
   11096 
   11097   if (htab->multi_toc_needed)
   11098     {
   11099       /* If a code section has a function that uses the TOC then we need
   11100 	 to use the right TOC (obviously).  Also, make sure that .opd gets
   11101 	 the correct TOC value for R_PPC64_TOC relocs that don't have or
   11102 	 can't find their function symbol (shouldn't ever happen now).
   11103 	 Also specially treat .fixup for the linux kernel.  .fixup
   11104 	 contains branches, but only back to the function that hit an
   11105 	 exception.  */
   11106       if (isec->has_toc_reloc
   11107 	  || (isec->flags & SEC_CODE) == 0
   11108 	  || strcmp (isec->name, ".fixup") == 0)
   11109 	{
   11110 	  if (elf_gp (isec->owner) != 0)
   11111 	    htab->toc_curr = elf_gp (isec->owner);
   11112 	}
   11113       else
   11114 	{
   11115 	  if (!isec->call_check_done
   11116 	      && toc_adjusting_stub_needed (info, isec) < 0)
   11117 	    return FALSE;
   11118 	  /* If we make a local call from this section, ie. a branch
   11119 	     without a following nop, then we have no place to put a
   11120 	     toc restoring insn.  We must use the same toc group as
   11121 	     the callee.
   11122 	     Testing makes_toc_func_call actually tests for *any*
   11123 	     calls to functions that need a good toc pointer.  A more
   11124 	     precise test would be better, as this one will set
   11125 	     incorrect values for pasted .init/.fini fragments.
   11126 	     (Fixed later in check_pasted_section.)  */
   11127 	  if (isec->makes_toc_func_call
   11128 	      && elf_gp (isec->owner) != 0)
   11129 	    htab->toc_curr = elf_gp (isec->owner);
   11130 	}
   11131     }
   11132 
   11133   /* Functions that don't use the TOC can belong in any TOC group.
   11134      Use the last TOC base.  */
   11135   htab->stub_group[isec->id].toc_off = htab->toc_curr;
   11136   return TRUE;
   11137 }
   11138 
   11139 /* Check that all .init and .fini sections use the same toc, if they
   11140    have toc relocs.  */
   11141 
   11142 static bfd_boolean
   11143 check_pasted_section (struct bfd_link_info *info, const char *name)
   11144 {
   11145   asection *o = bfd_get_section_by_name (info->output_bfd, name);
   11146 
   11147   if (o != NULL)
   11148     {
   11149       struct ppc_link_hash_table *htab = ppc_hash_table (info);
   11150       bfd_vma toc_off = 0;
   11151       asection *i;
   11152 
   11153       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   11154 	if (i->has_toc_reloc)
   11155 	  {
   11156 	    if (toc_off == 0)
   11157 	      toc_off = htab->stub_group[i->id].toc_off;
   11158 	    else if (toc_off != htab->stub_group[i->id].toc_off)
   11159 	      return FALSE;
   11160 	  }
   11161 
   11162       if (toc_off == 0)
   11163 	for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   11164 	  if (i->makes_toc_func_call)
   11165 	    {
   11166 	      toc_off = htab->stub_group[i->id].toc_off;
   11167 	      break;
   11168 	    }
   11169 
   11170       /* Make sure the whole pasted function uses the same toc offset.  */
   11171       if (toc_off != 0)
   11172 	for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   11173 	  htab->stub_group[i->id].toc_off = toc_off;
   11174     }
   11175   return TRUE;
   11176 }
   11177 
   11178 bfd_boolean
   11179 ppc64_elf_check_init_fini (struct bfd_link_info *info)
   11180 {
   11181   return (check_pasted_section (info, ".init")
   11182 	  & check_pasted_section (info, ".fini"));
   11183 }
   11184 
   11185 /* See whether we can group stub sections together.  Grouping stub
   11186    sections may result in fewer stubs.  More importantly, we need to
   11187    put all .init* and .fini* stubs at the beginning of the .init or
   11188    .fini output sections respectively, because glibc splits the
   11189    _init and _fini functions into multiple parts.  Putting a stub in
   11190    the middle of a function is not a good idea.  */
   11191 
   11192 static void
   11193 group_sections (struct ppc_link_hash_table *htab,
   11194 		bfd_size_type stub_group_size,
   11195 		bfd_boolean stubs_always_before_branch)
   11196 {
   11197   asection **list;
   11198   bfd_size_type stub14_group_size;
   11199   bfd_boolean suppress_size_errors;
   11200 
   11201   suppress_size_errors = FALSE;
   11202   stub14_group_size = stub_group_size;
   11203   if (stub_group_size == 1)
   11204     {
   11205       /* Default values.  */
   11206       if (stubs_always_before_branch)
   11207 	{
   11208 	  stub_group_size = 0x1e00000;
   11209 	  stub14_group_size = 0x7800;
   11210 	}
   11211       else
   11212 	{
   11213 	  stub_group_size = 0x1c00000;
   11214 	  stub14_group_size = 0x7000;
   11215 	}
   11216       suppress_size_errors = TRUE;
   11217     }
   11218 
   11219   list = htab->input_list + htab->top_index;
   11220   do
   11221     {
   11222       asection *tail = *list;
   11223       while (tail != NULL)
   11224 	{
   11225 	  asection *curr;
   11226 	  asection *prev;
   11227 	  bfd_size_type total;
   11228 	  bfd_boolean big_sec;
   11229 	  bfd_vma curr_toc;
   11230 
   11231 	  curr = tail;
   11232 	  total = tail->size;
   11233 	  big_sec = total > (ppc64_elf_section_data (tail) != NULL
   11234 			     && ppc64_elf_section_data (tail)->has_14bit_branch
   11235 			     ? stub14_group_size : stub_group_size);
   11236 	  if (big_sec && !suppress_size_errors)
   11237 	    (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
   11238 				     tail->owner, tail);
   11239 	  curr_toc = htab->stub_group[tail->id].toc_off;
   11240 
   11241 	  while ((prev = PREV_SEC (curr)) != NULL
   11242 		 && ((total += curr->output_offset - prev->output_offset)
   11243 		     < (ppc64_elf_section_data (prev) != NULL
   11244 			&& ppc64_elf_section_data (prev)->has_14bit_branch
   11245 			? stub14_group_size : stub_group_size))
   11246 		 && htab->stub_group[prev->id].toc_off == curr_toc)
   11247 	    curr = prev;
   11248 
   11249 	  /* OK, the size from the start of CURR to the end is less
   11250 	     than stub_group_size and thus can be handled by one stub
   11251 	     section.  (or the tail section is itself larger than
   11252 	     stub_group_size, in which case we may be toast.)  We
   11253 	     should really be keeping track of the total size of stubs
   11254 	     added here, as stubs contribute to the final output
   11255 	     section size.  That's a little tricky, and this way will
   11256 	     only break if stubs added make the total size more than
   11257 	     2^25, ie. for the default stub_group_size, if stubs total
   11258 	     more than 2097152 bytes, or nearly 75000 plt call stubs.  */
   11259 	  do
   11260 	    {
   11261 	      prev = PREV_SEC (tail);
   11262 	      /* Set up this stub group.  */
   11263 	      htab->stub_group[tail->id].link_sec = curr;
   11264 	    }
   11265 	  while (tail != curr && (tail = prev) != NULL);
   11266 
   11267 	  /* But wait, there's more!  Input sections up to stub_group_size
   11268 	     bytes before the stub section can be handled by it too.
   11269 	     Don't do this if we have a really large section after the
   11270 	     stubs, as adding more stubs increases the chance that
   11271 	     branches may not reach into the stub section.  */
   11272 	  if (!stubs_always_before_branch && !big_sec)
   11273 	    {
   11274 	      total = 0;
   11275 	      while (prev != NULL
   11276 		     && ((total += tail->output_offset - prev->output_offset)
   11277 			 < (ppc64_elf_section_data (prev) != NULL
   11278 			    && ppc64_elf_section_data (prev)->has_14bit_branch
   11279 			    ? stub14_group_size : stub_group_size))
   11280 		     && htab->stub_group[prev->id].toc_off == curr_toc)
   11281 		{
   11282 		  tail = prev;
   11283 		  prev = PREV_SEC (tail);
   11284 		  htab->stub_group[tail->id].link_sec = curr;
   11285 		}
   11286 	    }
   11287 	  tail = prev;
   11288 	}
   11289     }
   11290   while (list-- != htab->input_list);
   11291   free (htab->input_list);
   11292 #undef PREV_SEC
   11293 }
   11294 
   11295 static const unsigned char glink_eh_frame_cie[] =
   11296 {
   11297   0, 0, 0, 16,				/* length.  */
   11298   0, 0, 0, 0,				/* id.  */
   11299   1,					/* CIE version.  */
   11300   'z', 'R', 0,				/* Augmentation string.  */
   11301   4,					/* Code alignment.  */
   11302   0x78,					/* Data alignment.  */
   11303   65,					/* RA reg.  */
   11304   1,					/* Augmentation size.  */
   11305   DW_EH_PE_pcrel | DW_EH_PE_sdata4,	/* FDE encoding.  */
   11306   DW_CFA_def_cfa, 1, 0			/* def_cfa: r1 offset 0.  */
   11307 };
   11308 
   11309 /* Stripping output sections is normally done before dynamic section
   11310    symbols have been allocated.  This function is called later, and
   11311    handles cases like htab->brlt which is mapped to its own output
   11312    section.  */
   11313 
   11314 static void
   11315 maybe_strip_output (struct bfd_link_info *info, asection *isec)
   11316 {
   11317   if (isec->size == 0
   11318       && isec->output_section->size == 0
   11319       && !(isec->output_section->flags & SEC_KEEP)
   11320       && !bfd_section_removed_from_list (info->output_bfd,
   11321 					 isec->output_section)
   11322       && elf_section_data (isec->output_section)->dynindx == 0)
   11323     {
   11324       isec->output_section->flags |= SEC_EXCLUDE;
   11325       bfd_section_list_remove (info->output_bfd, isec->output_section);
   11326       info->output_bfd->section_count--;
   11327     }
   11328 }
   11329 
   11330 /* Determine and set the size of the stub section for a final link.
   11331 
   11332    The basic idea here is to examine all the relocations looking for
   11333    PC-relative calls to a target that is unreachable with a "bl"
   11334    instruction.  */
   11335 
   11336 bfd_boolean
   11337 ppc64_elf_size_stubs (struct bfd_link_info *info, bfd_signed_vma group_size,
   11338 		      bfd_boolean plt_static_chain, int plt_thread_safe,
   11339 		      int plt_stub_align)
   11340 {
   11341   bfd_size_type stub_group_size;
   11342   bfd_boolean stubs_always_before_branch;
   11343   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   11344 
   11345   if (htab == NULL)
   11346     return FALSE;
   11347 
   11348   htab->plt_static_chain = plt_static_chain;
   11349   htab->plt_stub_align = plt_stub_align;
   11350   if (plt_thread_safe == -1 && !info->executable)
   11351     plt_thread_safe = 1;
   11352   if (plt_thread_safe == -1)
   11353     {
   11354       static const char *const thread_starter[] =
   11355 	{
   11356 	  "pthread_create",
   11357 	  /* libstdc++ */
   11358 	  "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
   11359 	  /* librt */
   11360 	  "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
   11361 	  "mq_notify", "create_timer",
   11362 	  /* libanl */
   11363 	  "getaddrinfo_a",
   11364 	  /* libgomp */
   11365 	  "GOMP_parallel_start",
   11366 	  "GOMP_parallel_loop_static_start",
   11367 	  "GOMP_parallel_loop_dynamic_start",
   11368 	  "GOMP_parallel_loop_guided_start",
   11369 	  "GOMP_parallel_loop_runtime_start",
   11370 	  "GOMP_parallel_sections_start",
   11371 	};
   11372       unsigned i;
   11373 
   11374       for (i = 0; i < sizeof (thread_starter)/ sizeof (thread_starter[0]); i++)
   11375 	{
   11376 	  struct elf_link_hash_entry *h;
   11377 	  h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
   11378 				    FALSE, FALSE, TRUE);
   11379 	  plt_thread_safe = h != NULL && h->ref_regular;
   11380 	  if (plt_thread_safe)
   11381 	    break;
   11382 	}
   11383     }
   11384   htab->plt_thread_safe = plt_thread_safe;
   11385   stubs_always_before_branch = group_size < 0;
   11386   if (group_size < 0)
   11387     stub_group_size = -group_size;
   11388   else
   11389     stub_group_size = group_size;
   11390 
   11391   group_sections (htab, stub_group_size, stubs_always_before_branch);
   11392 
   11393   while (1)
   11394     {
   11395       bfd *input_bfd;
   11396       unsigned int bfd_indx;
   11397       asection *stub_sec;
   11398 
   11399       htab->stub_iteration += 1;
   11400 
   11401       for (input_bfd = info->input_bfds, bfd_indx = 0;
   11402 	   input_bfd != NULL;
   11403 	   input_bfd = input_bfd->link_next, bfd_indx++)
   11404 	{
   11405 	  Elf_Internal_Shdr *symtab_hdr;
   11406 	  asection *section;
   11407 	  Elf_Internal_Sym *local_syms = NULL;
   11408 
   11409 	  if (!is_ppc64_elf (input_bfd))
   11410 	    continue;
   11411 
   11412 	  /* We'll need the symbol table in a second.  */
   11413 	  symtab_hdr = &elf_symtab_hdr (input_bfd);
   11414 	  if (symtab_hdr->sh_info == 0)
   11415 	    continue;
   11416 
   11417 	  /* Walk over each section attached to the input bfd.  */
   11418 	  for (section = input_bfd->sections;
   11419 	       section != NULL;
   11420 	       section = section->next)
   11421 	    {
   11422 	      Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
   11423 
   11424 	      /* If there aren't any relocs, then there's nothing more
   11425 		 to do.  */
   11426 	      if ((section->flags & SEC_RELOC) == 0
   11427 		  || (section->flags & SEC_ALLOC) == 0
   11428 		  || (section->flags & SEC_LOAD) == 0
   11429 		  || (section->flags & SEC_CODE) == 0
   11430 		  || section->reloc_count == 0)
   11431 		continue;
   11432 
   11433 	      /* If this section is a link-once section that will be
   11434 		 discarded, then don't create any stubs.  */
   11435 	      if (section->output_section == NULL
   11436 		  || section->output_section->owner != info->output_bfd)
   11437 		continue;
   11438 
   11439 	      /* Get the relocs.  */
   11440 	      internal_relocs
   11441 		= _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
   11442 					     info->keep_memory);
   11443 	      if (internal_relocs == NULL)
   11444 		goto error_ret_free_local;
   11445 
   11446 	      /* Now examine each relocation.  */
   11447 	      irela = internal_relocs;
   11448 	      irelaend = irela + section->reloc_count;
   11449 	      for (; irela < irelaend; irela++)
   11450 		{
   11451 		  enum elf_ppc64_reloc_type r_type;
   11452 		  unsigned int r_indx;
   11453 		  enum ppc_stub_type stub_type;
   11454 		  struct ppc_stub_hash_entry *stub_entry;
   11455 		  asection *sym_sec, *code_sec;
   11456 		  bfd_vma sym_value, code_value;
   11457 		  bfd_vma destination;
   11458 		  bfd_boolean ok_dest;
   11459 		  struct ppc_link_hash_entry *hash;
   11460 		  struct ppc_link_hash_entry *fdh;
   11461 		  struct elf_link_hash_entry *h;
   11462 		  Elf_Internal_Sym *sym;
   11463 		  char *stub_name;
   11464 		  const asection *id_sec;
   11465 		  struct _opd_sec_data *opd;
   11466 		  struct plt_entry *plt_ent;
   11467 
   11468 		  r_type = ELF64_R_TYPE (irela->r_info);
   11469 		  r_indx = ELF64_R_SYM (irela->r_info);
   11470 
   11471 		  if (r_type >= R_PPC64_max)
   11472 		    {
   11473 		      bfd_set_error (bfd_error_bad_value);
   11474 		      goto error_ret_free_internal;
   11475 		    }
   11476 
   11477 		  /* Only look for stubs on branch instructions.  */
   11478 		  if (r_type != R_PPC64_REL24
   11479 		      && r_type != R_PPC64_REL14
   11480 		      && r_type != R_PPC64_REL14_BRTAKEN
   11481 		      && r_type != R_PPC64_REL14_BRNTAKEN)
   11482 		    continue;
   11483 
   11484 		  /* Now determine the call target, its name, value,
   11485 		     section.  */
   11486 		  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   11487 				  r_indx, input_bfd))
   11488 		    goto error_ret_free_internal;
   11489 		  hash = (struct ppc_link_hash_entry *) h;
   11490 
   11491 		  ok_dest = FALSE;
   11492 		  fdh = NULL;
   11493 		  sym_value = 0;
   11494 		  if (hash == NULL)
   11495 		    {
   11496 		      sym_value = sym->st_value;
   11497 		      ok_dest = TRUE;
   11498 		    }
   11499 		  else if (hash->elf.root.type == bfd_link_hash_defined
   11500 			   || hash->elf.root.type == bfd_link_hash_defweak)
   11501 		    {
   11502 		      sym_value = hash->elf.root.u.def.value;
   11503 		      if (sym_sec->output_section != NULL)
   11504 			ok_dest = TRUE;
   11505 		    }
   11506 		  else if (hash->elf.root.type == bfd_link_hash_undefweak
   11507 			   || hash->elf.root.type == bfd_link_hash_undefined)
   11508 		    {
   11509 		      /* Recognise an old ABI func code entry sym, and
   11510 			 use the func descriptor sym instead if it is
   11511 			 defined.  */
   11512 		      if (hash->elf.root.root.string[0] == '.'
   11513 			  && (fdh = lookup_fdh (hash, htab)) != NULL)
   11514 			{
   11515 			  if (fdh->elf.root.type == bfd_link_hash_defined
   11516 			      || fdh->elf.root.type == bfd_link_hash_defweak)
   11517 			    {
   11518 			      sym_sec = fdh->elf.root.u.def.section;
   11519 			      sym_value = fdh->elf.root.u.def.value;
   11520 			      if (sym_sec->output_section != NULL)
   11521 				ok_dest = TRUE;
   11522 			    }
   11523 			  else
   11524 			    fdh = NULL;
   11525 			}
   11526 		    }
   11527 		  else
   11528 		    {
   11529 		      bfd_set_error (bfd_error_bad_value);
   11530 		      goto error_ret_free_internal;
   11531 		    }
   11532 
   11533 		  destination = 0;
   11534 		  if (ok_dest)
   11535 		    {
   11536 		      sym_value += irela->r_addend;
   11537 		      destination = (sym_value
   11538 				     + sym_sec->output_offset
   11539 				     + sym_sec->output_section->vma);
   11540 		    }
   11541 
   11542 		  code_sec = sym_sec;
   11543 		  code_value = sym_value;
   11544 		  opd = get_opd_info (sym_sec);
   11545 		  if (opd != NULL)
   11546 		    {
   11547 		      bfd_vma dest;
   11548 
   11549 		      if (hash == NULL && opd->adjust != NULL)
   11550 			{
   11551 			  long adjust = opd->adjust[sym_value / 8];
   11552 			  if (adjust == -1)
   11553 			    continue;
   11554 			  code_value += adjust;
   11555 			  sym_value += adjust;
   11556 			}
   11557 		      dest = opd_entry_value (sym_sec, sym_value,
   11558 					      &code_sec, &code_value, FALSE);
   11559 		      if (dest != (bfd_vma) -1)
   11560 			{
   11561 			  destination = dest;
   11562 			  if (fdh != NULL)
   11563 			    {
   11564 			      /* Fixup old ABI sym to point at code
   11565 				 entry.  */
   11566 			      hash->elf.root.type = bfd_link_hash_defweak;
   11567 			      hash->elf.root.u.def.section = code_sec;
   11568 			      hash->elf.root.u.def.value = code_value;
   11569 			    }
   11570 			}
   11571 		    }
   11572 
   11573 		  /* Determine what (if any) linker stub is needed.  */
   11574 		  plt_ent = NULL;
   11575 		  stub_type = ppc_type_of_stub (section, irela, &hash,
   11576 						&plt_ent, destination);
   11577 
   11578 		  if (stub_type != ppc_stub_plt_call)
   11579 		    {
   11580 		      /* Check whether we need a TOC adjusting stub.
   11581 			 Since the linker pastes together pieces from
   11582 			 different object files when creating the
   11583 			 _init and _fini functions, it may be that a
   11584 			 call to what looks like a local sym is in
   11585 			 fact a call needing a TOC adjustment.  */
   11586 		      if (code_sec != NULL
   11587 			  && code_sec->output_section != NULL
   11588 			  && (htab->stub_group[code_sec->id].toc_off
   11589 			      != htab->stub_group[section->id].toc_off)
   11590 			  && (code_sec->has_toc_reloc
   11591 			      || code_sec->makes_toc_func_call))
   11592 			stub_type = ppc_stub_long_branch_r2off;
   11593 		    }
   11594 
   11595 		  if (stub_type == ppc_stub_none)
   11596 		    continue;
   11597 
   11598 		  /* __tls_get_addr calls might be eliminated.  */
   11599 		  if (stub_type != ppc_stub_plt_call
   11600 		      && hash != NULL
   11601 		      && (hash == htab->tls_get_addr
   11602 			  || hash == htab->tls_get_addr_fd)
   11603 		      && section->has_tls_reloc
   11604 		      && irela != internal_relocs)
   11605 		    {
   11606 		      /* Get tls info.  */
   11607 		      unsigned char *tls_mask;
   11608 
   11609 		      if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
   11610 					 irela - 1, input_bfd))
   11611 			goto error_ret_free_internal;
   11612 		      if (*tls_mask != 0)
   11613 			continue;
   11614 		    }
   11615 
   11616 		  if (stub_type == ppc_stub_plt_call
   11617 		      && irela + 1 < irelaend
   11618 		      && irela[1].r_offset == irela->r_offset + 4
   11619 		      && ELF64_R_TYPE (irela[1].r_info) == R_PPC64_TOCSAVE)
   11620 		    {
   11621 		      if (!tocsave_find (htab, INSERT,
   11622 					 &local_syms, irela + 1, input_bfd))
   11623 			goto error_ret_free_internal;
   11624 		    }
   11625 		  else if (stub_type == ppc_stub_plt_call)
   11626 		    stub_type = ppc_stub_plt_call_r2save;
   11627 
   11628 		  /* Support for grouping stub sections.  */
   11629 		  id_sec = htab->stub_group[section->id].link_sec;
   11630 
   11631 		  /* Get the name of this stub.  */
   11632 		  stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
   11633 		  if (!stub_name)
   11634 		    goto error_ret_free_internal;
   11635 
   11636 		  stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
   11637 						     stub_name, FALSE, FALSE);
   11638 		  if (stub_entry != NULL)
   11639 		    {
   11640 		      /* The proper stub has already been created.  */
   11641 		      free (stub_name);
   11642 		      if (stub_type == ppc_stub_plt_call_r2save)
   11643 			stub_entry->stub_type = stub_type;
   11644 		      continue;
   11645 		    }
   11646 
   11647 		  stub_entry = ppc_add_stub (stub_name, section, info);
   11648 		  if (stub_entry == NULL)
   11649 		    {
   11650 		      free (stub_name);
   11651 		    error_ret_free_internal:
   11652 		      if (elf_section_data (section)->relocs == NULL)
   11653 			free (internal_relocs);
   11654 		    error_ret_free_local:
   11655 		      if (local_syms != NULL
   11656 			  && (symtab_hdr->contents
   11657 			      != (unsigned char *) local_syms))
   11658 			free (local_syms);
   11659 		      return FALSE;
   11660 		    }
   11661 
   11662 		  stub_entry->stub_type = stub_type;
   11663 		  if (stub_type != ppc_stub_plt_call
   11664 		      && stub_type != ppc_stub_plt_call_r2save)
   11665 		    {
   11666 		      stub_entry->target_value = code_value;
   11667 		      stub_entry->target_section = code_sec;
   11668 		    }
   11669 		  else
   11670 		    {
   11671 		      stub_entry->target_value = sym_value;
   11672 		      stub_entry->target_section = sym_sec;
   11673 		    }
   11674 		  stub_entry->h = hash;
   11675 		  stub_entry->plt_ent = plt_ent;
   11676 		  stub_entry->addend = irela->r_addend;
   11677 
   11678 		  if (stub_entry->h != NULL)
   11679 		    htab->stub_globals += 1;
   11680 		}
   11681 
   11682 	      /* We're done with the internal relocs, free them.  */
   11683 	      if (elf_section_data (section)->relocs != internal_relocs)
   11684 		free (internal_relocs);
   11685 	    }
   11686 
   11687 	  if (local_syms != NULL
   11688 	      && symtab_hdr->contents != (unsigned char *) local_syms)
   11689 	    {
   11690 	      if (!info->keep_memory)
   11691 		free (local_syms);
   11692 	      else
   11693 		symtab_hdr->contents = (unsigned char *) local_syms;
   11694 	    }
   11695 	}
   11696 
   11697       /* We may have added some stubs.  Find out the new size of the
   11698 	 stub sections.  */
   11699       for (stub_sec = htab->stub_bfd->sections;
   11700 	   stub_sec != NULL;
   11701 	   stub_sec = stub_sec->next)
   11702 	if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
   11703 	  {
   11704 	    stub_sec->rawsize = stub_sec->size;
   11705 	    stub_sec->size = 0;
   11706 	    stub_sec->reloc_count = 0;
   11707 	    stub_sec->flags &= ~SEC_RELOC;
   11708 	  }
   11709 
   11710       htab->brlt->size = 0;
   11711       htab->brlt->reloc_count = 0;
   11712       htab->brlt->flags &= ~SEC_RELOC;
   11713       if (htab->relbrlt != NULL)
   11714 	htab->relbrlt->size = 0;
   11715 
   11716       bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
   11717 
   11718       if (info->emitrelocations
   11719 	  && htab->glink != NULL && htab->glink->size != 0)
   11720 	{
   11721 	  htab->glink->reloc_count = 1;
   11722 	  htab->glink->flags |= SEC_RELOC;
   11723 	}
   11724 
   11725       if (htab->glink_eh_frame != NULL
   11726 	  && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
   11727 	  && htab->glink_eh_frame->output_section->size != 0)
   11728 	{
   11729 	  size_t size = 0, align;
   11730 
   11731 	  for (stub_sec = htab->stub_bfd->sections;
   11732 	       stub_sec != NULL;
   11733 	       stub_sec = stub_sec->next)
   11734 	    if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
   11735 	      size += 20;
   11736 	  if (htab->glink != NULL && htab->glink->size != 0)
   11737 	    size += 24;
   11738 	  if (size != 0)
   11739 	    size += sizeof (glink_eh_frame_cie);
   11740 	  align = 1;
   11741 	  align <<= htab->glink_eh_frame->output_section->alignment_power;
   11742 	  align -= 1;
   11743 	  size = (size + align) & ~align;
   11744 	  htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
   11745 	  htab->glink_eh_frame->size = size;
   11746 	}
   11747 
   11748       if (htab->plt_stub_align != 0)
   11749 	for (stub_sec = htab->stub_bfd->sections;
   11750 	     stub_sec != NULL;
   11751 	     stub_sec = stub_sec->next)
   11752 	  if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
   11753 	    stub_sec->size = ((stub_sec->size + (1 << htab->plt_stub_align) - 1)
   11754 			      & (-1 << htab->plt_stub_align));
   11755 
   11756       for (stub_sec = htab->stub_bfd->sections;
   11757 	   stub_sec != NULL;
   11758 	   stub_sec = stub_sec->next)
   11759 	if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
   11760 	    && stub_sec->rawsize != stub_sec->size)
   11761 	  break;
   11762 
   11763       /* Exit from this loop when no stubs have been added, and no stubs
   11764 	 have changed size.  */
   11765       if (stub_sec == NULL
   11766 	  && (htab->glink_eh_frame == NULL
   11767 	      || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
   11768 	break;
   11769 
   11770       /* Ask the linker to do its stuff.  */
   11771       (*htab->layout_sections_again) ();
   11772     }
   11773 
   11774   maybe_strip_output (info, htab->brlt);
   11775   if (htab->glink_eh_frame != NULL)
   11776     maybe_strip_output (info, htab->glink_eh_frame);
   11777 
   11778   return TRUE;
   11779 }
   11780 
   11781 /* Called after we have determined section placement.  If sections
   11782    move, we'll be called again.  Provide a value for TOCstart.  */
   11783 
   11784 bfd_vma
   11785 ppc64_elf_toc (bfd *obfd)
   11786 {
   11787   asection *s;
   11788   bfd_vma TOCstart;
   11789 
   11790   /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
   11791      order.  The TOC starts where the first of these sections starts.  */
   11792   s = bfd_get_section_by_name (obfd, ".got");
   11793   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
   11794     s = bfd_get_section_by_name (obfd, ".toc");
   11795   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
   11796     s = bfd_get_section_by_name (obfd, ".tocbss");
   11797   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
   11798     s = bfd_get_section_by_name (obfd, ".plt");
   11799   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
   11800     {
   11801       /* This may happen for
   11802 	 o  references to TOC base (SYM@toc / TOC[tc0]) without a
   11803 	 .toc directive
   11804 	 o  bad linker script
   11805 	 o --gc-sections and empty TOC sections
   11806 
   11807 	 FIXME: Warn user?  */
   11808 
   11809       /* Look for a likely section.  We probably won't even be
   11810 	 using TOCstart.  */
   11811       for (s = obfd->sections; s != NULL; s = s->next)
   11812 	if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
   11813 			 | SEC_EXCLUDE))
   11814 	    == (SEC_ALLOC | SEC_SMALL_DATA))
   11815 	  break;
   11816       if (s == NULL)
   11817 	for (s = obfd->sections; s != NULL; s = s->next)
   11818 	  if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
   11819 	      == (SEC_ALLOC | SEC_SMALL_DATA))
   11820 	    break;
   11821       if (s == NULL)
   11822 	for (s = obfd->sections; s != NULL; s = s->next)
   11823 	  if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
   11824 	      == SEC_ALLOC)
   11825 	    break;
   11826       if (s == NULL)
   11827 	for (s = obfd->sections; s != NULL; s = s->next)
   11828 	  if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
   11829 	    break;
   11830     }
   11831 
   11832   TOCstart = 0;
   11833   if (s != NULL)
   11834     TOCstart = s->output_section->vma + s->output_offset;
   11835 
   11836   return TOCstart;
   11837 }
   11838 
   11839 /* Build all the stubs associated with the current output file.
   11840    The stubs are kept in a hash table attached to the main linker
   11841    hash table.  This function is called via gldelf64ppc_finish.  */
   11842 
   11843 bfd_boolean
   11844 ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
   11845 		       struct bfd_link_info *info,
   11846 		       char **stats)
   11847 {
   11848   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   11849   asection *stub_sec;
   11850   bfd_byte *p;
   11851   int stub_sec_count = 0;
   11852 
   11853   if (htab == NULL)
   11854     return FALSE;
   11855 
   11856   htab->emit_stub_syms = emit_stub_syms;
   11857 
   11858   /* Allocate memory to hold the linker stubs.  */
   11859   for (stub_sec = htab->stub_bfd->sections;
   11860        stub_sec != NULL;
   11861        stub_sec = stub_sec->next)
   11862     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
   11863 	&& stub_sec->size != 0)
   11864       {
   11865 	stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
   11866 	if (stub_sec->contents == NULL)
   11867 	  return FALSE;
   11868 	/* We want to check that built size is the same as calculated
   11869 	   size.  rawsize is a convenient location to use.  */
   11870 	stub_sec->rawsize = stub_sec->size;
   11871 	stub_sec->size = 0;
   11872       }
   11873 
   11874   if (htab->glink != NULL && htab->glink->size != 0)
   11875     {
   11876       unsigned int indx;
   11877       bfd_vma plt0;
   11878 
   11879       /* Build the .glink plt call stub.  */
   11880       if (htab->emit_stub_syms)
   11881 	{
   11882 	  struct elf_link_hash_entry *h;
   11883 	  h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
   11884 				    TRUE, FALSE, FALSE);
   11885 	  if (h == NULL)
   11886 	    return FALSE;
   11887 	  if (h->root.type == bfd_link_hash_new)
   11888 	    {
   11889 	      h->root.type = bfd_link_hash_defined;
   11890 	      h->root.u.def.section = htab->glink;
   11891 	      h->root.u.def.value = 8;
   11892 	      h->ref_regular = 1;
   11893 	      h->def_regular = 1;
   11894 	      h->ref_regular_nonweak = 1;
   11895 	      h->forced_local = 1;
   11896 	      h->non_elf = 0;
   11897 	    }
   11898 	}
   11899       plt0 = htab->plt->output_section->vma + htab->plt->output_offset - 16;
   11900       if (info->emitrelocations)
   11901 	{
   11902 	  Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
   11903 	  if (r == NULL)
   11904 	    return FALSE;
   11905 	  r->r_offset = (htab->glink->output_offset
   11906 			 + htab->glink->output_section->vma);
   11907 	  r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
   11908 	  r->r_addend = plt0;
   11909 	}
   11910       p = htab->glink->contents;
   11911       plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
   11912       bfd_put_64 (htab->glink->owner, plt0, p);
   11913       p += 8;
   11914       bfd_put_32 (htab->glink->owner, MFLR_R12, p);
   11915       p += 4;
   11916       bfd_put_32 (htab->glink->owner, BCL_20_31, p);
   11917       p += 4;
   11918       bfd_put_32 (htab->glink->owner, MFLR_R11, p);
   11919       p += 4;
   11920       bfd_put_32 (htab->glink->owner, LD_R2_M16R11, p);
   11921       p += 4;
   11922       bfd_put_32 (htab->glink->owner, MTLR_R12, p);
   11923       p += 4;
   11924       bfd_put_32 (htab->glink->owner, ADD_R12_R2_R11, p);
   11925       p += 4;
   11926       bfd_put_32 (htab->glink->owner, LD_R11_0R12, p);
   11927       p += 4;
   11928       bfd_put_32 (htab->glink->owner, LD_R2_0R12 | 8, p);
   11929       p += 4;
   11930       bfd_put_32 (htab->glink->owner, MTCTR_R11, p);
   11931       p += 4;
   11932       bfd_put_32 (htab->glink->owner, LD_R11_0R12 | 16, p);
   11933       p += 4;
   11934       bfd_put_32 (htab->glink->owner, BCTR, p);
   11935       p += 4;
   11936       while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
   11937 	{
   11938 	  bfd_put_32 (htab->glink->owner, NOP, p);
   11939 	  p += 4;
   11940 	}
   11941 
   11942       /* Build the .glink lazy link call stubs.  */
   11943       indx = 0;
   11944       while (p < htab->glink->contents + htab->glink->size)
   11945 	{
   11946 	  if (indx < 0x8000)
   11947 	    {
   11948 	      bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
   11949 	      p += 4;
   11950 	    }
   11951 	  else
   11952 	    {
   11953 	      bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
   11954 	      p += 4;
   11955 	      bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
   11956 	      p += 4;
   11957 	    }
   11958 	  bfd_put_32 (htab->glink->owner,
   11959 		      B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
   11960 	  indx++;
   11961 	  p += 4;
   11962 	}
   11963       htab->glink->rawsize = p - htab->glink->contents;
   11964     }
   11965 
   11966   if (htab->brlt->size != 0)
   11967     {
   11968       htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
   11969 					 htab->brlt->size);
   11970       if (htab->brlt->contents == NULL)
   11971 	return FALSE;
   11972     }
   11973   if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
   11974     {
   11975       htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
   11976 					    htab->relbrlt->size);
   11977       if (htab->relbrlt->contents == NULL)
   11978 	return FALSE;
   11979     }
   11980 
   11981   if (htab->glink_eh_frame != NULL
   11982       && htab->glink_eh_frame->size != 0)
   11983     {
   11984       bfd_vma val;
   11985       bfd_byte *last_fde;
   11986       size_t last_fde_len, size, align, pad;
   11987 
   11988       p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
   11989       if (p == NULL)
   11990 	return FALSE;
   11991       htab->glink_eh_frame->contents = p;
   11992       last_fde = p;
   11993 
   11994       htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
   11995 
   11996       memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
   11997       /* CIE length (rewrite in case little-endian).  */
   11998       last_fde_len = sizeof (glink_eh_frame_cie) - 4;
   11999       bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
   12000       p += sizeof (glink_eh_frame_cie);
   12001 
   12002       for (stub_sec = htab->stub_bfd->sections;
   12003 	   stub_sec != NULL;
   12004 	   stub_sec = stub_sec->next)
   12005 	if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
   12006 	  {
   12007 	    last_fde = p;
   12008 	    last_fde_len = 16;
   12009 	    /* FDE length.  */
   12010 	    bfd_put_32 (htab->elf.dynobj, 16, p);
   12011 	    p += 4;
   12012 	    /* CIE pointer.  */
   12013 	    val = p - htab->glink_eh_frame->contents;
   12014 	    bfd_put_32 (htab->elf.dynobj, val, p);
   12015 	    p += 4;
   12016 	    /* Offset to stub section.  */
   12017 	    val = (stub_sec->output_section->vma
   12018 		   + stub_sec->output_offset);
   12019 	    val -= (htab->glink_eh_frame->output_section->vma
   12020 		    + htab->glink_eh_frame->output_offset);
   12021 	    val -= p - htab->glink_eh_frame->contents;
   12022 	    if (val + 0x80000000 > 0xffffffff)
   12023 	      {
   12024 		info->callbacks->einfo
   12025 		  (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
   12026 		   stub_sec->name);
   12027 		return FALSE;
   12028 	      }
   12029 	    bfd_put_32 (htab->elf.dynobj, val, p);
   12030 	    p += 4;
   12031 	    /* stub section size.  */
   12032 	    bfd_put_32 (htab->elf.dynobj, stub_sec->rawsize, p);
   12033 	    p += 4;
   12034 	    /* Augmentation.  */
   12035 	    p += 1;
   12036 	    /* Pad.  */
   12037 	    p += 3;
   12038 	  }
   12039       if (htab->glink != NULL && htab->glink->size != 0)
   12040 	{
   12041 	  last_fde = p;
   12042 	  last_fde_len = 20;
   12043 	  /* FDE length.  */
   12044 	  bfd_put_32 (htab->elf.dynobj, 20, p);
   12045 	  p += 4;
   12046 	  /* CIE pointer.  */
   12047 	  val = p - htab->glink_eh_frame->contents;
   12048 	  bfd_put_32 (htab->elf.dynobj, val, p);
   12049 	  p += 4;
   12050 	  /* Offset to .glink.  */
   12051 	  val = (htab->glink->output_section->vma
   12052 		 + htab->glink->output_offset
   12053 		 + 8);
   12054 	  val -= (htab->glink_eh_frame->output_section->vma
   12055 		  + htab->glink_eh_frame->output_offset);
   12056 	  val -= p - htab->glink_eh_frame->contents;
   12057 	  if (val + 0x80000000 > 0xffffffff)
   12058 	    {
   12059 	      info->callbacks->einfo
   12060 		(_("%P: %s offset too large for .eh_frame sdata4 encoding"),
   12061 		 htab->glink->name);
   12062 	      return FALSE;
   12063 	    }
   12064 	  bfd_put_32 (htab->elf.dynobj, val, p);
   12065 	  p += 4;
   12066 	  /* .glink size.  */
   12067 	  bfd_put_32 (htab->elf.dynobj, htab->glink->rawsize - 8, p);
   12068 	  p += 4;
   12069 	  /* Augmentation.  */
   12070 	  p += 1;
   12071 
   12072 	  *p++ = DW_CFA_advance_loc + 1;
   12073 	  *p++ = DW_CFA_register;
   12074 	  *p++ = 65;
   12075 	  *p++ = 12;
   12076 	  *p++ = DW_CFA_advance_loc + 4;
   12077 	  *p++ = DW_CFA_restore_extended;
   12078 	  *p++ = 65;
   12079 	}
   12080       /* Subsume any padding into the last FDE if user .eh_frame
   12081 	 sections are aligned more than glink_eh_frame.  Otherwise any
   12082 	 zero padding will be seen as a terminator.  */
   12083       size = p - htab->glink_eh_frame->contents;
   12084       align = 1;
   12085       align <<= htab->glink_eh_frame->output_section->alignment_power;
   12086       align -= 1;
   12087       pad = ((size + align) & ~align) - size;
   12088       htab->glink_eh_frame->size = size + pad;
   12089       bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
   12090     }
   12091 
   12092   /* Build the stubs as directed by the stub hash table.  */
   12093   bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
   12094 
   12095   if (htab->relbrlt != NULL)
   12096     htab->relbrlt->reloc_count = 0;
   12097 
   12098   if (htab->plt_stub_align != 0)
   12099     for (stub_sec = htab->stub_bfd->sections;
   12100 	 stub_sec != NULL;
   12101 	 stub_sec = stub_sec->next)
   12102       if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
   12103 	stub_sec->size = ((stub_sec->size + (1 << htab->plt_stub_align) - 1)
   12104 			  & (-1 << htab->plt_stub_align));
   12105 
   12106   for (stub_sec = htab->stub_bfd->sections;
   12107        stub_sec != NULL;
   12108        stub_sec = stub_sec->next)
   12109     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
   12110       {
   12111 	stub_sec_count += 1;
   12112 	if (stub_sec->rawsize != stub_sec->size)
   12113 	  break;
   12114       }
   12115 
   12116   if (stub_sec != NULL
   12117       || htab->glink->rawsize != htab->glink->size
   12118       || (htab->glink_eh_frame != NULL
   12119 	  && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
   12120     {
   12121       htab->stub_error = TRUE;
   12122       info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
   12123     }
   12124 
   12125   if (htab->stub_error)
   12126     return FALSE;
   12127 
   12128   if (stats != NULL)
   12129     {
   12130       *stats = bfd_malloc (500);
   12131       if (*stats == NULL)
   12132 	return FALSE;
   12133 
   12134       sprintf (*stats, _("linker stubs in %u group%s\n"
   12135 			 "  branch       %lu\n"
   12136 			 "  toc adjust   %lu\n"
   12137 			 "  long branch  %lu\n"
   12138 			 "  long toc adj %lu\n"
   12139 			 "  plt call     %lu\n"
   12140 			 "  plt call toc %lu"),
   12141 	       stub_sec_count,
   12142 	       stub_sec_count == 1 ? "" : "s",
   12143 	       htab->stub_count[ppc_stub_long_branch - 1],
   12144 	       htab->stub_count[ppc_stub_long_branch_r2off - 1],
   12145 	       htab->stub_count[ppc_stub_plt_branch - 1],
   12146 	       htab->stub_count[ppc_stub_plt_branch_r2off - 1],
   12147 	       htab->stub_count[ppc_stub_plt_call - 1],
   12148 	       htab->stub_count[ppc_stub_plt_call_r2save - 1]);
   12149     }
   12150   return TRUE;
   12151 }
   12152 
   12153 /* This function undoes the changes made by add_symbol_adjust.  */
   12154 
   12155 static bfd_boolean
   12156 undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
   12157 {
   12158   struct ppc_link_hash_entry *eh;
   12159 
   12160   if (h->root.type == bfd_link_hash_indirect)
   12161     return TRUE;
   12162 
   12163   eh = (struct ppc_link_hash_entry *) h;
   12164   if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
   12165     return TRUE;
   12166 
   12167   eh->elf.root.type = bfd_link_hash_undefined;
   12168   return TRUE;
   12169 }
   12170 
   12171 void
   12172 ppc64_elf_restore_symbols (struct bfd_link_info *info)
   12173 {
   12174   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   12175 
   12176   if (htab != NULL)
   12177     elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
   12178 }
   12179 
   12180 /* What to do when ld finds relocations against symbols defined in
   12181    discarded sections.  */
   12182 
   12183 static unsigned int
   12184 ppc64_elf_action_discarded (asection *sec)
   12185 {
   12186   if (strcmp (".opd", sec->name) == 0)
   12187     return 0;
   12188 
   12189   if (strcmp (".toc", sec->name) == 0)
   12190     return 0;
   12191 
   12192   if (strcmp (".toc1", sec->name) == 0)
   12193     return 0;
   12194 
   12195   return _bfd_elf_default_action_discarded (sec);
   12196 }
   12197 
   12198 /* The RELOCATE_SECTION function is called by the ELF backend linker
   12199    to handle the relocations for a section.
   12200 
   12201    The relocs are always passed as Rela structures; if the section
   12202    actually uses Rel structures, the r_addend field will always be
   12203    zero.
   12204 
   12205    This function is responsible for adjust the section contents as
   12206    necessary, and (if using Rela relocs and generating a
   12207    relocatable output file) adjusting the reloc addend as
   12208    necessary.
   12209 
   12210    This function does not have to worry about setting the reloc
   12211    address or the reloc symbol index.
   12212 
   12213    LOCAL_SYMS is a pointer to the swapped in local symbols.
   12214 
   12215    LOCAL_SECTIONS is an array giving the section in the input file
   12216    corresponding to the st_shndx field of each local symbol.
   12217 
   12218    The global hash table entry for the global symbols can be found
   12219    via elf_sym_hashes (input_bfd).
   12220 
   12221    When generating relocatable output, this function must handle
   12222    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
   12223    going to be the section symbol corresponding to the output
   12224    section, which means that the addend must be adjusted
   12225    accordingly.  */
   12226 
   12227 static bfd_boolean
   12228 ppc64_elf_relocate_section (bfd *output_bfd,
   12229 			    struct bfd_link_info *info,
   12230 			    bfd *input_bfd,
   12231 			    asection *input_section,
   12232 			    bfd_byte *contents,
   12233 			    Elf_Internal_Rela *relocs,
   12234 			    Elf_Internal_Sym *local_syms,
   12235 			    asection **local_sections)
   12236 {
   12237   struct ppc_link_hash_table *htab;
   12238   Elf_Internal_Shdr *symtab_hdr;
   12239   struct elf_link_hash_entry **sym_hashes;
   12240   Elf_Internal_Rela *rel;
   12241   Elf_Internal_Rela *relend;
   12242   Elf_Internal_Rela outrel;
   12243   bfd_byte *loc;
   12244   struct got_entry **local_got_ents;
   12245   bfd_vma TOCstart;
   12246   bfd_boolean ret = TRUE;
   12247   bfd_boolean is_opd;
   12248   /* Assume 'at' branch hints.  */
   12249   bfd_boolean is_isa_v2 = TRUE;
   12250   bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
   12251 
   12252   /* Initialize howto table if needed.  */
   12253   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
   12254     ppc_howto_init ();
   12255 
   12256   htab = ppc_hash_table (info);
   12257   if (htab == NULL)
   12258     return FALSE;
   12259 
   12260   /* Don't relocate stub sections.  */
   12261   if (input_section->owner == htab->stub_bfd)
   12262     return TRUE;
   12263 
   12264   BFD_ASSERT (is_ppc64_elf (input_bfd));
   12265 
   12266   local_got_ents = elf_local_got_ents (input_bfd);
   12267   TOCstart = elf_gp (output_bfd);
   12268   symtab_hdr = &elf_symtab_hdr (input_bfd);
   12269   sym_hashes = elf_sym_hashes (input_bfd);
   12270   is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
   12271 
   12272   rel = relocs;
   12273   relend = relocs + input_section->reloc_count;
   12274   for (; rel < relend; rel++)
   12275     {
   12276       enum elf_ppc64_reloc_type r_type;
   12277       bfd_vma addend;
   12278       bfd_reloc_status_type r;
   12279       Elf_Internal_Sym *sym;
   12280       asection *sec;
   12281       struct elf_link_hash_entry *h_elf;
   12282       struct ppc_link_hash_entry *h;
   12283       struct ppc_link_hash_entry *fdh;
   12284       const char *sym_name;
   12285       unsigned long r_symndx, toc_symndx;
   12286       bfd_vma toc_addend;
   12287       unsigned char tls_mask, tls_gd, tls_type;
   12288       unsigned char sym_type;
   12289       bfd_vma relocation;
   12290       bfd_boolean unresolved_reloc;
   12291       bfd_boolean warned;
   12292       enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
   12293       unsigned int insn;
   12294       unsigned int mask;
   12295       struct ppc_stub_hash_entry *stub_entry;
   12296       bfd_vma max_br_offset;
   12297       bfd_vma from;
   12298       const Elf_Internal_Rela orig_rel = *rel;
   12299 
   12300       r_type = ELF64_R_TYPE (rel->r_info);
   12301       r_symndx = ELF64_R_SYM (rel->r_info);
   12302 
   12303       /* For old style R_PPC64_TOC relocs with a zero symbol, use the
   12304 	 symbol of the previous ADDR64 reloc.  The symbol gives us the
   12305 	 proper TOC base to use.  */
   12306       if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
   12307 	  && rel != relocs
   12308 	  && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
   12309 	  && is_opd)
   12310 	r_symndx = ELF64_R_SYM (rel[-1].r_info);
   12311 
   12312       sym = NULL;
   12313       sec = NULL;
   12314       h_elf = NULL;
   12315       sym_name = NULL;
   12316       unresolved_reloc = FALSE;
   12317       warned = FALSE;
   12318 
   12319       if (r_symndx < symtab_hdr->sh_info)
   12320 	{
   12321 	  /* It's a local symbol.  */
   12322 	  struct _opd_sec_data *opd;
   12323 
   12324 	  sym = local_syms + r_symndx;
   12325 	  sec = local_sections[r_symndx];
   12326 	  sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
   12327 	  sym_type = ELF64_ST_TYPE (sym->st_info);
   12328 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
   12329 	  opd = get_opd_info (sec);
   12330 	  if (opd != NULL && opd->adjust != NULL)
   12331 	    {
   12332 	      long adjust = opd->adjust[(sym->st_value + rel->r_addend) / 8];
   12333 	      if (adjust == -1)
   12334 		relocation = 0;
   12335 	      else
   12336 		{
   12337 		  /* If this is a relocation against the opd section sym
   12338 		     and we have edited .opd, adjust the reloc addend so
   12339 		     that ld -r and ld --emit-relocs output is correct.
   12340 		     If it is a reloc against some other .opd symbol,
   12341 		     then the symbol value will be adjusted later.  */
   12342 		  if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
   12343 		    rel->r_addend += adjust;
   12344 		  else
   12345 		    relocation += adjust;
   12346 		}
   12347 	    }
   12348 	}
   12349       else
   12350 	{
   12351 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
   12352 				   r_symndx, symtab_hdr, sym_hashes,
   12353 				   h_elf, sec, relocation,
   12354 				   unresolved_reloc, warned);
   12355 	  sym_name = h_elf->root.root.string;
   12356 	  sym_type = h_elf->type;
   12357 	  if (sec != NULL
   12358 	      && sec->owner == output_bfd
   12359 	      && strcmp (sec->name, ".opd") == 0)
   12360 	    {
   12361 	      /* This is a symbol defined in a linker script.  All
   12362 		 such are defined in output sections, even those
   12363 		 defined by simple assignment from a symbol defined in
   12364 		 an input section.  Transfer the symbol to an
   12365 		 appropriate input .opd section, so that a branch to
   12366 		 this symbol will be mapped to the location specified
   12367 		 by the opd entry.  */
   12368 	      struct bfd_link_order *lo;
   12369 	      for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
   12370 		if (lo->type == bfd_indirect_link_order)
   12371 		  {
   12372 		    asection *isec = lo->u.indirect.section;
   12373 		    if (h_elf->root.u.def.value >= isec->output_offset
   12374 			&& h_elf->root.u.def.value < (isec->output_offset
   12375 						      + isec->size))
   12376 		      {
   12377 			h_elf->root.u.def.value -= isec->output_offset;
   12378 			h_elf->root.u.def.section = isec;
   12379 			sec = isec;
   12380 			break;
   12381 		      }
   12382 		  }
   12383 	    }
   12384 	}
   12385       h = (struct ppc_link_hash_entry *) h_elf;
   12386 
   12387       if (sec != NULL && discarded_section (sec))
   12388 	RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
   12389 					 rel, 1, relend,
   12390 					 ppc64_elf_howto_table[r_type], 0,
   12391 					 contents);
   12392 
   12393       if (info->relocatable)
   12394 	continue;
   12395 
   12396       /* TLS optimizations.  Replace instruction sequences and relocs
   12397 	 based on information we collected in tls_optimize.  We edit
   12398 	 RELOCS so that --emit-relocs will output something sensible
   12399 	 for the final instruction stream.  */
   12400       tls_mask = 0;
   12401       tls_gd = 0;
   12402       toc_symndx = 0;
   12403       if (h != NULL)
   12404 	tls_mask = h->tls_mask;
   12405       else if (local_got_ents != NULL)
   12406 	{
   12407 	  struct plt_entry **local_plt = (struct plt_entry **)
   12408 	    (local_got_ents + symtab_hdr->sh_info);
   12409 	  unsigned char *lgot_masks = (unsigned char *)
   12410 	    (local_plt + symtab_hdr->sh_info);
   12411 	  tls_mask = lgot_masks[r_symndx];
   12412 	}
   12413       if (tls_mask == 0
   12414 	  && (r_type == R_PPC64_TLS
   12415 	      || r_type == R_PPC64_TLSGD
   12416 	      || r_type == R_PPC64_TLSLD))
   12417 	{
   12418 	  /* Check for toc tls entries.  */
   12419 	  unsigned char *toc_tls;
   12420 
   12421 	  if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
   12422 			     &local_syms, rel, input_bfd))
   12423 	    return FALSE;
   12424 
   12425 	  if (toc_tls)
   12426 	    tls_mask = *toc_tls;
   12427 	}
   12428 
   12429       /* Check that tls relocs are used with tls syms, and non-tls
   12430 	 relocs are used with non-tls syms.  */
   12431       if (r_symndx != STN_UNDEF
   12432 	  && r_type != R_PPC64_NONE
   12433 	  && (h == NULL
   12434 	      || h->elf.root.type == bfd_link_hash_defined
   12435 	      || h->elf.root.type == bfd_link_hash_defweak)
   12436 	  && (IS_PPC64_TLS_RELOC (r_type)
   12437 	      != (sym_type == STT_TLS
   12438 		  || (sym_type == STT_SECTION
   12439 		      && (sec->flags & SEC_THREAD_LOCAL) != 0))))
   12440 	{
   12441 	  if (tls_mask != 0
   12442 	      && (r_type == R_PPC64_TLS
   12443 		  || r_type == R_PPC64_TLSGD
   12444 		  || r_type == R_PPC64_TLSLD))
   12445 	    /* R_PPC64_TLS is OK against a symbol in the TOC.  */
   12446 	    ;
   12447 	  else
   12448 	    info->callbacks->einfo
   12449 	      (!IS_PPC64_TLS_RELOC (r_type)
   12450 	       ? _("%P: %H: %s used with TLS symbol `%T'\n")
   12451 	       : _("%P: %H: %s used with non-TLS symbol `%T'\n"),
   12452 	       input_bfd, input_section, rel->r_offset,
   12453 	       ppc64_elf_howto_table[r_type]->name,
   12454 	       sym_name);
   12455 	}
   12456 
   12457       /* Ensure reloc mapping code below stays sane.  */
   12458       if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
   12459 	  || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
   12460 	  || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TLSGD16 & 3)
   12461 	  || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
   12462 	  || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
   12463 	  || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
   12464 	  || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TPREL16_DS & 3)
   12465 	  || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
   12466 	  || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
   12467 	  || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
   12468 	abort ();
   12469 
   12470       switch (r_type)
   12471 	{
   12472 	default:
   12473 	  break;
   12474 
   12475 	case R_PPC64_LO_DS_OPT:
   12476 	  insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
   12477 	  if ((insn & (0x3f << 26)) != 58u << 26)
   12478 	    abort ();
   12479 	  insn += (14u << 26) - (58u << 26);
   12480 	  bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
   12481 	  r_type = R_PPC64_TOC16_LO;
   12482 	  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12483 	  break;
   12484 
   12485 	case R_PPC64_TOC16:
   12486 	case R_PPC64_TOC16_LO:
   12487 	case R_PPC64_TOC16_DS:
   12488 	case R_PPC64_TOC16_LO_DS:
   12489 	  {
   12490 	    /* Check for toc tls entries.  */
   12491 	    unsigned char *toc_tls;
   12492 	    int retval;
   12493 
   12494 	    retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
   12495 				   &local_syms, rel, input_bfd);
   12496 	    if (retval == 0)
   12497 	      return FALSE;
   12498 
   12499 	    if (toc_tls)
   12500 	      {
   12501 		tls_mask = *toc_tls;
   12502 		if (r_type == R_PPC64_TOC16_DS
   12503 		    || r_type == R_PPC64_TOC16_LO_DS)
   12504 		  {
   12505 		    if (tls_mask != 0
   12506 			&& (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
   12507 		      goto toctprel;
   12508 		  }
   12509 		else
   12510 		  {
   12511 		    /* If we found a GD reloc pair, then we might be
   12512 		       doing a GD->IE transition.  */
   12513 		    if (retval == 2)
   12514 		      {
   12515 			tls_gd = TLS_TPRELGD;
   12516 			if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
   12517 			  goto tls_ldgd_opt;
   12518 		      }
   12519 		    else if (retval == 3)
   12520 		      {
   12521 			if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
   12522 			  goto tls_ldgd_opt;
   12523 		      }
   12524 		  }
   12525 	      }
   12526 	  }
   12527 	  break;
   12528 
   12529 	case R_PPC64_GOT_TPREL16_HI:
   12530 	case R_PPC64_GOT_TPREL16_HA:
   12531 	  if (tls_mask != 0
   12532 	      && (tls_mask & TLS_TPREL) == 0)
   12533 	    {
   12534 	      rel->r_offset -= d_offset;
   12535 	      bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
   12536 	      r_type = R_PPC64_NONE;
   12537 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12538 	    }
   12539 	  break;
   12540 
   12541 	case R_PPC64_GOT_TPREL16_DS:
   12542 	case R_PPC64_GOT_TPREL16_LO_DS:
   12543 	  if (tls_mask != 0
   12544 	      && (tls_mask & TLS_TPREL) == 0)
   12545 	    {
   12546 	    toctprel:
   12547 	      insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
   12548 	      insn &= 31 << 21;
   12549 	      insn |= 0x3c0d0000;	/* addis 0,13,0 */
   12550 	      bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
   12551 	      r_type = R_PPC64_TPREL16_HA;
   12552 	      if (toc_symndx != 0)
   12553 		{
   12554 		  rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
   12555 		  rel->r_addend = toc_addend;
   12556 		  /* We changed the symbol.  Start over in order to
   12557 		     get h, sym, sec etc. right.  */
   12558 		  rel--;
   12559 		  continue;
   12560 		}
   12561 	      else
   12562 		rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12563 	    }
   12564 	  break;
   12565 
   12566 	case R_PPC64_TLS:
   12567 	  if (tls_mask != 0
   12568 	      && (tls_mask & TLS_TPREL) == 0)
   12569 	    {
   12570 	      insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
   12571 	      insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
   12572 	      if (insn == 0)
   12573 		abort ();
   12574 	      bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
   12575 	      /* Was PPC64_TLS which sits on insn boundary, now
   12576 		 PPC64_TPREL16_LO which is at low-order half-word.  */
   12577 	      rel->r_offset += d_offset;
   12578 	      r_type = R_PPC64_TPREL16_LO;
   12579 	      if (toc_symndx != 0)
   12580 		{
   12581 		  rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
   12582 		  rel->r_addend = toc_addend;
   12583 		  /* We changed the symbol.  Start over in order to
   12584 		     get h, sym, sec etc. right.  */
   12585 		  rel--;
   12586 		  continue;
   12587 		}
   12588 	      else
   12589 		rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12590 	    }
   12591 	  break;
   12592 
   12593 	case R_PPC64_GOT_TLSGD16_HI:
   12594 	case R_PPC64_GOT_TLSGD16_HA:
   12595 	  tls_gd = TLS_TPRELGD;
   12596 	  if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
   12597 	    goto tls_gdld_hi;
   12598 	  break;
   12599 
   12600 	case R_PPC64_GOT_TLSLD16_HI:
   12601 	case R_PPC64_GOT_TLSLD16_HA:
   12602 	  if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
   12603 	    {
   12604 	    tls_gdld_hi:
   12605 	      if ((tls_mask & tls_gd) != 0)
   12606 		r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
   12607 			  + R_PPC64_GOT_TPREL16_DS);
   12608 	      else
   12609 		{
   12610 		  rel->r_offset -= d_offset;
   12611 		  bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
   12612 		  r_type = R_PPC64_NONE;
   12613 		}
   12614 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12615 	    }
   12616 	  break;
   12617 
   12618 	case R_PPC64_GOT_TLSGD16:
   12619 	case R_PPC64_GOT_TLSGD16_LO:
   12620 	  tls_gd = TLS_TPRELGD;
   12621 	  if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
   12622 	    goto tls_ldgd_opt;
   12623 	  break;
   12624 
   12625 	case R_PPC64_GOT_TLSLD16:
   12626 	case R_PPC64_GOT_TLSLD16_LO:
   12627 	  if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
   12628 	    {
   12629 	      unsigned int insn1, insn2, insn3;
   12630 	      bfd_vma offset;
   12631 
   12632 	    tls_ldgd_opt:
   12633 	      offset = (bfd_vma) -1;
   12634 	      /* If not using the newer R_PPC64_TLSGD/LD to mark
   12635 		 __tls_get_addr calls, we must trust that the call
   12636 		 stays with its arg setup insns, ie. that the next
   12637 		 reloc is the __tls_get_addr call associated with
   12638 		 the current reloc.  Edit both insns.  */
   12639 	      if (input_section->has_tls_get_addr_call
   12640 		  && rel + 1 < relend
   12641 		  && branch_reloc_hash_match (input_bfd, rel + 1,
   12642 					      htab->tls_get_addr,
   12643 					      htab->tls_get_addr_fd))
   12644 		offset = rel[1].r_offset;
   12645 	      if ((tls_mask & tls_gd) != 0)
   12646 		{
   12647 		  /* IE */
   12648 		  insn1 = bfd_get_32 (output_bfd,
   12649 				      contents + rel->r_offset - d_offset);
   12650 		  insn1 &= (1 << 26) - (1 << 2);
   12651 		  insn1 |= 58 << 26;	/* ld */
   12652 		  insn2 = 0x7c636a14;	/* add 3,3,13 */
   12653 		  if (offset != (bfd_vma) -1)
   12654 		    rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
   12655 		  if ((tls_mask & TLS_EXPLICIT) == 0)
   12656 		    r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
   12657 			      + R_PPC64_GOT_TPREL16_DS);
   12658 		  else
   12659 		    r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
   12660 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12661 		}
   12662 	      else
   12663 		{
   12664 		  /* LE */
   12665 		  insn1 = 0x3c6d0000;	/* addis 3,13,0 */
   12666 		  insn2 = 0x38630000;	/* addi 3,3,0 */
   12667 		  if (tls_gd == 0)
   12668 		    {
   12669 		      /* Was an LD reloc.  */
   12670 		      if (toc_symndx)
   12671 			sec = local_sections[toc_symndx];
   12672 		      for (r_symndx = 0;
   12673 			   r_symndx < symtab_hdr->sh_info;
   12674 			   r_symndx++)
   12675 			if (local_sections[r_symndx] == sec)
   12676 			  break;
   12677 		      if (r_symndx >= symtab_hdr->sh_info)
   12678 			r_symndx = STN_UNDEF;
   12679 		      rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
   12680 		      if (r_symndx != STN_UNDEF)
   12681 			rel->r_addend -= (local_syms[r_symndx].st_value
   12682 					  + sec->output_offset
   12683 					  + sec->output_section->vma);
   12684 		    }
   12685 		  else if (toc_symndx != 0)
   12686 		    {
   12687 		      r_symndx = toc_symndx;
   12688 		      rel->r_addend = toc_addend;
   12689 		    }
   12690 		  r_type = R_PPC64_TPREL16_HA;
   12691 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12692 		  if (offset != (bfd_vma) -1)
   12693 		    {
   12694 		      rel[1].r_info = ELF64_R_INFO (r_symndx,
   12695 						    R_PPC64_TPREL16_LO);
   12696 		      rel[1].r_offset = offset + d_offset;
   12697 		      rel[1].r_addend = rel->r_addend;
   12698 		    }
   12699 		}
   12700 	      bfd_put_32 (output_bfd, insn1,
   12701 			  contents + rel->r_offset - d_offset);
   12702 	      if (offset != (bfd_vma) -1)
   12703 		{
   12704 		  insn3 = bfd_get_32 (output_bfd,
   12705 				      contents + offset + 4);
   12706 		  if (insn3 == NOP
   12707 		      || insn3 == CROR_151515 || insn3 == CROR_313131)
   12708 		    {
   12709 		      rel[1].r_offset += 4;
   12710 		      bfd_put_32 (output_bfd, insn2, contents + offset + 4);
   12711 		      insn2 = NOP;
   12712 		    }
   12713 		  bfd_put_32 (output_bfd, insn2, contents + offset);
   12714 		}
   12715 	      if ((tls_mask & tls_gd) == 0
   12716 		  && (tls_gd == 0 || toc_symndx != 0))
   12717 		{
   12718 		  /* We changed the symbol.  Start over in order
   12719 		     to get h, sym, sec etc. right.  */
   12720 		  rel--;
   12721 		  continue;
   12722 		}
   12723 	    }
   12724 	  break;
   12725 
   12726 	case R_PPC64_TLSGD:
   12727 	  if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
   12728 	    {
   12729 	      unsigned int insn2, insn3;
   12730 	      bfd_vma offset = rel->r_offset;
   12731 
   12732 	      if ((tls_mask & TLS_TPRELGD) != 0)
   12733 		{
   12734 		  /* IE */
   12735 		  r_type = R_PPC64_NONE;
   12736 		  insn2 = 0x7c636a14;	/* add 3,3,13 */
   12737 		}
   12738 	      else
   12739 		{
   12740 		  /* LE */
   12741 		  if (toc_symndx != 0)
   12742 		    {
   12743 		      r_symndx = toc_symndx;
   12744 		      rel->r_addend = toc_addend;
   12745 		    }
   12746 		  r_type = R_PPC64_TPREL16_LO;
   12747 		  rel->r_offset = offset + d_offset;
   12748 		  insn2 = 0x38630000;	/* addi 3,3,0 */
   12749 		}
   12750 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12751 	      /* Zap the reloc on the _tls_get_addr call too.  */
   12752 	      BFD_ASSERT (offset == rel[1].r_offset);
   12753 	      rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
   12754 	      insn3 = bfd_get_32 (output_bfd,
   12755 				  contents + offset + 4);
   12756 	      if (insn3 == NOP
   12757 		  || insn3 == CROR_151515 || insn3 == CROR_313131)
   12758 		{
   12759 		  rel->r_offset += 4;
   12760 		  bfd_put_32 (output_bfd, insn2, contents + offset + 4);
   12761 		  insn2 = NOP;
   12762 		}
   12763 	      bfd_put_32 (output_bfd, insn2, contents + offset);
   12764 	      if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
   12765 		{
   12766 		  rel--;
   12767 		  continue;
   12768 		}
   12769 	    }
   12770 	  break;
   12771 
   12772 	case R_PPC64_TLSLD:
   12773 	  if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
   12774 	    {
   12775 	      unsigned int insn2, insn3;
   12776 	      bfd_vma offset = rel->r_offset;
   12777 
   12778 	      if (toc_symndx)
   12779 		sec = local_sections[toc_symndx];
   12780 	      for (r_symndx = 0;
   12781 		   r_symndx < symtab_hdr->sh_info;
   12782 		   r_symndx++)
   12783 		if (local_sections[r_symndx] == sec)
   12784 		  break;
   12785 	      if (r_symndx >= symtab_hdr->sh_info)
   12786 		r_symndx = STN_UNDEF;
   12787 	      rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
   12788 	      if (r_symndx != STN_UNDEF)
   12789 		rel->r_addend -= (local_syms[r_symndx].st_value
   12790 				  + sec->output_offset
   12791 				  + sec->output_section->vma);
   12792 
   12793 	      r_type = R_PPC64_TPREL16_LO;
   12794 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12795 	      rel->r_offset = offset + d_offset;
   12796 	      /* Zap the reloc on the _tls_get_addr call too.  */
   12797 	      BFD_ASSERT (offset == rel[1].r_offset);
   12798 	      rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
   12799 	      insn2 = 0x38630000;	/* addi 3,3,0 */
   12800 	      insn3 = bfd_get_32 (output_bfd,
   12801 				  contents + offset + 4);
   12802 	      if (insn3 == NOP
   12803 		  || insn3 == CROR_151515 || insn3 == CROR_313131)
   12804 		{
   12805 		  rel->r_offset += 4;
   12806 		  bfd_put_32 (output_bfd, insn2, contents + offset + 4);
   12807 		  insn2 = NOP;
   12808 		}
   12809 	      bfd_put_32 (output_bfd, insn2, contents + offset);
   12810 	      rel--;
   12811 	      continue;
   12812 	    }
   12813 	  break;
   12814 
   12815 	case R_PPC64_DTPMOD64:
   12816 	  if (rel + 1 < relend
   12817 	      && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
   12818 	      && rel[1].r_offset == rel->r_offset + 8)
   12819 	    {
   12820 	      if ((tls_mask & TLS_GD) == 0)
   12821 		{
   12822 		  rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
   12823 		  if ((tls_mask & TLS_TPRELGD) != 0)
   12824 		    r_type = R_PPC64_TPREL64;
   12825 		  else
   12826 		    {
   12827 		      bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
   12828 		      r_type = R_PPC64_NONE;
   12829 		    }
   12830 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12831 		}
   12832 	    }
   12833 	  else
   12834 	    {
   12835 	      if ((tls_mask & TLS_LD) == 0)
   12836 		{
   12837 		  bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
   12838 		  r_type = R_PPC64_NONE;
   12839 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12840 		}
   12841 	    }
   12842 	  break;
   12843 
   12844 	case R_PPC64_TPREL64:
   12845 	  if ((tls_mask & TLS_TPREL) == 0)
   12846 	    {
   12847 	      r_type = R_PPC64_NONE;
   12848 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12849 	    }
   12850 	  break;
   12851 	}
   12852 
   12853       /* Handle other relocations that tweak non-addend part of insn.  */
   12854       insn = 0;
   12855       max_br_offset = 1 << 25;
   12856       addend = rel->r_addend;
   12857       reloc_dest = DEST_NORMAL;
   12858       switch (r_type)
   12859 	{
   12860 	default:
   12861 	  break;
   12862 
   12863 	case R_PPC64_TOCSAVE:
   12864 	  if (relocation + addend == (rel->r_offset
   12865 				      + input_section->output_offset
   12866 				      + input_section->output_section->vma)
   12867 	      && tocsave_find (htab, NO_INSERT,
   12868 			       &local_syms, rel, input_bfd))
   12869 	    {
   12870 	      insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   12871 	      if (insn == NOP
   12872 		  || insn == CROR_151515 || insn == CROR_313131)
   12873 		bfd_put_32 (input_bfd, STD_R2_40R1,
   12874 			    contents + rel->r_offset);
   12875 	    }
   12876 	  break;
   12877 
   12878 	  /* Branch taken prediction relocations.  */
   12879 	case R_PPC64_ADDR14_BRTAKEN:
   12880 	case R_PPC64_REL14_BRTAKEN:
   12881 	  insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
   12882 	  /* Fall thru.  */
   12883 
   12884 	  /* Branch not taken prediction relocations.  */
   12885 	case R_PPC64_ADDR14_BRNTAKEN:
   12886 	case R_PPC64_REL14_BRNTAKEN:
   12887 	  insn |= bfd_get_32 (output_bfd,
   12888 			      contents + rel->r_offset) & ~(0x01 << 21);
   12889 	  /* Fall thru.  */
   12890 
   12891 	case R_PPC64_REL14:
   12892 	  max_br_offset = 1 << 15;
   12893 	  /* Fall thru.  */
   12894 
   12895 	case R_PPC64_REL24:
   12896 	  /* Calls to functions with a different TOC, such as calls to
   12897 	     shared objects, need to alter the TOC pointer.  This is
   12898 	     done using a linkage stub.  A REL24 branching to these
   12899 	     linkage stubs needs to be followed by a nop, as the nop
   12900 	     will be replaced with an instruction to restore the TOC
   12901 	     base pointer.  */
   12902 	  fdh = h;
   12903 	  if (h != NULL
   12904 	      && h->oh != NULL
   12905 	      && h->oh->is_func_descriptor)
   12906 	    fdh = ppc_follow_link (h->oh);
   12907 	  stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
   12908 					   htab);
   12909 	  if (stub_entry != NULL
   12910 	      && (stub_entry->stub_type == ppc_stub_plt_call
   12911 		  || stub_entry->stub_type == ppc_stub_plt_call_r2save
   12912 		  || stub_entry->stub_type == ppc_stub_plt_branch_r2off
   12913 		  || stub_entry->stub_type == ppc_stub_long_branch_r2off))
   12914 	    {
   12915 	      bfd_boolean can_plt_call = FALSE;
   12916 
   12917 	      if (rel->r_offset + 8 <= input_section->size)
   12918 		{
   12919 		  unsigned long nop;
   12920 		  nop = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
   12921 		  if (nop == NOP
   12922 		      || nop == CROR_151515 || nop == CROR_313131)
   12923 		    {
   12924 		      if (h != NULL
   12925 			  && (h == htab->tls_get_addr_fd
   12926 			      || h == htab->tls_get_addr)
   12927 			  && !htab->no_tls_get_addr_opt)
   12928 			{
   12929 			  /* Special stub used, leave nop alone.  */
   12930 			}
   12931 		      else
   12932 			bfd_put_32 (input_bfd, LD_R2_40R1,
   12933 				    contents + rel->r_offset + 4);
   12934 		      can_plt_call = TRUE;
   12935 		    }
   12936 		}
   12937 
   12938 	      if (!can_plt_call)
   12939 		{
   12940 		  if (stub_entry->stub_type == ppc_stub_plt_call
   12941 		      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   12942 		    {
   12943 		      /* If this is a plain branch rather than a branch
   12944 			 and link, don't require a nop.  However, don't
   12945 			 allow tail calls in a shared library as they
   12946 			 will result in r2 being corrupted.  */
   12947 		      unsigned long br;
   12948 		      br = bfd_get_32 (input_bfd, contents + rel->r_offset);
   12949 		      if (info->executable && (br & 1) == 0)
   12950 			can_plt_call = TRUE;
   12951 		      else
   12952 			stub_entry = NULL;
   12953 		    }
   12954 		  else if (h != NULL
   12955 			   && strcmp (h->elf.root.root.string,
   12956 				      ".__libc_start_main") == 0)
   12957 		    {
   12958 		      /* Allow crt1 branch to go via a toc adjusting stub.  */
   12959 		      can_plt_call = TRUE;
   12960 		    }
   12961 		  else
   12962 		    {
   12963 		      info->callbacks->einfo
   12964 			(_("%P: %H: call to `%T' lacks nop, can't restore toc; "
   12965 			   "recompile with -fPIC"),
   12966 			   input_bfd, input_section, rel->r_offset, sym_name);
   12967 
   12968 		      bfd_set_error (bfd_error_bad_value);
   12969 		      ret = FALSE;
   12970 		    }
   12971 		}
   12972 
   12973 	      if (can_plt_call
   12974 		  && (stub_entry->stub_type == ppc_stub_plt_call
   12975 		      || stub_entry->stub_type == ppc_stub_plt_call_r2save))
   12976 		unresolved_reloc = FALSE;
   12977 	    }
   12978 
   12979 	  if ((stub_entry == NULL
   12980 	       || stub_entry->stub_type == ppc_stub_long_branch
   12981 	       || stub_entry->stub_type == ppc_stub_plt_branch)
   12982 	      && get_opd_info (sec) != NULL)
   12983 	    {
   12984 	      /* The branch destination is the value of the opd entry. */
   12985 	      bfd_vma off = (relocation + addend
   12986 			     - sec->output_section->vma
   12987 			     - sec->output_offset);
   12988 	      bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
   12989 	      if (dest != (bfd_vma) -1)
   12990 		{
   12991 		  relocation = dest;
   12992 		  addend = 0;
   12993 		  reloc_dest = DEST_OPD;
   12994 		}
   12995 	    }
   12996 
   12997 	  /* If the branch is out of reach we ought to have a long
   12998 	     branch stub.  */
   12999 	  from = (rel->r_offset
   13000 		  + input_section->output_offset
   13001 		  + input_section->output_section->vma);
   13002 
   13003 	  if (stub_entry != NULL
   13004 	      && (stub_entry->stub_type == ppc_stub_long_branch
   13005 		  || stub_entry->stub_type == ppc_stub_plt_branch)
   13006 	      && (r_type == R_PPC64_ADDR14_BRTAKEN
   13007 		  || r_type == R_PPC64_ADDR14_BRNTAKEN
   13008 		  || (relocation + addend - from + max_br_offset
   13009 		      < 2 * max_br_offset)))
   13010 	    /* Don't use the stub if this branch is in range.  */
   13011 	    stub_entry = NULL;
   13012 
   13013 	  if (stub_entry != NULL)
   13014 	    {
   13015 	      /* Munge up the value and addend so that we call the stub
   13016 		 rather than the procedure directly.  */
   13017 	      relocation = (stub_entry->stub_offset
   13018 			    + stub_entry->stub_sec->output_offset
   13019 			    + stub_entry->stub_sec->output_section->vma);
   13020 	      addend = 0;
   13021 	      reloc_dest = DEST_STUB;
   13022 
   13023  	      if ((stub_entry->stub_type == ppc_stub_plt_call
   13024 		   || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   13025 		  && (ALWAYS_EMIT_R2SAVE
   13026 		      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   13027 		  && rel + 1 < relend
   13028 		  && rel[1].r_offset == rel->r_offset + 4
   13029 		  && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
   13030 		relocation += 4;
   13031 	    }
   13032 
   13033 	  if (insn != 0)
   13034 	    {
   13035 	      if (is_isa_v2)
   13036 		{
   13037 		  /* Set 'a' bit.  This is 0b00010 in BO field for branch
   13038 		     on CR(BI) insns (BO == 001at or 011at), and 0b01000
   13039 		     for branch on CTR insns (BO == 1a00t or 1a01t).  */
   13040 		  if ((insn & (0x14 << 21)) == (0x04 << 21))
   13041 		    insn |= 0x02 << 21;
   13042 		  else if ((insn & (0x14 << 21)) == (0x10 << 21))
   13043 		    insn |= 0x08 << 21;
   13044 		  else
   13045 		    break;
   13046 		}
   13047 	      else
   13048 		{
   13049 		  /* Invert 'y' bit if not the default.  */
   13050 		  if ((bfd_signed_vma) (relocation + addend - from) < 0)
   13051 		    insn ^= 0x01 << 21;
   13052 		}
   13053 
   13054 	      bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
   13055 	    }
   13056 
   13057 	  /* NOP out calls to undefined weak functions.
   13058 	     We can thus call a weak function without first
   13059 	     checking whether the function is defined.  */
   13060 	  else if (h != NULL
   13061 		   && h->elf.root.type == bfd_link_hash_undefweak
   13062 		   && h->elf.dynindx == -1
   13063 		   && r_type == R_PPC64_REL24
   13064 		   && relocation == 0
   13065 		   && addend == 0)
   13066 	    {
   13067 	      bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
   13068 	      continue;
   13069 	    }
   13070 	  break;
   13071 	}
   13072 
   13073       /* Set `addend'.  */
   13074       tls_type = 0;
   13075       switch (r_type)
   13076 	{
   13077 	default:
   13078 	  info->callbacks->einfo
   13079 	    (_("%P: %B: unknown relocation type %d for `%T'\n"),
   13080 	     input_bfd, (int) r_type, sym_name);
   13081 
   13082 	  bfd_set_error (bfd_error_bad_value);
   13083 	  ret = FALSE;
   13084 	  continue;
   13085 
   13086 	case R_PPC64_NONE:
   13087 	case R_PPC64_TLS:
   13088 	case R_PPC64_TLSGD:
   13089 	case R_PPC64_TLSLD:
   13090 	case R_PPC64_TOCSAVE:
   13091 	case R_PPC64_GNU_VTINHERIT:
   13092 	case R_PPC64_GNU_VTENTRY:
   13093 	  continue;
   13094 
   13095 	  /* GOT16 relocations.  Like an ADDR16 using the symbol's
   13096 	     address in the GOT as relocation value instead of the
   13097 	     symbol's value itself.  Also, create a GOT entry for the
   13098 	     symbol and put the symbol value there.  */
   13099 	case R_PPC64_GOT_TLSGD16:
   13100 	case R_PPC64_GOT_TLSGD16_LO:
   13101 	case R_PPC64_GOT_TLSGD16_HI:
   13102 	case R_PPC64_GOT_TLSGD16_HA:
   13103 	  tls_type = TLS_TLS | TLS_GD;
   13104 	  goto dogot;
   13105 
   13106 	case R_PPC64_GOT_TLSLD16:
   13107 	case R_PPC64_GOT_TLSLD16_LO:
   13108 	case R_PPC64_GOT_TLSLD16_HI:
   13109 	case R_PPC64_GOT_TLSLD16_HA:
   13110 	  tls_type = TLS_TLS | TLS_LD;
   13111 	  goto dogot;
   13112 
   13113 	case R_PPC64_GOT_TPREL16_DS:
   13114 	case R_PPC64_GOT_TPREL16_LO_DS:
   13115 	case R_PPC64_GOT_TPREL16_HI:
   13116 	case R_PPC64_GOT_TPREL16_HA:
   13117 	  tls_type = TLS_TLS | TLS_TPREL;
   13118 	  goto dogot;
   13119 
   13120 	case R_PPC64_GOT_DTPREL16_DS:
   13121 	case R_PPC64_GOT_DTPREL16_LO_DS:
   13122 	case R_PPC64_GOT_DTPREL16_HI:
   13123 	case R_PPC64_GOT_DTPREL16_HA:
   13124 	  tls_type = TLS_TLS | TLS_DTPREL;
   13125 	  goto dogot;
   13126 
   13127 	case R_PPC64_GOT16:
   13128 	case R_PPC64_GOT16_LO:
   13129 	case R_PPC64_GOT16_HI:
   13130 	case R_PPC64_GOT16_HA:
   13131 	case R_PPC64_GOT16_DS:
   13132 	case R_PPC64_GOT16_LO_DS:
   13133 	dogot:
   13134 	  {
   13135 	    /* Relocation is to the entry for this symbol in the global
   13136 	       offset table.  */
   13137 	    asection *got;
   13138 	    bfd_vma *offp;
   13139 	    bfd_vma off;
   13140 	    unsigned long indx = 0;
   13141 	    struct got_entry *ent;
   13142 
   13143 	    if (tls_type == (TLS_TLS | TLS_LD)
   13144 		&& (h == NULL
   13145 		    || !h->elf.def_dynamic))
   13146 	      ent = ppc64_tlsld_got (input_bfd);
   13147 	    else
   13148 	      {
   13149 
   13150 		if (h != NULL)
   13151 		  {
   13152 		    bfd_boolean dyn = htab->elf.dynamic_sections_created;
   13153 		    if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
   13154 							  &h->elf)
   13155 			|| (info->shared
   13156 			    && SYMBOL_CALLS_LOCAL (info, &h->elf)))
   13157 		      /* This is actually a static link, or it is a
   13158 			 -Bsymbolic link and the symbol is defined
   13159 			 locally, or the symbol was forced to be local
   13160 			 because of a version file.  */
   13161 		      ;
   13162 		    else
   13163 		      {
   13164 			BFD_ASSERT (h->elf.dynindx != -1);
   13165 			indx = h->elf.dynindx;
   13166 			unresolved_reloc = FALSE;
   13167 		      }
   13168 		    ent = h->elf.got.glist;
   13169 		  }
   13170 		else
   13171 		  {
   13172 		    if (local_got_ents == NULL)
   13173 		      abort ();
   13174 		    ent = local_got_ents[r_symndx];
   13175 		  }
   13176 
   13177 		for (; ent != NULL; ent = ent->next)
   13178 		  if (ent->addend == orig_rel.r_addend
   13179 		      && ent->owner == input_bfd
   13180 		      && ent->tls_type == tls_type)
   13181 		    break;
   13182 	      }
   13183 
   13184 	    if (ent == NULL)
   13185 	      abort ();
   13186 	    if (ent->is_indirect)
   13187 	      ent = ent->got.ent;
   13188 	    offp = &ent->got.offset;
   13189 	    got = ppc64_elf_tdata (ent->owner)->got;
   13190 	    if (got == NULL)
   13191 	      abort ();
   13192 
   13193 	    /* The offset must always be a multiple of 8.  We use the
   13194 	       least significant bit to record whether we have already
   13195 	       processed this entry.  */
   13196 	    off = *offp;
   13197 	    if ((off & 1) != 0)
   13198 	      off &= ~1;
   13199 	    else
   13200 	      {
   13201 		/* Generate relocs for the dynamic linker, except in
   13202 		   the case of TLSLD where we'll use one entry per
   13203 		   module.  */
   13204 		asection *relgot;
   13205 		bfd_boolean ifunc;
   13206 
   13207 		*offp = off | 1;
   13208 		relgot = NULL;
   13209 		ifunc = (h != NULL
   13210 			 ? h->elf.type == STT_GNU_IFUNC
   13211 			 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
   13212 		if ((info->shared || indx != 0)
   13213 		    && (h == NULL
   13214 			|| (tls_type == (TLS_TLS | TLS_LD)
   13215 			    && !h->elf.def_dynamic)
   13216 			|| ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
   13217 			|| h->elf.root.type != bfd_link_hash_undefweak))
   13218 		  relgot = ppc64_elf_tdata (ent->owner)->relgot;
   13219 		else if (ifunc)
   13220 		  relgot = htab->reliplt;
   13221 		if (relgot != NULL)
   13222 		  {
   13223 		    outrel.r_offset = (got->output_section->vma
   13224 				       + got->output_offset
   13225 				       + off);
   13226 		    outrel.r_addend = addend;
   13227 		    if (tls_type & (TLS_LD | TLS_GD))
   13228 		      {
   13229 			outrel.r_addend = 0;
   13230 			outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
   13231 			if (tls_type == (TLS_TLS | TLS_GD))
   13232 			  {
   13233 			    loc = relgot->contents;
   13234 			    loc += (relgot->reloc_count++
   13235 				    * sizeof (Elf64_External_Rela));
   13236 			    bfd_elf64_swap_reloca_out (output_bfd,
   13237 						       &outrel, loc);
   13238 			    outrel.r_offset += 8;
   13239 			    outrel.r_addend = addend;
   13240 			    outrel.r_info
   13241 			      = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
   13242 			  }
   13243 		      }
   13244 		    else if (tls_type == (TLS_TLS | TLS_DTPREL))
   13245 		      outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
   13246 		    else if (tls_type == (TLS_TLS | TLS_TPREL))
   13247 		      outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
   13248 		    else if (indx != 0)
   13249 		      outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
   13250 		    else
   13251 		      {
   13252 			if (ifunc)
   13253 			  outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
   13254 			else
   13255 			  outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   13256 
   13257 			/* Write the .got section contents for the sake
   13258 			   of prelink.  */
   13259 			loc = got->contents + off;
   13260 			bfd_put_64 (output_bfd, outrel.r_addend + relocation,
   13261 				    loc);
   13262 		      }
   13263 
   13264 		    if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
   13265 		      {
   13266 			outrel.r_addend += relocation;
   13267 			if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
   13268 			  outrel.r_addend -= htab->elf.tls_sec->vma;
   13269 		      }
   13270 		    loc = relgot->contents;
   13271 		    loc += (relgot->reloc_count++
   13272 			    * sizeof (Elf64_External_Rela));
   13273 		    bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
   13274 		  }
   13275 
   13276 		/* Init the .got section contents here if we're not
   13277 		   emitting a reloc.  */
   13278 		else
   13279 		  {
   13280 		    relocation += addend;
   13281 		    if (tls_type == (TLS_TLS | TLS_LD))
   13282 		      relocation = 1;
   13283 		    else if (tls_type != 0)
   13284 		      {
   13285 			relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
   13286 			if (tls_type == (TLS_TLS | TLS_TPREL))
   13287 			  relocation += DTP_OFFSET - TP_OFFSET;
   13288 
   13289 			if (tls_type == (TLS_TLS | TLS_GD))
   13290 			  {
   13291 			    bfd_put_64 (output_bfd, relocation,
   13292 					got->contents + off + 8);
   13293 			    relocation = 1;
   13294 			  }
   13295 		      }
   13296 
   13297 		    bfd_put_64 (output_bfd, relocation,
   13298 				got->contents + off);
   13299 		  }
   13300 	      }
   13301 
   13302 	    if (off >= (bfd_vma) -2)
   13303 	      abort ();
   13304 
   13305 	    relocation = got->output_section->vma + got->output_offset + off;
   13306 	    addend = -(TOCstart + htab->stub_group[input_section->id].toc_off);
   13307 	  }
   13308 	  break;
   13309 
   13310 	case R_PPC64_PLT16_HA:
   13311 	case R_PPC64_PLT16_HI:
   13312 	case R_PPC64_PLT16_LO:
   13313 	case R_PPC64_PLT32:
   13314 	case R_PPC64_PLT64:
   13315 	  /* Relocation is to the entry for this symbol in the
   13316 	     procedure linkage table.  */
   13317 
   13318 	  /* Resolve a PLT reloc against a local symbol directly,
   13319 	     without using the procedure linkage table.  */
   13320 	  if (h == NULL)
   13321 	    break;
   13322 
   13323 	  /* It's possible that we didn't make a PLT entry for this
   13324 	     symbol.  This happens when statically linking PIC code,
   13325 	     or when using -Bsymbolic.  Go find a match if there is a
   13326 	     PLT entry.  */
   13327 	  if (htab->plt != NULL)
   13328 	    {
   13329 	      struct plt_entry *ent;
   13330 	      for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
   13331 		if (ent->addend == orig_rel.r_addend
   13332 		    && ent->plt.offset != (bfd_vma) -1)
   13333 		  {
   13334 		    relocation = (htab->plt->output_section->vma
   13335 				  + htab->plt->output_offset
   13336 				  + ent->plt.offset);
   13337 		    unresolved_reloc = FALSE;
   13338 		  }
   13339 	    }
   13340 	  break;
   13341 
   13342 	case R_PPC64_TOC:
   13343 	  /* Relocation value is TOC base.  */
   13344 	  relocation = TOCstart;
   13345 	  if (r_symndx == STN_UNDEF)
   13346 	    relocation += htab->stub_group[input_section->id].toc_off;
   13347 	  else if (unresolved_reloc)
   13348 	    ;
   13349 	  else if (sec != NULL && sec->id <= htab->top_id)
   13350 	    relocation += htab->stub_group[sec->id].toc_off;
   13351 	  else
   13352 	    unresolved_reloc = TRUE;
   13353 	  goto dodyn;
   13354 
   13355 	  /* TOC16 relocs.  We want the offset relative to the TOC base,
   13356 	     which is the address of the start of the TOC plus 0x8000.
   13357 	     The TOC consists of sections .got, .toc, .tocbss, and .plt,
   13358 	     in this order.  */
   13359 	case R_PPC64_TOC16:
   13360 	case R_PPC64_TOC16_LO:
   13361 	case R_PPC64_TOC16_HI:
   13362 	case R_PPC64_TOC16_DS:
   13363 	case R_PPC64_TOC16_LO_DS:
   13364 	case R_PPC64_TOC16_HA:
   13365 	  addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
   13366 	  break;
   13367 
   13368 	  /* Relocate against the beginning of the section.  */
   13369 	case R_PPC64_SECTOFF:
   13370 	case R_PPC64_SECTOFF_LO:
   13371 	case R_PPC64_SECTOFF_HI:
   13372 	case R_PPC64_SECTOFF_DS:
   13373 	case R_PPC64_SECTOFF_LO_DS:
   13374 	case R_PPC64_SECTOFF_HA:
   13375 	  if (sec != NULL)
   13376 	    addend -= sec->output_section->vma;
   13377 	  break;
   13378 
   13379 	case R_PPC64_REL16:
   13380 	case R_PPC64_REL16_LO:
   13381 	case R_PPC64_REL16_HI:
   13382 	case R_PPC64_REL16_HA:
   13383 	  break;
   13384 
   13385 	case R_PPC64_REL14:
   13386 	case R_PPC64_REL14_BRNTAKEN:
   13387 	case R_PPC64_REL14_BRTAKEN:
   13388 	case R_PPC64_REL24:
   13389 	  break;
   13390 
   13391 	case R_PPC64_TPREL16:
   13392 	case R_PPC64_TPREL16_LO:
   13393 	case R_PPC64_TPREL16_HI:
   13394 	case R_PPC64_TPREL16_HA:
   13395 	case R_PPC64_TPREL16_DS:
   13396 	case R_PPC64_TPREL16_LO_DS:
   13397 	case R_PPC64_TPREL16_HIGHER:
   13398 	case R_PPC64_TPREL16_HIGHERA:
   13399 	case R_PPC64_TPREL16_HIGHEST:
   13400 	case R_PPC64_TPREL16_HIGHESTA:
   13401 	  if (h != NULL
   13402 	      && h->elf.root.type == bfd_link_hash_undefweak
   13403 	      && h->elf.dynindx == -1)
   13404 	    {
   13405 	      /* Make this relocation against an undefined weak symbol
   13406 		 resolve to zero.  This is really just a tweak, since
   13407 		 code using weak externs ought to check that they are
   13408 		 defined before using them.  */
   13409 	      bfd_byte *p = contents + rel->r_offset - d_offset;
   13410 
   13411 	      insn = bfd_get_32 (output_bfd, p);
   13412 	      insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
   13413 	      if (insn != 0)
   13414 		bfd_put_32 (output_bfd, insn, p);
   13415 	      break;
   13416 	    }
   13417 	  addend -= htab->elf.tls_sec->vma + TP_OFFSET;
   13418 	  if (info->shared)
   13419 	    /* The TPREL16 relocs shouldn't really be used in shared
   13420 	       libs as they will result in DT_TEXTREL being set, but
   13421 	       support them anyway.  */
   13422 	    goto dodyn;
   13423 	  break;
   13424 
   13425 	case R_PPC64_DTPREL16:
   13426 	case R_PPC64_DTPREL16_LO:
   13427 	case R_PPC64_DTPREL16_HI:
   13428 	case R_PPC64_DTPREL16_HA:
   13429 	case R_PPC64_DTPREL16_DS:
   13430 	case R_PPC64_DTPREL16_LO_DS:
   13431 	case R_PPC64_DTPREL16_HIGHER:
   13432 	case R_PPC64_DTPREL16_HIGHERA:
   13433 	case R_PPC64_DTPREL16_HIGHEST:
   13434 	case R_PPC64_DTPREL16_HIGHESTA:
   13435 	  addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
   13436 	  break;
   13437 
   13438 	case R_PPC64_DTPMOD64:
   13439 	  relocation = 1;
   13440 	  addend = 0;
   13441 	  goto dodyn;
   13442 
   13443 	case R_PPC64_TPREL64:
   13444 	  addend -= htab->elf.tls_sec->vma + TP_OFFSET;
   13445 	  goto dodyn;
   13446 
   13447 	case R_PPC64_DTPREL64:
   13448 	  addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
   13449 	  /* Fall thru */
   13450 
   13451 	  /* Relocations that may need to be propagated if this is a
   13452 	     dynamic object.  */
   13453 	case R_PPC64_REL30:
   13454 	case R_PPC64_REL32:
   13455 	case R_PPC64_REL64:
   13456 	case R_PPC64_ADDR14:
   13457 	case R_PPC64_ADDR14_BRNTAKEN:
   13458 	case R_PPC64_ADDR14_BRTAKEN:
   13459 	case R_PPC64_ADDR16:
   13460 	case R_PPC64_ADDR16_DS:
   13461 	case R_PPC64_ADDR16_HA:
   13462 	case R_PPC64_ADDR16_HI:
   13463 	case R_PPC64_ADDR16_HIGHER:
   13464 	case R_PPC64_ADDR16_HIGHERA:
   13465 	case R_PPC64_ADDR16_HIGHEST:
   13466 	case R_PPC64_ADDR16_HIGHESTA:
   13467 	case R_PPC64_ADDR16_LO:
   13468 	case R_PPC64_ADDR16_LO_DS:
   13469 	case R_PPC64_ADDR24:
   13470 	case R_PPC64_ADDR32:
   13471 	case R_PPC64_ADDR64:
   13472 	case R_PPC64_UADDR16:
   13473 	case R_PPC64_UADDR32:
   13474 	case R_PPC64_UADDR64:
   13475 	dodyn:
   13476 	  if ((input_section->flags & SEC_ALLOC) == 0)
   13477 	    break;
   13478 
   13479 	  if (NO_OPD_RELOCS && is_opd)
   13480 	    break;
   13481 
   13482 	  if ((info->shared
   13483 	       && (h == NULL
   13484 		   || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
   13485 		   || h->elf.root.type != bfd_link_hash_undefweak)
   13486 	       && (must_be_dyn_reloc (info, r_type)
   13487 		   || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
   13488 	      || (ELIMINATE_COPY_RELOCS
   13489 		  && !info->shared
   13490 		  && h != NULL
   13491 		  && h->elf.dynindx != -1
   13492 		  && !h->elf.non_got_ref
   13493 		  && !h->elf.def_regular)
   13494 	      || (!info->shared
   13495 		  && (h != NULL
   13496 		      ? h->elf.type == STT_GNU_IFUNC
   13497 		      : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
   13498 	    {
   13499 	      bfd_boolean skip, relocate;
   13500 	      asection *sreloc;
   13501 	      bfd_vma out_off;
   13502 
   13503 	      /* When generating a dynamic object, these relocations
   13504 		 are copied into the output file to be resolved at run
   13505 		 time.  */
   13506 
   13507 	      skip = FALSE;
   13508 	      relocate = FALSE;
   13509 
   13510 	      out_off = _bfd_elf_section_offset (output_bfd, info,
   13511 						 input_section, rel->r_offset);
   13512 	      if (out_off == (bfd_vma) -1)
   13513 		skip = TRUE;
   13514 	      else if (out_off == (bfd_vma) -2)
   13515 		skip = TRUE, relocate = TRUE;
   13516 	      out_off += (input_section->output_section->vma
   13517 			  + input_section->output_offset);
   13518 	      outrel.r_offset = out_off;
   13519 	      outrel.r_addend = rel->r_addend;
   13520 
   13521 	      /* Optimize unaligned reloc use.  */
   13522 	      if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
   13523 		  || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
   13524 		r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
   13525 	      else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
   13526 		       || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
   13527 		r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
   13528 	      else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
   13529 		       || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
   13530 		r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
   13531 
   13532 	      if (skip)
   13533 		memset (&outrel, 0, sizeof outrel);
   13534 	      else if (!SYMBOL_CALLS_LOCAL (info, &h->elf)
   13535 		       && !is_opd
   13536 		       && r_type != R_PPC64_TOC)
   13537 		{
   13538 		  BFD_ASSERT (h->elf.dynindx != -1);
   13539 		  outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
   13540 		}
   13541 	      else
   13542 		{
   13543 		  /* This symbol is local, or marked to become local,
   13544 		     or this is an opd section reloc which must point
   13545 		     at a local function.  */
   13546 		  outrel.r_addend += relocation;
   13547 		  if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
   13548 		    {
   13549 		      if (is_opd && h != NULL)
   13550 			{
   13551 			  /* Lie about opd entries.  This case occurs
   13552 			     when building shared libraries and we
   13553 			     reference a function in another shared
   13554 			     lib.  The same thing happens for a weak
   13555 			     definition in an application that's
   13556 			     overridden by a strong definition in a
   13557 			     shared lib.  (I believe this is a generic
   13558 			     bug in binutils handling of weak syms.)
   13559 			     In these cases we won't use the opd
   13560 			     entry in this lib.  */
   13561 			  unresolved_reloc = FALSE;
   13562 			}
   13563 		      if (!is_opd
   13564 			  && r_type == R_PPC64_ADDR64
   13565 			  && (h != NULL
   13566 			      ? h->elf.type == STT_GNU_IFUNC
   13567 			      : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
   13568 			outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
   13569 		      else
   13570 			{
   13571 			  outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   13572 
   13573 			  /* We need to relocate .opd contents for ld.so.
   13574 			     Prelink also wants simple and consistent rules
   13575 			     for relocs.  This make all RELATIVE relocs have
   13576 			     *r_offset equal to r_addend.  */
   13577 			  relocate = TRUE;
   13578 			}
   13579 		    }
   13580 		  else
   13581 		    {
   13582 		      long indx = 0;
   13583 
   13584 		      if (h != NULL
   13585 			  ? h->elf.type == STT_GNU_IFUNC
   13586 			  : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
   13587 			{
   13588 			  info->callbacks->einfo
   13589 			    (_("%P: %H: %s for indirect "
   13590 			       "function `%T' unsupported\n"),
   13591 			     input_bfd, input_section, rel->r_offset,
   13592 			     ppc64_elf_howto_table[r_type]->name,
   13593 			     sym_name);
   13594 			  ret = FALSE;
   13595 			}
   13596 		      else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
   13597 			;
   13598 		      else if (sec == NULL || sec->owner == NULL)
   13599 			{
   13600 			  bfd_set_error (bfd_error_bad_value);
   13601 			  return FALSE;
   13602 			}
   13603 		      else
   13604 			{
   13605 			  asection *osec;
   13606 
   13607 			  osec = sec->output_section;
   13608 			  indx = elf_section_data (osec)->dynindx;
   13609 
   13610 			  if (indx == 0)
   13611 			    {
   13612 			      if ((osec->flags & SEC_READONLY) == 0
   13613 				  && htab->elf.data_index_section != NULL)
   13614 				osec = htab->elf.data_index_section;
   13615 			      else
   13616 				osec = htab->elf.text_index_section;
   13617 			      indx = elf_section_data (osec)->dynindx;
   13618 			    }
   13619 			  BFD_ASSERT (indx != 0);
   13620 
   13621 			  /* We are turning this relocation into one
   13622 			     against a section symbol, so subtract out
   13623 			     the output section's address but not the
   13624 			     offset of the input section in the output
   13625 			     section.  */
   13626 			  outrel.r_addend -= osec->vma;
   13627 			}
   13628 
   13629 		      outrel.r_info = ELF64_R_INFO (indx, r_type);
   13630 		    }
   13631 		}
   13632 
   13633 	      sreloc = elf_section_data (input_section)->sreloc;
   13634 	      if (!htab->elf.dynamic_sections_created)
   13635 		sreloc = htab->reliplt;
   13636 	      if (sreloc == NULL)
   13637 		abort ();
   13638 
   13639 	      if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
   13640 		  >= sreloc->size)
   13641 		abort ();
   13642 	      loc = sreloc->contents;
   13643 	      loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
   13644 	      bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
   13645 
   13646 	      /* If this reloc is against an external symbol, it will
   13647 		 be computed at runtime, so there's no need to do
   13648 		 anything now.  However, for the sake of prelink ensure
   13649 		 that the section contents are a known value.  */
   13650 	      if (! relocate)
   13651 		{
   13652 		  unresolved_reloc = FALSE;
   13653 		  /* The value chosen here is quite arbitrary as ld.so
   13654 		     ignores section contents except for the special
   13655 		     case of .opd where the contents might be accessed
   13656 		     before relocation.  Choose zero, as that won't
   13657 		     cause reloc overflow.  */
   13658 		  relocation = 0;
   13659 		  addend = 0;
   13660 		  /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
   13661 		     to improve backward compatibility with older
   13662 		     versions of ld.  */
   13663 		  if (r_type == R_PPC64_ADDR64)
   13664 		    addend = outrel.r_addend;
   13665 		  /* Adjust pc_relative relocs to have zero in *r_offset.  */
   13666 		  else if (ppc64_elf_howto_table[r_type]->pc_relative)
   13667 		    addend = (input_section->output_section->vma
   13668 			      + input_section->output_offset
   13669 			      + rel->r_offset);
   13670 		}
   13671 	    }
   13672 	  break;
   13673 
   13674 	case R_PPC64_COPY:
   13675 	case R_PPC64_GLOB_DAT:
   13676 	case R_PPC64_JMP_SLOT:
   13677 	case R_PPC64_JMP_IREL:
   13678 	case R_PPC64_RELATIVE:
   13679 	  /* We shouldn't ever see these dynamic relocs in relocatable
   13680 	     files.  */
   13681 	  /* Fall through.  */
   13682 
   13683 	case R_PPC64_PLTGOT16:
   13684 	case R_PPC64_PLTGOT16_DS:
   13685 	case R_PPC64_PLTGOT16_HA:
   13686 	case R_PPC64_PLTGOT16_HI:
   13687 	case R_PPC64_PLTGOT16_LO:
   13688 	case R_PPC64_PLTGOT16_LO_DS:
   13689 	case R_PPC64_PLTREL32:
   13690 	case R_PPC64_PLTREL64:
   13691 	  /* These ones haven't been implemented yet.  */
   13692 
   13693 	  info->callbacks->einfo
   13694 	    (_("%P: %B: %s is not supported for `%T'\n"),
   13695 	     input_bfd,
   13696 	     ppc64_elf_howto_table[r_type]->name, sym_name);
   13697 
   13698 	  bfd_set_error (bfd_error_invalid_operation);
   13699 	  ret = FALSE;
   13700 	  continue;
   13701 	}
   13702 
   13703       /* Multi-instruction sequences that access the TOC can be
   13704 	 optimized, eg. addis ra,r2,0; addi rb,ra,x;
   13705 	 to             nop;           addi rb,r2,x;  */
   13706       switch (r_type)
   13707 	{
   13708 	default:
   13709 	  break;
   13710 
   13711 	case R_PPC64_GOT_TLSLD16_HI:
   13712 	case R_PPC64_GOT_TLSGD16_HI:
   13713 	case R_PPC64_GOT_TPREL16_HI:
   13714 	case R_PPC64_GOT_DTPREL16_HI:
   13715 	case R_PPC64_GOT16_HI:
   13716 	case R_PPC64_TOC16_HI:
   13717 	  /* These relocs would only be useful if building up an
   13718 	     offset to later add to r2, perhaps in an indexed
   13719 	     addressing mode instruction.  Don't try to optimize.
   13720 	     Unfortunately, the possibility of someone building up an
   13721 	     offset like this or even with the HA relocs, means that
   13722 	     we need to check the high insn when optimizing the low
   13723 	     insn.  */
   13724 	  break;
   13725 
   13726 	case R_PPC64_GOT_TLSLD16_HA:
   13727 	case R_PPC64_GOT_TLSGD16_HA:
   13728 	case R_PPC64_GOT_TPREL16_HA:
   13729 	case R_PPC64_GOT_DTPREL16_HA:
   13730 	case R_PPC64_GOT16_HA:
   13731 	case R_PPC64_TOC16_HA:
   13732 	  if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
   13733 	      && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
   13734 	    {
   13735 	      bfd_byte *p = contents + (rel->r_offset & ~3);
   13736 	      bfd_put_32 (input_bfd, NOP, p);
   13737 	    }
   13738 	  break;
   13739 
   13740 	case R_PPC64_GOT_TLSLD16_LO:
   13741 	case R_PPC64_GOT_TLSGD16_LO:
   13742 	case R_PPC64_GOT_TPREL16_LO_DS:
   13743 	case R_PPC64_GOT_DTPREL16_LO_DS:
   13744 	case R_PPC64_GOT16_LO:
   13745 	case R_PPC64_GOT16_LO_DS:
   13746 	case R_PPC64_TOC16_LO:
   13747 	case R_PPC64_TOC16_LO_DS:
   13748 	  if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
   13749 	      && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
   13750 	    {
   13751 	      bfd_byte *p = contents + (rel->r_offset & ~3);
   13752 	      insn = bfd_get_32 (input_bfd, p);
   13753 	      if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
   13754 		{
   13755 		  /* Transform addic to addi when we change reg.  */
   13756 		  insn &= ~((0x3f << 26) | (0x1f << 16));
   13757 		  insn |= (14u << 26) | (2 << 16);
   13758 		}
   13759 	      else
   13760 		{
   13761 		  insn &= ~(0x1f << 16);
   13762 		  insn |= 2 << 16;
   13763 		}
   13764 	      bfd_put_32 (input_bfd, insn, p);
   13765 	    }
   13766 	  break;
   13767 	}
   13768 
   13769       /* Do any further special processing.  */
   13770       switch (r_type)
   13771 	{
   13772 	default:
   13773 	  break;
   13774 
   13775 	case R_PPC64_ADDR16_HA:
   13776 	case R_PPC64_REL16_HA:
   13777 	case R_PPC64_ADDR16_HIGHERA:
   13778 	case R_PPC64_ADDR16_HIGHESTA:
   13779 	case R_PPC64_TOC16_HA:
   13780 	case R_PPC64_SECTOFF_HA:
   13781 	case R_PPC64_TPREL16_HA:
   13782 	case R_PPC64_DTPREL16_HA:
   13783 	case R_PPC64_TPREL16_HIGHER:
   13784 	case R_PPC64_TPREL16_HIGHERA:
   13785 	case R_PPC64_TPREL16_HIGHEST:
   13786 	case R_PPC64_TPREL16_HIGHESTA:
   13787 	case R_PPC64_DTPREL16_HIGHER:
   13788 	case R_PPC64_DTPREL16_HIGHERA:
   13789 	case R_PPC64_DTPREL16_HIGHEST:
   13790 	case R_PPC64_DTPREL16_HIGHESTA:
   13791 	  /* It's just possible that this symbol is a weak symbol
   13792 	     that's not actually defined anywhere. In that case,
   13793 	     'sec' would be NULL, and we should leave the symbol
   13794 	     alone (it will be set to zero elsewhere in the link).  */
   13795 	  if (sec == NULL)
   13796 	    break;
   13797 	  /* Fall thru */
   13798 
   13799 	case R_PPC64_GOT16_HA:
   13800 	case R_PPC64_PLTGOT16_HA:
   13801 	case R_PPC64_PLT16_HA:
   13802 	case R_PPC64_GOT_TLSGD16_HA:
   13803 	case R_PPC64_GOT_TLSLD16_HA:
   13804 	case R_PPC64_GOT_TPREL16_HA:
   13805 	case R_PPC64_GOT_DTPREL16_HA:
   13806 	  /* Add 0x10000 if sign bit in 0:15 is set.
   13807 	     Bits 0:15 are not used.  */
   13808 	  addend += 0x8000;
   13809 	  break;
   13810 
   13811 	case R_PPC64_ADDR16_DS:
   13812 	case R_PPC64_ADDR16_LO_DS:
   13813 	case R_PPC64_GOT16_DS:
   13814 	case R_PPC64_GOT16_LO_DS:
   13815 	case R_PPC64_PLT16_LO_DS:
   13816 	case R_PPC64_SECTOFF_DS:
   13817 	case R_PPC64_SECTOFF_LO_DS:
   13818 	case R_PPC64_TOC16_DS:
   13819 	case R_PPC64_TOC16_LO_DS:
   13820 	case R_PPC64_PLTGOT16_DS:
   13821 	case R_PPC64_PLTGOT16_LO_DS:
   13822 	case R_PPC64_GOT_TPREL16_DS:
   13823 	case R_PPC64_GOT_TPREL16_LO_DS:
   13824 	case R_PPC64_GOT_DTPREL16_DS:
   13825 	case R_PPC64_GOT_DTPREL16_LO_DS:
   13826 	case R_PPC64_TPREL16_DS:
   13827 	case R_PPC64_TPREL16_LO_DS:
   13828 	case R_PPC64_DTPREL16_DS:
   13829 	case R_PPC64_DTPREL16_LO_DS:
   13830 	  insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
   13831 	  mask = 3;
   13832 	  /* If this reloc is against an lq insn, then the value must be
   13833 	     a multiple of 16.  This is somewhat of a hack, but the
   13834 	     "correct" way to do this by defining _DQ forms of all the
   13835 	     _DS relocs bloats all reloc switches in this file.  It
   13836 	     doesn't seem to make much sense to use any of these relocs
   13837 	     in data, so testing the insn should be safe.  */
   13838 	  if ((insn & (0x3f << 26)) == (56u << 26))
   13839 	    mask = 15;
   13840 	  if (((relocation + addend) & mask) != 0)
   13841 	    {
   13842 	      info->callbacks->einfo
   13843 		(_("%P: %H: error: %s not a multiple of %u\n"),
   13844 		 input_bfd, input_section, rel->r_offset,
   13845 		 ppc64_elf_howto_table[r_type]->name,
   13846 		 mask + 1);
   13847 	      bfd_set_error (bfd_error_bad_value);
   13848 	      ret = FALSE;
   13849 	      continue;
   13850 	    }
   13851 	  break;
   13852 	}
   13853 
   13854       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
   13855 	 because such sections are not SEC_ALLOC and thus ld.so will
   13856 	 not process them.  */
   13857       if (unresolved_reloc
   13858 	  && !((input_section->flags & SEC_DEBUGGING) != 0
   13859 	       && h->elf.def_dynamic)
   13860 	  && _bfd_elf_section_offset (output_bfd, info, input_section,
   13861 				      rel->r_offset) != (bfd_vma) -1)
   13862 	{
   13863 	  info->callbacks->einfo
   13864 	    (_("%P: %H: unresolvable %s against `%T'\n"),
   13865 	     input_bfd, input_section, rel->r_offset,
   13866 	     ppc64_elf_howto_table[(int) r_type]->name,
   13867 	     h->elf.root.root.string);
   13868 	  ret = FALSE;
   13869 	}
   13870 
   13871       r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
   13872 				    input_bfd,
   13873 				    input_section,
   13874 				    contents,
   13875 				    rel->r_offset,
   13876 				    relocation,
   13877 				    addend);
   13878 
   13879       if (r != bfd_reloc_ok)
   13880 	{
   13881 	  char *more_info = NULL;
   13882 	  const char *reloc_name = ppc64_elf_howto_table[r_type]->name;
   13883 
   13884 	  if (reloc_dest != DEST_NORMAL)
   13885 	    {
   13886 	      more_info = bfd_malloc (strlen (reloc_name) + 8);
   13887 	      if (more_info != NULL)
   13888 		{
   13889 		  strcpy (more_info, reloc_name);
   13890 		  strcat (more_info, (reloc_dest == DEST_OPD
   13891 				      ? " (OPD)" : " (stub)"));
   13892 		  reloc_name = more_info;
   13893 		}
   13894 	    }
   13895 
   13896 	  if (r == bfd_reloc_overflow)
   13897 	    {
   13898 	      if (warned)
   13899 		continue;
   13900 	      if (h != NULL
   13901 		  && h->elf.root.type == bfd_link_hash_undefweak
   13902 		  && ppc64_elf_howto_table[r_type]->pc_relative)
   13903 		{
   13904 		  /* Assume this is a call protected by other code that
   13905 		     detects the symbol is undefined.  If this is the case,
   13906 		     we can safely ignore the overflow.  If not, the
   13907 		     program is hosed anyway, and a little warning isn't
   13908 		     going to help.  */
   13909 
   13910 		  continue;
   13911 		}
   13912 
   13913 	      if (!((*info->callbacks->reloc_overflow)
   13914 		    (info, &h->elf.root, sym_name,
   13915 		     reloc_name, orig_rel.r_addend,
   13916 		     input_bfd, input_section, rel->r_offset)))
   13917 		return FALSE;
   13918 	    }
   13919 	  else
   13920 	    {
   13921 	      info->callbacks->einfo
   13922 		(_("%P: %H: %s against `%T': error %d\n"),
   13923 		 input_bfd, input_section, rel->r_offset,
   13924 		 reloc_name, sym_name, (int) r);
   13925 	      ret = FALSE;
   13926 	    }
   13927 	  if (more_info != NULL)
   13928 	    free (more_info);
   13929 	}
   13930     }
   13931 
   13932   /* If we're emitting relocations, then shortly after this function
   13933      returns, reloc offsets and addends for this section will be
   13934      adjusted.  Worse, reloc symbol indices will be for the output
   13935      file rather than the input.  Save a copy of the relocs for
   13936      opd_entry_value.  */
   13937   if (is_opd && (info->emitrelocations || info->relocatable))
   13938     {
   13939       bfd_size_type amt;
   13940       amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
   13941       rel = bfd_alloc (input_bfd, amt);
   13942       BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd_relocs == NULL);
   13943       ppc64_elf_tdata (input_bfd)->opd_relocs = rel;
   13944       if (rel == NULL)
   13945 	return FALSE;
   13946       memcpy (rel, relocs, amt);
   13947     }
   13948   return ret;
   13949 }
   13950 
   13951 /* Adjust the value of any local symbols in opd sections.  */
   13952 
   13953 static int
   13954 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
   13955 			      const char *name ATTRIBUTE_UNUSED,
   13956 			      Elf_Internal_Sym *elfsym,
   13957 			      asection *input_sec,
   13958 			      struct elf_link_hash_entry *h)
   13959 {
   13960   struct _opd_sec_data *opd;
   13961   long adjust;
   13962   bfd_vma value;
   13963 
   13964   if (h != NULL)
   13965     return 1;
   13966 
   13967   opd = get_opd_info (input_sec);
   13968   if (opd == NULL || opd->adjust == NULL)
   13969     return 1;
   13970 
   13971   value = elfsym->st_value - input_sec->output_offset;
   13972   if (!info->relocatable)
   13973     value -= input_sec->output_section->vma;
   13974 
   13975   adjust = opd->adjust[value / 8];
   13976   if (adjust == -1)
   13977     return 2;
   13978 
   13979   elfsym->st_value += adjust;
   13980   return 1;
   13981 }
   13982 
   13983 /* Finish up dynamic symbol handling.  We set the contents of various
   13984    dynamic sections here.  */
   13985 
   13986 static bfd_boolean
   13987 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
   13988 				 struct bfd_link_info *info,
   13989 				 struct elf_link_hash_entry *h,
   13990 				 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
   13991 {
   13992   struct ppc_link_hash_table *htab;
   13993   struct plt_entry *ent;
   13994   Elf_Internal_Rela rela;
   13995   bfd_byte *loc;
   13996 
   13997   htab = ppc_hash_table (info);
   13998   if (htab == NULL)
   13999     return FALSE;
   14000 
   14001   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
   14002     if (ent->plt.offset != (bfd_vma) -1)
   14003       {
   14004 	/* This symbol has an entry in the procedure linkage
   14005 	   table.  Set it up.  */
   14006 	if (!htab->elf.dynamic_sections_created
   14007 	    || h->dynindx == -1)
   14008 	  {
   14009 	    BFD_ASSERT (h->type == STT_GNU_IFUNC
   14010 			&& h->def_regular
   14011 			&& (h->root.type == bfd_link_hash_defined
   14012 			    || h->root.type == bfd_link_hash_defweak));
   14013 	    rela.r_offset = (htab->iplt->output_section->vma
   14014 			     + htab->iplt->output_offset
   14015 			     + ent->plt.offset);
   14016 	    rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
   14017 	    rela.r_addend = (h->root.u.def.value
   14018 			     + h->root.u.def.section->output_offset
   14019 			     + h->root.u.def.section->output_section->vma
   14020 			     + ent->addend);
   14021 	    loc = (htab->reliplt->contents
   14022 		   + (htab->reliplt->reloc_count++
   14023 		      * sizeof (Elf64_External_Rela)));
   14024 	  }
   14025 	else
   14026 	  {
   14027 	    rela.r_offset = (htab->plt->output_section->vma
   14028 			     + htab->plt->output_offset
   14029 			     + ent->plt.offset);
   14030 	    rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
   14031 	    rela.r_addend = ent->addend;
   14032 	    loc = (htab->relplt->contents
   14033 		   + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE)
   14034 		      / (PLT_ENTRY_SIZE / sizeof (Elf64_External_Rela))));
   14035 	  }
   14036 	bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
   14037       }
   14038 
   14039   if (h->needs_copy)
   14040     {
   14041       /* This symbol needs a copy reloc.  Set it up.  */
   14042 
   14043       if (h->dynindx == -1
   14044 	  || (h->root.type != bfd_link_hash_defined
   14045 	      && h->root.type != bfd_link_hash_defweak)
   14046 	  || htab->relbss == NULL)
   14047 	abort ();
   14048 
   14049       rela.r_offset = (h->root.u.def.value
   14050 		       + h->root.u.def.section->output_section->vma
   14051 		       + h->root.u.def.section->output_offset);
   14052       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
   14053       rela.r_addend = 0;
   14054       loc = htab->relbss->contents;
   14055       loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
   14056       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
   14057     }
   14058 
   14059   return TRUE;
   14060 }
   14061 
   14062 /* Used to decide how to sort relocs in an optimal manner for the
   14063    dynamic linker, before writing them out.  */
   14064 
   14065 static enum elf_reloc_type_class
   14066 ppc64_elf_reloc_type_class (const Elf_Internal_Rela *rela)
   14067 {
   14068   enum elf_ppc64_reloc_type r_type;
   14069 
   14070   r_type = ELF64_R_TYPE (rela->r_info);
   14071   switch (r_type)
   14072     {
   14073     case R_PPC64_RELATIVE:
   14074       return reloc_class_relative;
   14075     case R_PPC64_JMP_SLOT:
   14076       return reloc_class_plt;
   14077     case R_PPC64_COPY:
   14078       return reloc_class_copy;
   14079     default:
   14080       return reloc_class_normal;
   14081     }
   14082 }
   14083 
   14084 /* Finish up the dynamic sections.  */
   14085 
   14086 static bfd_boolean
   14087 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
   14088 				   struct bfd_link_info *info)
   14089 {
   14090   struct ppc_link_hash_table *htab;
   14091   bfd *dynobj;
   14092   asection *sdyn;
   14093 
   14094   htab = ppc_hash_table (info);
   14095   if (htab == NULL)
   14096     return FALSE;
   14097 
   14098   dynobj = htab->elf.dynobj;
   14099   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
   14100 
   14101   if (htab->elf.dynamic_sections_created)
   14102     {
   14103       Elf64_External_Dyn *dyncon, *dynconend;
   14104 
   14105       if (sdyn == NULL || htab->got == NULL)
   14106 	abort ();
   14107 
   14108       dyncon = (Elf64_External_Dyn *) sdyn->contents;
   14109       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
   14110       for (; dyncon < dynconend; dyncon++)
   14111 	{
   14112 	  Elf_Internal_Dyn dyn;
   14113 	  asection *s;
   14114 
   14115 	  bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
   14116 
   14117 	  switch (dyn.d_tag)
   14118 	    {
   14119 	    default:
   14120 	      continue;
   14121 
   14122 	    case DT_PPC64_GLINK:
   14123 	      s = htab->glink;
   14124 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   14125 	      /* We stupidly defined DT_PPC64_GLINK to be the start
   14126 		 of glink rather than the first entry point, which is
   14127 		 what ld.so needs, and now have a bigger stub to
   14128 		 support automatic multiple TOCs.  */
   14129 	      dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
   14130 	      break;
   14131 
   14132 	    case DT_PPC64_OPD:
   14133 	      s = bfd_get_section_by_name (output_bfd, ".opd");
   14134 	      if (s == NULL)
   14135 		continue;
   14136 	      dyn.d_un.d_ptr = s->vma;
   14137 	      break;
   14138 
   14139 	    case DT_PPC64_OPDSZ:
   14140 	      s = bfd_get_section_by_name (output_bfd, ".opd");
   14141 	      if (s == NULL)
   14142 		continue;
   14143 	      dyn.d_un.d_val = s->size;
   14144 	      break;
   14145 
   14146 	    case DT_PLTGOT:
   14147 	      s = htab->plt;
   14148 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   14149 	      break;
   14150 
   14151 	    case DT_JMPREL:
   14152 	      s = htab->relplt;
   14153 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   14154 	      break;
   14155 
   14156 	    case DT_PLTRELSZ:
   14157 	      dyn.d_un.d_val = htab->relplt->size;
   14158 	      break;
   14159 
   14160 	    case DT_RELASZ:
   14161 	      /* Don't count procedure linkage table relocs in the
   14162 		 overall reloc count.  */
   14163 	      s = htab->relplt;
   14164 	      if (s == NULL)
   14165 		continue;
   14166 	      dyn.d_un.d_val -= s->size;
   14167 	      break;
   14168 
   14169 	    case DT_RELA:
   14170 	      /* We may not be using the standard ELF linker script.
   14171 		 If .rela.plt is the first .rela section, we adjust
   14172 		 DT_RELA to not include it.  */
   14173 	      s = htab->relplt;
   14174 	      if (s == NULL)
   14175 		continue;
   14176 	      if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
   14177 		continue;
   14178 	      dyn.d_un.d_ptr += s->size;
   14179 	      break;
   14180 	    }
   14181 
   14182 	  bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
   14183 	}
   14184     }
   14185 
   14186   if (htab->got != NULL && htab->got->size != 0)
   14187     {
   14188       /* Fill in the first entry in the global offset table.
   14189 	 We use it to hold the link-time TOCbase.  */
   14190       bfd_put_64 (output_bfd,
   14191 		  elf_gp (output_bfd) + TOC_BASE_OFF,
   14192 		  htab->got->contents);
   14193 
   14194       /* Set .got entry size.  */
   14195       elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
   14196     }
   14197 
   14198   if (htab->plt != NULL && htab->plt->size != 0)
   14199     {
   14200       /* Set .plt entry size.  */
   14201       elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
   14202 	= PLT_ENTRY_SIZE;
   14203     }
   14204 
   14205   /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
   14206      brlt ourselves if emitrelocations.  */
   14207   if (htab->brlt != NULL
   14208       && htab->brlt->reloc_count != 0
   14209       && !_bfd_elf_link_output_relocs (output_bfd,
   14210 				       htab->brlt,
   14211 				       elf_section_data (htab->brlt)->rela.hdr,
   14212 				       elf_section_data (htab->brlt)->relocs,
   14213 				       NULL))
   14214     return FALSE;
   14215 
   14216   if (htab->glink != NULL
   14217       && htab->glink->reloc_count != 0
   14218       && !_bfd_elf_link_output_relocs (output_bfd,
   14219 				       htab->glink,
   14220 				       elf_section_data (htab->glink)->rela.hdr,
   14221 				       elf_section_data (htab->glink)->relocs,
   14222 				       NULL))
   14223     return FALSE;
   14224 
   14225 
   14226   if (htab->glink_eh_frame != NULL
   14227       && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
   14228       && !_bfd_elf_write_section_eh_frame (output_bfd, info,
   14229 					   htab->glink_eh_frame,
   14230 					   htab->glink_eh_frame->contents))
   14231     return FALSE;
   14232 
   14233   /* We need to handle writing out multiple GOT sections ourselves,
   14234      since we didn't add them to DYNOBJ.  We know dynobj is the first
   14235      bfd.  */
   14236   while ((dynobj = dynobj->link_next) != NULL)
   14237     {
   14238       asection *s;
   14239 
   14240       if (!is_ppc64_elf (dynobj))
   14241 	continue;
   14242 
   14243       s = ppc64_elf_tdata (dynobj)->got;
   14244       if (s != NULL
   14245 	  && s->size != 0
   14246 	  && s->output_section != bfd_abs_section_ptr
   14247 	  && !bfd_set_section_contents (output_bfd, s->output_section,
   14248 					s->contents, s->output_offset,
   14249 					s->size))
   14250 	return FALSE;
   14251       s = ppc64_elf_tdata (dynobj)->relgot;
   14252       if (s != NULL
   14253 	  && s->size != 0
   14254 	  && s->output_section != bfd_abs_section_ptr
   14255 	  && !bfd_set_section_contents (output_bfd, s->output_section,
   14256 					s->contents, s->output_offset,
   14257 					s->size))
   14258 	return FALSE;
   14259     }
   14260 
   14261   return TRUE;
   14262 }
   14263 
   14264 #include "elf64-target.h"
   14265 
   14266 /* FreeBSD support */
   14267 
   14268 #undef  TARGET_LITTLE_SYM
   14269 #undef  TARGET_LITTLE_NAME
   14270 
   14271 #undef  TARGET_BIG_SYM
   14272 #define TARGET_BIG_SYM	bfd_elf64_powerpc_freebsd_vec
   14273 #undef  TARGET_BIG_NAME
   14274 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
   14275 
   14276 #undef  ELF_OSABI
   14277 #define	ELF_OSABI       ELFOSABI_FREEBSD
   14278 
   14279 #undef  elf64_bed
   14280 #define elf64_bed	elf64_powerpc_fbsd_bed
   14281 
   14282 #include "elf64-target.h"
   14283 
   14284