Home | History | Annotate | Line # | Download | only in bfd
elf64-ppc.c revision 1.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 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 
     38 static bfd_reloc_status_type ppc64_elf_ha_reloc
     39   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     40 static bfd_reloc_status_type ppc64_elf_branch_reloc
     41   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     42 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
     43   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     44 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
     45   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     46 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
     47   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     48 static bfd_reloc_status_type ppc64_elf_toc_reloc
     49   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     50 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
     51   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     52 static bfd_reloc_status_type ppc64_elf_toc64_reloc
     53   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     54 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
     55   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     56 static bfd_vma opd_entry_value
     57   (asection *, bfd_vma, asection **, bfd_vma *);
     58 
     59 #define TARGET_LITTLE_SYM	bfd_elf64_powerpcle_vec
     60 #define TARGET_LITTLE_NAME	"elf64-powerpcle"
     61 #define TARGET_BIG_SYM		bfd_elf64_powerpc_vec
     62 #define TARGET_BIG_NAME		"elf64-powerpc"
     63 #define ELF_ARCH		bfd_arch_powerpc
     64 #define ELF_TARGET_ID		PPC64_ELF_DATA
     65 #define ELF_MACHINE_CODE	EM_PPC64
     66 #define ELF_MAXPAGESIZE		0x10000
     67 #define ELF_COMMONPAGESIZE	0x1000
     68 #define elf_info_to_howto	ppc64_elf_info_to_howto
     69 
     70 #define elf_backend_want_got_sym 0
     71 #define elf_backend_want_plt_sym 0
     72 #define elf_backend_plt_alignment 3
     73 #define elf_backend_plt_not_loaded 1
     74 #define elf_backend_got_header_size 8
     75 #define elf_backend_can_gc_sections 1
     76 #define elf_backend_can_refcount 1
     77 #define elf_backend_rela_normal 1
     78 #define elf_backend_default_execstack 0
     79 
     80 #define bfd_elf64_mkobject		      ppc64_elf_mkobject
     81 #define bfd_elf64_bfd_reloc_type_lookup	      ppc64_elf_reloc_type_lookup
     82 #define bfd_elf64_bfd_reloc_name_lookup	      ppc64_elf_reloc_name_lookup
     83 #define bfd_elf64_bfd_merge_private_bfd_data  ppc64_elf_merge_private_bfd_data
     84 #define bfd_elf64_new_section_hook	      ppc64_elf_new_section_hook
     85 #define bfd_elf64_bfd_link_hash_table_create  ppc64_elf_link_hash_table_create
     86 #define bfd_elf64_bfd_link_hash_table_free    ppc64_elf_link_hash_table_free
     87 #define bfd_elf64_get_synthetic_symtab	      ppc64_elf_get_synthetic_symtab
     88 #define bfd_elf64_bfd_link_just_syms	      ppc64_elf_link_just_syms
     89 
     90 #define elf_backend_object_p		      ppc64_elf_object_p
     91 #define elf_backend_grok_prstatus	      ppc64_elf_grok_prstatus
     92 #define elf_backend_grok_psinfo		      ppc64_elf_grok_psinfo
     93 #define elf_backend_write_core_note	      ppc64_elf_write_core_note
     94 #define elf_backend_create_dynamic_sections   ppc64_elf_create_dynamic_sections
     95 #define elf_backend_copy_indirect_symbol      ppc64_elf_copy_indirect_symbol
     96 #define elf_backend_add_symbol_hook	      ppc64_elf_add_symbol_hook
     97 #define elf_backend_check_directives	      ppc64_elf_process_dot_syms
     98 #define elf_backend_as_needed_cleanup	      ppc64_elf_as_needed_cleanup
     99 #define elf_backend_archive_symbol_lookup     ppc64_elf_archive_symbol_lookup
    100 #define elf_backend_check_relocs	      ppc64_elf_check_relocs
    101 #define elf_backend_gc_keep		      ppc64_elf_gc_keep
    102 #define elf_backend_gc_mark_dynamic_ref       ppc64_elf_gc_mark_dynamic_ref
    103 #define elf_backend_gc_mark_hook	      ppc64_elf_gc_mark_hook
    104 #define elf_backend_gc_sweep_hook	      ppc64_elf_gc_sweep_hook
    105 #define elf_backend_adjust_dynamic_symbol     ppc64_elf_adjust_dynamic_symbol
    106 #define elf_backend_hide_symbol		      ppc64_elf_hide_symbol
    107 #define elf_backend_always_size_sections      ppc64_elf_func_desc_adjust
    108 #define elf_backend_size_dynamic_sections     ppc64_elf_size_dynamic_sections
    109 #define elf_backend_init_index_section	      _bfd_elf_init_2_index_sections
    110 #define elf_backend_action_discarded	      ppc64_elf_action_discarded
    111 #define elf_backend_relocate_section	      ppc64_elf_relocate_section
    112 #define elf_backend_finish_dynamic_symbol     ppc64_elf_finish_dynamic_symbol
    113 #define elf_backend_reloc_type_class	      ppc64_elf_reloc_type_class
    114 #define elf_backend_finish_dynamic_sections   ppc64_elf_finish_dynamic_sections
    115 #define elf_backend_link_output_symbol_hook   ppc64_elf_output_symbol_hook
    116 #define elf_backend_special_sections	      ppc64_elf_special_sections
    117 #define elf_backend_post_process_headers      _bfd_elf_set_osabi
    118 
    119 /* The name of the dynamic interpreter.  This is put in the .interp
    120    section.  */
    121 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
    122 
    123 /* The size in bytes of an entry in the procedure linkage table.  */
    124 #define PLT_ENTRY_SIZE 24
    125 
    126 /* The initial size of the plt reserved for the dynamic linker.  */
    127 #define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
    128 
    129 /* TOC base pointers offset from start of TOC.  */
    130 #define TOC_BASE_OFF	0x8000
    131 
    132 /* Offset of tp and dtp pointers from start of TLS block.  */
    133 #define TP_OFFSET	0x7000
    134 #define DTP_OFFSET	0x8000
    135 
    136 /* .plt call stub instructions.  The normal stub is like this, but
    137    sometimes the .plt entry crosses a 64k boundary and we need to
    138    insert an addi to adjust r12.  */
    139 #define PLT_CALL_STUB_SIZE (7*4)
    140 #define ADDIS_R12_R2	0x3d820000	/* addis %r12,%r2,xxx@ha     */
    141 #define STD_R2_40R1	0xf8410028	/* std	 %r2,40(%r1)	     */
    142 #define LD_R11_0R12	0xe96c0000	/* ld	 %r11,xxx+0@l(%r12)  */
    143 #define MTCTR_R11	0x7d6903a6	/* mtctr %r11		     */
    144 #define LD_R2_0R12	0xe84c0000	/* ld	 %r2,xxx+8@l(%r12)   */
    145 					/* ld	 %r11,xxx+16@l(%r12) */
    146 #define BCTR		0x4e800420	/* bctr			     */
    147 
    148 
    149 #define ADDIS_R12_R12	0x3d8c0000	/* addis %r12,%r12,off@ha  */
    150 #define ADDI_R12_R12	0x398c0000	/* addi %r12,%r12,off@l  */
    151 #define ADDIS_R2_R2	0x3c420000	/* addis %r2,%r2,off@ha  */
    152 #define ADDI_R2_R2	0x38420000	/* addi  %r2,%r2,off@l   */
    153 
    154 #define LD_R11_0R2	0xe9620000	/* ld	 %r11,xxx+0(%r2) */
    155 #define LD_R2_0R2	0xe8420000	/* ld	 %r2,xxx+0(%r2)  */
    156 
    157 #define LD_R2_40R1	0xe8410028	/* ld    %r2,40(%r1)     */
    158 
    159 /* glink call stub instructions.  We enter with the index in R0.  */
    160 #define GLINK_CALL_STUB_SIZE (16*4)
    161 					/* 0:				*/
    162 					/*  .quad plt0-1f		*/
    163 					/* __glink:			*/
    164 #define MFLR_R12	0x7d8802a6	/*  mflr %12			*/
    165 #define BCL_20_31	0x429f0005	/*  bcl 20,31,1f		*/
    166 					/* 1:				*/
    167 #define MFLR_R11	0x7d6802a6	/*  mflr %11			*/
    168 #define LD_R2_M16R11	0xe84bfff0	/*  ld %2,(0b-1b)(%11)		*/
    169 #define MTLR_R12	0x7d8803a6	/*  mtlr %12			*/
    170 #define ADD_R12_R2_R11	0x7d825a14	/*  add %12,%2,%11		*/
    171 					/*  ld %11,0(%12)		*/
    172 					/*  ld %2,8(%12)		*/
    173 					/*  mtctr %11			*/
    174 					/*  ld %11,16(%12)		*/
    175 					/*  bctr			*/
    176 
    177 /* Pad with this.  */
    178 #define NOP		0x60000000
    179 
    180 /* Some other nops.  */
    181 #define CROR_151515	0x4def7b82
    182 #define CROR_313131	0x4ffffb82
    183 
    184 /* .glink entries for the first 32k functions are two instructions.  */
    185 #define LI_R0_0		0x38000000	/* li    %r0,0		*/
    186 #define B_DOT		0x48000000	/* b     .		*/
    187 
    188 /* After that, we need two instructions to load the index, followed by
    189    a branch.  */
    190 #define LIS_R0_0	0x3c000000	/* lis   %r0,0		*/
    191 #define ORI_R0_R0_0	0x60000000	/* ori	 %r0,%r0,0	*/
    192 
    193 /* Instructions used by the save and restore reg functions.  */
    194 #define STD_R0_0R1	0xf8010000	/* std   %r0,0(%r1)	*/
    195 #define STD_R0_0R12	0xf80c0000	/* std   %r0,0(%r12)	*/
    196 #define LD_R0_0R1	0xe8010000	/* ld    %r0,0(%r1)	*/
    197 #define LD_R0_0R12	0xe80c0000	/* ld    %r0,0(%r12)	*/
    198 #define STFD_FR0_0R1	0xd8010000	/* stfd  %fr0,0(%r1)	*/
    199 #define LFD_FR0_0R1	0xc8010000	/* lfd   %fr0,0(%r1)	*/
    200 #define LI_R12_0	0x39800000	/* li    %r12,0		*/
    201 #define STVX_VR0_R12_R0	0x7c0c01ce	/* stvx  %v0,%r12,%r0	*/
    202 #define LVX_VR0_R12_R0	0x7c0c00ce	/* lvx   %v0,%r12,%r0	*/
    203 #define MTLR_R0		0x7c0803a6	/* mtlr  %r0		*/
    204 #define BLR		0x4e800020	/* blr			*/
    205 
    206 /* Since .opd is an array of descriptors and each entry will end up
    207    with identical R_PPC64_RELATIVE relocs, there is really no need to
    208    propagate .opd relocs;  The dynamic linker should be taught to
    209    relocate .opd without reloc entries.  */
    210 #ifndef NO_OPD_RELOCS
    211 #define NO_OPD_RELOCS 0
    212 #endif
    213 
    214 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
    216 
    217 /* Relocation HOWTO's.  */
    218 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
    219 
    220 static reloc_howto_type ppc64_elf_howto_raw[] = {
    221   /* This reloc does nothing.  */
    222   HOWTO (R_PPC64_NONE,		/* type */
    223 	 0,			/* rightshift */
    224 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    225 	 32,			/* bitsize */
    226 	 FALSE,			/* pc_relative */
    227 	 0,			/* bitpos */
    228 	 complain_overflow_dont, /* complain_on_overflow */
    229 	 bfd_elf_generic_reloc,	/* special_function */
    230 	 "R_PPC64_NONE",	/* name */
    231 	 FALSE,			/* partial_inplace */
    232 	 0,			/* src_mask */
    233 	 0,			/* dst_mask */
    234 	 FALSE),		/* pcrel_offset */
    235 
    236   /* A standard 32 bit relocation.  */
    237   HOWTO (R_PPC64_ADDR32,	/* type */
    238 	 0,			/* rightshift */
    239 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    240 	 32,			/* bitsize */
    241 	 FALSE,			/* pc_relative */
    242 	 0,			/* bitpos */
    243 	 complain_overflow_bitfield, /* complain_on_overflow */
    244 	 bfd_elf_generic_reloc,	/* special_function */
    245 	 "R_PPC64_ADDR32",	/* name */
    246 	 FALSE,			/* partial_inplace */
    247 	 0,			/* src_mask */
    248 	 0xffffffff,		/* dst_mask */
    249 	 FALSE),		/* pcrel_offset */
    250 
    251   /* An absolute 26 bit branch; the lower two bits must be zero.
    252      FIXME: we don't check that, we just clear them.  */
    253   HOWTO (R_PPC64_ADDR24,	/* type */
    254 	 0,			/* rightshift */
    255 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    256 	 26,			/* bitsize */
    257 	 FALSE,			/* pc_relative */
    258 	 0,			/* bitpos */
    259 	 complain_overflow_bitfield, /* complain_on_overflow */
    260 	 bfd_elf_generic_reloc,	/* special_function */
    261 	 "R_PPC64_ADDR24",	/* name */
    262 	 FALSE,			/* partial_inplace */
    263 	 0,			/* src_mask */
    264 	 0x03fffffc,		/* dst_mask */
    265 	 FALSE),		/* pcrel_offset */
    266 
    267   /* A standard 16 bit relocation.  */
    268   HOWTO (R_PPC64_ADDR16,	/* type */
    269 	 0,			/* rightshift */
    270 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    271 	 16,			/* bitsize */
    272 	 FALSE,			/* pc_relative */
    273 	 0,			/* bitpos */
    274 	 complain_overflow_bitfield, /* complain_on_overflow */
    275 	 bfd_elf_generic_reloc,	/* special_function */
    276 	 "R_PPC64_ADDR16",	/* name */
    277 	 FALSE,			/* partial_inplace */
    278 	 0,			/* src_mask */
    279 	 0xffff,		/* dst_mask */
    280 	 FALSE),		/* pcrel_offset */
    281 
    282   /* A 16 bit relocation without overflow.  */
    283   HOWTO (R_PPC64_ADDR16_LO,	/* type */
    284 	 0,			/* rightshift */
    285 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    286 	 16,			/* bitsize */
    287 	 FALSE,			/* pc_relative */
    288 	 0,			/* bitpos */
    289 	 complain_overflow_dont,/* complain_on_overflow */
    290 	 bfd_elf_generic_reloc,	/* special_function */
    291 	 "R_PPC64_ADDR16_LO",	/* name */
    292 	 FALSE,			/* partial_inplace */
    293 	 0,			/* src_mask */
    294 	 0xffff,		/* dst_mask */
    295 	 FALSE),		/* pcrel_offset */
    296 
    297   /* Bits 16-31 of an address.  */
    298   HOWTO (R_PPC64_ADDR16_HI,	/* type */
    299 	 16,			/* rightshift */
    300 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    301 	 16,			/* bitsize */
    302 	 FALSE,			/* pc_relative */
    303 	 0,			/* bitpos */
    304 	 complain_overflow_dont, /* complain_on_overflow */
    305 	 bfd_elf_generic_reloc,	/* special_function */
    306 	 "R_PPC64_ADDR16_HI",	/* name */
    307 	 FALSE,			/* partial_inplace */
    308 	 0,			/* src_mask */
    309 	 0xffff,		/* dst_mask */
    310 	 FALSE),		/* pcrel_offset */
    311 
    312   /* Bits 16-31 of an address, plus 1 if the contents of the low 16
    313      bits, treated as a signed number, is negative.  */
    314   HOWTO (R_PPC64_ADDR16_HA,	/* type */
    315 	 16,			/* rightshift */
    316 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    317 	 16,			/* bitsize */
    318 	 FALSE,			/* pc_relative */
    319 	 0,			/* bitpos */
    320 	 complain_overflow_dont, /* complain_on_overflow */
    321 	 ppc64_elf_ha_reloc,	/* special_function */
    322 	 "R_PPC64_ADDR16_HA",	/* name */
    323 	 FALSE,			/* partial_inplace */
    324 	 0,			/* src_mask */
    325 	 0xffff,		/* dst_mask */
    326 	 FALSE),		/* pcrel_offset */
    327 
    328   /* An absolute 16 bit branch; the lower two bits must be zero.
    329      FIXME: we don't check that, we just clear them.  */
    330   HOWTO (R_PPC64_ADDR14,	/* type */
    331 	 0,			/* rightshift */
    332 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    333 	 16,			/* bitsize */
    334 	 FALSE,			/* pc_relative */
    335 	 0,			/* bitpos */
    336 	 complain_overflow_bitfield, /* complain_on_overflow */
    337 	 ppc64_elf_branch_reloc, /* special_function */
    338 	 "R_PPC64_ADDR14",	/* name */
    339 	 FALSE,			/* partial_inplace */
    340 	 0,			/* src_mask */
    341 	 0x0000fffc,		/* dst_mask */
    342 	 FALSE),		/* pcrel_offset */
    343 
    344   /* An absolute 16 bit branch, for which bit 10 should be set to
    345      indicate that the branch is expected to be taken.  The lower two
    346      bits must be zero.  */
    347   HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
    348 	 0,			/* rightshift */
    349 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    350 	 16,			/* bitsize */
    351 	 FALSE,			/* pc_relative */
    352 	 0,			/* bitpos */
    353 	 complain_overflow_bitfield, /* complain_on_overflow */
    354 	 ppc64_elf_brtaken_reloc, /* special_function */
    355 	 "R_PPC64_ADDR14_BRTAKEN",/* name */
    356 	 FALSE,			/* partial_inplace */
    357 	 0,			/* src_mask */
    358 	 0x0000fffc,		/* dst_mask */
    359 	 FALSE),		/* pcrel_offset */
    360 
    361   /* An absolute 16 bit branch, for which bit 10 should be set to
    362      indicate that the branch is not expected to be taken.  The lower
    363      two bits must be zero.  */
    364   HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
    365 	 0,			/* rightshift */
    366 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    367 	 16,			/* bitsize */
    368 	 FALSE,			/* pc_relative */
    369 	 0,			/* bitpos */
    370 	 complain_overflow_bitfield, /* complain_on_overflow */
    371 	 ppc64_elf_brtaken_reloc, /* special_function */
    372 	 "R_PPC64_ADDR14_BRNTAKEN",/* name */
    373 	 FALSE,			/* partial_inplace */
    374 	 0,			/* src_mask */
    375 	 0x0000fffc,		/* dst_mask */
    376 	 FALSE),		/* pcrel_offset */
    377 
    378   /* A relative 26 bit branch; the lower two bits must be zero.  */
    379   HOWTO (R_PPC64_REL24,		/* type */
    380 	 0,			/* rightshift */
    381 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    382 	 26,			/* bitsize */
    383 	 TRUE,			/* pc_relative */
    384 	 0,			/* bitpos */
    385 	 complain_overflow_signed, /* complain_on_overflow */
    386 	 ppc64_elf_branch_reloc, /* special_function */
    387 	 "R_PPC64_REL24",	/* name */
    388 	 FALSE,			/* partial_inplace */
    389 	 0,			/* src_mask */
    390 	 0x03fffffc,		/* dst_mask */
    391 	 TRUE),			/* pcrel_offset */
    392 
    393   /* A relative 16 bit branch; the lower two bits must be zero.  */
    394   HOWTO (R_PPC64_REL14,		/* type */
    395 	 0,			/* rightshift */
    396 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    397 	 16,			/* bitsize */
    398 	 TRUE,			/* pc_relative */
    399 	 0,			/* bitpos */
    400 	 complain_overflow_signed, /* complain_on_overflow */
    401 	 ppc64_elf_branch_reloc, /* special_function */
    402 	 "R_PPC64_REL14",	/* name */
    403 	 FALSE,			/* partial_inplace */
    404 	 0,			/* src_mask */
    405 	 0x0000fffc,		/* dst_mask */
    406 	 TRUE),			/* pcrel_offset */
    407 
    408   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
    409      the branch is expected to be taken.  The lower two bits must be
    410      zero.  */
    411   HOWTO (R_PPC64_REL14_BRTAKEN,	/* type */
    412 	 0,			/* rightshift */
    413 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    414 	 16,			/* bitsize */
    415 	 TRUE,			/* pc_relative */
    416 	 0,			/* bitpos */
    417 	 complain_overflow_signed, /* complain_on_overflow */
    418 	 ppc64_elf_brtaken_reloc, /* special_function */
    419 	 "R_PPC64_REL14_BRTAKEN", /* name */
    420 	 FALSE,			/* partial_inplace */
    421 	 0,			/* src_mask */
    422 	 0x0000fffc,		/* dst_mask */
    423 	 TRUE),			/* pcrel_offset */
    424 
    425   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
    426      the branch is not expected to be taken.  The lower two bits must
    427      be zero.  */
    428   HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
    429 	 0,			/* rightshift */
    430 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    431 	 16,			/* bitsize */
    432 	 TRUE,			/* pc_relative */
    433 	 0,			/* bitpos */
    434 	 complain_overflow_signed, /* complain_on_overflow */
    435 	 ppc64_elf_brtaken_reloc, /* special_function */
    436 	 "R_PPC64_REL14_BRNTAKEN",/* name */
    437 	 FALSE,			/* partial_inplace */
    438 	 0,			/* src_mask */
    439 	 0x0000fffc,		/* dst_mask */
    440 	 TRUE),			/* pcrel_offset */
    441 
    442   /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
    443      symbol.  */
    444   HOWTO (R_PPC64_GOT16,		/* type */
    445 	 0,			/* rightshift */
    446 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    447 	 16,			/* bitsize */
    448 	 FALSE,			/* pc_relative */
    449 	 0,			/* bitpos */
    450 	 complain_overflow_signed, /* complain_on_overflow */
    451 	 ppc64_elf_unhandled_reloc, /* special_function */
    452 	 "R_PPC64_GOT16",	/* name */
    453 	 FALSE,			/* partial_inplace */
    454 	 0,			/* src_mask */
    455 	 0xffff,		/* dst_mask */
    456 	 FALSE),		/* pcrel_offset */
    457 
    458   /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
    459      the symbol.  */
    460   HOWTO (R_PPC64_GOT16_LO,	/* type */
    461 	 0,			/* rightshift */
    462 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    463 	 16,			/* bitsize */
    464 	 FALSE,			/* pc_relative */
    465 	 0,			/* bitpos */
    466 	 complain_overflow_dont, /* complain_on_overflow */
    467 	 ppc64_elf_unhandled_reloc, /* special_function */
    468 	 "R_PPC64_GOT16_LO",	/* name */
    469 	 FALSE,			/* partial_inplace */
    470 	 0,			/* src_mask */
    471 	 0xffff,		/* dst_mask */
    472 	 FALSE),		/* pcrel_offset */
    473 
    474   /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
    475      the symbol.  */
    476   HOWTO (R_PPC64_GOT16_HI,	/* type */
    477 	 16,			/* rightshift */
    478 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    479 	 16,			/* bitsize */
    480 	 FALSE,			/* pc_relative */
    481 	 0,			/* bitpos */
    482 	 complain_overflow_dont,/* complain_on_overflow */
    483 	 ppc64_elf_unhandled_reloc, /* special_function */
    484 	 "R_PPC64_GOT16_HI",	/* name */
    485 	 FALSE,			/* partial_inplace */
    486 	 0,			/* src_mask */
    487 	 0xffff,		/* dst_mask */
    488 	 FALSE),		/* pcrel_offset */
    489 
    490   /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
    491      the symbol.  */
    492   HOWTO (R_PPC64_GOT16_HA,	/* type */
    493 	 16,			/* rightshift */
    494 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    495 	 16,			/* bitsize */
    496 	 FALSE,			/* pc_relative */
    497 	 0,			/* bitpos */
    498 	 complain_overflow_dont,/* complain_on_overflow */
    499 	 ppc64_elf_unhandled_reloc, /* special_function */
    500 	 "R_PPC64_GOT16_HA",	/* name */
    501 	 FALSE,			/* partial_inplace */
    502 	 0,			/* src_mask */
    503 	 0xffff,		/* dst_mask */
    504 	 FALSE),		/* pcrel_offset */
    505 
    506   /* This is used only by the dynamic linker.  The symbol should exist
    507      both in the object being run and in some shared library.  The
    508      dynamic linker copies the data addressed by the symbol from the
    509      shared library into the object, because the object being
    510      run has to have the data at some particular address.  */
    511   HOWTO (R_PPC64_COPY,		/* type */
    512 	 0,			/* rightshift */
    513 	 0,			/* this one is variable size */
    514 	 0,			/* bitsize */
    515 	 FALSE,			/* pc_relative */
    516 	 0,			/* bitpos */
    517 	 complain_overflow_dont, /* complain_on_overflow */
    518 	 ppc64_elf_unhandled_reloc, /* special_function */
    519 	 "R_PPC64_COPY",	/* name */
    520 	 FALSE,			/* partial_inplace */
    521 	 0,			/* src_mask */
    522 	 0,			/* dst_mask */
    523 	 FALSE),		/* pcrel_offset */
    524 
    525   /* Like R_PPC64_ADDR64, but used when setting global offset table
    526      entries.  */
    527   HOWTO (R_PPC64_GLOB_DAT,	/* type */
    528 	 0,			/* rightshift */
    529 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    530 	 64,			/* bitsize */
    531 	 FALSE,			/* pc_relative */
    532 	 0,			/* bitpos */
    533 	 complain_overflow_dont, /* complain_on_overflow */
    534 	 ppc64_elf_unhandled_reloc,  /* special_function */
    535 	 "R_PPC64_GLOB_DAT",	/* name */
    536 	 FALSE,			/* partial_inplace */
    537 	 0,			/* src_mask */
    538 	 ONES (64),		/* dst_mask */
    539 	 FALSE),		/* pcrel_offset */
    540 
    541   /* Created by the link editor.  Marks a procedure linkage table
    542      entry for a symbol.  */
    543   HOWTO (R_PPC64_JMP_SLOT,	/* type */
    544 	 0,			/* rightshift */
    545 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
    546 	 0,			/* bitsize */
    547 	 FALSE,			/* pc_relative */
    548 	 0,			/* bitpos */
    549 	 complain_overflow_dont, /* complain_on_overflow */
    550 	 ppc64_elf_unhandled_reloc, /* special_function */
    551 	 "R_PPC64_JMP_SLOT",	/* name */
    552 	 FALSE,			/* partial_inplace */
    553 	 0,			/* src_mask */
    554 	 0,			/* dst_mask */
    555 	 FALSE),		/* pcrel_offset */
    556 
    557   /* Used only by the dynamic linker.  When the object is run, this
    558      doubleword64 is set to the load address of the object, plus the
    559      addend.  */
    560   HOWTO (R_PPC64_RELATIVE,	/* type */
    561 	 0,			/* rightshift */
    562 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    563 	 64,			/* bitsize */
    564 	 FALSE,			/* pc_relative */
    565 	 0,			/* bitpos */
    566 	 complain_overflow_dont, /* complain_on_overflow */
    567 	 bfd_elf_generic_reloc,	/* special_function */
    568 	 "R_PPC64_RELATIVE",	/* name */
    569 	 FALSE,			/* partial_inplace */
    570 	 0,			/* src_mask */
    571 	 ONES (64),		/* dst_mask */
    572 	 FALSE),		/* pcrel_offset */
    573 
    574   /* Like R_PPC64_ADDR32, but may be unaligned.  */
    575   HOWTO (R_PPC64_UADDR32,	/* type */
    576 	 0,			/* rightshift */
    577 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    578 	 32,			/* bitsize */
    579 	 FALSE,			/* pc_relative */
    580 	 0,			/* bitpos */
    581 	 complain_overflow_bitfield, /* complain_on_overflow */
    582 	 bfd_elf_generic_reloc,	/* special_function */
    583 	 "R_PPC64_UADDR32",	/* name */
    584 	 FALSE,			/* partial_inplace */
    585 	 0,			/* src_mask */
    586 	 0xffffffff,		/* dst_mask */
    587 	 FALSE),		/* pcrel_offset */
    588 
    589   /* Like R_PPC64_ADDR16, but may be unaligned.  */
    590   HOWTO (R_PPC64_UADDR16,	/* type */
    591 	 0,			/* rightshift */
    592 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    593 	 16,			/* bitsize */
    594 	 FALSE,			/* pc_relative */
    595 	 0,			/* bitpos */
    596 	 complain_overflow_bitfield, /* complain_on_overflow */
    597 	 bfd_elf_generic_reloc,	/* special_function */
    598 	 "R_PPC64_UADDR16",	/* name */
    599 	 FALSE,			/* partial_inplace */
    600 	 0,			/* src_mask */
    601 	 0xffff,		/* dst_mask */
    602 	 FALSE),		/* pcrel_offset */
    603 
    604   /* 32-bit PC relative.  */
    605   HOWTO (R_PPC64_REL32,		/* type */
    606 	 0,			/* rightshift */
    607 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    608 	 32,			/* bitsize */
    609 	 TRUE,			/* pc_relative */
    610 	 0,			/* bitpos */
    611 	 /* FIXME: Verify.  Was complain_overflow_bitfield.  */
    612 	 complain_overflow_signed, /* complain_on_overflow */
    613 	 bfd_elf_generic_reloc,	/* special_function */
    614 	 "R_PPC64_REL32",	/* name */
    615 	 FALSE,			/* partial_inplace */
    616 	 0,			/* src_mask */
    617 	 0xffffffff,		/* dst_mask */
    618 	 TRUE),			/* pcrel_offset */
    619 
    620   /* 32-bit relocation to the symbol's procedure linkage table.  */
    621   HOWTO (R_PPC64_PLT32,		/* type */
    622 	 0,			/* rightshift */
    623 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    624 	 32,			/* bitsize */
    625 	 FALSE,			/* pc_relative */
    626 	 0,			/* bitpos */
    627 	 complain_overflow_bitfield, /* complain_on_overflow */
    628 	 ppc64_elf_unhandled_reloc, /* special_function */
    629 	 "R_PPC64_PLT32",	/* name */
    630 	 FALSE,			/* partial_inplace */
    631 	 0,			/* src_mask */
    632 	 0xffffffff,		/* dst_mask */
    633 	 FALSE),		/* pcrel_offset */
    634 
    635   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
    636      FIXME: R_PPC64_PLTREL32 not supported.  */
    637   HOWTO (R_PPC64_PLTREL32,	/* type */
    638 	 0,			/* rightshift */
    639 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    640 	 32,			/* bitsize */
    641 	 TRUE,			/* pc_relative */
    642 	 0,			/* bitpos */
    643 	 complain_overflow_signed, /* complain_on_overflow */
    644 	 bfd_elf_generic_reloc,	/* special_function */
    645 	 "R_PPC64_PLTREL32",	/* name */
    646 	 FALSE,			/* partial_inplace */
    647 	 0,			/* src_mask */
    648 	 0xffffffff,		/* dst_mask */
    649 	 TRUE),			/* pcrel_offset */
    650 
    651   /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
    652      the symbol.  */
    653   HOWTO (R_PPC64_PLT16_LO,	/* type */
    654 	 0,			/* rightshift */
    655 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    656 	 16,			/* bitsize */
    657 	 FALSE,			/* pc_relative */
    658 	 0,			/* bitpos */
    659 	 complain_overflow_dont, /* complain_on_overflow */
    660 	 ppc64_elf_unhandled_reloc, /* special_function */
    661 	 "R_PPC64_PLT16_LO",	/* name */
    662 	 FALSE,			/* partial_inplace */
    663 	 0,			/* src_mask */
    664 	 0xffff,		/* dst_mask */
    665 	 FALSE),		/* pcrel_offset */
    666 
    667   /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
    668      the symbol.  */
    669   HOWTO (R_PPC64_PLT16_HI,	/* type */
    670 	 16,			/* rightshift */
    671 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    672 	 16,			/* bitsize */
    673 	 FALSE,			/* pc_relative */
    674 	 0,			/* bitpos */
    675 	 complain_overflow_dont, /* complain_on_overflow */
    676 	 ppc64_elf_unhandled_reloc, /* special_function */
    677 	 "R_PPC64_PLT16_HI",	/* name */
    678 	 FALSE,			/* partial_inplace */
    679 	 0,			/* src_mask */
    680 	 0xffff,		/* dst_mask */
    681 	 FALSE),		/* pcrel_offset */
    682 
    683   /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
    684      the symbol.  */
    685   HOWTO (R_PPC64_PLT16_HA,	/* type */
    686 	 16,			/* rightshift */
    687 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    688 	 16,			/* bitsize */
    689 	 FALSE,			/* pc_relative */
    690 	 0,			/* bitpos */
    691 	 complain_overflow_dont, /* complain_on_overflow */
    692 	 ppc64_elf_unhandled_reloc, /* special_function */
    693 	 "R_PPC64_PLT16_HA",	/* name */
    694 	 FALSE,			/* partial_inplace */
    695 	 0,			/* src_mask */
    696 	 0xffff,		/* dst_mask */
    697 	 FALSE),		/* pcrel_offset */
    698 
    699   /* 16-bit section relative relocation.  */
    700   HOWTO (R_PPC64_SECTOFF,	/* type */
    701 	 0,			/* rightshift */
    702 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    703 	 16,			/* bitsize */
    704 	 FALSE,			/* pc_relative */
    705 	 0,			/* bitpos */
    706 	 complain_overflow_bitfield, /* complain_on_overflow */
    707 	 ppc64_elf_sectoff_reloc, /* special_function */
    708 	 "R_PPC64_SECTOFF",	/* name */
    709 	 FALSE,			/* partial_inplace */
    710 	 0,			/* src_mask */
    711 	 0xffff,		/* dst_mask */
    712 	 FALSE),		/* pcrel_offset */
    713 
    714   /* Like R_PPC64_SECTOFF, but no overflow warning.  */
    715   HOWTO (R_PPC64_SECTOFF_LO,	/* type */
    716 	 0,			/* rightshift */
    717 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    718 	 16,			/* bitsize */
    719 	 FALSE,			/* pc_relative */
    720 	 0,			/* bitpos */
    721 	 complain_overflow_dont, /* complain_on_overflow */
    722 	 ppc64_elf_sectoff_reloc, /* special_function */
    723 	 "R_PPC64_SECTOFF_LO",	/* name */
    724 	 FALSE,			/* partial_inplace */
    725 	 0,			/* src_mask */
    726 	 0xffff,		/* dst_mask */
    727 	 FALSE),		/* pcrel_offset */
    728 
    729   /* 16-bit upper half section relative relocation.  */
    730   HOWTO (R_PPC64_SECTOFF_HI,	/* type */
    731 	 16,			/* rightshift */
    732 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    733 	 16,			/* bitsize */
    734 	 FALSE,			/* pc_relative */
    735 	 0,			/* bitpos */
    736 	 complain_overflow_dont, /* complain_on_overflow */
    737 	 ppc64_elf_sectoff_reloc, /* special_function */
    738 	 "R_PPC64_SECTOFF_HI",	/* name */
    739 	 FALSE,			/* partial_inplace */
    740 	 0,			/* src_mask */
    741 	 0xffff,		/* dst_mask */
    742 	 FALSE),		/* pcrel_offset */
    743 
    744   /* 16-bit upper half adjusted section relative relocation.  */
    745   HOWTO (R_PPC64_SECTOFF_HA,	/* type */
    746 	 16,			/* rightshift */
    747 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    748 	 16,			/* bitsize */
    749 	 FALSE,			/* pc_relative */
    750 	 0,			/* bitpos */
    751 	 complain_overflow_dont, /* complain_on_overflow */
    752 	 ppc64_elf_sectoff_ha_reloc, /* special_function */
    753 	 "R_PPC64_SECTOFF_HA",	/* name */
    754 	 FALSE,			/* partial_inplace */
    755 	 0,			/* src_mask */
    756 	 0xffff,		/* dst_mask */
    757 	 FALSE),		/* pcrel_offset */
    758 
    759   /* Like R_PPC64_REL24 without touching the two least significant bits.  */
    760   HOWTO (R_PPC64_REL30,		/* type */
    761 	 2,			/* rightshift */
    762 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    763 	 30,			/* bitsize */
    764 	 TRUE,			/* pc_relative */
    765 	 0,			/* bitpos */
    766 	 complain_overflow_dont, /* complain_on_overflow */
    767 	 bfd_elf_generic_reloc, /* special_function */
    768 	 "R_PPC64_REL30",	/* name */
    769 	 FALSE,			/* partial_inplace */
    770 	 0,			/* src_mask */
    771 	 0xfffffffc,		/* dst_mask */
    772 	 TRUE),			/* pcrel_offset */
    773 
    774   /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
    775 
    776   /* A standard 64-bit relocation.  */
    777   HOWTO (R_PPC64_ADDR64,	/* type */
    778 	 0,			/* rightshift */
    779 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    780 	 64,			/* bitsize */
    781 	 FALSE,			/* pc_relative */
    782 	 0,			/* bitpos */
    783 	 complain_overflow_dont, /* complain_on_overflow */
    784 	 bfd_elf_generic_reloc,	/* special_function */
    785 	 "R_PPC64_ADDR64",	/* name */
    786 	 FALSE,			/* partial_inplace */
    787 	 0,			/* src_mask */
    788 	 ONES (64),		/* dst_mask */
    789 	 FALSE),		/* pcrel_offset */
    790 
    791   /* The bits 32-47 of an address.  */
    792   HOWTO (R_PPC64_ADDR16_HIGHER,	/* type */
    793 	 32,			/* rightshift */
    794 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    795 	 16,			/* bitsize */
    796 	 FALSE,			/* pc_relative */
    797 	 0,			/* bitpos */
    798 	 complain_overflow_dont, /* complain_on_overflow */
    799 	 bfd_elf_generic_reloc,	/* special_function */
    800 	 "R_PPC64_ADDR16_HIGHER", /* name */
    801 	 FALSE,			/* partial_inplace */
    802 	 0,			/* src_mask */
    803 	 0xffff,		/* dst_mask */
    804 	 FALSE),		/* pcrel_offset */
    805 
    806   /* The bits 32-47 of an address, plus 1 if the contents of the low
    807      16 bits, treated as a signed number, is negative.  */
    808   HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
    809 	 32,			/* rightshift */
    810 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    811 	 16,			/* bitsize */
    812 	 FALSE,			/* pc_relative */
    813 	 0,			/* bitpos */
    814 	 complain_overflow_dont, /* complain_on_overflow */
    815 	 ppc64_elf_ha_reloc,	/* special_function */
    816 	 "R_PPC64_ADDR16_HIGHERA", /* name */
    817 	 FALSE,			/* partial_inplace */
    818 	 0,			/* src_mask */
    819 	 0xffff,		/* dst_mask */
    820 	 FALSE),		/* pcrel_offset */
    821 
    822   /* The bits 48-63 of an address.  */
    823   HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
    824 	 48,			/* rightshift */
    825 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    826 	 16,			/* bitsize */
    827 	 FALSE,			/* pc_relative */
    828 	 0,			/* bitpos */
    829 	 complain_overflow_dont, /* complain_on_overflow */
    830 	 bfd_elf_generic_reloc,	/* special_function */
    831 	 "R_PPC64_ADDR16_HIGHEST", /* name */
    832 	 FALSE,			/* partial_inplace */
    833 	 0,			/* src_mask */
    834 	 0xffff,		/* dst_mask */
    835 	 FALSE),		/* pcrel_offset */
    836 
    837   /* The bits 48-63 of an address, plus 1 if the contents of the low
    838      16 bits, treated as a signed number, is negative.  */
    839   HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
    840 	 48,			/* rightshift */
    841 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    842 	 16,			/* bitsize */
    843 	 FALSE,			/* pc_relative */
    844 	 0,			/* bitpos */
    845 	 complain_overflow_dont, /* complain_on_overflow */
    846 	 ppc64_elf_ha_reloc,	/* special_function */
    847 	 "R_PPC64_ADDR16_HIGHESTA", /* name */
    848 	 FALSE,			/* partial_inplace */
    849 	 0,			/* src_mask */
    850 	 0xffff,		/* dst_mask */
    851 	 FALSE),		/* pcrel_offset */
    852 
    853   /* Like ADDR64, but may be unaligned.  */
    854   HOWTO (R_PPC64_UADDR64,	/* type */
    855 	 0,			/* rightshift */
    856 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    857 	 64,			/* bitsize */
    858 	 FALSE,			/* pc_relative */
    859 	 0,			/* bitpos */
    860 	 complain_overflow_dont, /* complain_on_overflow */
    861 	 bfd_elf_generic_reloc,	/* special_function */
    862 	 "R_PPC64_UADDR64",	/* name */
    863 	 FALSE,			/* partial_inplace */
    864 	 0,			/* src_mask */
    865 	 ONES (64),		/* dst_mask */
    866 	 FALSE),		/* pcrel_offset */
    867 
    868   /* 64-bit relative relocation.  */
    869   HOWTO (R_PPC64_REL64,		/* type */
    870 	 0,			/* rightshift */
    871 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    872 	 64,			/* bitsize */
    873 	 TRUE,			/* pc_relative */
    874 	 0,			/* bitpos */
    875 	 complain_overflow_dont, /* complain_on_overflow */
    876 	 bfd_elf_generic_reloc,	/* special_function */
    877 	 "R_PPC64_REL64",	/* name */
    878 	 FALSE,			/* partial_inplace */
    879 	 0,			/* src_mask */
    880 	 ONES (64),		/* dst_mask */
    881 	 TRUE),			/* pcrel_offset */
    882 
    883   /* 64-bit relocation to the symbol's procedure linkage table.  */
    884   HOWTO (R_PPC64_PLT64,		/* type */
    885 	 0,			/* rightshift */
    886 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    887 	 64,			/* bitsize */
    888 	 FALSE,			/* pc_relative */
    889 	 0,			/* bitpos */
    890 	 complain_overflow_dont, /* complain_on_overflow */
    891 	 ppc64_elf_unhandled_reloc, /* special_function */
    892 	 "R_PPC64_PLT64",	/* name */
    893 	 FALSE,			/* partial_inplace */
    894 	 0,			/* src_mask */
    895 	 ONES (64),		/* dst_mask */
    896 	 FALSE),		/* pcrel_offset */
    897 
    898   /* 64-bit PC relative relocation to the symbol's procedure linkage
    899      table.  */
    900   /* FIXME: R_PPC64_PLTREL64 not supported.  */
    901   HOWTO (R_PPC64_PLTREL64,	/* type */
    902 	 0,			/* rightshift */
    903 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    904 	 64,			/* bitsize */
    905 	 TRUE,			/* pc_relative */
    906 	 0,			/* bitpos */
    907 	 complain_overflow_dont, /* complain_on_overflow */
    908 	 ppc64_elf_unhandled_reloc, /* special_function */
    909 	 "R_PPC64_PLTREL64",	/* name */
    910 	 FALSE,			/* partial_inplace */
    911 	 0,			/* src_mask */
    912 	 ONES (64),		/* dst_mask */
    913 	 TRUE),			/* pcrel_offset */
    914 
    915   /* 16 bit TOC-relative relocation.  */
    916 
    917   /* R_PPC64_TOC16	  47	   half16*	S + A - .TOC.  */
    918   HOWTO (R_PPC64_TOC16,		/* type */
    919 	 0,			/* rightshift */
    920 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    921 	 16,			/* bitsize */
    922 	 FALSE,			/* pc_relative */
    923 	 0,			/* bitpos */
    924 	 complain_overflow_signed, /* complain_on_overflow */
    925 	 ppc64_elf_toc_reloc,	/* special_function */
    926 	 "R_PPC64_TOC16",	/* name */
    927 	 FALSE,			/* partial_inplace */
    928 	 0,			/* src_mask */
    929 	 0xffff,		/* dst_mask */
    930 	 FALSE),		/* pcrel_offset */
    931 
    932   /* 16 bit TOC-relative relocation without overflow.  */
    933 
    934   /* R_PPC64_TOC16_LO	  48	   half16	 #lo (S + A - .TOC.)  */
    935   HOWTO (R_PPC64_TOC16_LO,	/* type */
    936 	 0,			/* rightshift */
    937 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    938 	 16,			/* bitsize */
    939 	 FALSE,			/* pc_relative */
    940 	 0,			/* bitpos */
    941 	 complain_overflow_dont, /* complain_on_overflow */
    942 	 ppc64_elf_toc_reloc,	/* special_function */
    943 	 "R_PPC64_TOC16_LO",	/* name */
    944 	 FALSE,			/* partial_inplace */
    945 	 0,			/* src_mask */
    946 	 0xffff,		/* dst_mask */
    947 	 FALSE),		/* pcrel_offset */
    948 
    949   /* 16 bit TOC-relative relocation, high 16 bits.  */
    950 
    951   /* R_PPC64_TOC16_HI	  49	   half16	 #hi (S + A - .TOC.)  */
    952   HOWTO (R_PPC64_TOC16_HI,	/* type */
    953 	 16,			/* rightshift */
    954 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    955 	 16,			/* bitsize */
    956 	 FALSE,			/* pc_relative */
    957 	 0,			/* bitpos */
    958 	 complain_overflow_dont, /* complain_on_overflow */
    959 	 ppc64_elf_toc_reloc,	/* special_function */
    960 	 "R_PPC64_TOC16_HI",	/* name */
    961 	 FALSE,			/* partial_inplace */
    962 	 0,			/* src_mask */
    963 	 0xffff,		/* dst_mask */
    964 	 FALSE),		/* pcrel_offset */
    965 
    966   /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
    967      contents of the low 16 bits, treated as a signed number, is
    968      negative.  */
    969 
    970   /* R_PPC64_TOC16_HA	  50	   half16	 #ha (S + A - .TOC.)  */
    971   HOWTO (R_PPC64_TOC16_HA,	/* type */
    972 	 16,			/* rightshift */
    973 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    974 	 16,			/* bitsize */
    975 	 FALSE,			/* pc_relative */
    976 	 0,			/* bitpos */
    977 	 complain_overflow_dont, /* complain_on_overflow */
    978 	 ppc64_elf_toc_ha_reloc, /* special_function */
    979 	 "R_PPC64_TOC16_HA",	/* name */
    980 	 FALSE,			/* partial_inplace */
    981 	 0,			/* src_mask */
    982 	 0xffff,		/* dst_mask */
    983 	 FALSE),		/* pcrel_offset */
    984 
    985   /* 64-bit relocation; insert value of TOC base (.TOC.).  */
    986 
    987   /* R_PPC64_TOC		  51	   doubleword64	 .TOC.  */
    988   HOWTO (R_PPC64_TOC,		/* type */
    989 	 0,			/* rightshift */
    990 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    991 	 64,			/* bitsize */
    992 	 FALSE,			/* pc_relative */
    993 	 0,			/* bitpos */
    994 	 complain_overflow_bitfield, /* complain_on_overflow */
    995 	 ppc64_elf_toc64_reloc,	/* special_function */
    996 	 "R_PPC64_TOC",		/* name */
    997 	 FALSE,			/* partial_inplace */
    998 	 0,			/* src_mask */
    999 	 ONES (64),		/* dst_mask */
   1000 	 FALSE),		/* pcrel_offset */
   1001 
   1002   /* Like R_PPC64_GOT16, but also informs the link editor that the
   1003      value to relocate may (!) refer to a PLT entry which the link
   1004      editor (a) may replace with the symbol value.  If the link editor
   1005      is unable to fully resolve the symbol, it may (b) create a PLT
   1006      entry and store the address to the new PLT entry in the GOT.
   1007      This permits lazy resolution of function symbols at run time.
   1008      The link editor may also skip all of this and just (c) emit a
   1009      R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
   1010   /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
   1011     HOWTO (R_PPC64_PLTGOT16,	/* type */
   1012 	 0,			/* rightshift */
   1013 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1014 	 16,			/* bitsize */
   1015 	 FALSE,			/* pc_relative */
   1016 	 0,			/* bitpos */
   1017 	 complain_overflow_signed, /* complain_on_overflow */
   1018 	 ppc64_elf_unhandled_reloc, /* special_function */
   1019 	 "R_PPC64_PLTGOT16",	/* name */
   1020 	 FALSE,			/* partial_inplace */
   1021 	 0,			/* src_mask */
   1022 	 0xffff,		/* dst_mask */
   1023 	 FALSE),		/* pcrel_offset */
   1024 
   1025   /* Like R_PPC64_PLTGOT16, but without overflow.  */
   1026   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
   1027   HOWTO (R_PPC64_PLTGOT16_LO,	/* type */
   1028 	 0,			/* rightshift */
   1029 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1030 	 16,			/* bitsize */
   1031 	 FALSE,			/* pc_relative */
   1032 	 0,			/* bitpos */
   1033 	 complain_overflow_dont, /* complain_on_overflow */
   1034 	 ppc64_elf_unhandled_reloc, /* special_function */
   1035 	 "R_PPC64_PLTGOT16_LO",	/* name */
   1036 	 FALSE,			/* partial_inplace */
   1037 	 0,			/* src_mask */
   1038 	 0xffff,		/* dst_mask */
   1039 	 FALSE),		/* pcrel_offset */
   1040 
   1041   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
   1042   /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
   1043   HOWTO (R_PPC64_PLTGOT16_HI,	/* type */
   1044 	 16,			/* rightshift */
   1045 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1046 	 16,			/* bitsize */
   1047 	 FALSE,			/* pc_relative */
   1048 	 0,			/* bitpos */
   1049 	 complain_overflow_dont, /* complain_on_overflow */
   1050 	 ppc64_elf_unhandled_reloc, /* special_function */
   1051 	 "R_PPC64_PLTGOT16_HI",	/* name */
   1052 	 FALSE,			/* partial_inplace */
   1053 	 0,			/* src_mask */
   1054 	 0xffff,		/* dst_mask */
   1055 	 FALSE),		/* pcrel_offset */
   1056 
   1057   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
   1058      1 if the contents of the low 16 bits, treated as a signed number,
   1059      is negative.  */
   1060   /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
   1061   HOWTO (R_PPC64_PLTGOT16_HA,	/* type */
   1062 	 16,			/* rightshift */
   1063 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1064 	 16,			/* bitsize */
   1065 	 FALSE,			/* pc_relative */
   1066 	 0,			/* bitpos */
   1067 	 complain_overflow_dont,/* complain_on_overflow */
   1068 	 ppc64_elf_unhandled_reloc, /* special_function */
   1069 	 "R_PPC64_PLTGOT16_HA",	/* name */
   1070 	 FALSE,			/* partial_inplace */
   1071 	 0,			/* src_mask */
   1072 	 0xffff,		/* dst_mask */
   1073 	 FALSE),		/* pcrel_offset */
   1074 
   1075   /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
   1076   HOWTO (R_PPC64_ADDR16_DS,	/* type */
   1077 	 0,			/* rightshift */
   1078 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1079 	 16,			/* bitsize */
   1080 	 FALSE,			/* pc_relative */
   1081 	 0,			/* bitpos */
   1082 	 complain_overflow_bitfield, /* complain_on_overflow */
   1083 	 bfd_elf_generic_reloc,	/* special_function */
   1084 	 "R_PPC64_ADDR16_DS",	/* name */
   1085 	 FALSE,			/* partial_inplace */
   1086 	 0,			/* src_mask */
   1087 	 0xfffc,		/* dst_mask */
   1088 	 FALSE),		/* pcrel_offset */
   1089 
   1090   /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
   1091   HOWTO (R_PPC64_ADDR16_LO_DS,	/* type */
   1092 	 0,			/* rightshift */
   1093 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1094 	 16,			/* bitsize */
   1095 	 FALSE,			/* pc_relative */
   1096 	 0,			/* bitpos */
   1097 	 complain_overflow_dont,/* complain_on_overflow */
   1098 	 bfd_elf_generic_reloc,	/* special_function */
   1099 	 "R_PPC64_ADDR16_LO_DS",/* name */
   1100 	 FALSE,			/* partial_inplace */
   1101 	 0,			/* src_mask */
   1102 	 0xfffc,		/* dst_mask */
   1103 	 FALSE),		/* pcrel_offset */
   1104 
   1105   /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
   1106   HOWTO (R_PPC64_GOT16_DS,	/* type */
   1107 	 0,			/* rightshift */
   1108 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1109 	 16,			/* bitsize */
   1110 	 FALSE,			/* pc_relative */
   1111 	 0,			/* bitpos */
   1112 	 complain_overflow_signed, /* complain_on_overflow */
   1113 	 ppc64_elf_unhandled_reloc, /* special_function */
   1114 	 "R_PPC64_GOT16_DS",	/* name */
   1115 	 FALSE,			/* partial_inplace */
   1116 	 0,			/* src_mask */
   1117 	 0xfffc,		/* dst_mask */
   1118 	 FALSE),		/* pcrel_offset */
   1119 
   1120   /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
   1121   HOWTO (R_PPC64_GOT16_LO_DS,	/* type */
   1122 	 0,			/* rightshift */
   1123 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1124 	 16,			/* bitsize */
   1125 	 FALSE,			/* pc_relative */
   1126 	 0,			/* bitpos */
   1127 	 complain_overflow_dont, /* complain_on_overflow */
   1128 	 ppc64_elf_unhandled_reloc, /* special_function */
   1129 	 "R_PPC64_GOT16_LO_DS",	/* name */
   1130 	 FALSE,			/* partial_inplace */
   1131 	 0,			/* src_mask */
   1132 	 0xfffc,		/* dst_mask */
   1133 	 FALSE),		/* pcrel_offset */
   1134 
   1135   /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
   1136   HOWTO (R_PPC64_PLT16_LO_DS,	/* type */
   1137 	 0,			/* rightshift */
   1138 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1139 	 16,			/* bitsize */
   1140 	 FALSE,			/* pc_relative */
   1141 	 0,			/* bitpos */
   1142 	 complain_overflow_dont, /* complain_on_overflow */
   1143 	 ppc64_elf_unhandled_reloc, /* special_function */
   1144 	 "R_PPC64_PLT16_LO_DS",	/* name */
   1145 	 FALSE,			/* partial_inplace */
   1146 	 0,			/* src_mask */
   1147 	 0xfffc,		/* dst_mask */
   1148 	 FALSE),		/* pcrel_offset */
   1149 
   1150   /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
   1151   HOWTO (R_PPC64_SECTOFF_DS,	/* type */
   1152 	 0,			/* rightshift */
   1153 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1154 	 16,			/* bitsize */
   1155 	 FALSE,			/* pc_relative */
   1156 	 0,			/* bitpos */
   1157 	 complain_overflow_bitfield, /* complain_on_overflow */
   1158 	 ppc64_elf_sectoff_reloc, /* special_function */
   1159 	 "R_PPC64_SECTOFF_DS",	/* name */
   1160 	 FALSE,			/* partial_inplace */
   1161 	 0,			/* src_mask */
   1162 	 0xfffc,		/* dst_mask */
   1163 	 FALSE),		/* pcrel_offset */
   1164 
   1165   /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
   1166   HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
   1167 	 0,			/* rightshift */
   1168 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1169 	 16,			/* bitsize */
   1170 	 FALSE,			/* pc_relative */
   1171 	 0,			/* bitpos */
   1172 	 complain_overflow_dont, /* complain_on_overflow */
   1173 	 ppc64_elf_sectoff_reloc, /* special_function */
   1174 	 "R_PPC64_SECTOFF_LO_DS",/* name */
   1175 	 FALSE,			/* partial_inplace */
   1176 	 0,			/* src_mask */
   1177 	 0xfffc,		/* dst_mask */
   1178 	 FALSE),		/* pcrel_offset */
   1179 
   1180   /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
   1181   HOWTO (R_PPC64_TOC16_DS,	/* type */
   1182 	 0,			/* rightshift */
   1183 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1184 	 16,			/* bitsize */
   1185 	 FALSE,			/* pc_relative */
   1186 	 0,			/* bitpos */
   1187 	 complain_overflow_signed, /* complain_on_overflow */
   1188 	 ppc64_elf_toc_reloc,	/* special_function */
   1189 	 "R_PPC64_TOC16_DS",	/* name */
   1190 	 FALSE,			/* partial_inplace */
   1191 	 0,			/* src_mask */
   1192 	 0xfffc,		/* dst_mask */
   1193 	 FALSE),		/* pcrel_offset */
   1194 
   1195   /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
   1196   HOWTO (R_PPC64_TOC16_LO_DS,	/* type */
   1197 	 0,			/* rightshift */
   1198 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1199 	 16,			/* bitsize */
   1200 	 FALSE,			/* pc_relative */
   1201 	 0,			/* bitpos */
   1202 	 complain_overflow_dont, /* complain_on_overflow */
   1203 	 ppc64_elf_toc_reloc,	/* special_function */
   1204 	 "R_PPC64_TOC16_LO_DS",	/* name */
   1205 	 FALSE,			/* partial_inplace */
   1206 	 0,			/* src_mask */
   1207 	 0xfffc,		/* dst_mask */
   1208 	 FALSE),		/* pcrel_offset */
   1209 
   1210   /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
   1211   /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
   1212   HOWTO (R_PPC64_PLTGOT16_DS,	/* type */
   1213 	 0,			/* rightshift */
   1214 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1215 	 16,			/* bitsize */
   1216 	 FALSE,			/* pc_relative */
   1217 	 0,			/* bitpos */
   1218 	 complain_overflow_signed, /* complain_on_overflow */
   1219 	 ppc64_elf_unhandled_reloc, /* special_function */
   1220 	 "R_PPC64_PLTGOT16_DS",	/* name */
   1221 	 FALSE,			/* partial_inplace */
   1222 	 0,			/* src_mask */
   1223 	 0xfffc,		/* dst_mask */
   1224 	 FALSE),		/* pcrel_offset */
   1225 
   1226   /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
   1227   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
   1228   HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
   1229 	 0,			/* rightshift */
   1230 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1231 	 16,			/* bitsize */
   1232 	 FALSE,			/* pc_relative */
   1233 	 0,			/* bitpos */
   1234 	 complain_overflow_dont, /* complain_on_overflow */
   1235 	 ppc64_elf_unhandled_reloc, /* special_function */
   1236 	 "R_PPC64_PLTGOT16_LO_DS",/* name */
   1237 	 FALSE,			/* partial_inplace */
   1238 	 0,			/* src_mask */
   1239 	 0xfffc,		/* dst_mask */
   1240 	 FALSE),		/* pcrel_offset */
   1241 
   1242   /* Marker relocs for TLS.  */
   1243   HOWTO (R_PPC64_TLS,
   1244 	 0,			/* rightshift */
   1245 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1246 	 32,			/* bitsize */
   1247 	 FALSE,			/* pc_relative */
   1248 	 0,			/* bitpos */
   1249 	 complain_overflow_dont, /* complain_on_overflow */
   1250 	 bfd_elf_generic_reloc,	/* special_function */
   1251 	 "R_PPC64_TLS",		/* name */
   1252 	 FALSE,			/* partial_inplace */
   1253 	 0,			/* src_mask */
   1254 	 0,			/* dst_mask */
   1255 	 FALSE),		/* pcrel_offset */
   1256 
   1257   HOWTO (R_PPC64_TLSGD,
   1258 	 0,			/* rightshift */
   1259 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1260 	 32,			/* bitsize */
   1261 	 FALSE,			/* pc_relative */
   1262 	 0,			/* bitpos */
   1263 	 complain_overflow_dont, /* complain_on_overflow */
   1264 	 bfd_elf_generic_reloc,	/* special_function */
   1265 	 "R_PPC64_TLSGD",	/* name */
   1266 	 FALSE,			/* partial_inplace */
   1267 	 0,			/* src_mask */
   1268 	 0,			/* dst_mask */
   1269 	 FALSE),		/* pcrel_offset */
   1270 
   1271   HOWTO (R_PPC64_TLSLD,
   1272 	 0,			/* rightshift */
   1273 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1274 	 32,			/* bitsize */
   1275 	 FALSE,			/* pc_relative */
   1276 	 0,			/* bitpos */
   1277 	 complain_overflow_dont, /* complain_on_overflow */
   1278 	 bfd_elf_generic_reloc,	/* special_function */
   1279 	 "R_PPC64_TLSLD",	/* name */
   1280 	 FALSE,			/* partial_inplace */
   1281 	 0,			/* src_mask */
   1282 	 0,			/* dst_mask */
   1283 	 FALSE),		/* pcrel_offset */
   1284 
   1285   /* Computes the load module index of the load module that contains the
   1286      definition of its TLS sym.  */
   1287   HOWTO (R_PPC64_DTPMOD64,
   1288 	 0,			/* rightshift */
   1289 	 4,			/* size (0 = byte, 1 = short, 2 = long) */
   1290 	 64,			/* bitsize */
   1291 	 FALSE,			/* pc_relative */
   1292 	 0,			/* bitpos */
   1293 	 complain_overflow_dont, /* complain_on_overflow */
   1294 	 ppc64_elf_unhandled_reloc, /* special_function */
   1295 	 "R_PPC64_DTPMOD64",	/* name */
   1296 	 FALSE,			/* partial_inplace */
   1297 	 0,			/* src_mask */
   1298 	 ONES (64),		/* dst_mask */
   1299 	 FALSE),		/* pcrel_offset */
   1300 
   1301   /* Computes a dtv-relative displacement, the difference between the value
   1302      of sym+add and the base address of the thread-local storage block that
   1303      contains the definition of sym, minus 0x8000.  */
   1304   HOWTO (R_PPC64_DTPREL64,
   1305 	 0,			/* rightshift */
   1306 	 4,			/* size (0 = byte, 1 = short, 2 = long) */
   1307 	 64,			/* bitsize */
   1308 	 FALSE,			/* pc_relative */
   1309 	 0,			/* bitpos */
   1310 	 complain_overflow_dont, /* complain_on_overflow */
   1311 	 ppc64_elf_unhandled_reloc, /* special_function */
   1312 	 "R_PPC64_DTPREL64",	/* name */
   1313 	 FALSE,			/* partial_inplace */
   1314 	 0,			/* src_mask */
   1315 	 ONES (64),		/* dst_mask */
   1316 	 FALSE),		/* pcrel_offset */
   1317 
   1318   /* A 16 bit dtprel reloc.  */
   1319   HOWTO (R_PPC64_DTPREL16,
   1320 	 0,			/* rightshift */
   1321 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1322 	 16,			/* bitsize */
   1323 	 FALSE,			/* pc_relative */
   1324 	 0,			/* bitpos */
   1325 	 complain_overflow_signed, /* complain_on_overflow */
   1326 	 ppc64_elf_unhandled_reloc, /* special_function */
   1327 	 "R_PPC64_DTPREL16",	/* name */
   1328 	 FALSE,			/* partial_inplace */
   1329 	 0,			/* src_mask */
   1330 	 0xffff,		/* dst_mask */
   1331 	 FALSE),		/* pcrel_offset */
   1332 
   1333   /* Like DTPREL16, but no overflow.  */
   1334   HOWTO (R_PPC64_DTPREL16_LO,
   1335 	 0,			/* rightshift */
   1336 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1337 	 16,			/* bitsize */
   1338 	 FALSE,			/* pc_relative */
   1339 	 0,			/* bitpos */
   1340 	 complain_overflow_dont, /* complain_on_overflow */
   1341 	 ppc64_elf_unhandled_reloc, /* special_function */
   1342 	 "R_PPC64_DTPREL16_LO",	/* name */
   1343 	 FALSE,			/* partial_inplace */
   1344 	 0,			/* src_mask */
   1345 	 0xffff,		/* dst_mask */
   1346 	 FALSE),		/* pcrel_offset */
   1347 
   1348   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
   1349   HOWTO (R_PPC64_DTPREL16_HI,
   1350 	 16,			/* rightshift */
   1351 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1352 	 16,			/* bitsize */
   1353 	 FALSE,			/* pc_relative */
   1354 	 0,			/* bitpos */
   1355 	 complain_overflow_dont, /* complain_on_overflow */
   1356 	 ppc64_elf_unhandled_reloc, /* special_function */
   1357 	 "R_PPC64_DTPREL16_HI",	/* name */
   1358 	 FALSE,			/* partial_inplace */
   1359 	 0,			/* src_mask */
   1360 	 0xffff,		/* dst_mask */
   1361 	 FALSE),		/* pcrel_offset */
   1362 
   1363   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
   1364   HOWTO (R_PPC64_DTPREL16_HA,
   1365 	 16,			/* rightshift */
   1366 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1367 	 16,			/* bitsize */
   1368 	 FALSE,			/* pc_relative */
   1369 	 0,			/* bitpos */
   1370 	 complain_overflow_dont, /* complain_on_overflow */
   1371 	 ppc64_elf_unhandled_reloc, /* special_function */
   1372 	 "R_PPC64_DTPREL16_HA",	/* name */
   1373 	 FALSE,			/* partial_inplace */
   1374 	 0,			/* src_mask */
   1375 	 0xffff,		/* dst_mask */
   1376 	 FALSE),		/* pcrel_offset */
   1377 
   1378   /* Like DTPREL16_HI, but next higher group of 16 bits.  */
   1379   HOWTO (R_PPC64_DTPREL16_HIGHER,
   1380 	 32,			/* rightshift */
   1381 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1382 	 16,			/* bitsize */
   1383 	 FALSE,			/* pc_relative */
   1384 	 0,			/* bitpos */
   1385 	 complain_overflow_dont, /* complain_on_overflow */
   1386 	 ppc64_elf_unhandled_reloc, /* special_function */
   1387 	 "R_PPC64_DTPREL16_HIGHER", /* name */
   1388 	 FALSE,			/* partial_inplace */
   1389 	 0,			/* src_mask */
   1390 	 0xffff,		/* dst_mask */
   1391 	 FALSE),		/* pcrel_offset */
   1392 
   1393   /* Like DTPREL16_HIGHER, but adjust for low 16 bits.  */
   1394   HOWTO (R_PPC64_DTPREL16_HIGHERA,
   1395 	 32,			/* rightshift */
   1396 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1397 	 16,			/* bitsize */
   1398 	 FALSE,			/* pc_relative */
   1399 	 0,			/* bitpos */
   1400 	 complain_overflow_dont, /* complain_on_overflow */
   1401 	 ppc64_elf_unhandled_reloc, /* special_function */
   1402 	 "R_PPC64_DTPREL16_HIGHERA", /* name */
   1403 	 FALSE,			/* partial_inplace */
   1404 	 0,			/* src_mask */
   1405 	 0xffff,		/* dst_mask */
   1406 	 FALSE),		/* pcrel_offset */
   1407 
   1408   /* Like DTPREL16_HIGHER, but next higher group of 16 bits.  */
   1409   HOWTO (R_PPC64_DTPREL16_HIGHEST,
   1410 	 48,			/* rightshift */
   1411 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1412 	 16,			/* bitsize */
   1413 	 FALSE,			/* pc_relative */
   1414 	 0,			/* bitpos */
   1415 	 complain_overflow_dont, /* complain_on_overflow */
   1416 	 ppc64_elf_unhandled_reloc, /* special_function */
   1417 	 "R_PPC64_DTPREL16_HIGHEST", /* name */
   1418 	 FALSE,			/* partial_inplace */
   1419 	 0,			/* src_mask */
   1420 	 0xffff,		/* dst_mask */
   1421 	 FALSE),		/* pcrel_offset */
   1422 
   1423   /* Like DTPREL16_HIGHEST, but adjust for low 16 bits.  */
   1424   HOWTO (R_PPC64_DTPREL16_HIGHESTA,
   1425 	 48,			/* rightshift */
   1426 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1427 	 16,			/* bitsize */
   1428 	 FALSE,			/* pc_relative */
   1429 	 0,			/* bitpos */
   1430 	 complain_overflow_dont, /* complain_on_overflow */
   1431 	 ppc64_elf_unhandled_reloc, /* special_function */
   1432 	 "R_PPC64_DTPREL16_HIGHESTA", /* name */
   1433 	 FALSE,			/* partial_inplace */
   1434 	 0,			/* src_mask */
   1435 	 0xffff,		/* dst_mask */
   1436 	 FALSE),		/* pcrel_offset */
   1437 
   1438   /* Like DTPREL16, but for insns with a DS field.  */
   1439   HOWTO (R_PPC64_DTPREL16_DS,
   1440 	 0,			/* rightshift */
   1441 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1442 	 16,			/* bitsize */
   1443 	 FALSE,			/* pc_relative */
   1444 	 0,			/* bitpos */
   1445 	 complain_overflow_signed, /* complain_on_overflow */
   1446 	 ppc64_elf_unhandled_reloc, /* special_function */
   1447 	 "R_PPC64_DTPREL16_DS",	/* name */
   1448 	 FALSE,			/* partial_inplace */
   1449 	 0,			/* src_mask */
   1450 	 0xfffc,		/* dst_mask */
   1451 	 FALSE),		/* pcrel_offset */
   1452 
   1453   /* Like DTPREL16_DS, but no overflow.  */
   1454   HOWTO (R_PPC64_DTPREL16_LO_DS,
   1455 	 0,			/* rightshift */
   1456 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1457 	 16,			/* bitsize */
   1458 	 FALSE,			/* pc_relative */
   1459 	 0,			/* bitpos */
   1460 	 complain_overflow_dont, /* complain_on_overflow */
   1461 	 ppc64_elf_unhandled_reloc, /* special_function */
   1462 	 "R_PPC64_DTPREL16_LO_DS", /* name */
   1463 	 FALSE,			/* partial_inplace */
   1464 	 0,			/* src_mask */
   1465 	 0xfffc,		/* dst_mask */
   1466 	 FALSE),		/* pcrel_offset */
   1467 
   1468   /* Computes a tp-relative displacement, the difference between the value of
   1469      sym+add and the value of the thread pointer (r13).  */
   1470   HOWTO (R_PPC64_TPREL64,
   1471 	 0,			/* rightshift */
   1472 	 4,			/* size (0 = byte, 1 = short, 2 = long) */
   1473 	 64,			/* bitsize */
   1474 	 FALSE,			/* pc_relative */
   1475 	 0,			/* bitpos */
   1476 	 complain_overflow_dont, /* complain_on_overflow */
   1477 	 ppc64_elf_unhandled_reloc, /* special_function */
   1478 	 "R_PPC64_TPREL64",	/* name */
   1479 	 FALSE,			/* partial_inplace */
   1480 	 0,			/* src_mask */
   1481 	 ONES (64),		/* dst_mask */
   1482 	 FALSE),		/* pcrel_offset */
   1483 
   1484   /* A 16 bit tprel reloc.  */
   1485   HOWTO (R_PPC64_TPREL16,
   1486 	 0,			/* rightshift */
   1487 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1488 	 16,			/* bitsize */
   1489 	 FALSE,			/* pc_relative */
   1490 	 0,			/* bitpos */
   1491 	 complain_overflow_signed, /* complain_on_overflow */
   1492 	 ppc64_elf_unhandled_reloc, /* special_function */
   1493 	 "R_PPC64_TPREL16",	/* name */
   1494 	 FALSE,			/* partial_inplace */
   1495 	 0,			/* src_mask */
   1496 	 0xffff,		/* dst_mask */
   1497 	 FALSE),		/* pcrel_offset */
   1498 
   1499   /* Like TPREL16, but no overflow.  */
   1500   HOWTO (R_PPC64_TPREL16_LO,
   1501 	 0,			/* rightshift */
   1502 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1503 	 16,			/* bitsize */
   1504 	 FALSE,			/* pc_relative */
   1505 	 0,			/* bitpos */
   1506 	 complain_overflow_dont, /* complain_on_overflow */
   1507 	 ppc64_elf_unhandled_reloc, /* special_function */
   1508 	 "R_PPC64_TPREL16_LO",	/* name */
   1509 	 FALSE,			/* partial_inplace */
   1510 	 0,			/* src_mask */
   1511 	 0xffff,		/* dst_mask */
   1512 	 FALSE),		/* pcrel_offset */
   1513 
   1514   /* Like TPREL16_LO, but next higher group of 16 bits.  */
   1515   HOWTO (R_PPC64_TPREL16_HI,
   1516 	 16,			/* rightshift */
   1517 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1518 	 16,			/* bitsize */
   1519 	 FALSE,			/* pc_relative */
   1520 	 0,			/* bitpos */
   1521 	 complain_overflow_dont, /* complain_on_overflow */
   1522 	 ppc64_elf_unhandled_reloc, /* special_function */
   1523 	 "R_PPC64_TPREL16_HI",	/* name */
   1524 	 FALSE,			/* partial_inplace */
   1525 	 0,			/* src_mask */
   1526 	 0xffff,		/* dst_mask */
   1527 	 FALSE),		/* pcrel_offset */
   1528 
   1529   /* Like TPREL16_HI, but adjust for low 16 bits.  */
   1530   HOWTO (R_PPC64_TPREL16_HA,
   1531 	 16,			/* rightshift */
   1532 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1533 	 16,			/* bitsize */
   1534 	 FALSE,			/* pc_relative */
   1535 	 0,			/* bitpos */
   1536 	 complain_overflow_dont, /* complain_on_overflow */
   1537 	 ppc64_elf_unhandled_reloc, /* special_function */
   1538 	 "R_PPC64_TPREL16_HA",	/* name */
   1539 	 FALSE,			/* partial_inplace */
   1540 	 0,			/* src_mask */
   1541 	 0xffff,		/* dst_mask */
   1542 	 FALSE),		/* pcrel_offset */
   1543 
   1544   /* Like TPREL16_HI, but next higher group of 16 bits.  */
   1545   HOWTO (R_PPC64_TPREL16_HIGHER,
   1546 	 32,			/* rightshift */
   1547 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1548 	 16,			/* bitsize */
   1549 	 FALSE,			/* pc_relative */
   1550 	 0,			/* bitpos */
   1551 	 complain_overflow_dont, /* complain_on_overflow */
   1552 	 ppc64_elf_unhandled_reloc, /* special_function */
   1553 	 "R_PPC64_TPREL16_HIGHER",	/* name */
   1554 	 FALSE,			/* partial_inplace */
   1555 	 0,			/* src_mask */
   1556 	 0xffff,		/* dst_mask */
   1557 	 FALSE),		/* pcrel_offset */
   1558 
   1559   /* Like TPREL16_HIGHER, but adjust for low 16 bits.  */
   1560   HOWTO (R_PPC64_TPREL16_HIGHERA,
   1561 	 32,			/* rightshift */
   1562 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1563 	 16,			/* bitsize */
   1564 	 FALSE,			/* pc_relative */
   1565 	 0,			/* bitpos */
   1566 	 complain_overflow_dont, /* complain_on_overflow */
   1567 	 ppc64_elf_unhandled_reloc, /* special_function */
   1568 	 "R_PPC64_TPREL16_HIGHERA", /* name */
   1569 	 FALSE,			/* partial_inplace */
   1570 	 0,			/* src_mask */
   1571 	 0xffff,		/* dst_mask */
   1572 	 FALSE),		/* pcrel_offset */
   1573 
   1574   /* Like TPREL16_HIGHER, but next higher group of 16 bits.  */
   1575   HOWTO (R_PPC64_TPREL16_HIGHEST,
   1576 	 48,			/* rightshift */
   1577 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1578 	 16,			/* bitsize */
   1579 	 FALSE,			/* pc_relative */
   1580 	 0,			/* bitpos */
   1581 	 complain_overflow_dont, /* complain_on_overflow */
   1582 	 ppc64_elf_unhandled_reloc, /* special_function */
   1583 	 "R_PPC64_TPREL16_HIGHEST", /* name */
   1584 	 FALSE,			/* partial_inplace */
   1585 	 0,			/* src_mask */
   1586 	 0xffff,		/* dst_mask */
   1587 	 FALSE),		/* pcrel_offset */
   1588 
   1589   /* Like TPREL16_HIGHEST, but adjust for low 16 bits.  */
   1590   HOWTO (R_PPC64_TPREL16_HIGHESTA,
   1591 	 48,			/* rightshift */
   1592 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1593 	 16,			/* bitsize */
   1594 	 FALSE,			/* pc_relative */
   1595 	 0,			/* bitpos */
   1596 	 complain_overflow_dont, /* complain_on_overflow */
   1597 	 ppc64_elf_unhandled_reloc, /* special_function */
   1598 	 "R_PPC64_TPREL16_HIGHESTA", /* name */
   1599 	 FALSE,			/* partial_inplace */
   1600 	 0,			/* src_mask */
   1601 	 0xffff,		/* dst_mask */
   1602 	 FALSE),		/* pcrel_offset */
   1603 
   1604   /* Like TPREL16, but for insns with a DS field.  */
   1605   HOWTO (R_PPC64_TPREL16_DS,
   1606 	 0,			/* rightshift */
   1607 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1608 	 16,			/* bitsize */
   1609 	 FALSE,			/* pc_relative */
   1610 	 0,			/* bitpos */
   1611 	 complain_overflow_signed, /* complain_on_overflow */
   1612 	 ppc64_elf_unhandled_reloc, /* special_function */
   1613 	 "R_PPC64_TPREL16_DS",	/* name */
   1614 	 FALSE,			/* partial_inplace */
   1615 	 0,			/* src_mask */
   1616 	 0xfffc,		/* dst_mask */
   1617 	 FALSE),		/* pcrel_offset */
   1618 
   1619   /* Like TPREL16_DS, but no overflow.  */
   1620   HOWTO (R_PPC64_TPREL16_LO_DS,
   1621 	 0,			/* rightshift */
   1622 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1623 	 16,			/* bitsize */
   1624 	 FALSE,			/* pc_relative */
   1625 	 0,			/* bitpos */
   1626 	 complain_overflow_dont, /* complain_on_overflow */
   1627 	 ppc64_elf_unhandled_reloc, /* special_function */
   1628 	 "R_PPC64_TPREL16_LO_DS", /* name */
   1629 	 FALSE,			/* partial_inplace */
   1630 	 0,			/* src_mask */
   1631 	 0xfffc,		/* dst_mask */
   1632 	 FALSE),		/* pcrel_offset */
   1633 
   1634   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
   1635      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
   1636      to the first entry relative to the TOC base (r2).  */
   1637   HOWTO (R_PPC64_GOT_TLSGD16,
   1638 	 0,			/* rightshift */
   1639 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1640 	 16,			/* bitsize */
   1641 	 FALSE,			/* pc_relative */
   1642 	 0,			/* bitpos */
   1643 	 complain_overflow_signed, /* complain_on_overflow */
   1644 	 ppc64_elf_unhandled_reloc, /* special_function */
   1645 	 "R_PPC64_GOT_TLSGD16",	/* name */
   1646 	 FALSE,			/* partial_inplace */
   1647 	 0,			/* src_mask */
   1648 	 0xffff,		/* dst_mask */
   1649 	 FALSE),		/* pcrel_offset */
   1650 
   1651   /* Like GOT_TLSGD16, but no overflow.  */
   1652   HOWTO (R_PPC64_GOT_TLSGD16_LO,
   1653 	 0,			/* rightshift */
   1654 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1655 	 16,			/* bitsize */
   1656 	 FALSE,			/* pc_relative */
   1657 	 0,			/* bitpos */
   1658 	 complain_overflow_dont, /* complain_on_overflow */
   1659 	 ppc64_elf_unhandled_reloc, /* special_function */
   1660 	 "R_PPC64_GOT_TLSGD16_LO", /* name */
   1661 	 FALSE,			/* partial_inplace */
   1662 	 0,			/* src_mask */
   1663 	 0xffff,		/* dst_mask */
   1664 	 FALSE),		/* pcrel_offset */
   1665 
   1666   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
   1667   HOWTO (R_PPC64_GOT_TLSGD16_HI,
   1668 	 16,			/* rightshift */
   1669 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1670 	 16,			/* bitsize */
   1671 	 FALSE,			/* pc_relative */
   1672 	 0,			/* bitpos */
   1673 	 complain_overflow_dont, /* complain_on_overflow */
   1674 	 ppc64_elf_unhandled_reloc, /* special_function */
   1675 	 "R_PPC64_GOT_TLSGD16_HI", /* name */
   1676 	 FALSE,			/* partial_inplace */
   1677 	 0,			/* src_mask */
   1678 	 0xffff,		/* dst_mask */
   1679 	 FALSE),		/* pcrel_offset */
   1680 
   1681   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
   1682   HOWTO (R_PPC64_GOT_TLSGD16_HA,
   1683 	 16,			/* rightshift */
   1684 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1685 	 16,			/* bitsize */
   1686 	 FALSE,			/* pc_relative */
   1687 	 0,			/* bitpos */
   1688 	 complain_overflow_dont, /* complain_on_overflow */
   1689 	 ppc64_elf_unhandled_reloc, /* special_function */
   1690 	 "R_PPC64_GOT_TLSGD16_HA", /* name */
   1691 	 FALSE,			/* partial_inplace */
   1692 	 0,			/* src_mask */
   1693 	 0xffff,		/* dst_mask */
   1694 	 FALSE),		/* pcrel_offset */
   1695 
   1696   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
   1697      with values (sym+add)@dtpmod and zero, and computes the offset to the
   1698      first entry relative to the TOC base (r2).  */
   1699   HOWTO (R_PPC64_GOT_TLSLD16,
   1700 	 0,			/* rightshift */
   1701 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1702 	 16,			/* bitsize */
   1703 	 FALSE,			/* pc_relative */
   1704 	 0,			/* bitpos */
   1705 	 complain_overflow_signed, /* complain_on_overflow */
   1706 	 ppc64_elf_unhandled_reloc, /* special_function */
   1707 	 "R_PPC64_GOT_TLSLD16",	/* name */
   1708 	 FALSE,			/* partial_inplace */
   1709 	 0,			/* src_mask */
   1710 	 0xffff,		/* dst_mask */
   1711 	 FALSE),		/* pcrel_offset */
   1712 
   1713   /* Like GOT_TLSLD16, but no overflow.  */
   1714   HOWTO (R_PPC64_GOT_TLSLD16_LO,
   1715 	 0,			/* rightshift */
   1716 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1717 	 16,			/* bitsize */
   1718 	 FALSE,			/* pc_relative */
   1719 	 0,			/* bitpos */
   1720 	 complain_overflow_dont, /* complain_on_overflow */
   1721 	 ppc64_elf_unhandled_reloc, /* special_function */
   1722 	 "R_PPC64_GOT_TLSLD16_LO", /* name */
   1723 	 FALSE,			/* partial_inplace */
   1724 	 0,			/* src_mask */
   1725 	 0xffff,		/* dst_mask */
   1726 	 FALSE),		/* pcrel_offset */
   1727 
   1728   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
   1729   HOWTO (R_PPC64_GOT_TLSLD16_HI,
   1730 	 16,			/* rightshift */
   1731 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1732 	 16,			/* bitsize */
   1733 	 FALSE,			/* pc_relative */
   1734 	 0,			/* bitpos */
   1735 	 complain_overflow_dont, /* complain_on_overflow */
   1736 	 ppc64_elf_unhandled_reloc, /* special_function */
   1737 	 "R_PPC64_GOT_TLSLD16_HI", /* name */
   1738 	 FALSE,			/* partial_inplace */
   1739 	 0,			/* src_mask */
   1740 	 0xffff,		/* dst_mask */
   1741 	 FALSE),		/* pcrel_offset */
   1742 
   1743   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
   1744   HOWTO (R_PPC64_GOT_TLSLD16_HA,
   1745 	 16,			/* rightshift */
   1746 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1747 	 16,			/* bitsize */
   1748 	 FALSE,			/* pc_relative */
   1749 	 0,			/* bitpos */
   1750 	 complain_overflow_dont, /* complain_on_overflow */
   1751 	 ppc64_elf_unhandled_reloc, /* special_function */
   1752 	 "R_PPC64_GOT_TLSLD16_HA", /* name */
   1753 	 FALSE,			/* partial_inplace */
   1754 	 0,			/* src_mask */
   1755 	 0xffff,		/* dst_mask */
   1756 	 FALSE),		/* pcrel_offset */
   1757 
   1758   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
   1759      the offset to the entry relative to the TOC base (r2).  */
   1760   HOWTO (R_PPC64_GOT_DTPREL16_DS,
   1761 	 0,			/* rightshift */
   1762 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1763 	 16,			/* bitsize */
   1764 	 FALSE,			/* pc_relative */
   1765 	 0,			/* bitpos */
   1766 	 complain_overflow_signed, /* complain_on_overflow */
   1767 	 ppc64_elf_unhandled_reloc, /* special_function */
   1768 	 "R_PPC64_GOT_DTPREL16_DS", /* name */
   1769 	 FALSE,			/* partial_inplace */
   1770 	 0,			/* src_mask */
   1771 	 0xfffc,		/* dst_mask */
   1772 	 FALSE),		/* pcrel_offset */
   1773 
   1774   /* Like GOT_DTPREL16_DS, but no overflow.  */
   1775   HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
   1776 	 0,			/* rightshift */
   1777 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1778 	 16,			/* bitsize */
   1779 	 FALSE,			/* pc_relative */
   1780 	 0,			/* bitpos */
   1781 	 complain_overflow_dont, /* complain_on_overflow */
   1782 	 ppc64_elf_unhandled_reloc, /* special_function */
   1783 	 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
   1784 	 FALSE,			/* partial_inplace */
   1785 	 0,			/* src_mask */
   1786 	 0xfffc,		/* dst_mask */
   1787 	 FALSE),		/* pcrel_offset */
   1788 
   1789   /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits.  */
   1790   HOWTO (R_PPC64_GOT_DTPREL16_HI,
   1791 	 16,			/* rightshift */
   1792 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1793 	 16,			/* bitsize */
   1794 	 FALSE,			/* pc_relative */
   1795 	 0,			/* bitpos */
   1796 	 complain_overflow_dont, /* complain_on_overflow */
   1797 	 ppc64_elf_unhandled_reloc, /* special_function */
   1798 	 "R_PPC64_GOT_DTPREL16_HI", /* name */
   1799 	 FALSE,			/* partial_inplace */
   1800 	 0,			/* src_mask */
   1801 	 0xffff,		/* dst_mask */
   1802 	 FALSE),		/* pcrel_offset */
   1803 
   1804   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
   1805   HOWTO (R_PPC64_GOT_DTPREL16_HA,
   1806 	 16,			/* rightshift */
   1807 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1808 	 16,			/* bitsize */
   1809 	 FALSE,			/* pc_relative */
   1810 	 0,			/* bitpos */
   1811 	 complain_overflow_dont, /* complain_on_overflow */
   1812 	 ppc64_elf_unhandled_reloc, /* special_function */
   1813 	 "R_PPC64_GOT_DTPREL16_HA", /* name */
   1814 	 FALSE,			/* partial_inplace */
   1815 	 0,			/* src_mask */
   1816 	 0xffff,		/* dst_mask */
   1817 	 FALSE),		/* pcrel_offset */
   1818 
   1819   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
   1820      offset to the entry relative to the TOC base (r2).  */
   1821   HOWTO (R_PPC64_GOT_TPREL16_DS,
   1822 	 0,			/* rightshift */
   1823 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1824 	 16,			/* bitsize */
   1825 	 FALSE,			/* pc_relative */
   1826 	 0,			/* bitpos */
   1827 	 complain_overflow_signed, /* complain_on_overflow */
   1828 	 ppc64_elf_unhandled_reloc, /* special_function */
   1829 	 "R_PPC64_GOT_TPREL16_DS", /* name */
   1830 	 FALSE,			/* partial_inplace */
   1831 	 0,			/* src_mask */
   1832 	 0xfffc,		/* dst_mask */
   1833 	 FALSE),		/* pcrel_offset */
   1834 
   1835   /* Like GOT_TPREL16_DS, but no overflow.  */
   1836   HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
   1837 	 0,			/* rightshift */
   1838 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1839 	 16,			/* bitsize */
   1840 	 FALSE,			/* pc_relative */
   1841 	 0,			/* bitpos */
   1842 	 complain_overflow_dont, /* complain_on_overflow */
   1843 	 ppc64_elf_unhandled_reloc, /* special_function */
   1844 	 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
   1845 	 FALSE,			/* partial_inplace */
   1846 	 0,			/* src_mask */
   1847 	 0xfffc,		/* dst_mask */
   1848 	 FALSE),		/* pcrel_offset */
   1849 
   1850   /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits.  */
   1851   HOWTO (R_PPC64_GOT_TPREL16_HI,
   1852 	 16,			/* rightshift */
   1853 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1854 	 16,			/* bitsize */
   1855 	 FALSE,			/* pc_relative */
   1856 	 0,			/* bitpos */
   1857 	 complain_overflow_dont, /* complain_on_overflow */
   1858 	 ppc64_elf_unhandled_reloc, /* special_function */
   1859 	 "R_PPC64_GOT_TPREL16_HI", /* name */
   1860 	 FALSE,			/* partial_inplace */
   1861 	 0,			/* src_mask */
   1862 	 0xffff,		/* dst_mask */
   1863 	 FALSE),		/* pcrel_offset */
   1864 
   1865   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
   1866   HOWTO (R_PPC64_GOT_TPREL16_HA,
   1867 	 16,			/* rightshift */
   1868 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1869 	 16,			/* bitsize */
   1870 	 FALSE,			/* pc_relative */
   1871 	 0,			/* bitpos */
   1872 	 complain_overflow_dont, /* complain_on_overflow */
   1873 	 ppc64_elf_unhandled_reloc, /* special_function */
   1874 	 "R_PPC64_GOT_TPREL16_HA", /* name */
   1875 	 FALSE,			/* partial_inplace */
   1876 	 0,			/* src_mask */
   1877 	 0xffff,		/* dst_mask */
   1878 	 FALSE),		/* pcrel_offset */
   1879 
   1880   HOWTO (R_PPC64_JMP_IREL,	/* type */
   1881 	 0,			/* rightshift */
   1882 	 0,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
   1883 	 0,			/* bitsize */
   1884 	 FALSE,			/* pc_relative */
   1885 	 0,			/* bitpos */
   1886 	 complain_overflow_dont, /* complain_on_overflow */
   1887 	 ppc64_elf_unhandled_reloc, /* special_function */
   1888 	 "R_PPC64_JMP_IREL",	/* name */
   1889 	 FALSE,			/* partial_inplace */
   1890 	 0,			/* src_mask */
   1891 	 0,			/* dst_mask */
   1892 	 FALSE),		/* pcrel_offset */
   1893 
   1894   HOWTO (R_PPC64_IRELATIVE,	/* type */
   1895 	 0,			/* rightshift */
   1896 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
   1897 	 64,			/* bitsize */
   1898 	 FALSE,			/* pc_relative */
   1899 	 0,			/* bitpos */
   1900 	 complain_overflow_dont, /* complain_on_overflow */
   1901 	 bfd_elf_generic_reloc,	/* special_function */
   1902 	 "R_PPC64_IRELATIVE",	/* name */
   1903 	 FALSE,			/* partial_inplace */
   1904 	 0,			/* src_mask */
   1905 	 ONES (64),		/* dst_mask */
   1906 	 FALSE),		/* pcrel_offset */
   1907 
   1908   /* A 16 bit relative relocation.  */
   1909   HOWTO (R_PPC64_REL16,		/* type */
   1910 	 0,			/* rightshift */
   1911 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1912 	 16,			/* bitsize */
   1913 	 TRUE,			/* pc_relative */
   1914 	 0,			/* bitpos */
   1915 	 complain_overflow_bitfield, /* complain_on_overflow */
   1916 	 bfd_elf_generic_reloc,	/* special_function */
   1917 	 "R_PPC64_REL16",	/* name */
   1918 	 FALSE,			/* partial_inplace */
   1919 	 0,			/* src_mask */
   1920 	 0xffff,		/* dst_mask */
   1921 	 TRUE),			/* pcrel_offset */
   1922 
   1923   /* A 16 bit relative relocation without overflow.  */
   1924   HOWTO (R_PPC64_REL16_LO,	/* type */
   1925 	 0,			/* rightshift */
   1926 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1927 	 16,			/* bitsize */
   1928 	 TRUE,			/* pc_relative */
   1929 	 0,			/* bitpos */
   1930 	 complain_overflow_dont,/* complain_on_overflow */
   1931 	 bfd_elf_generic_reloc,	/* special_function */
   1932 	 "R_PPC64_REL16_LO",	/* name */
   1933 	 FALSE,			/* partial_inplace */
   1934 	 0,			/* src_mask */
   1935 	 0xffff,		/* dst_mask */
   1936 	 TRUE),			/* pcrel_offset */
   1937 
   1938   /* The high order 16 bits of a relative address.  */
   1939   HOWTO (R_PPC64_REL16_HI,	/* type */
   1940 	 16,			/* rightshift */
   1941 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1942 	 16,			/* bitsize */
   1943 	 TRUE,			/* pc_relative */
   1944 	 0,			/* bitpos */
   1945 	 complain_overflow_dont, /* complain_on_overflow */
   1946 	 bfd_elf_generic_reloc,	/* special_function */
   1947 	 "R_PPC64_REL16_HI",	/* name */
   1948 	 FALSE,			/* partial_inplace */
   1949 	 0,			/* src_mask */
   1950 	 0xffff,		/* dst_mask */
   1951 	 TRUE),			/* pcrel_offset */
   1952 
   1953   /* The high order 16 bits of a relative address, plus 1 if the contents of
   1954      the low 16 bits, treated as a signed number, is negative.  */
   1955   HOWTO (R_PPC64_REL16_HA,	/* type */
   1956 	 16,			/* rightshift */
   1957 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1958 	 16,			/* bitsize */
   1959 	 TRUE,			/* pc_relative */
   1960 	 0,			/* bitpos */
   1961 	 complain_overflow_dont, /* complain_on_overflow */
   1962 	 ppc64_elf_ha_reloc,	/* special_function */
   1963 	 "R_PPC64_REL16_HA",	/* name */
   1964 	 FALSE,			/* partial_inplace */
   1965 	 0,			/* src_mask */
   1966 	 0xffff,		/* dst_mask */
   1967 	 TRUE),			/* pcrel_offset */
   1968 
   1969   /* GNU extension to record C++ vtable hierarchy.  */
   1970   HOWTO (R_PPC64_GNU_VTINHERIT,	/* type */
   1971 	 0,			/* rightshift */
   1972 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
   1973 	 0,			/* bitsize */
   1974 	 FALSE,			/* pc_relative */
   1975 	 0,			/* bitpos */
   1976 	 complain_overflow_dont, /* complain_on_overflow */
   1977 	 NULL,			/* special_function */
   1978 	 "R_PPC64_GNU_VTINHERIT", /* name */
   1979 	 FALSE,			/* partial_inplace */
   1980 	 0,			/* src_mask */
   1981 	 0,			/* dst_mask */
   1982 	 FALSE),		/* pcrel_offset */
   1983 
   1984   /* GNU extension to record C++ vtable member usage.  */
   1985   HOWTO (R_PPC64_GNU_VTENTRY,	/* type */
   1986 	 0,			/* rightshift */
   1987 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
   1988 	 0,			/* bitsize */
   1989 	 FALSE,			/* pc_relative */
   1990 	 0,			/* bitpos */
   1991 	 complain_overflow_dont, /* complain_on_overflow */
   1992 	 NULL,			/* special_function */
   1993 	 "R_PPC64_GNU_VTENTRY",	/* name */
   1994 	 FALSE,			/* partial_inplace */
   1995 	 0,			/* src_mask */
   1996 	 0,			/* dst_mask */
   1997 	 FALSE),		/* pcrel_offset */
   1998 };
   1999 
   2000 
   2001 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
   2003    be done.  */
   2004 
   2005 static void
   2006 ppc_howto_init (void)
   2007 {
   2008   unsigned int i, type;
   2009 
   2010   for (i = 0;
   2011        i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
   2012        i++)
   2013     {
   2014       type = ppc64_elf_howto_raw[i].type;
   2015       BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
   2016 			  / sizeof (ppc64_elf_howto_table[0])));
   2017       ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
   2018     }
   2019 }
   2020 
   2021 static reloc_howto_type *
   2022 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
   2023 			     bfd_reloc_code_real_type code)
   2024 {
   2025   enum elf_ppc64_reloc_type r = R_PPC64_NONE;
   2026 
   2027   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
   2028     /* Initialize howto table if needed.  */
   2029     ppc_howto_init ();
   2030 
   2031   switch (code)
   2032     {
   2033     default:
   2034       return NULL;
   2035 
   2036     case BFD_RELOC_NONE:			r = R_PPC64_NONE;
   2037       break;
   2038     case BFD_RELOC_32:				r = R_PPC64_ADDR32;
   2039       break;
   2040     case BFD_RELOC_PPC_BA26:			r = R_PPC64_ADDR24;
   2041       break;
   2042     case BFD_RELOC_16:				r = R_PPC64_ADDR16;
   2043       break;
   2044     case BFD_RELOC_LO16:			r = R_PPC64_ADDR16_LO;
   2045       break;
   2046     case BFD_RELOC_HI16:			r = R_PPC64_ADDR16_HI;
   2047       break;
   2048     case BFD_RELOC_HI16_S:			r = R_PPC64_ADDR16_HA;
   2049       break;
   2050     case BFD_RELOC_PPC_BA16:			r = R_PPC64_ADDR14;
   2051       break;
   2052     case BFD_RELOC_PPC_BA16_BRTAKEN:		r = R_PPC64_ADDR14_BRTAKEN;
   2053       break;
   2054     case BFD_RELOC_PPC_BA16_BRNTAKEN:		r = R_PPC64_ADDR14_BRNTAKEN;
   2055       break;
   2056     case BFD_RELOC_PPC_B26:			r = R_PPC64_REL24;
   2057       break;
   2058     case BFD_RELOC_PPC_B16:			r = R_PPC64_REL14;
   2059       break;
   2060     case BFD_RELOC_PPC_B16_BRTAKEN:		r = R_PPC64_REL14_BRTAKEN;
   2061       break;
   2062     case BFD_RELOC_PPC_B16_BRNTAKEN:		r = R_PPC64_REL14_BRNTAKEN;
   2063       break;
   2064     case BFD_RELOC_16_GOTOFF:			r = R_PPC64_GOT16;
   2065       break;
   2066     case BFD_RELOC_LO16_GOTOFF:			r = R_PPC64_GOT16_LO;
   2067       break;
   2068     case BFD_RELOC_HI16_GOTOFF:			r = R_PPC64_GOT16_HI;
   2069       break;
   2070     case BFD_RELOC_HI16_S_GOTOFF:		r = R_PPC64_GOT16_HA;
   2071       break;
   2072     case BFD_RELOC_PPC_COPY:			r = R_PPC64_COPY;
   2073       break;
   2074     case BFD_RELOC_PPC_GLOB_DAT:		r = R_PPC64_GLOB_DAT;
   2075       break;
   2076     case BFD_RELOC_32_PCREL:			r = R_PPC64_REL32;
   2077       break;
   2078     case BFD_RELOC_32_PLTOFF:			r = R_PPC64_PLT32;
   2079       break;
   2080     case BFD_RELOC_32_PLT_PCREL:		r = R_PPC64_PLTREL32;
   2081       break;
   2082     case BFD_RELOC_LO16_PLTOFF:			r = R_PPC64_PLT16_LO;
   2083       break;
   2084     case BFD_RELOC_HI16_PLTOFF:			r = R_PPC64_PLT16_HI;
   2085       break;
   2086     case BFD_RELOC_HI16_S_PLTOFF:		r = R_PPC64_PLT16_HA;
   2087       break;
   2088     case BFD_RELOC_16_BASEREL:			r = R_PPC64_SECTOFF;
   2089       break;
   2090     case BFD_RELOC_LO16_BASEREL:		r = R_PPC64_SECTOFF_LO;
   2091       break;
   2092     case BFD_RELOC_HI16_BASEREL:		r = R_PPC64_SECTOFF_HI;
   2093       break;
   2094     case BFD_RELOC_HI16_S_BASEREL:		r = R_PPC64_SECTOFF_HA;
   2095       break;
   2096     case BFD_RELOC_CTOR:			r = R_PPC64_ADDR64;
   2097       break;
   2098     case BFD_RELOC_64:				r = R_PPC64_ADDR64;
   2099       break;
   2100     case BFD_RELOC_PPC64_HIGHER:		r = R_PPC64_ADDR16_HIGHER;
   2101       break;
   2102     case BFD_RELOC_PPC64_HIGHER_S:		r = R_PPC64_ADDR16_HIGHERA;
   2103       break;
   2104     case BFD_RELOC_PPC64_HIGHEST:		r = R_PPC64_ADDR16_HIGHEST;
   2105       break;
   2106     case BFD_RELOC_PPC64_HIGHEST_S:		r = R_PPC64_ADDR16_HIGHESTA;
   2107       break;
   2108     case BFD_RELOC_64_PCREL:			r = R_PPC64_REL64;
   2109       break;
   2110     case BFD_RELOC_64_PLTOFF:			r = R_PPC64_PLT64;
   2111       break;
   2112     case BFD_RELOC_64_PLT_PCREL:		r = R_PPC64_PLTREL64;
   2113       break;
   2114     case BFD_RELOC_PPC_TOC16:			r = R_PPC64_TOC16;
   2115       break;
   2116     case BFD_RELOC_PPC64_TOC16_LO:		r = R_PPC64_TOC16_LO;
   2117       break;
   2118     case BFD_RELOC_PPC64_TOC16_HI:		r = R_PPC64_TOC16_HI;
   2119       break;
   2120     case BFD_RELOC_PPC64_TOC16_HA:		r = R_PPC64_TOC16_HA;
   2121       break;
   2122     case BFD_RELOC_PPC64_TOC:			r = R_PPC64_TOC;
   2123       break;
   2124     case BFD_RELOC_PPC64_PLTGOT16:		r = R_PPC64_PLTGOT16;
   2125       break;
   2126     case BFD_RELOC_PPC64_PLTGOT16_LO:		r = R_PPC64_PLTGOT16_LO;
   2127       break;
   2128     case BFD_RELOC_PPC64_PLTGOT16_HI:		r = R_PPC64_PLTGOT16_HI;
   2129       break;
   2130     case BFD_RELOC_PPC64_PLTGOT16_HA:		r = R_PPC64_PLTGOT16_HA;
   2131       break;
   2132     case BFD_RELOC_PPC64_ADDR16_DS:		r = R_PPC64_ADDR16_DS;
   2133       break;
   2134     case BFD_RELOC_PPC64_ADDR16_LO_DS:		r = R_PPC64_ADDR16_LO_DS;
   2135       break;
   2136     case BFD_RELOC_PPC64_GOT16_DS:		r = R_PPC64_GOT16_DS;
   2137       break;
   2138     case BFD_RELOC_PPC64_GOT16_LO_DS:		r = R_PPC64_GOT16_LO_DS;
   2139       break;
   2140     case BFD_RELOC_PPC64_PLT16_LO_DS:		r = R_PPC64_PLT16_LO_DS;
   2141       break;
   2142     case BFD_RELOC_PPC64_SECTOFF_DS:		r = R_PPC64_SECTOFF_DS;
   2143       break;
   2144     case BFD_RELOC_PPC64_SECTOFF_LO_DS:		r = R_PPC64_SECTOFF_LO_DS;
   2145       break;
   2146     case BFD_RELOC_PPC64_TOC16_DS:		r = R_PPC64_TOC16_DS;
   2147       break;
   2148     case BFD_RELOC_PPC64_TOC16_LO_DS:		r = R_PPC64_TOC16_LO_DS;
   2149       break;
   2150     case BFD_RELOC_PPC64_PLTGOT16_DS:		r = R_PPC64_PLTGOT16_DS;
   2151       break;
   2152     case BFD_RELOC_PPC64_PLTGOT16_LO_DS:	r = R_PPC64_PLTGOT16_LO_DS;
   2153       break;
   2154     case BFD_RELOC_PPC_TLS:			r = R_PPC64_TLS;
   2155       break;
   2156     case BFD_RELOC_PPC_TLSGD:			r = R_PPC64_TLSGD;
   2157       break;
   2158     case BFD_RELOC_PPC_TLSLD:			r = R_PPC64_TLSLD;
   2159       break;
   2160     case BFD_RELOC_PPC_DTPMOD:			r = R_PPC64_DTPMOD64;
   2161       break;
   2162     case BFD_RELOC_PPC_TPREL16:			r = R_PPC64_TPREL16;
   2163       break;
   2164     case BFD_RELOC_PPC_TPREL16_LO:		r = R_PPC64_TPREL16_LO;
   2165       break;
   2166     case BFD_RELOC_PPC_TPREL16_HI:		r = R_PPC64_TPREL16_HI;
   2167       break;
   2168     case BFD_RELOC_PPC_TPREL16_HA:		r = R_PPC64_TPREL16_HA;
   2169       break;
   2170     case BFD_RELOC_PPC_TPREL:			r = R_PPC64_TPREL64;
   2171       break;
   2172     case BFD_RELOC_PPC_DTPREL16:		r = R_PPC64_DTPREL16;
   2173       break;
   2174     case BFD_RELOC_PPC_DTPREL16_LO:		r = R_PPC64_DTPREL16_LO;
   2175       break;
   2176     case BFD_RELOC_PPC_DTPREL16_HI:		r = R_PPC64_DTPREL16_HI;
   2177       break;
   2178     case BFD_RELOC_PPC_DTPREL16_HA:		r = R_PPC64_DTPREL16_HA;
   2179       break;
   2180     case BFD_RELOC_PPC_DTPREL:			r = R_PPC64_DTPREL64;
   2181       break;
   2182     case BFD_RELOC_PPC_GOT_TLSGD16:		r = R_PPC64_GOT_TLSGD16;
   2183       break;
   2184     case BFD_RELOC_PPC_GOT_TLSGD16_LO:		r = R_PPC64_GOT_TLSGD16_LO;
   2185       break;
   2186     case BFD_RELOC_PPC_GOT_TLSGD16_HI:		r = R_PPC64_GOT_TLSGD16_HI;
   2187       break;
   2188     case BFD_RELOC_PPC_GOT_TLSGD16_HA:		r = R_PPC64_GOT_TLSGD16_HA;
   2189       break;
   2190     case BFD_RELOC_PPC_GOT_TLSLD16:		r = R_PPC64_GOT_TLSLD16;
   2191       break;
   2192     case BFD_RELOC_PPC_GOT_TLSLD16_LO:		r = R_PPC64_GOT_TLSLD16_LO;
   2193       break;
   2194     case BFD_RELOC_PPC_GOT_TLSLD16_HI:		r = R_PPC64_GOT_TLSLD16_HI;
   2195       break;
   2196     case BFD_RELOC_PPC_GOT_TLSLD16_HA:		r = R_PPC64_GOT_TLSLD16_HA;
   2197       break;
   2198     case BFD_RELOC_PPC_GOT_TPREL16:		r = R_PPC64_GOT_TPREL16_DS;
   2199       break;
   2200     case BFD_RELOC_PPC_GOT_TPREL16_LO:		r = R_PPC64_GOT_TPREL16_LO_DS;
   2201       break;
   2202     case BFD_RELOC_PPC_GOT_TPREL16_HI:		r = R_PPC64_GOT_TPREL16_HI;
   2203       break;
   2204     case BFD_RELOC_PPC_GOT_TPREL16_HA:		r = R_PPC64_GOT_TPREL16_HA;
   2205       break;
   2206     case BFD_RELOC_PPC_GOT_DTPREL16:		r = R_PPC64_GOT_DTPREL16_DS;
   2207       break;
   2208     case BFD_RELOC_PPC_GOT_DTPREL16_LO:		r = R_PPC64_GOT_DTPREL16_LO_DS;
   2209       break;
   2210     case BFD_RELOC_PPC_GOT_DTPREL16_HI:		r = R_PPC64_GOT_DTPREL16_HI;
   2211       break;
   2212     case BFD_RELOC_PPC_GOT_DTPREL16_HA:		r = R_PPC64_GOT_DTPREL16_HA;
   2213       break;
   2214     case BFD_RELOC_PPC64_TPREL16_DS:		r = R_PPC64_TPREL16_DS;
   2215       break;
   2216     case BFD_RELOC_PPC64_TPREL16_LO_DS:		r = R_PPC64_TPREL16_LO_DS;
   2217       break;
   2218     case BFD_RELOC_PPC64_TPREL16_HIGHER:	r = R_PPC64_TPREL16_HIGHER;
   2219       break;
   2220     case BFD_RELOC_PPC64_TPREL16_HIGHERA:	r = R_PPC64_TPREL16_HIGHERA;
   2221       break;
   2222     case BFD_RELOC_PPC64_TPREL16_HIGHEST:	r = R_PPC64_TPREL16_HIGHEST;
   2223       break;
   2224     case BFD_RELOC_PPC64_TPREL16_HIGHESTA:	r = R_PPC64_TPREL16_HIGHESTA;
   2225       break;
   2226     case BFD_RELOC_PPC64_DTPREL16_DS:		r = R_PPC64_DTPREL16_DS;
   2227       break;
   2228     case BFD_RELOC_PPC64_DTPREL16_LO_DS:	r = R_PPC64_DTPREL16_LO_DS;
   2229       break;
   2230     case BFD_RELOC_PPC64_DTPREL16_HIGHER:	r = R_PPC64_DTPREL16_HIGHER;
   2231       break;
   2232     case BFD_RELOC_PPC64_DTPREL16_HIGHERA:	r = R_PPC64_DTPREL16_HIGHERA;
   2233       break;
   2234     case BFD_RELOC_PPC64_DTPREL16_HIGHEST:	r = R_PPC64_DTPREL16_HIGHEST;
   2235       break;
   2236     case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:	r = R_PPC64_DTPREL16_HIGHESTA;
   2237       break;
   2238     case BFD_RELOC_16_PCREL:			r = R_PPC64_REL16;
   2239       break;
   2240     case BFD_RELOC_LO16_PCREL:			r = R_PPC64_REL16_LO;
   2241       break;
   2242     case BFD_RELOC_HI16_PCREL:			r = R_PPC64_REL16_HI;
   2243       break;
   2244     case BFD_RELOC_HI16_S_PCREL:		r = R_PPC64_REL16_HA;
   2245       break;
   2246     case BFD_RELOC_VTABLE_INHERIT:		r = R_PPC64_GNU_VTINHERIT;
   2247       break;
   2248     case BFD_RELOC_VTABLE_ENTRY:		r = R_PPC64_GNU_VTENTRY;
   2249       break;
   2250     }
   2251 
   2252   return ppc64_elf_howto_table[r];
   2253 };
   2254 
   2255 static reloc_howto_type *
   2256 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
   2257 			     const char *r_name)
   2258 {
   2259   unsigned int i;
   2260 
   2261   for (i = 0;
   2262        i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
   2263        i++)
   2264     if (ppc64_elf_howto_raw[i].name != NULL
   2265 	&& strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
   2266       return &ppc64_elf_howto_raw[i];
   2267 
   2268   return NULL;
   2269 }
   2270 
   2271 /* Set the howto pointer for a PowerPC ELF reloc.  */
   2272 
   2273 static void
   2274 ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
   2275 			 Elf_Internal_Rela *dst)
   2276 {
   2277   unsigned int type;
   2278 
   2279   /* Initialize howto table if needed.  */
   2280   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
   2281     ppc_howto_init ();
   2282 
   2283   type = ELF64_R_TYPE (dst->r_info);
   2284   if (type >= (sizeof (ppc64_elf_howto_table)
   2285 	       / sizeof (ppc64_elf_howto_table[0])))
   2286     {
   2287       (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
   2288 			     abfd, (int) type);
   2289       type = R_PPC64_NONE;
   2290     }
   2291   cache_ptr->howto = ppc64_elf_howto_table[type];
   2292 }
   2293 
   2294 /* Handle the R_PPC64_ADDR16_HA and similar relocs.  */
   2295 
   2296 static bfd_reloc_status_type
   2297 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2298 		    void *data, asection *input_section,
   2299 		    bfd *output_bfd, char **error_message)
   2300 {
   2301   /* If this is a relocatable link (output_bfd test tells us), just
   2302      call the generic function.  Any adjustment will be done at final
   2303      link time.  */
   2304   if (output_bfd != NULL)
   2305     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2306 				  input_section, output_bfd, error_message);
   2307 
   2308   /* Adjust the addend for sign extension of the low 16 bits.
   2309      We won't actually be using the low 16 bits, so trashing them
   2310      doesn't matter.  */
   2311   reloc_entry->addend += 0x8000;
   2312   return bfd_reloc_continue;
   2313 }
   2314 
   2315 static bfd_reloc_status_type
   2316 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2317 			void *data, asection *input_section,
   2318 			bfd *output_bfd, char **error_message)
   2319 {
   2320   if (output_bfd != NULL)
   2321     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2322 				  input_section, output_bfd, error_message);
   2323 
   2324   if (strcmp (symbol->section->name, ".opd") == 0
   2325       && (symbol->section->owner->flags & DYNAMIC) == 0)
   2326     {
   2327       bfd_vma dest = opd_entry_value (symbol->section,
   2328 				      symbol->value + reloc_entry->addend,
   2329 				      NULL, NULL);
   2330       if (dest != (bfd_vma) -1)
   2331 	reloc_entry->addend = dest - (symbol->value
   2332 				      + symbol->section->output_section->vma
   2333 				      + symbol->section->output_offset);
   2334     }
   2335   return bfd_reloc_continue;
   2336 }
   2337 
   2338 static bfd_reloc_status_type
   2339 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2340 			 void *data, asection *input_section,
   2341 			 bfd *output_bfd, char **error_message)
   2342 {
   2343   long insn;
   2344   enum elf_ppc64_reloc_type r_type;
   2345   bfd_size_type octets;
   2346   /* Disabled until we sort out how ld should choose 'y' vs 'at'.  */
   2347   bfd_boolean is_power4 = FALSE;
   2348 
   2349   /* If this is a relocatable link (output_bfd test tells us), just
   2350      call the generic function.  Any adjustment will be done at final
   2351      link time.  */
   2352   if (output_bfd != NULL)
   2353     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2354 				  input_section, output_bfd, error_message);
   2355 
   2356   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
   2357   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
   2358   insn &= ~(0x01 << 21);
   2359   r_type = reloc_entry->howto->type;
   2360   if (r_type == R_PPC64_ADDR14_BRTAKEN
   2361       || r_type == R_PPC64_REL14_BRTAKEN)
   2362     insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
   2363 
   2364   if (is_power4)
   2365     {
   2366       /* Set 'a' bit.  This is 0b00010 in BO field for branch
   2367 	 on CR(BI) insns (BO == 001at or 011at), and 0b01000
   2368 	 for branch on CTR insns (BO == 1a00t or 1a01t).  */
   2369       if ((insn & (0x14 << 21)) == (0x04 << 21))
   2370 	insn |= 0x02 << 21;
   2371       else if ((insn & (0x14 << 21)) == (0x10 << 21))
   2372 	insn |= 0x08 << 21;
   2373       else
   2374 	goto out;
   2375     }
   2376   else
   2377     {
   2378       bfd_vma target = 0;
   2379       bfd_vma from;
   2380 
   2381       if (!bfd_is_com_section (symbol->section))
   2382 	target = symbol->value;
   2383       target += symbol->section->output_section->vma;
   2384       target += symbol->section->output_offset;
   2385       target += reloc_entry->addend;
   2386 
   2387       from = (reloc_entry->address
   2388 	      + input_section->output_offset
   2389 	      + input_section->output_section->vma);
   2390 
   2391       /* Invert 'y' bit if not the default.  */
   2392       if ((bfd_signed_vma) (target - from) < 0)
   2393 	insn ^= 0x01 << 21;
   2394     }
   2395   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
   2396  out:
   2397   return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
   2398 				 input_section, output_bfd, error_message);
   2399 }
   2400 
   2401 static bfd_reloc_status_type
   2402 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2403 			 void *data, asection *input_section,
   2404 			 bfd *output_bfd, char **error_message)
   2405 {
   2406   /* If this is a relocatable link (output_bfd test tells us), just
   2407      call the generic function.  Any adjustment will be done at final
   2408      link time.  */
   2409   if (output_bfd != NULL)
   2410     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2411 				  input_section, output_bfd, error_message);
   2412 
   2413   /* Subtract the symbol section base address.  */
   2414   reloc_entry->addend -= symbol->section->output_section->vma;
   2415   return bfd_reloc_continue;
   2416 }
   2417 
   2418 static bfd_reloc_status_type
   2419 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2420 			    void *data, asection *input_section,
   2421 			    bfd *output_bfd, char **error_message)
   2422 {
   2423   /* If this is a relocatable link (output_bfd test tells us), just
   2424      call the generic function.  Any adjustment will be done at final
   2425      link time.  */
   2426   if (output_bfd != NULL)
   2427     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2428 				  input_section, output_bfd, error_message);
   2429 
   2430   /* Subtract the symbol section base address.  */
   2431   reloc_entry->addend -= symbol->section->output_section->vma;
   2432 
   2433   /* Adjust the addend for sign extension of the low 16 bits.  */
   2434   reloc_entry->addend += 0x8000;
   2435   return bfd_reloc_continue;
   2436 }
   2437 
   2438 static bfd_reloc_status_type
   2439 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2440 		     void *data, asection *input_section,
   2441 		     bfd *output_bfd, char **error_message)
   2442 {
   2443   bfd_vma TOCstart;
   2444 
   2445   /* If this is a relocatable link (output_bfd test tells us), just
   2446      call the generic function.  Any adjustment will be done at final
   2447      link time.  */
   2448   if (output_bfd != NULL)
   2449     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2450 				  input_section, output_bfd, error_message);
   2451 
   2452   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
   2453   if (TOCstart == 0)
   2454     TOCstart = ppc64_elf_toc (input_section->output_section->owner);
   2455 
   2456   /* Subtract the TOC base address.  */
   2457   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
   2458   return bfd_reloc_continue;
   2459 }
   2460 
   2461 static bfd_reloc_status_type
   2462 ppc64_elf_toc_ha_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 
   2482   /* Adjust the addend for sign extension of the low 16 bits.  */
   2483   reloc_entry->addend += 0x8000;
   2484   return bfd_reloc_continue;
   2485 }
   2486 
   2487 static bfd_reloc_status_type
   2488 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2489 		       void *data, asection *input_section,
   2490 		       bfd *output_bfd, char **error_message)
   2491 {
   2492   bfd_vma TOCstart;
   2493   bfd_size_type octets;
   2494 
   2495   /* If this is a relocatable link (output_bfd test tells us), just
   2496      call the generic function.  Any adjustment will be done at final
   2497      link time.  */
   2498   if (output_bfd != NULL)
   2499     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2500 				  input_section, output_bfd, error_message);
   2501 
   2502   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
   2503   if (TOCstart == 0)
   2504     TOCstart = ppc64_elf_toc (input_section->output_section->owner);
   2505 
   2506   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
   2507   bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
   2508   return bfd_reloc_ok;
   2509 }
   2510 
   2511 static bfd_reloc_status_type
   2512 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2513 			   void *data, asection *input_section,
   2514 			   bfd *output_bfd, char **error_message)
   2515 {
   2516   /* If this is a relocatable link (output_bfd test tells us), just
   2517      call the generic function.  Any adjustment will be done at final
   2518      link time.  */
   2519   if (output_bfd != NULL)
   2520     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2521 				  input_section, output_bfd, error_message);
   2522 
   2523   if (error_message != NULL)
   2524     {
   2525       static char buf[60];
   2526       sprintf (buf, "generic linker can't handle %s",
   2527 	       reloc_entry->howto->name);
   2528       *error_message = buf;
   2529     }
   2530   return bfd_reloc_dangerous;
   2531 }
   2532 
   2533 /* Track GOT entries needed for a given symbol.  We might need more
   2534    than one got entry per symbol.  */
   2535 struct got_entry
   2536 {
   2537   struct got_entry *next;
   2538 
   2539   /* The symbol addend that we'll be placing in the GOT.  */
   2540   bfd_vma addend;
   2541 
   2542   /* Unlike other ELF targets, we use separate GOT entries for the same
   2543      symbol referenced from different input files.  This is to support
   2544      automatic multiple TOC/GOT sections, where the TOC base can vary
   2545      from one input file to another.  After partitioning into TOC groups
   2546      we merge entries within the group.
   2547 
   2548      Point to the BFD owning this GOT entry.  */
   2549   bfd *owner;
   2550 
   2551   /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
   2552      TLS_TPREL or TLS_DTPREL for tls entries.  */
   2553   unsigned char tls_type;
   2554 
   2555   /* Non-zero if got.ent points to real entry.  */
   2556   unsigned char is_indirect;
   2557 
   2558   /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
   2559   union
   2560     {
   2561       bfd_signed_vma refcount;
   2562       bfd_vma offset;
   2563       struct got_entry *ent;
   2564     } got;
   2565 };
   2566 
   2567 /* The same for PLT.  */
   2568 struct plt_entry
   2569 {
   2570   struct plt_entry *next;
   2571 
   2572   bfd_vma addend;
   2573 
   2574   union
   2575     {
   2576       bfd_signed_vma refcount;
   2577       bfd_vma offset;
   2578     } plt;
   2579 };
   2580 
   2581 struct ppc64_elf_obj_tdata
   2582 {
   2583   struct elf_obj_tdata elf;
   2584 
   2585   /* Shortcuts to dynamic linker sections.  */
   2586   asection *got;
   2587   asection *relgot;
   2588 
   2589   /* Used during garbage collection.  We attach global symbols defined
   2590      on removed .opd entries to this section so that the sym is removed.  */
   2591   asection *deleted_section;
   2592 
   2593   /* TLS local dynamic got entry handling.  Support for multiple GOT
   2594      sections means we potentially need one of these for each input bfd.  */
   2595   struct got_entry tlsld_got;
   2596 
   2597   /* A copy of relocs before they are modified for --emit-relocs.  */
   2598   Elf_Internal_Rela *opd_relocs;
   2599 
   2600   /* Nonzero if this bfd has small toc/got relocs, ie. that expect
   2601      the reloc to be in the range -32768 to 32767.  */
   2602   unsigned int has_small_toc_reloc;
   2603 };
   2604 
   2605 #define ppc64_elf_tdata(bfd) \
   2606   ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
   2607 
   2608 #define ppc64_tlsld_got(bfd) \
   2609   (&ppc64_elf_tdata (bfd)->tlsld_got)
   2610 
   2611 #define is_ppc64_elf(bfd) \
   2612   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
   2613    && elf_object_id (bfd) == PPC64_ELF_DATA)
   2614 
   2615 /* Override the generic function because we store some extras.  */
   2616 
   2617 static bfd_boolean
   2618 ppc64_elf_mkobject (bfd *abfd)
   2619 {
   2620   return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
   2621 				  PPC64_ELF_DATA);
   2622 }
   2623 
   2624 /* Fix bad default arch selected for a 64 bit input bfd when the
   2625    default is 32 bit.  */
   2626 
   2627 static bfd_boolean
   2628 ppc64_elf_object_p (bfd *abfd)
   2629 {
   2630   if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
   2631     {
   2632       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
   2633 
   2634       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
   2635 	{
   2636 	  /* Relies on arch after 32 bit default being 64 bit default.  */
   2637 	  abfd->arch_info = abfd->arch_info->next;
   2638 	  BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
   2639 	}
   2640     }
   2641   return TRUE;
   2642 }
   2643 
   2644 /* Support for core dump NOTE sections.  */
   2645 
   2646 static bfd_boolean
   2647 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
   2648 {
   2649   size_t offset, size;
   2650 
   2651   if (note->descsz != 504)
   2652     return FALSE;
   2653 
   2654   /* pr_cursig */
   2655   elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
   2656 
   2657   /* pr_pid */
   2658   elf_tdata (abfd)->core_lwpid = bfd_get_32 (abfd, note->descdata + 32);
   2659 
   2660   /* pr_reg */
   2661   offset = 112;
   2662   size = 384;
   2663 
   2664   /* Make a ".reg/999" section.  */
   2665   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
   2666 					  size, note->descpos + offset);
   2667 }
   2668 
   2669 static bfd_boolean
   2670 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
   2671 {
   2672   if (note->descsz != 136)
   2673     return FALSE;
   2674 
   2675   elf_tdata (abfd)->core_program
   2676     = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
   2677   elf_tdata (abfd)->core_command
   2678     = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
   2679 
   2680   return TRUE;
   2681 }
   2682 
   2683 static char *
   2684 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
   2685 			   ...)
   2686 {
   2687   switch (note_type)
   2688     {
   2689     default:
   2690       return NULL;
   2691 
   2692     case NT_PRPSINFO:
   2693       {
   2694 	char data[136];
   2695 	va_list ap;
   2696 
   2697 	va_start (ap, note_type);
   2698 	memset (data, 0, 40);
   2699 	strncpy (data + 40, va_arg (ap, const char *), 16);
   2700 	strncpy (data + 56, va_arg (ap, const char *), 80);
   2701 	va_end (ap);
   2702 	return elfcore_write_note (abfd, buf, bufsiz,
   2703 				   "CORE", note_type, data, sizeof (data));
   2704       }
   2705 
   2706     case NT_PRSTATUS:
   2707       {
   2708 	char data[504];
   2709 	va_list ap;
   2710 	long pid;
   2711 	int cursig;
   2712 	const void *greg;
   2713 
   2714 	va_start (ap, note_type);
   2715 	memset (data, 0, 112);
   2716 	pid = va_arg (ap, long);
   2717 	bfd_put_32 (abfd, pid, data + 32);
   2718 	cursig = va_arg (ap, int);
   2719 	bfd_put_16 (abfd, cursig, data + 12);
   2720 	greg = va_arg (ap, const void *);
   2721 	memcpy (data + 112, greg, 384);
   2722 	memset (data + 496, 0, 8);
   2723 	va_end (ap);
   2724 	return elfcore_write_note (abfd, buf, bufsiz,
   2725 				   "CORE", note_type, data, sizeof (data));
   2726       }
   2727     }
   2728 }
   2729 
   2730 /* Merge backend specific data from an object file to the output
   2731    object file when linking.  */
   2732 
   2733 static bfd_boolean
   2734 ppc64_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
   2735 {
   2736   /* Check if we have the same endianess.  */
   2737   if (ibfd->xvec->byteorder != obfd->xvec->byteorder
   2738       && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
   2739       && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
   2740     {
   2741       const char *msg;
   2742 
   2743       if (bfd_big_endian (ibfd))
   2744 	msg = _("%B: compiled for a big endian system "
   2745 		"and target is little endian");
   2746       else
   2747 	msg = _("%B: compiled for a little endian system "
   2748 		"and target is big endian");
   2749 
   2750       (*_bfd_error_handler) (msg, ibfd);
   2751 
   2752       bfd_set_error (bfd_error_wrong_format);
   2753       return FALSE;
   2754     }
   2755 
   2756   return TRUE;
   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 		  if ((sec->flags & SEC_ALLOC) == 0
   3324 		      || (sec->flags & SEC_LOAD) == 0)
   3325 		    break;
   3326 		  if ((sec->flags & SEC_CODE) != 0)
   3327 		    s->section = sec;
   3328 		}
   3329 	      s->flags |= BSF_SYNTHETIC;
   3330 	      s->value = ent - s->section->vma;
   3331 	      s->name = names;
   3332 	      *names++ = '.';
   3333 	      len = strlen (syms[i]->name);
   3334 	      memcpy (names, syms[i]->name, len + 1);
   3335 	      names += len + 1;
   3336 	      /* Have udata.p point back to the original symbol this
   3337 		 synthetic symbol was derived from.  */
   3338 	      s->udata.p = syms[i];
   3339 	      s++;
   3340 	    }
   3341 	}
   3342       free (contents);
   3343 
   3344       if (glink != NULL && relplt != NULL)
   3345 	{
   3346 	  if (resolv_vma)
   3347 	    {
   3348 	      /* Add a symbol for the main glink trampoline.  */
   3349 	      memset (s, 0, sizeof *s);
   3350 	      s->the_bfd = abfd;
   3351 	      s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
   3352 	      s->section = glink;
   3353 	      s->value = resolv_vma - glink->vma;
   3354 	      s->name = names;
   3355 	      memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
   3356 	      names += sizeof ("__glink_PLTresolve");
   3357 	      s++;
   3358 	      count++;
   3359 	    }
   3360 
   3361 	  /* FIXME: It would be very much nicer to put sym@plt on the
   3362 	     stub rather than on the glink branch table entry.  The
   3363 	     objdump disassembler would then use a sensible symbol
   3364 	     name on plt calls.  The difficulty in doing so is
   3365 	     a) finding the stubs, and,
   3366 	     b) matching stubs against plt entries, and,
   3367 	     c) there can be multiple stubs for a given plt entry.
   3368 
   3369 	     Solving (a) could be done by code scanning, but older
   3370 	     ppc64 binaries used different stubs to current code.
   3371 	     (b) is the tricky one since you need to known the toc
   3372 	     pointer for at least one function that uses a pic stub to
   3373 	     be able to calculate the plt address referenced.
   3374 	     (c) means gdb would need to set multiple breakpoints (or
   3375 	     find the glink branch itself) when setting breakpoints
   3376 	     for pending shared library loads.  */
   3377 	  p = relplt->relocation;
   3378 	  for (i = 0; i < plt_count; i++, p++)
   3379 	    {
   3380 	      size_t len;
   3381 
   3382 	      *s = **p->sym_ptr_ptr;
   3383 	      /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
   3384 		 we are defining a symbol, ensure one of them is set.  */
   3385 	      if ((s->flags & BSF_LOCAL) == 0)
   3386 		s->flags |= BSF_GLOBAL;
   3387 	      s->flags |= BSF_SYNTHETIC;
   3388 	      s->section = glink;
   3389 	      s->value = glink_vma - glink->vma;
   3390 	      s->name = names;
   3391 	      s->udata.p = NULL;
   3392 	      len = strlen ((*p->sym_ptr_ptr)->name);
   3393 	      memcpy (names, (*p->sym_ptr_ptr)->name, len);
   3394 	      names += len;
   3395 	      if (p->addend != 0)
   3396 		{
   3397 		  memcpy (names, "+0x", sizeof ("+0x") - 1);
   3398 		  names += sizeof ("+0x") - 1;
   3399 		  bfd_sprintf_vma (abfd, names, p->addend);
   3400 		  names += strlen (names);
   3401 		}
   3402 	      memcpy (names, "@plt", sizeof ("@plt"));
   3403 	      names += sizeof ("@plt");
   3404 	      s++;
   3405 	      glink_vma += 8;
   3406 	      if (i >= 0x8000)
   3407 		glink_vma += 4;
   3408 	    }
   3409 	  count += plt_count;
   3410 	}
   3411     }
   3412 
   3413  done:
   3414   free (syms);
   3415   return count;
   3416 }
   3417 
   3418 /* The following functions are specific to the ELF linker, while
   3420    functions above are used generally.  Those named ppc64_elf_* are
   3421    called by the main ELF linker code.  They appear in this file more
   3422    or less in the order in which they are called.  eg.
   3423    ppc64_elf_check_relocs is called early in the link process,
   3424    ppc64_elf_finish_dynamic_sections is one of the last functions
   3425    called.
   3426 
   3427    PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
   3428    functions have both a function code symbol and a function descriptor
   3429    symbol.  A call to foo in a relocatable object file looks like:
   3430 
   3431    .		.text
   3432    .	x:
   3433    .		bl	.foo
   3434    .		nop
   3435 
   3436    The function definition in another object file might be:
   3437 
   3438    .		.section .opd
   3439    .	foo:	.quad	.foo
   3440    .		.quad	.TOC.@tocbase
   3441    .		.quad	0
   3442    .
   3443    .		.text
   3444    .	.foo:	blr
   3445 
   3446    When the linker resolves the call during a static link, the branch
   3447    unsurprisingly just goes to .foo and the .opd information is unused.
   3448    If the function definition is in a shared library, things are a little
   3449    different:  The call goes via a plt call stub, the opd information gets
   3450    copied to the plt, and the linker patches the nop.
   3451 
   3452    .	x:
   3453    .		bl	.foo_stub
   3454    .		ld	2,40(1)
   3455    .
   3456    .
   3457    .	.foo_stub:
   3458    .		addis	12,2,Lfoo@toc@ha	# in practice, the call stub
   3459    .		addi	12,12,Lfoo@toc@l	# is slightly optimized, but
   3460    .		std	2,40(1)			# this is the general idea
   3461    .		ld	11,0(12)
   3462    .		ld	2,8(12)
   3463    .		mtctr	11
   3464    .		ld	11,16(12)
   3465    .		bctr
   3466    .
   3467    .		.section .plt
   3468    .	Lfoo:	reloc (R_PPC64_JMP_SLOT, foo)
   3469 
   3470    The "reloc ()" notation is supposed to indicate that the linker emits
   3471    an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
   3472    copying.
   3473 
   3474    What are the difficulties here?  Well, firstly, the relocations
   3475    examined by the linker in check_relocs are against the function code
   3476    sym .foo, while the dynamic relocation in the plt is emitted against
   3477    the function descriptor symbol, foo.  Somewhere along the line, we need
   3478    to carefully copy dynamic link information from one symbol to the other.
   3479    Secondly, the generic part of the elf linker will make .foo a dynamic
   3480    symbol as is normal for most other backends.  We need foo dynamic
   3481    instead, at least for an application final link.  However, when
   3482    creating a shared library containing foo, we need to have both symbols
   3483    dynamic so that references to .foo are satisfied during the early
   3484    stages of linking.  Otherwise the linker might decide to pull in a
   3485    definition from some other object, eg. a static library.
   3486 
   3487    Update: As of August 2004, we support a new convention.  Function
   3488    calls may use the function descriptor symbol, ie. "bl foo".  This
   3489    behaves exactly as "bl .foo".  */
   3490 
   3491 /* The linker needs to keep track of the number of relocs that it
   3492    decides to copy as dynamic relocs in check_relocs for each symbol.
   3493    This is so that it can later discard them if they are found to be
   3494    unnecessary.  We store the information in a field extending the
   3495    regular ELF linker hash table.  */
   3496 
   3497 struct ppc_dyn_relocs
   3498 {
   3499   struct ppc_dyn_relocs *next;
   3500 
   3501   /* The input section of the reloc.  */
   3502   asection *sec;
   3503 
   3504   /* Total number of relocs copied for the input section.  */
   3505   bfd_size_type count;
   3506 
   3507   /* Number of pc-relative relocs copied for the input section.  */
   3508   bfd_size_type pc_count;
   3509 };
   3510 
   3511 /* Of those relocs that might be copied as dynamic relocs, this function
   3512    selects those that must be copied when linking a shared library,
   3513    even when the symbol is local.  */
   3514 
   3515 static int
   3516 must_be_dyn_reloc (struct bfd_link_info *info,
   3517 		   enum elf_ppc64_reloc_type r_type)
   3518 {
   3519   switch (r_type)
   3520     {
   3521     default:
   3522       return 1;
   3523 
   3524     case R_PPC64_REL32:
   3525     case R_PPC64_REL64:
   3526     case R_PPC64_REL30:
   3527       return 0;
   3528 
   3529     case R_PPC64_TPREL16:
   3530     case R_PPC64_TPREL16_LO:
   3531     case R_PPC64_TPREL16_HI:
   3532     case R_PPC64_TPREL16_HA:
   3533     case R_PPC64_TPREL16_DS:
   3534     case R_PPC64_TPREL16_LO_DS:
   3535     case R_PPC64_TPREL16_HIGHER:
   3536     case R_PPC64_TPREL16_HIGHERA:
   3537     case R_PPC64_TPREL16_HIGHEST:
   3538     case R_PPC64_TPREL16_HIGHESTA:
   3539     case R_PPC64_TPREL64:
   3540       return !info->executable;
   3541     }
   3542 }
   3543 
   3544 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
   3545    copying dynamic variables from a shared lib into an app's dynbss
   3546    section, and instead use a dynamic relocation to point into the
   3547    shared lib.  With code that gcc generates, it's vital that this be
   3548    enabled;  In the PowerPC64 ABI, the address of a function is actually
   3549    the address of a function descriptor, which resides in the .opd
   3550    section.  gcc uses the descriptor directly rather than going via the
   3551    GOT as some other ABI's do, which means that initialized function
   3552    pointers must reference the descriptor.  Thus, a function pointer
   3553    initialized to the address of a function in a shared library will
   3554    either require a copy reloc, or a dynamic reloc.  Using a copy reloc
   3555    redefines the function descriptor symbol to point to the copy.  This
   3556    presents a problem as a plt entry for that function is also
   3557    initialized from the function descriptor symbol and the copy reloc
   3558    may not be initialized first.  */
   3559 #define ELIMINATE_COPY_RELOCS 1
   3560 
   3561 /* Section name for stubs is the associated section name plus this
   3562    string.  */
   3563 #define STUB_SUFFIX ".stub"
   3564 
   3565 /* Linker stubs.
   3566    ppc_stub_long_branch:
   3567    Used when a 14 bit branch (or even a 24 bit branch) can't reach its
   3568    destination, but a 24 bit branch in a stub section will reach.
   3569    .	b	dest
   3570 
   3571    ppc_stub_plt_branch:
   3572    Similar to the above, but a 24 bit branch in the stub section won't
   3573    reach its destination.
   3574    .	addis	%r12,%r2,xxx@toc@ha
   3575    .	ld	%r11,xxx@toc@l(%r12)
   3576    .	mtctr	%r11
   3577    .	bctr
   3578 
   3579    ppc_stub_plt_call:
   3580    Used to call a function in a shared library.  If it so happens that
   3581    the plt entry referenced crosses a 64k boundary, then an extra
   3582    "addi %r12,%r12,xxx@toc@l" will be inserted before the "mtctr".
   3583    .	addis	%r12,%r2,xxx@toc@ha
   3584    .	std	%r2,40(%r1)
   3585    .	ld	%r11,xxx+0@toc@l(%r12)
   3586    .	mtctr	%r11
   3587    .	ld	%r2,xxx+8@toc@l(%r12)
   3588    .	ld	%r11,xxx+16@toc@l(%r12)
   3589    .	bctr
   3590 
   3591    ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
   3592    code to adjust the value and save r2 to support multiple toc sections.
   3593    A ppc_stub_long_branch with an r2 offset looks like:
   3594    .	std	%r2,40(%r1)
   3595    .	addis	%r2,%r2,off@ha
   3596    .	addi	%r2,%r2,off@l
   3597    .	b	dest
   3598 
   3599    A ppc_stub_plt_branch with an r2 offset looks like:
   3600    .	std	%r2,40(%r1)
   3601    .	addis	%r12,%r2,xxx@toc@ha
   3602    .	ld	%r11,xxx@toc@l(%r12)
   3603    .	addis	%r2,%r2,off@ha
   3604    .	addi	%r2,%r2,off@l
   3605    .	mtctr	%r11
   3606    .	bctr
   3607 
   3608    In cases where the "addis" instruction would add zero, the "addis" is
   3609    omitted and following instructions modified slightly in some cases.
   3610 */
   3611 
   3612 enum ppc_stub_type {
   3613   ppc_stub_none,
   3614   ppc_stub_long_branch,
   3615   ppc_stub_long_branch_r2off,
   3616   ppc_stub_plt_branch,
   3617   ppc_stub_plt_branch_r2off,
   3618   ppc_stub_plt_call
   3619 };
   3620 
   3621 struct ppc_stub_hash_entry {
   3622 
   3623   /* Base hash table entry structure.  */
   3624   struct bfd_hash_entry root;
   3625 
   3626   enum ppc_stub_type stub_type;
   3627 
   3628   /* The stub section.  */
   3629   asection *stub_sec;
   3630 
   3631   /* Offset within stub_sec of the beginning of this stub.  */
   3632   bfd_vma stub_offset;
   3633 
   3634   /* Given the symbol's value and its section we can determine its final
   3635      value when building the stubs (so the stub knows where to jump.  */
   3636   bfd_vma target_value;
   3637   asection *target_section;
   3638 
   3639   /* The symbol table entry, if any, that this was derived from.  */
   3640   struct ppc_link_hash_entry *h;
   3641   struct plt_entry *plt_ent;
   3642 
   3643   /* And the reloc addend that this was derived from.  */
   3644   bfd_vma addend;
   3645 
   3646   /* Where this stub is being called from, or, in the case of combined
   3647      stub sections, the first input section in the group.  */
   3648   asection *id_sec;
   3649 };
   3650 
   3651 struct ppc_branch_hash_entry {
   3652 
   3653   /* Base hash table entry structure.  */
   3654   struct bfd_hash_entry root;
   3655 
   3656   /* Offset within branch lookup table.  */
   3657   unsigned int offset;
   3658 
   3659   /* Generation marker.  */
   3660   unsigned int iter;
   3661 };
   3662 
   3663 struct ppc_link_hash_entry
   3664 {
   3665   struct elf_link_hash_entry elf;
   3666 
   3667   union {
   3668     /* A pointer to the most recently used stub hash entry against this
   3669        symbol.  */
   3670     struct ppc_stub_hash_entry *stub_cache;
   3671 
   3672     /* A pointer to the next symbol starting with a '.'  */
   3673     struct ppc_link_hash_entry *next_dot_sym;
   3674   } u;
   3675 
   3676   /* Track dynamic relocs copied for this symbol.  */
   3677   struct ppc_dyn_relocs *dyn_relocs;
   3678 
   3679   /* Link between function code and descriptor symbols.  */
   3680   struct ppc_link_hash_entry *oh;
   3681 
   3682   /* Flag function code and descriptor symbols.  */
   3683   unsigned int is_func:1;
   3684   unsigned int is_func_descriptor:1;
   3685   unsigned int fake:1;
   3686 
   3687   /* Whether global opd/toc sym has been adjusted or not.
   3688      After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
   3689      should be set for all globals defined in any opd/toc section.  */
   3690   unsigned int adjust_done:1;
   3691 
   3692   /* Set if we twiddled this symbol to weak at some stage.  */
   3693   unsigned int was_undefined:1;
   3694 
   3695   /* Contexts in which symbol is used in the GOT (or TOC).
   3696      TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
   3697      corresponding relocs are encountered during check_relocs.
   3698      tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
   3699      indicate the corresponding GOT entry type is not needed.
   3700      tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
   3701      a TPREL one.  We use a separate flag rather than setting TPREL
   3702      just for convenience in distinguishing the two cases.  */
   3703 #define TLS_GD		 1	/* GD reloc. */
   3704 #define TLS_LD		 2	/* LD reloc. */
   3705 #define TLS_TPREL	 4	/* TPREL reloc, => IE. */
   3706 #define TLS_DTPREL	 8	/* DTPREL reloc, => LD. */
   3707 #define TLS_TLS		16	/* Any TLS reloc.  */
   3708 #define TLS_EXPLICIT	32	/* Marks TOC section TLS relocs. */
   3709 #define TLS_TPRELGD	64	/* TPREL reloc resulting from GD->IE. */
   3710 #define PLT_IFUNC      128	/* STT_GNU_IFUNC.  */
   3711   unsigned char tls_mask;
   3712 };
   3713 
   3714 /* ppc64 ELF linker hash table.  */
   3715 
   3716 struct ppc_link_hash_table
   3717 {
   3718   struct elf_link_hash_table elf;
   3719 
   3720   /* The stub hash table.  */
   3721   struct bfd_hash_table stub_hash_table;
   3722 
   3723   /* Another hash table for plt_branch stubs.  */
   3724   struct bfd_hash_table branch_hash_table;
   3725 
   3726   /* Linker stub bfd.  */
   3727   bfd *stub_bfd;
   3728 
   3729   /* Linker call-backs.  */
   3730   asection * (*add_stub_section) (const char *, asection *);
   3731   void (*layout_sections_again) (void);
   3732 
   3733   /* Array to keep track of which stub sections have been created, and
   3734      information on stub grouping.  */
   3735   struct map_stub {
   3736     /* This is the section to which stubs in the group will be attached.  */
   3737     asection *link_sec;
   3738     /* The stub section.  */
   3739     asection *stub_sec;
   3740     /* Along with elf_gp, specifies the TOC pointer used in this group.  */
   3741     bfd_vma toc_off;
   3742   } *stub_group;
   3743 
   3744   /* Temp used when calculating TOC pointers.  */
   3745   bfd_vma toc_curr;
   3746   bfd *toc_bfd;
   3747   asection *toc_first_sec;
   3748 
   3749   /* Highest input section id.  */
   3750   int top_id;
   3751 
   3752   /* Highest output section index.  */
   3753   int top_index;
   3754 
   3755   /* Used when adding symbols.  */
   3756   struct ppc_link_hash_entry *dot_syms;
   3757 
   3758   /* List of input sections for each output section.  */
   3759   asection **input_list;
   3760 
   3761   /* Short-cuts to get to dynamic linker sections.  */
   3762   asection *got;
   3763   asection *plt;
   3764   asection *relplt;
   3765   asection *iplt;
   3766   asection *reliplt;
   3767   asection *dynbss;
   3768   asection *relbss;
   3769   asection *glink;
   3770   asection *sfpr;
   3771   asection *brlt;
   3772   asection *relbrlt;
   3773 
   3774   /* Shortcut to .__tls_get_addr and __tls_get_addr.  */
   3775   struct ppc_link_hash_entry *tls_get_addr;
   3776   struct ppc_link_hash_entry *tls_get_addr_fd;
   3777 
   3778   /* The size of reliplt used by got entry relocs.  */
   3779   bfd_size_type got_reli_size;
   3780 
   3781   /* Statistics.  */
   3782   unsigned long stub_count[ppc_stub_plt_call];
   3783 
   3784   /* Number of stubs against global syms.  */
   3785   unsigned long stub_globals;
   3786 
   3787   /* Set if we should emit symbols for stubs.  */
   3788   unsigned int emit_stub_syms:1;
   3789 
   3790   /* Set if __tls_get_addr optimization should not be done.  */
   3791   unsigned int no_tls_get_addr_opt:1;
   3792 
   3793   /* Support for multiple toc sections.  */
   3794   unsigned int do_multi_toc:1;
   3795   unsigned int multi_toc_needed:1;
   3796   unsigned int second_toc_pass:1;
   3797   unsigned int do_toc_opt:1;
   3798 
   3799   /* Set on error.  */
   3800   unsigned int stub_error:1;
   3801 
   3802   /* Temp used by ppc64_elf_process_dot_syms.  */
   3803   unsigned int twiddled_syms:1;
   3804 
   3805   /* Incremented every time we size stubs.  */
   3806   unsigned int stub_iteration;
   3807 
   3808   /* Small local sym cache.  */
   3809   struct sym_cache sym_cache;
   3810 };
   3811 
   3812 /* Rename some of the generic section flags to better document how they
   3813    are used here.  */
   3814 
   3815 /* Nonzero if this section has TLS related relocations.  */
   3816 #define has_tls_reloc sec_flg0
   3817 
   3818 /* Nonzero if this section has a call to __tls_get_addr.  */
   3819 #define has_tls_get_addr_call sec_flg1
   3820 
   3821 /* Nonzero if this section has any toc or got relocs.  */
   3822 #define has_toc_reloc sec_flg2
   3823 
   3824 /* Nonzero if this section has a call to another section that uses
   3825    the toc or got.  */
   3826 #define makes_toc_func_call sec_flg3
   3827 
   3828 /* Recursion protection when determining above flag.  */
   3829 #define call_check_in_progress sec_flg4
   3830 #define call_check_done sec_flg5
   3831 
   3832 /* Get the ppc64 ELF linker hash table from a link_info structure.  */
   3833 
   3834 #define ppc_hash_table(p) \
   3835   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
   3836   == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
   3837 
   3838 #define ppc_stub_hash_lookup(table, string, create, copy) \
   3839   ((struct ppc_stub_hash_entry *) \
   3840    bfd_hash_lookup ((table), (string), (create), (copy)))
   3841 
   3842 #define ppc_branch_hash_lookup(table, string, create, copy) \
   3843   ((struct ppc_branch_hash_entry *) \
   3844    bfd_hash_lookup ((table), (string), (create), (copy)))
   3845 
   3846 /* Create an entry in the stub hash table.  */
   3847 
   3848 static struct bfd_hash_entry *
   3849 stub_hash_newfunc (struct bfd_hash_entry *entry,
   3850 		   struct bfd_hash_table *table,
   3851 		   const char *string)
   3852 {
   3853   /* Allocate the structure if it has not already been allocated by a
   3854      subclass.  */
   3855   if (entry == NULL)
   3856     {
   3857       entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
   3858       if (entry == NULL)
   3859 	return entry;
   3860     }
   3861 
   3862   /* Call the allocation method of the superclass.  */
   3863   entry = bfd_hash_newfunc (entry, table, string);
   3864   if (entry != NULL)
   3865     {
   3866       struct ppc_stub_hash_entry *eh;
   3867 
   3868       /* Initialize the local fields.  */
   3869       eh = (struct ppc_stub_hash_entry *) entry;
   3870       eh->stub_type = ppc_stub_none;
   3871       eh->stub_sec = NULL;
   3872       eh->stub_offset = 0;
   3873       eh->target_value = 0;
   3874       eh->target_section = NULL;
   3875       eh->h = NULL;
   3876       eh->id_sec = NULL;
   3877     }
   3878 
   3879   return entry;
   3880 }
   3881 
   3882 /* Create an entry in the branch hash table.  */
   3883 
   3884 static struct bfd_hash_entry *
   3885 branch_hash_newfunc (struct bfd_hash_entry *entry,
   3886 		     struct bfd_hash_table *table,
   3887 		     const char *string)
   3888 {
   3889   /* Allocate the structure if it has not already been allocated by a
   3890      subclass.  */
   3891   if (entry == NULL)
   3892     {
   3893       entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
   3894       if (entry == NULL)
   3895 	return entry;
   3896     }
   3897 
   3898   /* Call the allocation method of the superclass.  */
   3899   entry = bfd_hash_newfunc (entry, table, string);
   3900   if (entry != NULL)
   3901     {
   3902       struct ppc_branch_hash_entry *eh;
   3903 
   3904       /* Initialize the local fields.  */
   3905       eh = (struct ppc_branch_hash_entry *) entry;
   3906       eh->offset = 0;
   3907       eh->iter = 0;
   3908     }
   3909 
   3910   return entry;
   3911 }
   3912 
   3913 /* Create an entry in a ppc64 ELF linker hash table.  */
   3914 
   3915 static struct bfd_hash_entry *
   3916 link_hash_newfunc (struct bfd_hash_entry *entry,
   3917 		   struct bfd_hash_table *table,
   3918 		   const char *string)
   3919 {
   3920   /* Allocate the structure if it has not already been allocated by a
   3921      subclass.  */
   3922   if (entry == NULL)
   3923     {
   3924       entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
   3925       if (entry == NULL)
   3926 	return entry;
   3927     }
   3928 
   3929   /* Call the allocation method of the superclass.  */
   3930   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
   3931   if (entry != NULL)
   3932     {
   3933       struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
   3934 
   3935       memset (&eh->u.stub_cache, 0,
   3936 	      (sizeof (struct ppc_link_hash_entry)
   3937 	       - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
   3938 
   3939       /* When making function calls, old ABI code references function entry
   3940 	 points (dot symbols), while new ABI code references the function
   3941 	 descriptor symbol.  We need to make any combination of reference and
   3942 	 definition work together, without breaking archive linking.
   3943 
   3944 	 For a defined function "foo" and an undefined call to "bar":
   3945 	 An old object defines "foo" and ".foo", references ".bar" (possibly
   3946 	 "bar" too).
   3947 	 A new object defines "foo" and references "bar".
   3948 
   3949 	 A new object thus has no problem with its undefined symbols being
   3950 	 satisfied by definitions in an old object.  On the other hand, the
   3951 	 old object won't have ".bar" satisfied by a new object.
   3952 
   3953 	 Keep a list of newly added dot-symbols.  */
   3954 
   3955       if (string[0] == '.')
   3956 	{
   3957 	  struct ppc_link_hash_table *htab;
   3958 
   3959 	  htab = (struct ppc_link_hash_table *) table;
   3960 	  eh->u.next_dot_sym = htab->dot_syms;
   3961 	  htab->dot_syms = eh;
   3962 	}
   3963     }
   3964 
   3965   return entry;
   3966 }
   3967 
   3968 /* Create a ppc64 ELF linker hash table.  */
   3969 
   3970 static struct bfd_link_hash_table *
   3971 ppc64_elf_link_hash_table_create (bfd *abfd)
   3972 {
   3973   struct ppc_link_hash_table *htab;
   3974   bfd_size_type amt = sizeof (struct ppc_link_hash_table);
   3975 
   3976   htab = bfd_zmalloc (amt);
   3977   if (htab == NULL)
   3978     return NULL;
   3979 
   3980   if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
   3981 				      sizeof (struct ppc_link_hash_entry),
   3982 				      PPC64_ELF_DATA))
   3983     {
   3984       free (htab);
   3985       return NULL;
   3986     }
   3987 
   3988   /* Init the stub hash table too.  */
   3989   if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
   3990 			    sizeof (struct ppc_stub_hash_entry)))
   3991     return NULL;
   3992 
   3993   /* And the branch hash table.  */
   3994   if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
   3995 			    sizeof (struct ppc_branch_hash_entry)))
   3996     return NULL;
   3997 
   3998   /* Initializing two fields of the union is just cosmetic.  We really
   3999      only care about glist, but when compiled on a 32-bit host the
   4000      bfd_vma fields are larger.  Setting the bfd_vma to zero makes
   4001      debugger inspection of these fields look nicer.  */
   4002   htab->elf.init_got_refcount.refcount = 0;
   4003   htab->elf.init_got_refcount.glist = NULL;
   4004   htab->elf.init_plt_refcount.refcount = 0;
   4005   htab->elf.init_plt_refcount.glist = NULL;
   4006   htab->elf.init_got_offset.offset = 0;
   4007   htab->elf.init_got_offset.glist = NULL;
   4008   htab->elf.init_plt_offset.offset = 0;
   4009   htab->elf.init_plt_offset.glist = NULL;
   4010 
   4011   return &htab->elf.root;
   4012 }
   4013 
   4014 /* Free the derived linker hash table.  */
   4015 
   4016 static void
   4017 ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
   4018 {
   4019   struct ppc_link_hash_table *ret = (struct ppc_link_hash_table *) hash;
   4020 
   4021   bfd_hash_table_free (&ret->stub_hash_table);
   4022   bfd_hash_table_free (&ret->branch_hash_table);
   4023   _bfd_generic_link_hash_table_free (hash);
   4024 }
   4025 
   4026 /* Satisfy the ELF linker by filling in some fields in our fake bfd.  */
   4027 
   4028 void
   4029 ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
   4030 {
   4031   struct ppc_link_hash_table *htab;
   4032 
   4033   elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
   4034 
   4035 /* Always hook our dynamic sections into the first bfd, which is the
   4036    linker created stub bfd.  This ensures that the GOT header is at
   4037    the start of the output TOC section.  */
   4038   htab = ppc_hash_table (info);
   4039   if (htab == NULL)
   4040     return;
   4041   htab->stub_bfd = abfd;
   4042   htab->elf.dynobj = abfd;
   4043 }
   4044 
   4045 /* Build a name for an entry in the stub hash table.  */
   4046 
   4047 static char *
   4048 ppc_stub_name (const asection *input_section,
   4049 	       const asection *sym_sec,
   4050 	       const struct ppc_link_hash_entry *h,
   4051 	       const Elf_Internal_Rela *rel)
   4052 {
   4053   char *stub_name;
   4054   bfd_size_type len;
   4055 
   4056   /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
   4057      offsets from a sym as a branch target?  In fact, we could
   4058      probably assume the addend is always zero.  */
   4059   BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
   4060 
   4061   if (h)
   4062     {
   4063       len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
   4064       stub_name = bfd_malloc (len);
   4065       if (stub_name == NULL)
   4066 	return stub_name;
   4067 
   4068       sprintf (stub_name, "%08x.%s+%x",
   4069 	       input_section->id & 0xffffffff,
   4070 	       h->elf.root.root.string,
   4071 	       (int) rel->r_addend & 0xffffffff);
   4072     }
   4073   else
   4074     {
   4075       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
   4076       stub_name = bfd_malloc (len);
   4077       if (stub_name == NULL)
   4078 	return stub_name;
   4079 
   4080       sprintf (stub_name, "%08x.%x:%x+%x",
   4081 	       input_section->id & 0xffffffff,
   4082 	       sym_sec->id & 0xffffffff,
   4083 	       (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
   4084 	       (int) rel->r_addend & 0xffffffff);
   4085     }
   4086   if (stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
   4087     stub_name[len - 2] = 0;
   4088   return stub_name;
   4089 }
   4090 
   4091 /* Look up an entry in the stub hash.  Stub entries are cached because
   4092    creating the stub name takes a bit of time.  */
   4093 
   4094 static struct ppc_stub_hash_entry *
   4095 ppc_get_stub_entry (const asection *input_section,
   4096 		    const asection *sym_sec,
   4097 		    struct ppc_link_hash_entry *h,
   4098 		    const Elf_Internal_Rela *rel,
   4099 		    struct ppc_link_hash_table *htab)
   4100 {
   4101   struct ppc_stub_hash_entry *stub_entry;
   4102   const asection *id_sec;
   4103 
   4104   /* If this input section is part of a group of sections sharing one
   4105      stub section, then use the id of the first section in the group.
   4106      Stub names need to include a section id, as there may well be
   4107      more than one stub used to reach say, printf, and we need to
   4108      distinguish between them.  */
   4109   id_sec = htab->stub_group[input_section->id].link_sec;
   4110 
   4111   if (h != NULL && h->u.stub_cache != NULL
   4112       && h->u.stub_cache->h == h
   4113       && h->u.stub_cache->id_sec == id_sec)
   4114     {
   4115       stub_entry = h->u.stub_cache;
   4116     }
   4117   else
   4118     {
   4119       char *stub_name;
   4120 
   4121       stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
   4122       if (stub_name == NULL)
   4123 	return NULL;
   4124 
   4125       stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
   4126 					 stub_name, FALSE, FALSE);
   4127       if (h != NULL)
   4128 	h->u.stub_cache = stub_entry;
   4129 
   4130       free (stub_name);
   4131     }
   4132 
   4133   return stub_entry;
   4134 }
   4135 
   4136 /* Add a new stub entry to the stub hash.  Not all fields of the new
   4137    stub entry are initialised.  */
   4138 
   4139 static struct ppc_stub_hash_entry *
   4140 ppc_add_stub (const char *stub_name,
   4141 	      asection *section,
   4142 	      struct ppc_link_hash_table *htab)
   4143 {
   4144   asection *link_sec;
   4145   asection *stub_sec;
   4146   struct ppc_stub_hash_entry *stub_entry;
   4147 
   4148   link_sec = htab->stub_group[section->id].link_sec;
   4149   stub_sec = htab->stub_group[section->id].stub_sec;
   4150   if (stub_sec == NULL)
   4151     {
   4152       stub_sec = htab->stub_group[link_sec->id].stub_sec;
   4153       if (stub_sec == NULL)
   4154 	{
   4155 	  size_t namelen;
   4156 	  bfd_size_type len;
   4157 	  char *s_name;
   4158 
   4159 	  namelen = strlen (link_sec->name);
   4160 	  len = namelen + sizeof (STUB_SUFFIX);
   4161 	  s_name = bfd_alloc (htab->stub_bfd, len);
   4162 	  if (s_name == NULL)
   4163 	    return NULL;
   4164 
   4165 	  memcpy (s_name, link_sec->name, namelen);
   4166 	  memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
   4167 	  stub_sec = (*htab->add_stub_section) (s_name, link_sec);
   4168 	  if (stub_sec == NULL)
   4169 	    return NULL;
   4170 	  htab->stub_group[link_sec->id].stub_sec = stub_sec;
   4171 	}
   4172       htab->stub_group[section->id].stub_sec = stub_sec;
   4173     }
   4174 
   4175   /* Enter this entry into the linker stub hash table.  */
   4176   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
   4177 				     TRUE, FALSE);
   4178   if (stub_entry == NULL)
   4179     {
   4180       (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
   4181 			     section->owner, stub_name);
   4182       return NULL;
   4183     }
   4184 
   4185   stub_entry->stub_sec = stub_sec;
   4186   stub_entry->stub_offset = 0;
   4187   stub_entry->id_sec = link_sec;
   4188   return stub_entry;
   4189 }
   4190 
   4191 /* Create sections for linker generated code.  */
   4192 
   4193 static bfd_boolean
   4194 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
   4195 {
   4196   struct ppc_link_hash_table *htab;
   4197   flagword flags;
   4198 
   4199   htab = ppc_hash_table (info);
   4200   if (htab == NULL)
   4201     return FALSE;
   4202 
   4203   /* Create .sfpr for code to save and restore fp regs.  */
   4204   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
   4205 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   4206   htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
   4207 						   flags);
   4208   if (htab->sfpr == NULL
   4209       || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
   4210     return FALSE;
   4211 
   4212   /* Create .glink for lazy dynamic linking support.  */
   4213   htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
   4214 						    flags);
   4215   if (htab->glink == NULL
   4216       || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
   4217     return FALSE;
   4218 
   4219   flags = SEC_ALLOC | SEC_LINKER_CREATED;
   4220   htab->iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
   4221   if (htab->iplt == NULL
   4222       || ! bfd_set_section_alignment (dynobj, htab->iplt, 3))
   4223     return FALSE;
   4224 
   4225   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
   4226 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   4227   htab->reliplt = bfd_make_section_anyway_with_flags (dynobj,
   4228 						      ".rela.iplt",
   4229 						      flags);
   4230   if (htab->reliplt == NULL
   4231       || ! bfd_set_section_alignment (dynobj, htab->reliplt, 3))
   4232     return FALSE;
   4233 
   4234   /* Create branch lookup table for plt_branch stubs.  */
   4235   flags = (SEC_ALLOC | SEC_LOAD
   4236 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   4237   htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
   4238 						   flags);
   4239   if (htab->brlt == NULL
   4240       || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
   4241     return FALSE;
   4242 
   4243   if (!info->shared)
   4244     return TRUE;
   4245 
   4246   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
   4247 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   4248   htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
   4249 						      ".rela.branch_lt",
   4250 						      flags);
   4251   if (htab->relbrlt == NULL
   4252       || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
   4253     return FALSE;
   4254 
   4255   return TRUE;
   4256 }
   4257 
   4258 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
   4259    not already done.  */
   4260 
   4261 static bfd_boolean
   4262 create_got_section (bfd *abfd, struct bfd_link_info *info)
   4263 {
   4264   asection *got, *relgot;
   4265   flagword flags;
   4266   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   4267 
   4268   if (!is_ppc64_elf (abfd))
   4269     return FALSE;
   4270   if (htab == NULL)
   4271     return FALSE;
   4272 
   4273   if (!htab->got)
   4274     {
   4275       if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
   4276 	return FALSE;
   4277 
   4278       htab->got = bfd_get_section_by_name (htab->elf.dynobj, ".got");
   4279       if (!htab->got)
   4280 	abort ();
   4281     }
   4282 
   4283   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
   4284 	   | SEC_LINKER_CREATED);
   4285 
   4286   got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
   4287   if (!got
   4288       || !bfd_set_section_alignment (abfd, got, 3))
   4289     return FALSE;
   4290 
   4291   relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
   4292 					       flags | SEC_READONLY);
   4293   if (!relgot
   4294       || ! bfd_set_section_alignment (abfd, relgot, 3))
   4295     return FALSE;
   4296 
   4297   ppc64_elf_tdata (abfd)->got = got;
   4298   ppc64_elf_tdata (abfd)->relgot = relgot;
   4299   return TRUE;
   4300 }
   4301 
   4302 /* Create the dynamic sections, and set up shortcuts.  */
   4303 
   4304 static bfd_boolean
   4305 ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
   4306 {
   4307   struct ppc_link_hash_table *htab;
   4308 
   4309   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
   4310     return FALSE;
   4311 
   4312   htab = ppc_hash_table (info);
   4313   if (htab == NULL)
   4314     return FALSE;
   4315 
   4316   if (!htab->got)
   4317     htab->got = bfd_get_section_by_name (dynobj, ".got");
   4318   htab->plt = bfd_get_section_by_name (dynobj, ".plt");
   4319   htab->relplt = bfd_get_section_by_name (dynobj, ".rela.plt");
   4320   htab->dynbss = bfd_get_section_by_name (dynobj, ".dynbss");
   4321   if (!info->shared)
   4322     htab->relbss = bfd_get_section_by_name (dynobj, ".rela.bss");
   4323 
   4324   if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
   4325       || (!info->shared && !htab->relbss))
   4326     abort ();
   4327 
   4328   return TRUE;
   4329 }
   4330 
   4331 /* Follow indirect and warning symbol links.  */
   4332 
   4333 static inline struct bfd_link_hash_entry *
   4334 follow_link (struct bfd_link_hash_entry *h)
   4335 {
   4336   while (h->type == bfd_link_hash_indirect
   4337 	 || h->type == bfd_link_hash_warning)
   4338     h = h->u.i.link;
   4339   return h;
   4340 }
   4341 
   4342 static inline struct elf_link_hash_entry *
   4343 elf_follow_link (struct elf_link_hash_entry *h)
   4344 {
   4345   return (struct elf_link_hash_entry *) follow_link (&h->root);
   4346 }
   4347 
   4348 static inline struct ppc_link_hash_entry *
   4349 ppc_follow_link (struct ppc_link_hash_entry *h)
   4350 {
   4351   return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
   4352 }
   4353 
   4354 /* Merge PLT info on FROM with that on TO.  */
   4355 
   4356 static void
   4357 move_plt_plist (struct ppc_link_hash_entry *from,
   4358 		struct ppc_link_hash_entry *to)
   4359 {
   4360   if (from->elf.plt.plist != NULL)
   4361     {
   4362       if (to->elf.plt.plist != NULL)
   4363 	{
   4364 	  struct plt_entry **entp;
   4365 	  struct plt_entry *ent;
   4366 
   4367 	  for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
   4368 	    {
   4369 	      struct plt_entry *dent;
   4370 
   4371 	      for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
   4372 		if (dent->addend == ent->addend)
   4373 		  {
   4374 		    dent->plt.refcount += ent->plt.refcount;
   4375 		    *entp = ent->next;
   4376 		    break;
   4377 		  }
   4378 	      if (dent == NULL)
   4379 		entp = &ent->next;
   4380 	    }
   4381 	  *entp = to->elf.plt.plist;
   4382 	}
   4383 
   4384       to->elf.plt.plist = from->elf.plt.plist;
   4385       from->elf.plt.plist = NULL;
   4386     }
   4387 }
   4388 
   4389 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
   4390 
   4391 static void
   4392 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
   4393 				struct elf_link_hash_entry *dir,
   4394 				struct elf_link_hash_entry *ind)
   4395 {
   4396   struct ppc_link_hash_entry *edir, *eind;
   4397 
   4398   edir = (struct ppc_link_hash_entry *) dir;
   4399   eind = (struct ppc_link_hash_entry *) ind;
   4400 
   4401   /* Copy over any dynamic relocs we may have on the indirect sym.  */
   4402   if (eind->dyn_relocs != NULL)
   4403     {
   4404       if (edir->dyn_relocs != NULL)
   4405 	{
   4406 	  struct ppc_dyn_relocs **pp;
   4407 	  struct ppc_dyn_relocs *p;
   4408 
   4409 	  /* Add reloc counts against the indirect sym to the direct sym
   4410 	     list.  Merge any entries against the same section.  */
   4411 	  for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
   4412 	    {
   4413 	      struct ppc_dyn_relocs *q;
   4414 
   4415 	      for (q = edir->dyn_relocs; q != NULL; q = q->next)
   4416 		if (q->sec == p->sec)
   4417 		  {
   4418 		    q->pc_count += p->pc_count;
   4419 		    q->count += p->count;
   4420 		    *pp = p->next;
   4421 		    break;
   4422 		  }
   4423 	      if (q == NULL)
   4424 		pp = &p->next;
   4425 	    }
   4426 	  *pp = edir->dyn_relocs;
   4427 	}
   4428 
   4429       edir->dyn_relocs = eind->dyn_relocs;
   4430       eind->dyn_relocs = NULL;
   4431     }
   4432 
   4433   edir->is_func |= eind->is_func;
   4434   edir->is_func_descriptor |= eind->is_func_descriptor;
   4435   edir->tls_mask |= eind->tls_mask;
   4436   if (eind->oh != NULL)
   4437     edir->oh = ppc_follow_link (eind->oh);
   4438 
   4439   /* If called to transfer flags for a weakdef during processing
   4440      of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
   4441      We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
   4442   if (!(ELIMINATE_COPY_RELOCS
   4443 	&& eind->elf.root.type != bfd_link_hash_indirect
   4444 	&& edir->elf.dynamic_adjusted))
   4445     edir->elf.non_got_ref |= eind->elf.non_got_ref;
   4446 
   4447   edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
   4448   edir->elf.ref_regular |= eind->elf.ref_regular;
   4449   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
   4450   edir->elf.needs_plt |= eind->elf.needs_plt;
   4451 
   4452   /* If we were called to copy over info for a weak sym, that's all.  */
   4453   if (eind->elf.root.type != bfd_link_hash_indirect)
   4454     return;
   4455 
   4456   /* Copy over got entries that we may have already seen to the
   4457      symbol which just became indirect.  */
   4458   if (eind->elf.got.glist != NULL)
   4459     {
   4460       if (edir->elf.got.glist != NULL)
   4461 	{
   4462 	  struct got_entry **entp;
   4463 	  struct got_entry *ent;
   4464 
   4465 	  for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
   4466 	    {
   4467 	      struct got_entry *dent;
   4468 
   4469 	      for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
   4470 		if (dent->addend == ent->addend
   4471 		    && dent->owner == ent->owner
   4472 		    && dent->tls_type == ent->tls_type)
   4473 		  {
   4474 		    dent->got.refcount += ent->got.refcount;
   4475 		    *entp = ent->next;
   4476 		    break;
   4477 		  }
   4478 	      if (dent == NULL)
   4479 		entp = &ent->next;
   4480 	    }
   4481 	  *entp = edir->elf.got.glist;
   4482 	}
   4483 
   4484       edir->elf.got.glist = eind->elf.got.glist;
   4485       eind->elf.got.glist = NULL;
   4486     }
   4487 
   4488   /* And plt entries.  */
   4489   move_plt_plist (eind, edir);
   4490 
   4491   if (eind->elf.dynindx != -1)
   4492     {
   4493       if (edir->elf.dynindx != -1)
   4494 	_bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
   4495 				edir->elf.dynstr_index);
   4496       edir->elf.dynindx = eind->elf.dynindx;
   4497       edir->elf.dynstr_index = eind->elf.dynstr_index;
   4498       eind->elf.dynindx = -1;
   4499       eind->elf.dynstr_index = 0;
   4500     }
   4501 }
   4502 
   4503 /* Find the function descriptor hash entry from the given function code
   4504    hash entry FH.  Link the entries via their OH fields.  */
   4505 
   4506 static struct ppc_link_hash_entry *
   4507 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
   4508 {
   4509   struct ppc_link_hash_entry *fdh = fh->oh;
   4510 
   4511   if (fdh == NULL)
   4512     {
   4513       const char *fd_name = fh->elf.root.root.string + 1;
   4514 
   4515       fdh = (struct ppc_link_hash_entry *)
   4516 	elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
   4517       if (fdh == NULL)
   4518 	return fdh;
   4519 
   4520       fdh->is_func_descriptor = 1;
   4521       fdh->oh = fh;
   4522       fh->is_func = 1;
   4523       fh->oh = fdh;
   4524     }
   4525 
   4526   return ppc_follow_link (fdh);
   4527 }
   4528 
   4529 /* Make a fake function descriptor sym for the code sym FH.  */
   4530 
   4531 static struct ppc_link_hash_entry *
   4532 make_fdh (struct bfd_link_info *info,
   4533 	  struct ppc_link_hash_entry *fh)
   4534 {
   4535   bfd *abfd;
   4536   asymbol *newsym;
   4537   struct bfd_link_hash_entry *bh;
   4538   struct ppc_link_hash_entry *fdh;
   4539 
   4540   abfd = fh->elf.root.u.undef.abfd;
   4541   newsym = bfd_make_empty_symbol (abfd);
   4542   newsym->name = fh->elf.root.root.string + 1;
   4543   newsym->section = bfd_und_section_ptr;
   4544   newsym->value = 0;
   4545   newsym->flags = BSF_WEAK;
   4546 
   4547   bh = NULL;
   4548   if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
   4549 					 newsym->flags, newsym->section,
   4550 					 newsym->value, NULL, FALSE, FALSE,
   4551 					 &bh))
   4552     return NULL;
   4553 
   4554   fdh = (struct ppc_link_hash_entry *) bh;
   4555   fdh->elf.non_elf = 0;
   4556   fdh->fake = 1;
   4557   fdh->is_func_descriptor = 1;
   4558   fdh->oh = fh;
   4559   fh->is_func = 1;
   4560   fh->oh = fdh;
   4561   return fdh;
   4562 }
   4563 
   4564 /* Fix function descriptor symbols defined in .opd sections to be
   4565    function type.  */
   4566 
   4567 static bfd_boolean
   4568 ppc64_elf_add_symbol_hook (bfd *ibfd,
   4569 			   struct bfd_link_info *info,
   4570 			   Elf_Internal_Sym *isym,
   4571 			   const char **name ATTRIBUTE_UNUSED,
   4572 			   flagword *flags ATTRIBUTE_UNUSED,
   4573 			   asection **sec,
   4574 			   bfd_vma *value ATTRIBUTE_UNUSED)
   4575 {
   4576   if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
   4577     {
   4578       if ((ibfd->flags & DYNAMIC) == 0)
   4579 	elf_tdata (info->output_bfd)->has_ifunc_symbols = TRUE;
   4580     }
   4581   else if (ELF_ST_TYPE (isym->st_info) == STT_FUNC)
   4582     ;
   4583   else if (*sec != NULL
   4584 	   && strcmp ((*sec)->name, ".opd") == 0)
   4585     isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
   4586 
   4587   return TRUE;
   4588 }
   4589 
   4590 /* This function makes an old ABI object reference to ".bar" cause the
   4591    inclusion of a new ABI object archive that defines "bar".
   4592    NAME is a symbol defined in an archive.  Return a symbol in the hash
   4593    table that might be satisfied by the archive symbols.  */
   4594 
   4595 static struct elf_link_hash_entry *
   4596 ppc64_elf_archive_symbol_lookup (bfd *abfd,
   4597 				 struct bfd_link_info *info,
   4598 				 const char *name)
   4599 {
   4600   struct elf_link_hash_entry *h;
   4601   char *dot_name;
   4602   size_t len;
   4603 
   4604   h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
   4605   if (h != NULL
   4606       /* Don't return this sym if it is a fake function descriptor
   4607 	 created by add_symbol_adjust.  */
   4608       && !(h->root.type == bfd_link_hash_undefweak
   4609 	   && ((struct ppc_link_hash_entry *) h)->fake))
   4610     return h;
   4611 
   4612   if (name[0] == '.')
   4613     return h;
   4614 
   4615   len = strlen (name);
   4616   dot_name = bfd_alloc (abfd, len + 2);
   4617   if (dot_name == NULL)
   4618     return (struct elf_link_hash_entry *) 0 - 1;
   4619   dot_name[0] = '.';
   4620   memcpy (dot_name + 1, name, len + 1);
   4621   h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
   4622   bfd_release (abfd, dot_name);
   4623   return h;
   4624 }
   4625 
   4626 /* This function satisfies all old ABI object references to ".bar" if a
   4627    new ABI object defines "bar".  Well, at least, undefined dot symbols
   4628    are made weak.  This stops later archive searches from including an
   4629    object if we already have a function descriptor definition.  It also
   4630    prevents the linker complaining about undefined symbols.
   4631    We also check and correct mismatched symbol visibility here.  The
   4632    most restrictive visibility of the function descriptor and the
   4633    function entry symbol is used.  */
   4634 
   4635 static bfd_boolean
   4636 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
   4637 {
   4638   struct ppc_link_hash_table *htab;
   4639   struct ppc_link_hash_entry *fdh;
   4640 
   4641   if (eh->elf.root.type == bfd_link_hash_indirect)
   4642     return TRUE;
   4643 
   4644   if (eh->elf.root.type == bfd_link_hash_warning)
   4645     eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
   4646 
   4647   if (eh->elf.root.root.string[0] != '.')
   4648     abort ();
   4649 
   4650   htab = ppc_hash_table (info);
   4651   if (htab == NULL)
   4652     return FALSE;
   4653 
   4654   fdh = lookup_fdh (eh, htab);
   4655   if (fdh == NULL)
   4656     {
   4657       if (!info->relocatable
   4658 	  && (eh->elf.root.type == bfd_link_hash_undefined
   4659 	      || eh->elf.root.type == bfd_link_hash_undefweak)
   4660 	  && eh->elf.ref_regular)
   4661 	{
   4662 	  /* Make an undefweak function descriptor sym, which is enough to
   4663 	     pull in an --as-needed shared lib, but won't cause link
   4664 	     errors.  Archives are handled elsewhere.  */
   4665 	  fdh = make_fdh (info, eh);
   4666 	  if (fdh == NULL)
   4667 	    return FALSE;
   4668 	  fdh->elf.ref_regular = 1;
   4669 	}
   4670     }
   4671   else
   4672     {
   4673       unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
   4674       unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
   4675       if (entry_vis < descr_vis)
   4676 	fdh->elf.other += entry_vis - descr_vis;
   4677       else if (entry_vis > descr_vis)
   4678 	eh->elf.other += descr_vis - entry_vis;
   4679 
   4680       if ((fdh->elf.root.type == bfd_link_hash_defined
   4681 	   || fdh->elf.root.type == bfd_link_hash_defweak)
   4682 	  && eh->elf.root.type == bfd_link_hash_undefined)
   4683 	{
   4684 	  eh->elf.root.type = bfd_link_hash_undefweak;
   4685 	  eh->was_undefined = 1;
   4686 	  htab->twiddled_syms = 1;
   4687 	}
   4688     }
   4689 
   4690   return TRUE;
   4691 }
   4692 
   4693 /* Process list of dot-symbols we made in link_hash_newfunc.  */
   4694 
   4695 static bfd_boolean
   4696 ppc64_elf_process_dot_syms (bfd *ibfd, struct bfd_link_info *info)
   4697 {
   4698   struct ppc_link_hash_table *htab;
   4699   struct ppc_link_hash_entry **p, *eh;
   4700 
   4701   if (!is_ppc64_elf (info->output_bfd))
   4702     return TRUE;
   4703   htab = ppc_hash_table (info);
   4704   if (htab == NULL)
   4705     return FALSE;
   4706 
   4707   if (is_ppc64_elf (ibfd))
   4708     {
   4709       p = &htab->dot_syms;
   4710       while ((eh = *p) != NULL)
   4711 	{
   4712 	  *p = NULL;
   4713 	  if (!add_symbol_adjust (eh, info))
   4714 	    return FALSE;
   4715 	  p = &eh->u.next_dot_sym;
   4716 	}
   4717     }
   4718 
   4719   /* Clear the list for non-ppc64 input files.  */
   4720   p = &htab->dot_syms;
   4721   while ((eh = *p) != NULL)
   4722     {
   4723       *p = NULL;
   4724       p = &eh->u.next_dot_sym;
   4725     }
   4726 
   4727   /* We need to fix the undefs list for any syms we have twiddled to
   4728      undef_weak.  */
   4729   if (htab->twiddled_syms)
   4730     {
   4731       bfd_link_repair_undef_list (&htab->elf.root);
   4732       htab->twiddled_syms = 0;
   4733     }
   4734   return TRUE;
   4735 }
   4736 
   4737 /* Undo hash table changes when an --as-needed input file is determined
   4738    not to be needed.  */
   4739 
   4740 static bfd_boolean
   4741 ppc64_elf_as_needed_cleanup (bfd *ibfd ATTRIBUTE_UNUSED,
   4742 			     struct bfd_link_info *info)
   4743 {
   4744   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   4745 
   4746   if (htab == NULL)
   4747     return FALSE;
   4748 
   4749   htab->dot_syms = NULL;
   4750   return TRUE;
   4751 }
   4752 
   4753 /* If --just-symbols against a final linked binary, then assume we need
   4754    toc adjusting stubs when calling functions defined there.  */
   4755 
   4756 static void
   4757 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
   4758 {
   4759   if ((sec->flags & SEC_CODE) != 0
   4760       && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
   4761       && is_ppc64_elf (sec->owner))
   4762     {
   4763       asection *got = bfd_get_section_by_name (sec->owner, ".got");
   4764       if (got != NULL
   4765 	  && got->size >= elf_backend_got_header_size
   4766 	  && bfd_get_section_by_name (sec->owner, ".opd") != NULL)
   4767 	sec->has_toc_reloc = 1;
   4768     }
   4769   _bfd_elf_link_just_syms (sec, info);
   4770 }
   4771 
   4772 static struct plt_entry **
   4773 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
   4774 		       unsigned long r_symndx, bfd_vma r_addend, int tls_type)
   4775 {
   4776   struct got_entry **local_got_ents = elf_local_got_ents (abfd);
   4777   struct plt_entry **local_plt;
   4778   unsigned char *local_got_tls_masks;
   4779 
   4780   if (local_got_ents == NULL)
   4781     {
   4782       bfd_size_type size = symtab_hdr->sh_info;
   4783 
   4784       size *= (sizeof (*local_got_ents)
   4785 	       + sizeof (*local_plt)
   4786 	       + sizeof (*local_got_tls_masks));
   4787       local_got_ents = bfd_zalloc (abfd, size);
   4788       if (local_got_ents == NULL)
   4789 	return NULL;
   4790       elf_local_got_ents (abfd) = local_got_ents;
   4791     }
   4792 
   4793   if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
   4794     {
   4795       struct got_entry *ent;
   4796 
   4797       for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
   4798 	if (ent->addend == r_addend
   4799 	    && ent->owner == abfd
   4800 	    && ent->tls_type == tls_type)
   4801 	  break;
   4802       if (ent == NULL)
   4803 	{
   4804 	  bfd_size_type amt = sizeof (*ent);
   4805 	  ent = bfd_alloc (abfd, amt);
   4806 	  if (ent == NULL)
   4807 	    return FALSE;
   4808 	  ent->next = local_got_ents[r_symndx];
   4809 	  ent->addend = r_addend;
   4810 	  ent->owner = abfd;
   4811 	  ent->tls_type = tls_type;
   4812 	  ent->is_indirect = FALSE;
   4813 	  ent->got.refcount = 0;
   4814 	  local_got_ents[r_symndx] = ent;
   4815 	}
   4816       ent->got.refcount += 1;
   4817     }
   4818 
   4819   local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
   4820   local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
   4821   local_got_tls_masks[r_symndx] |= tls_type;
   4822 
   4823   return local_plt + r_symndx;
   4824 }
   4825 
   4826 static bfd_boolean
   4827 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
   4828 {
   4829   struct plt_entry *ent;
   4830 
   4831   for (ent = *plist; ent != NULL; ent = ent->next)
   4832     if (ent->addend == addend)
   4833       break;
   4834   if (ent == NULL)
   4835     {
   4836       bfd_size_type amt = sizeof (*ent);
   4837       ent = bfd_alloc (abfd, amt);
   4838       if (ent == NULL)
   4839 	return FALSE;
   4840       ent->next = *plist;
   4841       ent->addend = addend;
   4842       ent->plt.refcount = 0;
   4843       *plist = ent;
   4844     }
   4845   ent->plt.refcount += 1;
   4846   return TRUE;
   4847 }
   4848 
   4849 static bfd_boolean
   4850 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
   4851 {
   4852   return (r_type == R_PPC64_REL24
   4853 	  || r_type == R_PPC64_REL14
   4854 	  || r_type == R_PPC64_REL14_BRTAKEN
   4855 	  || r_type == R_PPC64_REL14_BRNTAKEN
   4856 	  || r_type == R_PPC64_ADDR24
   4857 	  || r_type == R_PPC64_ADDR14
   4858 	  || r_type == R_PPC64_ADDR14_BRTAKEN
   4859 	  || r_type == R_PPC64_ADDR14_BRNTAKEN);
   4860 }
   4861 
   4862 /* Look through the relocs for a section during the first phase, and
   4863    calculate needed space in the global offset table, procedure
   4864    linkage table, and dynamic reloc sections.  */
   4865 
   4866 static bfd_boolean
   4867 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
   4868 			asection *sec, const Elf_Internal_Rela *relocs)
   4869 {
   4870   struct ppc_link_hash_table *htab;
   4871   Elf_Internal_Shdr *symtab_hdr;
   4872   struct elf_link_hash_entry **sym_hashes;
   4873   const Elf_Internal_Rela *rel;
   4874   const Elf_Internal_Rela *rel_end;
   4875   asection *sreloc;
   4876   asection **opd_sym_map;
   4877   struct elf_link_hash_entry *tga, *dottga;
   4878 
   4879   if (info->relocatable)
   4880     return TRUE;
   4881 
   4882   /* Don't do anything special with non-loaded, non-alloced sections.
   4883      In particular, any relocs in such sections should not affect GOT
   4884      and PLT reference counting (ie. we don't allow them to create GOT
   4885      or PLT entries), there's no possibility or desire to optimize TLS
   4886      relocs, and there's not much point in propagating relocs to shared
   4887      libs that the dynamic linker won't relocate.  */
   4888   if ((sec->flags & SEC_ALLOC) == 0)
   4889     return TRUE;
   4890 
   4891   BFD_ASSERT (is_ppc64_elf (abfd));
   4892 
   4893   htab = ppc_hash_table (info);
   4894   if (htab == NULL)
   4895     return FALSE;
   4896 
   4897   tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
   4898 			      FALSE, FALSE, TRUE);
   4899   dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
   4900 				 FALSE, FALSE, TRUE);
   4901   symtab_hdr = &elf_symtab_hdr (abfd);
   4902   sym_hashes = elf_sym_hashes (abfd);
   4903   sreloc = NULL;
   4904   opd_sym_map = NULL;
   4905   if (strcmp (sec->name, ".opd") == 0)
   4906     {
   4907       /* Garbage collection needs some extra help with .opd sections.
   4908 	 We don't want to necessarily keep everything referenced by
   4909 	 relocs in .opd, as that would keep all functions.  Instead,
   4910 	 if we reference an .opd symbol (a function descriptor), we
   4911 	 want to keep the function code symbol's section.  This is
   4912 	 easy for global symbols, but for local syms we need to keep
   4913 	 information about the associated function section.  */
   4914       bfd_size_type amt;
   4915 
   4916       amt = sec->size * sizeof (*opd_sym_map) / 8;
   4917       opd_sym_map = bfd_zalloc (abfd, amt);
   4918       if (opd_sym_map == NULL)
   4919 	return FALSE;
   4920       ppc64_elf_section_data (sec)->u.opd.func_sec = opd_sym_map;
   4921       BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
   4922       ppc64_elf_section_data (sec)->sec_type = sec_opd;
   4923     }
   4924 
   4925   if (htab->sfpr == NULL
   4926       && !create_linkage_sections (htab->elf.dynobj, info))
   4927     return FALSE;
   4928 
   4929   rel_end = relocs + sec->reloc_count;
   4930   for (rel = relocs; rel < rel_end; rel++)
   4931     {
   4932       unsigned long r_symndx;
   4933       struct elf_link_hash_entry *h;
   4934       enum elf_ppc64_reloc_type r_type;
   4935       int tls_type;
   4936       struct _ppc64_elf_section_data *ppc64_sec;
   4937       struct plt_entry **ifunc;
   4938 
   4939       r_symndx = ELF64_R_SYM (rel->r_info);
   4940       if (r_symndx < symtab_hdr->sh_info)
   4941 	h = NULL;
   4942       else
   4943 	{
   4944 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   4945 	  h = elf_follow_link (h);
   4946 	}
   4947 
   4948       tls_type = 0;
   4949       ifunc = NULL;
   4950       if (h != NULL)
   4951 	{
   4952 	  if (h->type == STT_GNU_IFUNC)
   4953 	    {
   4954 	      h->needs_plt = 1;
   4955 	      ifunc = &h->plt.plist;
   4956 	    }
   4957 	}
   4958       else
   4959 	{
   4960 	  Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
   4961 							  abfd, r_symndx);
   4962 	  if (isym == NULL)
   4963 	    return FALSE;
   4964 
   4965 	  if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
   4966 	    {
   4967 	      ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
   4968 					     rel->r_addend, PLT_IFUNC);
   4969 	      if (ifunc == NULL)
   4970 		return FALSE;
   4971 	    }
   4972 	}
   4973       r_type = ELF64_R_TYPE (rel->r_info);
   4974       if (is_branch_reloc (r_type))
   4975 	{
   4976 	  if (h != NULL && (h == tga || h == dottga))
   4977 	    {
   4978 	      if (rel != relocs
   4979 		  && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
   4980 		      || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
   4981 		/* We have a new-style __tls_get_addr call with a marker
   4982 		   reloc.  */
   4983 		;
   4984 	      else
   4985 		/* Mark this section as having an old-style call.  */
   4986 		sec->has_tls_get_addr_call = 1;
   4987 	    }
   4988 
   4989 	  /* STT_GNU_IFUNC symbols must have a PLT entry.  */
   4990 	  if (ifunc != NULL
   4991 	      && !update_plt_info (abfd, ifunc, rel->r_addend))
   4992 	    return FALSE;
   4993 	}
   4994 
   4995       switch (r_type)
   4996 	{
   4997 	case R_PPC64_TLSGD:
   4998 	case R_PPC64_TLSLD:
   4999 	  /* These special tls relocs tie a call to __tls_get_addr with
   5000 	     its parameter symbol.  */
   5001 	  break;
   5002 
   5003 	case R_PPC64_GOT_TLSLD16:
   5004 	case R_PPC64_GOT_TLSLD16_LO:
   5005 	case R_PPC64_GOT_TLSLD16_HI:
   5006 	case R_PPC64_GOT_TLSLD16_HA:
   5007 	  tls_type = TLS_TLS | TLS_LD;
   5008 	  goto dogottls;
   5009 
   5010 	case R_PPC64_GOT_TLSGD16:
   5011 	case R_PPC64_GOT_TLSGD16_LO:
   5012 	case R_PPC64_GOT_TLSGD16_HI:
   5013 	case R_PPC64_GOT_TLSGD16_HA:
   5014 	  tls_type = TLS_TLS | TLS_GD;
   5015 	  goto dogottls;
   5016 
   5017 	case R_PPC64_GOT_TPREL16_DS:
   5018 	case R_PPC64_GOT_TPREL16_LO_DS:
   5019 	case R_PPC64_GOT_TPREL16_HI:
   5020 	case R_PPC64_GOT_TPREL16_HA:
   5021 	  if (!info->executable)
   5022 	    info->flags |= DF_STATIC_TLS;
   5023 	  tls_type = TLS_TLS | TLS_TPREL;
   5024 	  goto dogottls;
   5025 
   5026 	case R_PPC64_GOT_DTPREL16_DS:
   5027 	case R_PPC64_GOT_DTPREL16_LO_DS:
   5028 	case R_PPC64_GOT_DTPREL16_HI:
   5029 	case R_PPC64_GOT_DTPREL16_HA:
   5030 	  tls_type = TLS_TLS | TLS_DTPREL;
   5031 	dogottls:
   5032 	  sec->has_tls_reloc = 1;
   5033 	  /* Fall thru */
   5034 
   5035 	case R_PPC64_GOT16:
   5036 	case R_PPC64_GOT16_DS:
   5037 	case R_PPC64_GOT16_HA:
   5038 	case R_PPC64_GOT16_HI:
   5039 	case R_PPC64_GOT16_LO:
   5040 	case R_PPC64_GOT16_LO_DS:
   5041 	  /* This symbol requires a global offset table entry.  */
   5042 	  sec->has_toc_reloc = 1;
   5043 	  if (r_type == R_PPC64_GOT_TLSLD16
   5044 	      || r_type == R_PPC64_GOT_TLSGD16
   5045 	      || r_type == R_PPC64_GOT_TPREL16_DS
   5046 	      || r_type == R_PPC64_GOT_DTPREL16_DS
   5047 	      || r_type == R_PPC64_GOT16
   5048 	      || r_type == R_PPC64_GOT16_DS)
   5049 	    {
   5050 	      htab->do_multi_toc = 1;
   5051 	      ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
   5052 	    }
   5053 
   5054 	  if (ppc64_elf_tdata (abfd)->got == NULL
   5055 	      && !create_got_section (abfd, info))
   5056 	    return FALSE;
   5057 
   5058 	  if (h != NULL)
   5059 	    {
   5060 	      struct ppc_link_hash_entry *eh;
   5061 	      struct got_entry *ent;
   5062 
   5063 	      eh = (struct ppc_link_hash_entry *) h;
   5064 	      for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
   5065 		if (ent->addend == rel->r_addend
   5066 		    && ent->owner == abfd
   5067 		    && ent->tls_type == tls_type)
   5068 		  break;
   5069 	      if (ent == NULL)
   5070 		{
   5071 		  bfd_size_type amt = sizeof (*ent);
   5072 		  ent = bfd_alloc (abfd, amt);
   5073 		  if (ent == NULL)
   5074 		    return FALSE;
   5075 		  ent->next = eh->elf.got.glist;
   5076 		  ent->addend = rel->r_addend;
   5077 		  ent->owner = abfd;
   5078 		  ent->tls_type = tls_type;
   5079 		  ent->is_indirect = FALSE;
   5080 		  ent->got.refcount = 0;
   5081 		  eh->elf.got.glist = ent;
   5082 		}
   5083 	      ent->got.refcount += 1;
   5084 	      eh->tls_mask |= tls_type;
   5085 	    }
   5086 	  else
   5087 	    /* This is a global offset table entry for a local symbol.  */
   5088 	    if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
   5089 					rel->r_addend, tls_type))
   5090 	      return FALSE;
   5091 	  break;
   5092 
   5093 	case R_PPC64_PLT16_HA:
   5094 	case R_PPC64_PLT16_HI:
   5095 	case R_PPC64_PLT16_LO:
   5096 	case R_PPC64_PLT32:
   5097 	case R_PPC64_PLT64:
   5098 	  /* This symbol requires a procedure linkage table entry.  We
   5099 	     actually build the entry in adjust_dynamic_symbol,
   5100 	     because this might be a case of linking PIC code without
   5101 	     linking in any dynamic objects, in which case we don't
   5102 	     need to generate a procedure linkage table after all.  */
   5103 	  if (h == NULL)
   5104 	    {
   5105 	      /* It does not make sense to have a procedure linkage
   5106 		 table entry for a local symbol.  */
   5107 	      bfd_set_error (bfd_error_bad_value);
   5108 	      return FALSE;
   5109 	    }
   5110 	  else
   5111 	    {
   5112 	      if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
   5113 		return FALSE;
   5114 	      h->needs_plt = 1;
   5115 	      if (h->root.root.string[0] == '.'
   5116 		  && h->root.root.string[1] != '\0')
   5117 		((struct ppc_link_hash_entry *) h)->is_func = 1;
   5118 	    }
   5119 	  break;
   5120 
   5121 	  /* The following relocations don't need to propagate the
   5122 	     relocation if linking a shared object since they are
   5123 	     section relative.  */
   5124 	case R_PPC64_SECTOFF:
   5125 	case R_PPC64_SECTOFF_LO:
   5126 	case R_PPC64_SECTOFF_HI:
   5127 	case R_PPC64_SECTOFF_HA:
   5128 	case R_PPC64_SECTOFF_DS:
   5129 	case R_PPC64_SECTOFF_LO_DS:
   5130 	case R_PPC64_DTPREL16:
   5131 	case R_PPC64_DTPREL16_LO:
   5132 	case R_PPC64_DTPREL16_HI:
   5133 	case R_PPC64_DTPREL16_HA:
   5134 	case R_PPC64_DTPREL16_DS:
   5135 	case R_PPC64_DTPREL16_LO_DS:
   5136 	case R_PPC64_DTPREL16_HIGHER:
   5137 	case R_PPC64_DTPREL16_HIGHERA:
   5138 	case R_PPC64_DTPREL16_HIGHEST:
   5139 	case R_PPC64_DTPREL16_HIGHESTA:
   5140 	  break;
   5141 
   5142 	  /* Nor do these.  */
   5143 	case R_PPC64_REL16:
   5144 	case R_PPC64_REL16_LO:
   5145 	case R_PPC64_REL16_HI:
   5146 	case R_PPC64_REL16_HA:
   5147 	  break;
   5148 
   5149 	case R_PPC64_TOC16:
   5150 	case R_PPC64_TOC16_DS:
   5151 	  htab->do_multi_toc = 1;
   5152 	  ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
   5153 	case R_PPC64_TOC16_LO:
   5154 	case R_PPC64_TOC16_HI:
   5155 	case R_PPC64_TOC16_HA:
   5156 	case R_PPC64_TOC16_LO_DS:
   5157 	  sec->has_toc_reloc = 1;
   5158 	  break;
   5159 
   5160 	  /* This relocation describes the C++ object vtable hierarchy.
   5161 	     Reconstruct it for later use during GC.  */
   5162 	case R_PPC64_GNU_VTINHERIT:
   5163 	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
   5164 	    return FALSE;
   5165 	  break;
   5166 
   5167 	  /* This relocation describes which C++ vtable entries are actually
   5168 	     used.  Record for later use during GC.  */
   5169 	case R_PPC64_GNU_VTENTRY:
   5170 	  BFD_ASSERT (h != NULL);
   5171 	  if (h != NULL
   5172 	      && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
   5173 	    return FALSE;
   5174 	  break;
   5175 
   5176 	case R_PPC64_REL14:
   5177 	case R_PPC64_REL14_BRTAKEN:
   5178 	case R_PPC64_REL14_BRNTAKEN:
   5179 	  {
   5180 	    asection *dest = NULL;
   5181 
   5182 	    /* Heuristic: If jumping outside our section, chances are
   5183 	       we are going to need a stub.  */
   5184 	    if (h != NULL)
   5185 	      {
   5186 		/* If the sym is weak it may be overridden later, so
   5187 		   don't assume we know where a weak sym lives.  */
   5188 		if (h->root.type == bfd_link_hash_defined)
   5189 		  dest = h->root.u.def.section;
   5190 	      }
   5191 	    else
   5192 	      {
   5193 		Elf_Internal_Sym *isym;
   5194 
   5195 		isym = bfd_sym_from_r_symndx (&htab->sym_cache,
   5196 					      abfd, r_symndx);
   5197 		if (isym == NULL)
   5198 		  return FALSE;
   5199 
   5200 		dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
   5201 	      }
   5202 
   5203 	    if (dest != sec)
   5204 	      ppc64_elf_section_data (sec)->has_14bit_branch = 1;
   5205 	  }
   5206 	  /* Fall through.  */
   5207 
   5208 	case R_PPC64_REL24:
   5209 	  if (h != NULL && ifunc == NULL)
   5210 	    {
   5211 	      /* We may need a .plt entry if the function this reloc
   5212 		 refers to is in a shared lib.  */
   5213 	      if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
   5214 		return FALSE;
   5215 	      h->needs_plt = 1;
   5216 	      if (h->root.root.string[0] == '.'
   5217 		  && h->root.root.string[1] != '\0')
   5218 		((struct ppc_link_hash_entry *) h)->is_func = 1;
   5219 	      if (h == tga || h == dottga)
   5220 		sec->has_tls_reloc = 1;
   5221 	    }
   5222 	  break;
   5223 
   5224 	case R_PPC64_TPREL64:
   5225 	  tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
   5226 	  if (!info->executable)
   5227 	    info->flags |= DF_STATIC_TLS;
   5228 	  goto dotlstoc;
   5229 
   5230 	case R_PPC64_DTPMOD64:
   5231 	  if (rel + 1 < rel_end
   5232 	      && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
   5233 	      && rel[1].r_offset == rel->r_offset + 8)
   5234 	    tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
   5235 	  else
   5236 	    tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
   5237 	  goto dotlstoc;
   5238 
   5239 	case R_PPC64_DTPREL64:
   5240 	  tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
   5241 	  if (rel != relocs
   5242 	      && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
   5243 	      && rel[-1].r_offset == rel->r_offset - 8)
   5244 	    /* This is the second reloc of a dtpmod, dtprel pair.
   5245 	       Don't mark with TLS_DTPREL.  */
   5246 	    goto dodyn;
   5247 
   5248 	dotlstoc:
   5249 	  sec->has_tls_reloc = 1;
   5250 	  if (h != NULL)
   5251 	    {
   5252 	      struct ppc_link_hash_entry *eh;
   5253 	      eh = (struct ppc_link_hash_entry *) h;
   5254 	      eh->tls_mask |= tls_type;
   5255 	    }
   5256 	  else
   5257 	    if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
   5258 					rel->r_addend, tls_type))
   5259 	      return FALSE;
   5260 
   5261 	  ppc64_sec = ppc64_elf_section_data (sec);
   5262 	  if (ppc64_sec->sec_type != sec_toc)
   5263 	    {
   5264 	      bfd_size_type amt;
   5265 
   5266 	      /* One extra to simplify get_tls_mask.  */
   5267 	      amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
   5268 	      ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
   5269 	      if (ppc64_sec->u.toc.symndx == NULL)
   5270 		return FALSE;
   5271 	      amt = sec->size * sizeof (bfd_vma) / 8;
   5272 	      ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
   5273 	      if (ppc64_sec->u.toc.add == NULL)
   5274 		return FALSE;
   5275 	      BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
   5276 	      ppc64_sec->sec_type = sec_toc;
   5277 	    }
   5278 	  BFD_ASSERT (rel->r_offset % 8 == 0);
   5279 	  ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
   5280 	  ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
   5281 
   5282 	  /* Mark the second slot of a GD or LD entry.
   5283 	     -1 to indicate GD and -2 to indicate LD.  */
   5284 	  if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
   5285 	    ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
   5286 	  else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
   5287 	    ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
   5288 	  goto dodyn;
   5289 
   5290 	case R_PPC64_TPREL16:
   5291 	case R_PPC64_TPREL16_LO:
   5292 	case R_PPC64_TPREL16_HI:
   5293 	case R_PPC64_TPREL16_HA:
   5294 	case R_PPC64_TPREL16_DS:
   5295 	case R_PPC64_TPREL16_LO_DS:
   5296 	case R_PPC64_TPREL16_HIGHER:
   5297 	case R_PPC64_TPREL16_HIGHERA:
   5298 	case R_PPC64_TPREL16_HIGHEST:
   5299 	case R_PPC64_TPREL16_HIGHESTA:
   5300 	  if (info->shared)
   5301 	    {
   5302 	      if (!info->executable)
   5303 		info->flags |= DF_STATIC_TLS;
   5304 	      goto dodyn;
   5305 	    }
   5306 	  break;
   5307 
   5308 	case R_PPC64_ADDR64:
   5309 	  if (opd_sym_map != NULL
   5310 	      && rel + 1 < rel_end
   5311 	      && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
   5312 	    {
   5313 	      if (h != NULL)
   5314 		{
   5315 		  if (h->root.root.string[0] == '.'
   5316 		      && h->root.root.string[1] != 0
   5317 		      && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
   5318 		    ;
   5319 		  else
   5320 		    ((struct ppc_link_hash_entry *) h)->is_func = 1;
   5321 		}
   5322 	      else
   5323 		{
   5324 		  asection *s;
   5325 		  Elf_Internal_Sym *isym;
   5326 
   5327 		  isym = bfd_sym_from_r_symndx (&htab->sym_cache,
   5328 						abfd, r_symndx);
   5329 		  if (isym == NULL)
   5330 		    return FALSE;
   5331 
   5332 		  s = bfd_section_from_elf_index (abfd, isym->st_shndx);
   5333 		  if (s != NULL && s != sec)
   5334 		    opd_sym_map[rel->r_offset / 8] = s;
   5335 		}
   5336 	    }
   5337 	  /* Fall through.  */
   5338 
   5339 	case R_PPC64_REL30:
   5340 	case R_PPC64_REL32:
   5341 	case R_PPC64_REL64:
   5342 	case R_PPC64_ADDR14:
   5343 	case R_PPC64_ADDR14_BRNTAKEN:
   5344 	case R_PPC64_ADDR14_BRTAKEN:
   5345 	case R_PPC64_ADDR16:
   5346 	case R_PPC64_ADDR16_DS:
   5347 	case R_PPC64_ADDR16_HA:
   5348 	case R_PPC64_ADDR16_HI:
   5349 	case R_PPC64_ADDR16_HIGHER:
   5350 	case R_PPC64_ADDR16_HIGHERA:
   5351 	case R_PPC64_ADDR16_HIGHEST:
   5352 	case R_PPC64_ADDR16_HIGHESTA:
   5353 	case R_PPC64_ADDR16_LO:
   5354 	case R_PPC64_ADDR16_LO_DS:
   5355 	case R_PPC64_ADDR24:
   5356 	case R_PPC64_ADDR32:
   5357 	case R_PPC64_UADDR16:
   5358 	case R_PPC64_UADDR32:
   5359 	case R_PPC64_UADDR64:
   5360 	case R_PPC64_TOC:
   5361 	  if (h != NULL && !info->shared)
   5362 	    /* We may need a copy reloc.  */
   5363 	    h->non_got_ref = 1;
   5364 
   5365 	  /* Don't propagate .opd relocs.  */
   5366 	  if (NO_OPD_RELOCS && opd_sym_map != NULL)
   5367 	    break;
   5368 
   5369 	  /* If we are creating a shared library, and this is a reloc
   5370 	     against a global symbol, or a non PC relative reloc
   5371 	     against a local symbol, then we need to copy the reloc
   5372 	     into the shared library.  However, if we are linking with
   5373 	     -Bsymbolic, we do not need to copy a reloc against a
   5374 	     global symbol which is defined in an object we are
   5375 	     including in the link (i.e., DEF_REGULAR is set).  At
   5376 	     this point we have not seen all the input files, so it is
   5377 	     possible that DEF_REGULAR is not set now but will be set
   5378 	     later (it is never cleared).  In case of a weak definition,
   5379 	     DEF_REGULAR may be cleared later by a strong definition in
   5380 	     a shared library.  We account for that possibility below by
   5381 	     storing information in the dyn_relocs field of the hash
   5382 	     table entry.  A similar situation occurs when creating
   5383 	     shared libraries and symbol visibility changes render the
   5384 	     symbol local.
   5385 
   5386 	     If on the other hand, we are creating an executable, we
   5387 	     may need to keep relocations for symbols satisfied by a
   5388 	     dynamic library if we manage to avoid copy relocs for the
   5389 	     symbol.  */
   5390 	dodyn:
   5391 	  if ((info->shared
   5392 	       && (must_be_dyn_reloc (info, r_type)
   5393 		   || (h != NULL
   5394 		       && (! info->symbolic
   5395 			   || h->root.type == bfd_link_hash_defweak
   5396 			   || !h->def_regular))))
   5397 	      || (ELIMINATE_COPY_RELOCS
   5398 		  && !info->shared
   5399 		  && h != NULL
   5400 		  && (h->root.type == bfd_link_hash_defweak
   5401 		      || !h->def_regular))
   5402 	      || (!info->shared
   5403 		  && ifunc != NULL))
   5404 	    {
   5405 	      struct ppc_dyn_relocs *p;
   5406 	      struct ppc_dyn_relocs **head;
   5407 
   5408 	      /* We must copy these reloc types into the output file.
   5409 		 Create a reloc section in dynobj and make room for
   5410 		 this reloc.  */
   5411 	      if (sreloc == NULL)
   5412 		{
   5413 		  sreloc = _bfd_elf_make_dynamic_reloc_section
   5414 		    (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
   5415 
   5416 		  if (sreloc == NULL)
   5417 		    return FALSE;
   5418 		}
   5419 
   5420 	      /* If this is a global symbol, we count the number of
   5421 		 relocations we need for this symbol.  */
   5422 	      if (h != NULL)
   5423 		{
   5424 		  head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
   5425 		}
   5426 	      else
   5427 		{
   5428 		  /* Track dynamic relocs needed for local syms too.
   5429 		     We really need local syms available to do this
   5430 		     easily.  Oh well.  */
   5431 		  asection *s;
   5432 		  void *vpp;
   5433 		  Elf_Internal_Sym *isym;
   5434 
   5435 		  isym = bfd_sym_from_r_symndx (&htab->sym_cache,
   5436 						abfd, r_symndx);
   5437 		  if (isym == NULL)
   5438 		    return FALSE;
   5439 
   5440 		  s = bfd_section_from_elf_index (abfd, isym->st_shndx);
   5441 		  if (s == NULL)
   5442 		    s = sec;
   5443 
   5444 		  vpp = &elf_section_data (s)->local_dynrel;
   5445 		  head = (struct ppc_dyn_relocs **) vpp;
   5446 		}
   5447 
   5448 	      p = *head;
   5449 	      if (p == NULL || p->sec != sec)
   5450 		{
   5451 		  p = bfd_alloc (htab->elf.dynobj, sizeof *p);
   5452 		  if (p == NULL)
   5453 		    return FALSE;
   5454 		  p->next = *head;
   5455 		  *head = p;
   5456 		  p->sec = sec;
   5457 		  p->count = 0;
   5458 		  p->pc_count = 0;
   5459 		}
   5460 
   5461 	      p->count += 1;
   5462 	      if (!must_be_dyn_reloc (info, r_type))
   5463 		p->pc_count += 1;
   5464 	    }
   5465 	  break;
   5466 
   5467 	default:
   5468 	  break;
   5469 	}
   5470     }
   5471 
   5472   return TRUE;
   5473 }
   5474 
   5475 /* OFFSET in OPD_SEC specifies a function descriptor.  Return the address
   5476    of the code entry point, and its section.  */
   5477 
   5478 static bfd_vma
   5479 opd_entry_value (asection *opd_sec,
   5480 		 bfd_vma offset,
   5481 		 asection **code_sec,
   5482 		 bfd_vma *code_off)
   5483 {
   5484   bfd *opd_bfd = opd_sec->owner;
   5485   Elf_Internal_Rela *relocs;
   5486   Elf_Internal_Rela *lo, *hi, *look;
   5487   bfd_vma val;
   5488 
   5489   /* No relocs implies we are linking a --just-symbols object.  */
   5490   if (opd_sec->reloc_count == 0)
   5491     {
   5492       char buf[8];
   5493 
   5494       if (!bfd_get_section_contents (opd_bfd, opd_sec, buf, offset, 8))
   5495 	return (bfd_vma) -1;
   5496 
   5497       val = bfd_get_64 (opd_bfd, buf);
   5498       if (code_sec != NULL)
   5499 	{
   5500 	  asection *sec, *likely = NULL;
   5501 	  for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
   5502 	    if (sec->vma <= val
   5503 		&& (sec->flags & SEC_LOAD) != 0
   5504 		&& (sec->flags & SEC_ALLOC) != 0)
   5505 	      likely = sec;
   5506 	  if (likely != NULL)
   5507 	    {
   5508 	      *code_sec = likely;
   5509 	      if (code_off != NULL)
   5510 		*code_off = val - likely->vma;
   5511 	    }
   5512 	}
   5513       return val;
   5514     }
   5515 
   5516   BFD_ASSERT (is_ppc64_elf (opd_bfd));
   5517 
   5518   relocs = ppc64_elf_tdata (opd_bfd)->opd_relocs;
   5519   if (relocs == NULL)
   5520     relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
   5521 
   5522   /* Go find the opd reloc at the sym address.  */
   5523   lo = relocs;
   5524   BFD_ASSERT (lo != NULL);
   5525   hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
   5526   val = (bfd_vma) -1;
   5527   while (lo < hi)
   5528     {
   5529       look = lo + (hi - lo) / 2;
   5530       if (look->r_offset < offset)
   5531 	lo = look + 1;
   5532       else if (look->r_offset > offset)
   5533 	hi = look;
   5534       else
   5535 	{
   5536 	  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
   5537 
   5538 	  if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
   5539 	      && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
   5540 	    {
   5541 	      unsigned long symndx = ELF64_R_SYM (look->r_info);
   5542 	      asection *sec;
   5543 
   5544 	      if (symndx < symtab_hdr->sh_info)
   5545 		{
   5546 		  Elf_Internal_Sym *sym;
   5547 
   5548 		  sym = (Elf_Internal_Sym *) symtab_hdr->contents;
   5549 		  if (sym == NULL)
   5550 		    {
   5551 		      sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
   5552 						  symtab_hdr->sh_info,
   5553 						  0, NULL, NULL, NULL);
   5554 		      if (sym == NULL)
   5555 			break;
   5556 		      symtab_hdr->contents = (bfd_byte *) sym;
   5557 		    }
   5558 
   5559 		  sym += symndx;
   5560 		  val = sym->st_value;
   5561 		  sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
   5562 		  BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
   5563 		}
   5564 	      else
   5565 		{
   5566 		  struct elf_link_hash_entry **sym_hashes;
   5567 		  struct elf_link_hash_entry *rh;
   5568 
   5569 		  sym_hashes = elf_sym_hashes (opd_bfd);
   5570 		  rh = sym_hashes[symndx - symtab_hdr->sh_info];
   5571 		  rh = elf_follow_link (rh);
   5572 		  BFD_ASSERT (rh->root.type == bfd_link_hash_defined
   5573 			      || rh->root.type == bfd_link_hash_defweak);
   5574 		  val = rh->root.u.def.value;
   5575 		  sec = rh->root.u.def.section;
   5576 		}
   5577 	      val += look->r_addend;
   5578 	      if (code_off != NULL)
   5579 		*code_off = val;
   5580 	      if (code_sec != NULL)
   5581 		*code_sec = sec;
   5582 	      if (sec != NULL && sec->output_section != NULL)
   5583 		val += sec->output_section->vma + sec->output_offset;
   5584 	    }
   5585 	  break;
   5586 	}
   5587     }
   5588 
   5589   return val;
   5590 }
   5591 
   5592 /* Return true if symbol is defined in a regular object file.  */
   5593 
   5594 static bfd_boolean
   5595 is_static_defined (struct elf_link_hash_entry *h)
   5596 {
   5597   return ((h->root.type == bfd_link_hash_defined
   5598 	   || h->root.type == bfd_link_hash_defweak)
   5599 	  && h->root.u.def.section != NULL
   5600 	  && h->root.u.def.section->output_section != NULL);
   5601 }
   5602 
   5603 /* If FDH is a function descriptor symbol, return the associated code
   5604    entry symbol if it is defined.  Return NULL otherwise.  */
   5605 
   5606 static struct ppc_link_hash_entry *
   5607 defined_code_entry (struct ppc_link_hash_entry *fdh)
   5608 {
   5609   if (fdh->is_func_descriptor)
   5610     {
   5611       struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
   5612       if (fh->elf.root.type == bfd_link_hash_defined
   5613 	  || fh->elf.root.type == bfd_link_hash_defweak)
   5614 	return fh;
   5615     }
   5616   return NULL;
   5617 }
   5618 
   5619 /* If FH is a function code entry symbol, return the associated
   5620    function descriptor symbol if it is defined.  Return NULL otherwise.  */
   5621 
   5622 static struct ppc_link_hash_entry *
   5623 defined_func_desc (struct ppc_link_hash_entry *fh)
   5624 {
   5625   if (fh->oh != NULL
   5626       && fh->oh->is_func_descriptor)
   5627     {
   5628       struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
   5629       if (fdh->elf.root.type == bfd_link_hash_defined
   5630 	  || fdh->elf.root.type == bfd_link_hash_defweak)
   5631 	return fdh;
   5632     }
   5633   return NULL;
   5634 }
   5635 
   5636 /* Mark all our entry sym sections, both opd and code section.  */
   5637 
   5638 static void
   5639 ppc64_elf_gc_keep (struct bfd_link_info *info)
   5640 {
   5641   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   5642   struct bfd_sym_chain *sym;
   5643 
   5644   if (htab == NULL)
   5645     return;
   5646 
   5647   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
   5648     {
   5649       struct ppc_link_hash_entry *eh, *fh;
   5650       asection *sec;
   5651 
   5652       eh = (struct ppc_link_hash_entry *)
   5653 	elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
   5654       if (eh == NULL)
   5655 	continue;
   5656       if (eh->elf.root.type != bfd_link_hash_defined
   5657 	  && eh->elf.root.type != bfd_link_hash_defweak)
   5658 	continue;
   5659 
   5660       fh = defined_code_entry (eh);
   5661       if (fh != NULL)
   5662 	{
   5663 	  sec = fh->elf.root.u.def.section;
   5664 	  sec->flags |= SEC_KEEP;
   5665 	}
   5666       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
   5667 	       && opd_entry_value (eh->elf.root.u.def.section,
   5668 				   eh->elf.root.u.def.value,
   5669 				   &sec, NULL) != (bfd_vma) -1)
   5670 	sec->flags |= SEC_KEEP;
   5671 
   5672       sec = eh->elf.root.u.def.section;
   5673       sec->flags |= SEC_KEEP;
   5674     }
   5675 }
   5676 
   5677 /* Mark sections containing dynamically referenced symbols.  When
   5678    building shared libraries, we must assume that any visible symbol is
   5679    referenced.  */
   5680 
   5681 static bfd_boolean
   5682 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
   5683 {
   5684   struct bfd_link_info *info = (struct bfd_link_info *) inf;
   5685   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
   5686   struct ppc_link_hash_entry *fdh;
   5687 
   5688   if (eh->elf.root.type == bfd_link_hash_warning)
   5689     eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
   5690 
   5691   /* Dynamic linking info is on the func descriptor sym.  */
   5692   fdh = defined_func_desc (eh);
   5693   if (fdh != NULL)
   5694     eh = fdh;
   5695 
   5696   if ((eh->elf.root.type == bfd_link_hash_defined
   5697        || eh->elf.root.type == bfd_link_hash_defweak)
   5698       && (eh->elf.ref_dynamic
   5699 	  || (!info->executable
   5700 	      && eh->elf.def_regular
   5701 	      && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
   5702 	      && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN)))
   5703     {
   5704       asection *code_sec;
   5705       struct ppc_link_hash_entry *fh;
   5706 
   5707       eh->elf.root.u.def.section->flags |= SEC_KEEP;
   5708 
   5709       /* Function descriptor syms cause the associated
   5710 	 function code sym section to be marked.  */
   5711       fh = defined_code_entry (eh);
   5712       if (fh != NULL)
   5713 	{
   5714 	  code_sec = fh->elf.root.u.def.section;
   5715 	  code_sec->flags |= SEC_KEEP;
   5716 	}
   5717       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
   5718 	       && opd_entry_value (eh->elf.root.u.def.section,
   5719 				   eh->elf.root.u.def.value,
   5720 				   &code_sec, NULL) != (bfd_vma) -1)
   5721 	code_sec->flags |= SEC_KEEP;
   5722     }
   5723 
   5724   return TRUE;
   5725 }
   5726 
   5727 /* Return the section that should be marked against GC for a given
   5728    relocation.  */
   5729 
   5730 static asection *
   5731 ppc64_elf_gc_mark_hook (asection *sec,
   5732 			struct bfd_link_info *info,
   5733 			Elf_Internal_Rela *rel,
   5734 			struct elf_link_hash_entry *h,
   5735 			Elf_Internal_Sym *sym)
   5736 {
   5737   asection *rsec;
   5738 
   5739   /* Syms return NULL if we're marking .opd, so we avoid marking all
   5740      function sections, as all functions are referenced in .opd.  */
   5741   rsec = NULL;
   5742   if (get_opd_info (sec) != NULL)
   5743     return rsec;
   5744 
   5745   if (h != NULL)
   5746     {
   5747       enum elf_ppc64_reloc_type r_type;
   5748       struct ppc_link_hash_entry *eh, *fh, *fdh;
   5749 
   5750       r_type = ELF64_R_TYPE (rel->r_info);
   5751       switch (r_type)
   5752 	{
   5753 	case R_PPC64_GNU_VTINHERIT:
   5754 	case R_PPC64_GNU_VTENTRY:
   5755 	  break;
   5756 
   5757 	default:
   5758 	  switch (h->root.type)
   5759 	    {
   5760 	    case bfd_link_hash_defined:
   5761 	    case bfd_link_hash_defweak:
   5762 	      eh = (struct ppc_link_hash_entry *) h;
   5763 	      fdh = defined_func_desc (eh);
   5764 	      if (fdh != NULL)
   5765 		eh = fdh;
   5766 
   5767 	      /* Function descriptor syms cause the associated
   5768 		 function code sym section to be marked.  */
   5769 	      fh = defined_code_entry (eh);
   5770 	      if (fh != NULL)
   5771 		{
   5772 		  /* They also mark their opd section.  */
   5773 		  eh->elf.root.u.def.section->gc_mark = 1;
   5774 
   5775 		  rsec = fh->elf.root.u.def.section;
   5776 		}
   5777 	      else if (get_opd_info (eh->elf.root.u.def.section) != NULL
   5778 		       && opd_entry_value (eh->elf.root.u.def.section,
   5779 					   eh->elf.root.u.def.value,
   5780 					   &rsec, NULL) != (bfd_vma) -1)
   5781 		eh->elf.root.u.def.section->gc_mark = 1;
   5782 	      else
   5783 		rsec = h->root.u.def.section;
   5784 	      break;
   5785 
   5786 	    case bfd_link_hash_common:
   5787 	      rsec = h->root.u.c.p->section;
   5788 	      break;
   5789 
   5790 	    default:
   5791 	      return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
   5792 	    }
   5793 	}
   5794     }
   5795   else
   5796     {
   5797       struct _opd_sec_data *opd;
   5798 
   5799       rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
   5800       opd = get_opd_info (rsec);
   5801       if (opd != NULL && opd->func_sec != NULL)
   5802 	{
   5803 	  rsec->gc_mark = 1;
   5804 
   5805 	  rsec = opd->func_sec[(sym->st_value + rel->r_addend) / 8];
   5806 	}
   5807     }
   5808 
   5809   return rsec;
   5810 }
   5811 
   5812 /* Update the .got, .plt. and dynamic reloc reference counts for the
   5813    section being removed.  */
   5814 
   5815 static bfd_boolean
   5816 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
   5817 			 asection *sec, const Elf_Internal_Rela *relocs)
   5818 {
   5819   struct ppc_link_hash_table *htab;
   5820   Elf_Internal_Shdr *symtab_hdr;
   5821   struct elf_link_hash_entry **sym_hashes;
   5822   struct got_entry **local_got_ents;
   5823   const Elf_Internal_Rela *rel, *relend;
   5824 
   5825   if (info->relocatable)
   5826     return TRUE;
   5827 
   5828   if ((sec->flags & SEC_ALLOC) == 0)
   5829     return TRUE;
   5830 
   5831   elf_section_data (sec)->local_dynrel = NULL;
   5832 
   5833   htab = ppc_hash_table (info);
   5834   if (htab == NULL)
   5835     return FALSE;
   5836 
   5837   symtab_hdr = &elf_symtab_hdr (abfd);
   5838   sym_hashes = elf_sym_hashes (abfd);
   5839   local_got_ents = elf_local_got_ents (abfd);
   5840 
   5841   relend = relocs + sec->reloc_count;
   5842   for (rel = relocs; rel < relend; rel++)
   5843     {
   5844       unsigned long r_symndx;
   5845       enum elf_ppc64_reloc_type r_type;
   5846       struct elf_link_hash_entry *h = NULL;
   5847       unsigned char tls_type = 0;
   5848 
   5849       r_symndx = ELF64_R_SYM (rel->r_info);
   5850       r_type = ELF64_R_TYPE (rel->r_info);
   5851       if (r_symndx >= symtab_hdr->sh_info)
   5852 	{
   5853 	  struct ppc_link_hash_entry *eh;
   5854 	  struct ppc_dyn_relocs **pp;
   5855 	  struct ppc_dyn_relocs *p;
   5856 
   5857 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   5858 	  h = elf_follow_link (h);
   5859 	  eh = (struct ppc_link_hash_entry *) h;
   5860 
   5861 	  for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
   5862 	    if (p->sec == sec)
   5863 	      {
   5864 		/* Everything must go for SEC.  */
   5865 		*pp = p->next;
   5866 		break;
   5867 	      }
   5868 	}
   5869 
   5870       if (is_branch_reloc (r_type))
   5871 	{
   5872 	  struct plt_entry **ifunc = NULL;
   5873 	  if (h != NULL)
   5874 	    {
   5875 	      if (h->type == STT_GNU_IFUNC)
   5876 		ifunc = &h->plt.plist;
   5877 	    }
   5878 	  else if (local_got_ents != NULL)
   5879 	    {
   5880 	      struct plt_entry **local_plt = (struct plt_entry **)
   5881 		(local_got_ents + symtab_hdr->sh_info);
   5882 	      unsigned char *local_got_tls_masks = (unsigned char *)
   5883 		(local_plt + symtab_hdr->sh_info);
   5884 	      if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
   5885 		ifunc = local_plt + r_symndx;
   5886 	    }
   5887 	  if (ifunc != NULL)
   5888 	    {
   5889 	      struct plt_entry *ent;
   5890 
   5891 	      for (ent = *ifunc; ent != NULL; ent = ent->next)
   5892 		if (ent->addend == rel->r_addend)
   5893 		  break;
   5894 	      if (ent == NULL)
   5895 		abort ();
   5896 	      if (ent->plt.refcount > 0)
   5897 		ent->plt.refcount -= 1;
   5898 	      continue;
   5899 	    }
   5900 	}
   5901 
   5902       switch (r_type)
   5903 	{
   5904 	case R_PPC64_GOT_TLSLD16:
   5905 	case R_PPC64_GOT_TLSLD16_LO:
   5906 	case R_PPC64_GOT_TLSLD16_HI:
   5907 	case R_PPC64_GOT_TLSLD16_HA:
   5908 	  tls_type = TLS_TLS | TLS_LD;
   5909 	  goto dogot;
   5910 
   5911 	case R_PPC64_GOT_TLSGD16:
   5912 	case R_PPC64_GOT_TLSGD16_LO:
   5913 	case R_PPC64_GOT_TLSGD16_HI:
   5914 	case R_PPC64_GOT_TLSGD16_HA:
   5915 	  tls_type = TLS_TLS | TLS_GD;
   5916 	  goto dogot;
   5917 
   5918 	case R_PPC64_GOT_TPREL16_DS:
   5919 	case R_PPC64_GOT_TPREL16_LO_DS:
   5920 	case R_PPC64_GOT_TPREL16_HI:
   5921 	case R_PPC64_GOT_TPREL16_HA:
   5922 	  tls_type = TLS_TLS | TLS_TPREL;
   5923 	  goto dogot;
   5924 
   5925 	case R_PPC64_GOT_DTPREL16_DS:
   5926 	case R_PPC64_GOT_DTPREL16_LO_DS:
   5927 	case R_PPC64_GOT_DTPREL16_HI:
   5928 	case R_PPC64_GOT_DTPREL16_HA:
   5929 	  tls_type = TLS_TLS | TLS_DTPREL;
   5930 	  goto dogot;
   5931 
   5932 	case R_PPC64_GOT16:
   5933 	case R_PPC64_GOT16_DS:
   5934 	case R_PPC64_GOT16_HA:
   5935 	case R_PPC64_GOT16_HI:
   5936 	case R_PPC64_GOT16_LO:
   5937 	case R_PPC64_GOT16_LO_DS:
   5938 	dogot:
   5939 	  {
   5940 	    struct got_entry *ent;
   5941 
   5942 	    if (h != NULL)
   5943 	      ent = h->got.glist;
   5944 	    else
   5945 	      ent = local_got_ents[r_symndx];
   5946 
   5947 	    for (; ent != NULL; ent = ent->next)
   5948 	      if (ent->addend == rel->r_addend
   5949 		  && ent->owner == abfd
   5950 		  && ent->tls_type == tls_type)
   5951 		break;
   5952 	    if (ent == NULL)
   5953 	      abort ();
   5954 	    if (ent->got.refcount > 0)
   5955 	      ent->got.refcount -= 1;
   5956 	  }
   5957 	  break;
   5958 
   5959 	case R_PPC64_PLT16_HA:
   5960 	case R_PPC64_PLT16_HI:
   5961 	case R_PPC64_PLT16_LO:
   5962 	case R_PPC64_PLT32:
   5963 	case R_PPC64_PLT64:
   5964 	case R_PPC64_REL14:
   5965 	case R_PPC64_REL14_BRNTAKEN:
   5966 	case R_PPC64_REL14_BRTAKEN:
   5967 	case R_PPC64_REL24:
   5968 	  if (h != NULL)
   5969 	    {
   5970 	      struct plt_entry *ent;
   5971 
   5972 	      for (ent = h->plt.plist; ent != NULL; ent = ent->next)
   5973 		if (ent->addend == rel->r_addend)
   5974 		  break;
   5975 	      if (ent != NULL && ent->plt.refcount > 0)
   5976 		ent->plt.refcount -= 1;
   5977 	    }
   5978 	  break;
   5979 
   5980 	default:
   5981 	  break;
   5982 	}
   5983     }
   5984   return TRUE;
   5985 }
   5986 
   5987 /* The maximum size of .sfpr.  */
   5988 #define SFPR_MAX (218*4)
   5989 
   5990 struct sfpr_def_parms
   5991 {
   5992   const char name[12];
   5993   unsigned char lo, hi;
   5994   bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
   5995   bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
   5996 };
   5997 
   5998 /* Auto-generate _save*, _rest* functions in .sfpr.  */
   5999 
   6000 static bfd_boolean
   6001 sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
   6002 {
   6003   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   6004   unsigned int i;
   6005   size_t len = strlen (parm->name);
   6006   bfd_boolean writing = FALSE;
   6007   char sym[16];
   6008 
   6009   if (htab == NULL)
   6010     return FALSE;
   6011 
   6012   memcpy (sym, parm->name, len);
   6013   sym[len + 2] = 0;
   6014 
   6015   for (i = parm->lo; i <= parm->hi; i++)
   6016     {
   6017       struct elf_link_hash_entry *h;
   6018 
   6019       sym[len + 0] = i / 10 + '0';
   6020       sym[len + 1] = i % 10 + '0';
   6021       h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
   6022       if (h != NULL
   6023 	  && !h->def_regular)
   6024 	{
   6025 	  h->root.type = bfd_link_hash_defined;
   6026 	  h->root.u.def.section = htab->sfpr;
   6027 	  h->root.u.def.value = htab->sfpr->size;
   6028 	  h->type = STT_FUNC;
   6029 	  h->def_regular = 1;
   6030 	  _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
   6031 	  writing = TRUE;
   6032 	  if (htab->sfpr->contents == NULL)
   6033 	    {
   6034 	      htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
   6035 	      if (htab->sfpr->contents == NULL)
   6036 		return FALSE;
   6037 	    }
   6038 	}
   6039       if (writing)
   6040 	{
   6041 	  bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
   6042 	  if (i != parm->hi)
   6043 	    p = (*parm->write_ent) (htab->elf.dynobj, p, i);
   6044 	  else
   6045 	    p = (*parm->write_tail) (htab->elf.dynobj, p, i);
   6046 	  htab->sfpr->size = p - htab->sfpr->contents;
   6047 	}
   6048     }
   6049 
   6050   return TRUE;
   6051 }
   6052 
   6053 static bfd_byte *
   6054 savegpr0 (bfd *abfd, bfd_byte *p, int r)
   6055 {
   6056   bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6057   return p + 4;
   6058 }
   6059 
   6060 static bfd_byte *
   6061 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
   6062 {
   6063   p = savegpr0 (abfd, p, r);
   6064   bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
   6065   p = p + 4;
   6066   bfd_put_32 (abfd, BLR, p);
   6067   return p + 4;
   6068 }
   6069 
   6070 static bfd_byte *
   6071 restgpr0 (bfd *abfd, bfd_byte *p, int r)
   6072 {
   6073   bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6074   return p + 4;
   6075 }
   6076 
   6077 static bfd_byte *
   6078 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
   6079 {
   6080   bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
   6081   p = p + 4;
   6082   p = restgpr0 (abfd, p, r);
   6083   bfd_put_32 (abfd, MTLR_R0, p);
   6084   p = p + 4;
   6085   if (r == 29)
   6086     {
   6087       p = restgpr0 (abfd, p, 30);
   6088       p = restgpr0 (abfd, p, 31);
   6089     }
   6090   bfd_put_32 (abfd, BLR, p);
   6091   return p + 4;
   6092 }
   6093 
   6094 static bfd_byte *
   6095 savegpr1 (bfd *abfd, bfd_byte *p, int r)
   6096 {
   6097   bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6098   return p + 4;
   6099 }
   6100 
   6101 static bfd_byte *
   6102 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
   6103 {
   6104   p = savegpr1 (abfd, p, r);
   6105   bfd_put_32 (abfd, BLR, p);
   6106   return p + 4;
   6107 }
   6108 
   6109 static bfd_byte *
   6110 restgpr1 (bfd *abfd, bfd_byte *p, int r)
   6111 {
   6112   bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6113   return p + 4;
   6114 }
   6115 
   6116 static bfd_byte *
   6117 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
   6118 {
   6119   p = restgpr1 (abfd, p, r);
   6120   bfd_put_32 (abfd, BLR, p);
   6121   return p + 4;
   6122 }
   6123 
   6124 static bfd_byte *
   6125 savefpr (bfd *abfd, bfd_byte *p, int r)
   6126 {
   6127   bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6128   return p + 4;
   6129 }
   6130 
   6131 static bfd_byte *
   6132 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
   6133 {
   6134   p = savefpr (abfd, p, r);
   6135   bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
   6136   p = p + 4;
   6137   bfd_put_32 (abfd, BLR, p);
   6138   return p + 4;
   6139 }
   6140 
   6141 static bfd_byte *
   6142 restfpr (bfd *abfd, bfd_byte *p, int r)
   6143 {
   6144   bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6145   return p + 4;
   6146 }
   6147 
   6148 static bfd_byte *
   6149 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
   6150 {
   6151   bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
   6152   p = p + 4;
   6153   p = restfpr (abfd, p, r);
   6154   bfd_put_32 (abfd, MTLR_R0, p);
   6155   p = p + 4;
   6156   if (r == 29)
   6157     {
   6158       p = restfpr (abfd, p, 30);
   6159       p = restfpr (abfd, p, 31);
   6160     }
   6161   bfd_put_32 (abfd, BLR, p);
   6162   return p + 4;
   6163 }
   6164 
   6165 static bfd_byte *
   6166 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
   6167 {
   6168   p = savefpr (abfd, p, r);
   6169   bfd_put_32 (abfd, BLR, p);
   6170   return p + 4;
   6171 }
   6172 
   6173 static bfd_byte *
   6174 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
   6175 {
   6176   p = restfpr (abfd, p, r);
   6177   bfd_put_32 (abfd, BLR, p);
   6178   return p + 4;
   6179 }
   6180 
   6181 static bfd_byte *
   6182 savevr (bfd *abfd, bfd_byte *p, int r)
   6183 {
   6184   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
   6185   p = p + 4;
   6186   bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
   6187   return p + 4;
   6188 }
   6189 
   6190 static bfd_byte *
   6191 savevr_tail (bfd *abfd, bfd_byte *p, int r)
   6192 {
   6193   p = savevr (abfd, p, r);
   6194   bfd_put_32 (abfd, BLR, p);
   6195   return p + 4;
   6196 }
   6197 
   6198 static bfd_byte *
   6199 restvr (bfd *abfd, bfd_byte *p, int r)
   6200 {
   6201   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
   6202   p = p + 4;
   6203   bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
   6204   return p + 4;
   6205 }
   6206 
   6207 static bfd_byte *
   6208 restvr_tail (bfd *abfd, bfd_byte *p, int r)
   6209 {
   6210   p = restvr (abfd, p, r);
   6211   bfd_put_32 (abfd, BLR, p);
   6212   return p + 4;
   6213 }
   6214 
   6215 /* Called via elf_link_hash_traverse to transfer dynamic linking
   6216    information on function code symbol entries to their corresponding
   6217    function descriptor symbol entries.  */
   6218 
   6219 static bfd_boolean
   6220 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
   6221 {
   6222   struct bfd_link_info *info;
   6223   struct ppc_link_hash_table *htab;
   6224   struct plt_entry *ent;
   6225   struct ppc_link_hash_entry *fh;
   6226   struct ppc_link_hash_entry *fdh;
   6227   bfd_boolean force_local;
   6228 
   6229   fh = (struct ppc_link_hash_entry *) h;
   6230   if (fh->elf.root.type == bfd_link_hash_indirect)
   6231     return TRUE;
   6232 
   6233   if (fh->elf.root.type == bfd_link_hash_warning)
   6234     fh = (struct ppc_link_hash_entry *) fh->elf.root.u.i.link;
   6235 
   6236   info = inf;
   6237   htab = ppc_hash_table (info);
   6238   if (htab == NULL)
   6239     return FALSE;
   6240 
   6241   /* Resolve undefined references to dot-symbols as the value
   6242      in the function descriptor, if we have one in a regular object.
   6243      This is to satisfy cases like ".quad .foo".  Calls to functions
   6244      in dynamic objects are handled elsewhere.  */
   6245   if (fh->elf.root.type == bfd_link_hash_undefweak
   6246       && fh->was_undefined
   6247       && (fdh = defined_func_desc (fh)) != NULL
   6248       && get_opd_info (fdh->elf.root.u.def.section) != NULL
   6249       && opd_entry_value (fdh->elf.root.u.def.section,
   6250 			  fdh->elf.root.u.def.value,
   6251 			  &fh->elf.root.u.def.section,
   6252 			  &fh->elf.root.u.def.value) != (bfd_vma) -1)
   6253     {
   6254       fh->elf.root.type = fdh->elf.root.type;
   6255       fh->elf.forced_local = 1;
   6256       fh->elf.def_regular = fdh->elf.def_regular;
   6257       fh->elf.def_dynamic = fdh->elf.def_dynamic;
   6258     }
   6259 
   6260   /* If this is a function code symbol, transfer dynamic linking
   6261      information to the function descriptor symbol.  */
   6262   if (!fh->is_func)
   6263     return TRUE;
   6264 
   6265   for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
   6266     if (ent->plt.refcount > 0)
   6267       break;
   6268   if (ent == NULL
   6269       || fh->elf.root.root.string[0] != '.'
   6270       || fh->elf.root.root.string[1] == '\0')
   6271     return TRUE;
   6272 
   6273   /* Find the corresponding function descriptor symbol.  Create it
   6274      as undefined if necessary.  */
   6275 
   6276   fdh = lookup_fdh (fh, htab);
   6277   if (fdh == NULL
   6278       && !info->executable
   6279       && (fh->elf.root.type == bfd_link_hash_undefined
   6280 	  || fh->elf.root.type == bfd_link_hash_undefweak))
   6281     {
   6282       fdh = make_fdh (info, fh);
   6283       if (fdh == NULL)
   6284 	return FALSE;
   6285     }
   6286 
   6287   /* Fake function descriptors are made undefweak.  If the function
   6288      code symbol is strong undefined, make the fake sym the same.
   6289      If the function code symbol is defined, then force the fake
   6290      descriptor local;  We can't support overriding of symbols in a
   6291      shared library on a fake descriptor.  */
   6292 
   6293   if (fdh != NULL
   6294       && fdh->fake
   6295       && fdh->elf.root.type == bfd_link_hash_undefweak)
   6296     {
   6297       if (fh->elf.root.type == bfd_link_hash_undefined)
   6298 	{
   6299 	  fdh->elf.root.type = bfd_link_hash_undefined;
   6300 	  bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
   6301 	}
   6302       else if (fh->elf.root.type == bfd_link_hash_defined
   6303 	       || fh->elf.root.type == bfd_link_hash_defweak)
   6304 	{
   6305 	  _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
   6306 	}
   6307     }
   6308 
   6309   if (fdh != NULL
   6310       && !fdh->elf.forced_local
   6311       && (!info->executable
   6312 	  || fdh->elf.def_dynamic
   6313 	  || fdh->elf.ref_dynamic
   6314 	  || (fdh->elf.root.type == bfd_link_hash_undefweak
   6315 	      && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
   6316     {
   6317       if (fdh->elf.dynindx == -1)
   6318 	if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
   6319 	  return FALSE;
   6320       fdh->elf.ref_regular |= fh->elf.ref_regular;
   6321       fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
   6322       fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
   6323       fdh->elf.non_got_ref |= fh->elf.non_got_ref;
   6324       if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
   6325 	{
   6326 	  move_plt_plist (fh, fdh);
   6327 	  fdh->elf.needs_plt = 1;
   6328 	}
   6329       fdh->is_func_descriptor = 1;
   6330       fdh->oh = fh;
   6331       fh->oh = fdh;
   6332     }
   6333 
   6334   /* Now that the info is on the function descriptor, clear the
   6335      function code sym info.  Any function code syms for which we
   6336      don't have a definition in a regular file, we force local.
   6337      This prevents a shared library from exporting syms that have
   6338      been imported from another library.  Function code syms that
   6339      are really in the library we must leave global to prevent the
   6340      linker dragging in a definition from a static library.  */
   6341   force_local = (!fh->elf.def_regular
   6342 		 || fdh == NULL
   6343 		 || !fdh->elf.def_regular
   6344 		 || fdh->elf.forced_local);
   6345   _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
   6346 
   6347   return TRUE;
   6348 }
   6349 
   6350 /* Called near the start of bfd_elf_size_dynamic_sections.  We use
   6351    this hook to a) provide some gcc support functions, and b) transfer
   6352    dynamic linking information gathered so far on function code symbol
   6353    entries, to their corresponding function descriptor symbol entries.  */
   6354 
   6355 static bfd_boolean
   6356 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
   6357 			    struct bfd_link_info *info)
   6358 {
   6359   struct ppc_link_hash_table *htab;
   6360   unsigned int i;
   6361   const struct sfpr_def_parms funcs[] =
   6362     {
   6363       { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
   6364       { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
   6365       { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
   6366       { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
   6367       { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
   6368       { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
   6369       { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
   6370       { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
   6371       { "._savef", 14, 31, savefpr, savefpr1_tail },
   6372       { "._restf", 14, 31, restfpr, restfpr1_tail },
   6373       { "_savevr_", 20, 31, savevr, savevr_tail },
   6374       { "_restvr_", 20, 31, restvr, restvr_tail }
   6375     };
   6376 
   6377   htab = ppc_hash_table (info);
   6378   if (htab == NULL)
   6379     return FALSE;
   6380 
   6381   if (htab->sfpr == NULL)
   6382     /* We don't have any relocs.  */
   6383     return TRUE;
   6384 
   6385   /* Provide any missing _save* and _rest* functions.  */
   6386   htab->sfpr->size = 0;
   6387   for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
   6388     if (!sfpr_define (info, &funcs[i]))
   6389       return FALSE;
   6390 
   6391   elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
   6392 
   6393   if (htab->sfpr->size == 0)
   6394     htab->sfpr->flags |= SEC_EXCLUDE;
   6395 
   6396   return TRUE;
   6397 }
   6398 
   6399 /* Adjust a symbol defined by a dynamic object and referenced by a
   6400    regular object.  The current definition is in some section of the
   6401    dynamic object, but we're not including those sections.  We have to
   6402    change the definition to something the rest of the link can
   6403    understand.  */
   6404 
   6405 static bfd_boolean
   6406 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
   6407 				 struct elf_link_hash_entry *h)
   6408 {
   6409   struct ppc_link_hash_table *htab;
   6410   asection *s;
   6411 
   6412   htab = ppc_hash_table (info);
   6413   if (htab == NULL)
   6414     return FALSE;
   6415 
   6416   /* Deal with function syms.  */
   6417   if (h->type == STT_FUNC
   6418       || h->type == STT_GNU_IFUNC
   6419       || h->needs_plt)
   6420     {
   6421       /* Clear procedure linkage table information for any symbol that
   6422 	 won't need a .plt entry.  */
   6423       struct plt_entry *ent;
   6424       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
   6425 	if (ent->plt.refcount > 0)
   6426 	  break;
   6427       if (ent == NULL
   6428 	  || (h->type != STT_GNU_IFUNC
   6429 	      && (SYMBOL_CALLS_LOCAL (info, h)
   6430 		  || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
   6431 		      && h->root.type == bfd_link_hash_undefweak))))
   6432 	{
   6433 	  h->plt.plist = NULL;
   6434 	  h->needs_plt = 0;
   6435 	}
   6436     }
   6437   else
   6438     h->plt.plist = NULL;
   6439 
   6440   /* If this is a weak symbol, and there is a real definition, the
   6441      processor independent code will have arranged for us to see the
   6442      real definition first, and we can just use the same value.  */
   6443   if (h->u.weakdef != NULL)
   6444     {
   6445       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
   6446 		  || h->u.weakdef->root.type == bfd_link_hash_defweak);
   6447       h->root.u.def.section = h->u.weakdef->root.u.def.section;
   6448       h->root.u.def.value = h->u.weakdef->root.u.def.value;
   6449       if (ELIMINATE_COPY_RELOCS)
   6450 	h->non_got_ref = h->u.weakdef->non_got_ref;
   6451       return TRUE;
   6452     }
   6453 
   6454   /* If we are creating a shared library, we must presume that the
   6455      only references to the symbol are via the global offset table.
   6456      For such cases we need not do anything here; the relocations will
   6457      be handled correctly by relocate_section.  */
   6458   if (info->shared)
   6459     return TRUE;
   6460 
   6461   /* If there are no references to this symbol that do not use the
   6462      GOT, we don't need to generate a copy reloc.  */
   6463   if (!h->non_got_ref)
   6464     return TRUE;
   6465 
   6466   /* Don't generate a copy reloc for symbols defined in the executable.  */
   6467   if (!h->def_dynamic || !h->ref_regular || h->def_regular)
   6468     return TRUE;
   6469 
   6470   if (ELIMINATE_COPY_RELOCS)
   6471     {
   6472       struct ppc_link_hash_entry * eh;
   6473       struct ppc_dyn_relocs *p;
   6474 
   6475       eh = (struct ppc_link_hash_entry *) h;
   6476       for (p = eh->dyn_relocs; p != NULL; p = p->next)
   6477 	{
   6478 	  s = p->sec->output_section;
   6479 	  if (s != NULL && (s->flags & SEC_READONLY) != 0)
   6480 	    break;
   6481 	}
   6482 
   6483       /* If we didn't find any dynamic relocs in read-only sections, then
   6484 	 we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
   6485       if (p == NULL)
   6486 	{
   6487 	  h->non_got_ref = 0;
   6488 	  return TRUE;
   6489 	}
   6490     }
   6491 
   6492   if (h->plt.plist != NULL)
   6493     {
   6494       /* We should never get here, but unfortunately there are versions
   6495 	 of gcc out there that improperly (for this ABI) put initialized
   6496 	 function pointers, vtable refs and suchlike in read-only
   6497 	 sections.  Allow them to proceed, but warn that this might
   6498 	 break at runtime.  */
   6499       (*_bfd_error_handler)
   6500 	(_("copy reloc against `%s' requires lazy plt linking; "
   6501 	   "avoid setting LD_BIND_NOW=1 or upgrade gcc"),
   6502 	 h->root.root.string);
   6503     }
   6504 
   6505   /* This is a reference to a symbol defined by a dynamic object which
   6506      is not a function.  */
   6507 
   6508   if (h->size == 0)
   6509     {
   6510       (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
   6511 			     h->root.root.string);
   6512       return TRUE;
   6513     }
   6514 
   6515   /* We must allocate the symbol in our .dynbss section, which will
   6516      become part of the .bss section of the executable.  There will be
   6517      an entry for this symbol in the .dynsym section.  The dynamic
   6518      object will contain position independent code, so all references
   6519      from the dynamic object to this symbol will go through the global
   6520      offset table.  The dynamic linker will use the .dynsym entry to
   6521      determine the address it must put in the global offset table, so
   6522      both the dynamic object and the regular object will refer to the
   6523      same memory location for the variable.  */
   6524 
   6525   /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
   6526      to copy the initial value out of the dynamic object and into the
   6527      runtime process image.  We need to remember the offset into the
   6528      .rela.bss section we are going to use.  */
   6529   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
   6530     {
   6531       htab->relbss->size += sizeof (Elf64_External_Rela);
   6532       h->needs_copy = 1;
   6533     }
   6534 
   6535   s = htab->dynbss;
   6536 
   6537   return _bfd_elf_adjust_dynamic_copy (h, s);
   6538 }
   6539 
   6540 /* If given a function descriptor symbol, hide both the function code
   6541    sym and the descriptor.  */
   6542 static void
   6543 ppc64_elf_hide_symbol (struct bfd_link_info *info,
   6544 		       struct elf_link_hash_entry *h,
   6545 		       bfd_boolean force_local)
   6546 {
   6547   struct ppc_link_hash_entry *eh;
   6548   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
   6549 
   6550   eh = (struct ppc_link_hash_entry *) h;
   6551   if (eh->is_func_descriptor)
   6552     {
   6553       struct ppc_link_hash_entry *fh = eh->oh;
   6554 
   6555       if (fh == NULL)
   6556 	{
   6557 	  const char *p, *q;
   6558 	  struct ppc_link_hash_table *htab;
   6559 	  char save;
   6560 
   6561 	  /* We aren't supposed to use alloca in BFD because on
   6562 	     systems which do not have alloca the version in libiberty
   6563 	     calls xmalloc, which might cause the program to crash
   6564 	     when it runs out of memory.  This function doesn't have a
   6565 	     return status, so there's no way to gracefully return an
   6566 	     error.  So cheat.  We know that string[-1] can be safely
   6567 	     accessed;  It's either a string in an ELF string table,
   6568 	     or allocated in an objalloc structure.  */
   6569 
   6570 	  p = eh->elf.root.root.string - 1;
   6571 	  save = *p;
   6572 	  *(char *) p = '.';
   6573 	  htab = ppc_hash_table (info);
   6574 	  if (htab == NULL)
   6575 	    return;
   6576 
   6577 	  fh = (struct ppc_link_hash_entry *)
   6578 	    elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
   6579 	  *(char *) p = save;
   6580 
   6581 	  /* Unfortunately, if it so happens that the string we were
   6582 	     looking for was allocated immediately before this string,
   6583 	     then we overwrote the string terminator.  That's the only
   6584 	     reason the lookup should fail.  */
   6585 	  if (fh == NULL)
   6586 	    {
   6587 	      q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
   6588 	      while (q >= eh->elf.root.root.string && *q == *p)
   6589 		--q, --p;
   6590 	      if (q < eh->elf.root.root.string && *p == '.')
   6591 		fh = (struct ppc_link_hash_entry *)
   6592 		  elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
   6593 	    }
   6594 	  if (fh != NULL)
   6595 	    {
   6596 	      eh->oh = fh;
   6597 	      fh->oh = eh;
   6598 	    }
   6599 	}
   6600       if (fh != NULL)
   6601 	_bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
   6602     }
   6603 }
   6604 
   6605 static bfd_boolean
   6606 get_sym_h (struct elf_link_hash_entry **hp,
   6607 	   Elf_Internal_Sym **symp,
   6608 	   asection **symsecp,
   6609 	   unsigned char **tls_maskp,
   6610 	   Elf_Internal_Sym **locsymsp,
   6611 	   unsigned long r_symndx,
   6612 	   bfd *ibfd)
   6613 {
   6614   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
   6615 
   6616   if (r_symndx >= symtab_hdr->sh_info)
   6617     {
   6618       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
   6619       struct elf_link_hash_entry *h;
   6620 
   6621       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   6622       h = elf_follow_link (h);
   6623 
   6624       if (hp != NULL)
   6625 	*hp = h;
   6626 
   6627       if (symp != NULL)
   6628 	*symp = NULL;
   6629 
   6630       if (symsecp != NULL)
   6631 	{
   6632 	  asection *symsec = NULL;
   6633 	  if (h->root.type == bfd_link_hash_defined
   6634 	      || h->root.type == bfd_link_hash_defweak)
   6635 	    symsec = h->root.u.def.section;
   6636 	  *symsecp = symsec;
   6637 	}
   6638 
   6639       if (tls_maskp != NULL)
   6640 	{
   6641 	  struct ppc_link_hash_entry *eh;
   6642 
   6643 	  eh = (struct ppc_link_hash_entry *) h;
   6644 	  *tls_maskp = &eh->tls_mask;
   6645 	}
   6646     }
   6647   else
   6648     {
   6649       Elf_Internal_Sym *sym;
   6650       Elf_Internal_Sym *locsyms = *locsymsp;
   6651 
   6652       if (locsyms == NULL)
   6653 	{
   6654 	  locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
   6655 	  if (locsyms == NULL)
   6656 	    locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
   6657 					    symtab_hdr->sh_info,
   6658 					    0, NULL, NULL, NULL);
   6659 	  if (locsyms == NULL)
   6660 	    return FALSE;
   6661 	  *locsymsp = locsyms;
   6662 	}
   6663       sym = locsyms + r_symndx;
   6664 
   6665       if (hp != NULL)
   6666 	*hp = NULL;
   6667 
   6668       if (symp != NULL)
   6669 	*symp = sym;
   6670 
   6671       if (symsecp != NULL)
   6672 	*symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
   6673 
   6674       if (tls_maskp != NULL)
   6675 	{
   6676 	  struct got_entry **lgot_ents;
   6677 	  unsigned char *tls_mask;
   6678 
   6679 	  tls_mask = NULL;
   6680 	  lgot_ents = elf_local_got_ents (ibfd);
   6681 	  if (lgot_ents != NULL)
   6682 	    {
   6683 	      struct plt_entry **local_plt = (struct plt_entry **)
   6684 		(lgot_ents + symtab_hdr->sh_info);
   6685 	      unsigned char *lgot_masks = (unsigned char *)
   6686 		(local_plt + symtab_hdr->sh_info);
   6687 	      tls_mask = &lgot_masks[r_symndx];
   6688 	    }
   6689 	  *tls_maskp = tls_mask;
   6690 	}
   6691     }
   6692   return TRUE;
   6693 }
   6694 
   6695 /* Returns TLS_MASKP for the given REL symbol.  Function return is 0 on
   6696    error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
   6697    type suitable for optimization, and 1 otherwise.  */
   6698 
   6699 static int
   6700 get_tls_mask (unsigned char **tls_maskp,
   6701 	      unsigned long *toc_symndx,
   6702 	      bfd_vma *toc_addend,
   6703 	      Elf_Internal_Sym **locsymsp,
   6704 	      const Elf_Internal_Rela *rel,
   6705 	      bfd *ibfd)
   6706 {
   6707   unsigned long r_symndx;
   6708   int next_r;
   6709   struct elf_link_hash_entry *h;
   6710   Elf_Internal_Sym *sym;
   6711   asection *sec;
   6712   bfd_vma off;
   6713 
   6714   r_symndx = ELF64_R_SYM (rel->r_info);
   6715   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
   6716     return 0;
   6717 
   6718   if ((*tls_maskp != NULL && **tls_maskp != 0)
   6719       || sec == NULL
   6720       || ppc64_elf_section_data (sec) == NULL
   6721       || ppc64_elf_section_data (sec)->sec_type != sec_toc)
   6722     return 1;
   6723 
   6724   /* Look inside a TOC section too.  */
   6725   if (h != NULL)
   6726     {
   6727       BFD_ASSERT (h->root.type == bfd_link_hash_defined);
   6728       off = h->root.u.def.value;
   6729     }
   6730   else
   6731     off = sym->st_value;
   6732   off += rel->r_addend;
   6733   BFD_ASSERT (off % 8 == 0);
   6734   r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
   6735   next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
   6736   if (toc_symndx != NULL)
   6737     *toc_symndx = r_symndx;
   6738   if (toc_addend != NULL)
   6739     *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
   6740   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
   6741     return 0;
   6742   if ((h == NULL || is_static_defined (h))
   6743       && (next_r == -1 || next_r == -2))
   6744     return 1 - next_r;
   6745   return 1;
   6746 }
   6747 
   6748 /* Adjust all global syms defined in opd sections.  In gcc generated
   6749    code for the old ABI, these will already have been done.  */
   6750 
   6751 static bfd_boolean
   6752 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
   6753 {
   6754   struct ppc_link_hash_entry *eh;
   6755   asection *sym_sec;
   6756   struct _opd_sec_data *opd;
   6757 
   6758   if (h->root.type == bfd_link_hash_indirect)
   6759     return TRUE;
   6760 
   6761   if (h->root.type == bfd_link_hash_warning)
   6762     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   6763 
   6764   if (h->root.type != bfd_link_hash_defined
   6765       && h->root.type != bfd_link_hash_defweak)
   6766     return TRUE;
   6767 
   6768   eh = (struct ppc_link_hash_entry *) h;
   6769   if (eh->adjust_done)
   6770     return TRUE;
   6771 
   6772   sym_sec = eh->elf.root.u.def.section;
   6773   opd = get_opd_info (sym_sec);
   6774   if (opd != NULL && opd->adjust != NULL)
   6775     {
   6776       long adjust = opd->adjust[eh->elf.root.u.def.value / 8];
   6777       if (adjust == -1)
   6778 	{
   6779 	  /* This entry has been deleted.  */
   6780 	  asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
   6781 	  if (dsec == NULL)
   6782 	    {
   6783 	      for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
   6784 		if (elf_discarded_section (dsec))
   6785 		  {
   6786 		    ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
   6787 		    break;
   6788 		  }
   6789 	    }
   6790 	  eh->elf.root.u.def.value = 0;
   6791 	  eh->elf.root.u.def.section = dsec;
   6792 	}
   6793       else
   6794 	eh->elf.root.u.def.value += adjust;
   6795       eh->adjust_done = 1;
   6796     }
   6797   return TRUE;
   6798 }
   6799 
   6800 /* Handles decrementing dynamic reloc counts for the reloc specified by
   6801    R_INFO in section SEC.  If LOCAL_SYMS is NULL, then H and SYM_SEC
   6802    have already been determined.  */
   6803 
   6804 static bfd_boolean
   6805 dec_dynrel_count (bfd_vma r_info,
   6806 		  asection *sec,
   6807 		  struct bfd_link_info *info,
   6808 		  Elf_Internal_Sym **local_syms,
   6809 		  struct elf_link_hash_entry *h,
   6810 		  asection *sym_sec)
   6811 {
   6812   enum elf_ppc64_reloc_type r_type;
   6813   struct ppc_dyn_relocs *p;
   6814   struct ppc_dyn_relocs **pp;
   6815 
   6816   /* Can this reloc be dynamic?  This switch, and later tests here
   6817      should be kept in sync with the code in check_relocs.  */
   6818   r_type = ELF64_R_TYPE (r_info);
   6819   switch (r_type)
   6820     {
   6821     default:
   6822       return TRUE;
   6823 
   6824     case R_PPC64_TPREL16:
   6825     case R_PPC64_TPREL16_LO:
   6826     case R_PPC64_TPREL16_HI:
   6827     case R_PPC64_TPREL16_HA:
   6828     case R_PPC64_TPREL16_DS:
   6829     case R_PPC64_TPREL16_LO_DS:
   6830     case R_PPC64_TPREL16_HIGHER:
   6831     case R_PPC64_TPREL16_HIGHERA:
   6832     case R_PPC64_TPREL16_HIGHEST:
   6833     case R_PPC64_TPREL16_HIGHESTA:
   6834       if (!info->shared)
   6835 	return TRUE;
   6836 
   6837     case R_PPC64_TPREL64:
   6838     case R_PPC64_DTPMOD64:
   6839     case R_PPC64_DTPREL64:
   6840     case R_PPC64_ADDR64:
   6841     case R_PPC64_REL30:
   6842     case R_PPC64_REL32:
   6843     case R_PPC64_REL64:
   6844     case R_PPC64_ADDR14:
   6845     case R_PPC64_ADDR14_BRNTAKEN:
   6846     case R_PPC64_ADDR14_BRTAKEN:
   6847     case R_PPC64_ADDR16:
   6848     case R_PPC64_ADDR16_DS:
   6849     case R_PPC64_ADDR16_HA:
   6850     case R_PPC64_ADDR16_HI:
   6851     case R_PPC64_ADDR16_HIGHER:
   6852     case R_PPC64_ADDR16_HIGHERA:
   6853     case R_PPC64_ADDR16_HIGHEST:
   6854     case R_PPC64_ADDR16_HIGHESTA:
   6855     case R_PPC64_ADDR16_LO:
   6856     case R_PPC64_ADDR16_LO_DS:
   6857     case R_PPC64_ADDR24:
   6858     case R_PPC64_ADDR32:
   6859     case R_PPC64_UADDR16:
   6860     case R_PPC64_UADDR32:
   6861     case R_PPC64_UADDR64:
   6862     case R_PPC64_TOC:
   6863       break;
   6864     }
   6865 
   6866   if (local_syms != NULL)
   6867     {
   6868       unsigned long r_symndx;
   6869       Elf_Internal_Sym *sym;
   6870       bfd *ibfd = sec->owner;
   6871 
   6872       r_symndx = ELF64_R_SYM (r_info);
   6873       if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
   6874 	return FALSE;
   6875     }
   6876 
   6877   if ((info->shared
   6878        && (must_be_dyn_reloc (info, r_type)
   6879 	   || (h != NULL
   6880 	       && (!info->symbolic
   6881 		   || h->root.type == bfd_link_hash_defweak
   6882 		   || !h->def_regular))))
   6883       || (ELIMINATE_COPY_RELOCS
   6884 	  && !info->shared
   6885 	  && h != NULL
   6886 	  && (h->root.type == bfd_link_hash_defweak
   6887 	      || !h->def_regular)))
   6888     ;
   6889   else
   6890     return TRUE;
   6891 
   6892   if (h != NULL)
   6893     pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
   6894   else
   6895     {
   6896       if (sym_sec != NULL)
   6897 	{
   6898 	  void *vpp = &elf_section_data (sym_sec)->local_dynrel;
   6899 	  pp = (struct ppc_dyn_relocs **) vpp;
   6900 	}
   6901       else
   6902 	{
   6903 	  void *vpp = &elf_section_data (sec)->local_dynrel;
   6904 	  pp = (struct ppc_dyn_relocs **) vpp;
   6905 	}
   6906 
   6907       /* elf_gc_sweep may have already removed all dyn relocs associated
   6908 	 with local syms for a given section.  Don't report a dynreloc
   6909 	 miscount.  */
   6910       if (*pp == NULL)
   6911 	return TRUE;
   6912     }
   6913 
   6914   while ((p = *pp) != NULL)
   6915     {
   6916       if (p->sec == sec)
   6917 	{
   6918 	  if (!must_be_dyn_reloc (info, r_type))
   6919 	    p->pc_count -= 1;
   6920 	  p->count -= 1;
   6921 	  if (p->count == 0)
   6922 	    *pp = p->next;
   6923 	  return TRUE;
   6924 	}
   6925       pp = &p->next;
   6926     }
   6927 
   6928   (*_bfd_error_handler) (_("dynreloc miscount for %B, section %A"),
   6929 			   sec->owner, sec);
   6930   bfd_set_error (bfd_error_bad_value);
   6931   return FALSE;
   6932 }
   6933 
   6934 /* Remove unused Official Procedure Descriptor entries.  Currently we
   6935    only remove those associated with functions in discarded link-once
   6936    sections, or weakly defined functions that have been overridden.  It
   6937    would be possible to remove many more entries for statically linked
   6938    applications.  */
   6939 
   6940 bfd_boolean
   6941 ppc64_elf_edit_opd (struct bfd_link_info *info, bfd_boolean non_overlapping)
   6942 {
   6943   bfd *ibfd;
   6944   bfd_boolean some_edited = FALSE;
   6945   asection *need_pad = NULL;
   6946 
   6947   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   6948     {
   6949       asection *sec;
   6950       Elf_Internal_Rela *relstart, *rel, *relend;
   6951       Elf_Internal_Shdr *symtab_hdr;
   6952       Elf_Internal_Sym *local_syms;
   6953       bfd_vma offset;
   6954       struct _opd_sec_data *opd;
   6955       bfd_boolean need_edit, add_aux_fields;
   6956       bfd_size_type cnt_16b = 0;
   6957 
   6958       if (!is_ppc64_elf (ibfd))
   6959 	continue;
   6960 
   6961       sec = bfd_get_section_by_name (ibfd, ".opd");
   6962       if (sec == NULL || sec->size == 0)
   6963 	continue;
   6964 
   6965       if (sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
   6966 	continue;
   6967 
   6968       if (sec->output_section == bfd_abs_section_ptr)
   6969 	continue;
   6970 
   6971       /* Look through the section relocs.  */
   6972       if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
   6973 	continue;
   6974 
   6975       local_syms = NULL;
   6976       symtab_hdr = &elf_symtab_hdr (ibfd);
   6977 
   6978       /* Read the relocations.  */
   6979       relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   6980 					    info->keep_memory);
   6981       if (relstart == NULL)
   6982 	return FALSE;
   6983 
   6984       /* First run through the relocs to check they are sane, and to
   6985 	 determine whether we need to edit this opd section.  */
   6986       need_edit = FALSE;
   6987       need_pad = sec;
   6988       offset = 0;
   6989       relend = relstart + sec->reloc_count;
   6990       for (rel = relstart; rel < relend; )
   6991 	{
   6992 	  enum elf_ppc64_reloc_type r_type;
   6993 	  unsigned long r_symndx;
   6994 	  asection *sym_sec;
   6995 	  struct elf_link_hash_entry *h;
   6996 	  Elf_Internal_Sym *sym;
   6997 
   6998 	  /* .opd contains a regular array of 16 or 24 byte entries.  We're
   6999 	     only interested in the reloc pointing to a function entry
   7000 	     point.  */
   7001 	  if (rel->r_offset != offset
   7002 	      || rel + 1 >= relend
   7003 	      || (rel + 1)->r_offset != offset + 8)
   7004 	    {
   7005 	      /* If someone messes with .opd alignment then after a
   7006 		 "ld -r" we might have padding in the middle of .opd.
   7007 		 Also, there's nothing to prevent someone putting
   7008 		 something silly in .opd with the assembler.  No .opd
   7009 		 optimization for them!  */
   7010 	    broken_opd:
   7011 	      (*_bfd_error_handler)
   7012 		(_("%B: .opd is not a regular array of opd entries"), ibfd);
   7013 	      need_edit = FALSE;
   7014 	      break;
   7015 	    }
   7016 
   7017 	  if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
   7018 	      || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
   7019 	    {
   7020 	      (*_bfd_error_handler)
   7021 		(_("%B: unexpected reloc type %u in .opd section"),
   7022 		 ibfd, r_type);
   7023 	      need_edit = FALSE;
   7024 	      break;
   7025 	    }
   7026 
   7027 	  r_symndx = ELF64_R_SYM (rel->r_info);
   7028 	  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   7029 			  r_symndx, ibfd))
   7030 	    goto error_ret;
   7031 
   7032 	  if (sym_sec == NULL || sym_sec->owner == NULL)
   7033 	    {
   7034 	      const char *sym_name;
   7035 	      if (h != NULL)
   7036 		sym_name = h->root.root.string;
   7037 	      else
   7038 		sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
   7039 					     sym_sec);
   7040 
   7041 	      (*_bfd_error_handler)
   7042 		(_("%B: undefined sym `%s' in .opd section"),
   7043 		 ibfd, sym_name);
   7044 	      need_edit = FALSE;
   7045 	      break;
   7046 	    }
   7047 
   7048 	  /* opd entries are always for functions defined in the
   7049 	     current input bfd.  If the symbol isn't defined in the
   7050 	     input bfd, then we won't be using the function in this
   7051 	     bfd;  It must be defined in a linkonce section in another
   7052 	     bfd, or is weak.  It's also possible that we are
   7053 	     discarding the function due to a linker script /DISCARD/,
   7054 	     which we test for via the output_section.  */
   7055 	  if (sym_sec->owner != ibfd
   7056 	      || sym_sec->output_section == bfd_abs_section_ptr)
   7057 	    need_edit = TRUE;
   7058 
   7059 	  rel += 2;
   7060 	  if (rel == relend
   7061 	      || (rel + 1 == relend && rel->r_offset == offset + 16))
   7062 	    {
   7063 	      if (sec->size == offset + 24)
   7064 		{
   7065 		  need_pad = NULL;
   7066 		  break;
   7067 		}
   7068 	      if (rel == relend && sec->size == offset + 16)
   7069 		{
   7070 		  cnt_16b++;
   7071 		  break;
   7072 		}
   7073 	      goto broken_opd;
   7074 	    }
   7075 
   7076 	  if (rel->r_offset == offset + 24)
   7077 	    offset += 24;
   7078 	  else if (rel->r_offset != offset + 16)
   7079 	    goto broken_opd;
   7080 	  else if (rel + 1 < relend
   7081 		   && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
   7082 		   && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
   7083 	    {
   7084 	      offset += 16;
   7085 	      cnt_16b++;
   7086 	    }
   7087 	  else if (rel + 2 < relend
   7088 		   && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
   7089 		   && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
   7090 	    {
   7091 	      offset += 24;
   7092 	      rel += 1;
   7093 	    }
   7094 	  else
   7095 	    goto broken_opd;
   7096 	}
   7097 
   7098       add_aux_fields = non_overlapping && cnt_16b > 0;
   7099 
   7100       if (need_edit || add_aux_fields)
   7101 	{
   7102 	  Elf_Internal_Rela *write_rel;
   7103 	  Elf_Internal_Shdr *rel_hdr;
   7104 	  bfd_byte *rptr, *wptr;
   7105 	  bfd_byte *new_contents;
   7106 	  bfd_boolean skip;
   7107 	  long opd_ent_size;
   7108 	  bfd_size_type amt;
   7109 
   7110 	  new_contents = NULL;
   7111 	  amt = sec->size * sizeof (long) / 8;
   7112 	  opd = &ppc64_elf_section_data (sec)->u.opd;
   7113 	  opd->adjust = bfd_zalloc (sec->owner, amt);
   7114 	  if (opd->adjust == NULL)
   7115 	    return FALSE;
   7116 	  ppc64_elf_section_data (sec)->sec_type = sec_opd;
   7117 
   7118 	  /* This seems a waste of time as input .opd sections are all
   7119 	     zeros as generated by gcc, but I suppose there's no reason
   7120 	     this will always be so.  We might start putting something in
   7121 	     the third word of .opd entries.  */
   7122 	  if ((sec->flags & SEC_IN_MEMORY) == 0)
   7123 	    {
   7124 	      bfd_byte *loc;
   7125 	      if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
   7126 		{
   7127 		  if (loc != NULL)
   7128 		    free (loc);
   7129 		error_ret:
   7130 		  if (local_syms != NULL
   7131 		      && symtab_hdr->contents != (unsigned char *) local_syms)
   7132 		    free (local_syms);
   7133 		  if (elf_section_data (sec)->relocs != relstart)
   7134 		    free (relstart);
   7135 		  return FALSE;
   7136 		}
   7137 	      sec->contents = loc;
   7138 	      sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
   7139 	    }
   7140 
   7141 	  elf_section_data (sec)->relocs = relstart;
   7142 
   7143 	  new_contents = sec->contents;
   7144 	  if (add_aux_fields)
   7145 	    {
   7146 	      new_contents = bfd_malloc (sec->size + cnt_16b * 8);
   7147 	      if (new_contents == NULL)
   7148 		return FALSE;
   7149 	      need_pad = FALSE;
   7150 	    }
   7151 	  wptr = new_contents;
   7152 	  rptr = sec->contents;
   7153 
   7154 	  write_rel = relstart;
   7155 	  skip = FALSE;
   7156 	  offset = 0;
   7157 	  opd_ent_size = 0;
   7158 	  for (rel = relstart; rel < relend; rel++)
   7159 	    {
   7160 	      unsigned long r_symndx;
   7161 	      asection *sym_sec;
   7162 	      struct elf_link_hash_entry *h;
   7163 	      Elf_Internal_Sym *sym;
   7164 
   7165 	      r_symndx = ELF64_R_SYM (rel->r_info);
   7166 	      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   7167 			      r_symndx, ibfd))
   7168 		goto error_ret;
   7169 
   7170 	      if (rel->r_offset == offset)
   7171 		{
   7172 		  struct ppc_link_hash_entry *fdh = NULL;
   7173 
   7174 		  /* See if the .opd entry is full 24 byte or
   7175 		     16 byte (with fd_aux entry overlapped with next
   7176 		     fd_func).  */
   7177 		  opd_ent_size = 24;
   7178 		  if ((rel + 2 == relend && sec->size == offset + 16)
   7179 		      || (rel + 3 < relend
   7180 			  && rel[2].r_offset == offset + 16
   7181 			  && rel[3].r_offset == offset + 24
   7182 			  && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
   7183 			  && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
   7184 		    opd_ent_size = 16;
   7185 
   7186 		  if (h != NULL
   7187 		      && h->root.root.string[0] == '.')
   7188 		    {
   7189 		      struct ppc_link_hash_table *htab;
   7190 
   7191 		      htab = ppc_hash_table (info);
   7192 		      if (htab != NULL)
   7193 			fdh = lookup_fdh ((struct ppc_link_hash_entry *) h,
   7194 					  htab);
   7195 		      if (fdh != NULL
   7196 			  && fdh->elf.root.type != bfd_link_hash_defined
   7197 			  && fdh->elf.root.type != bfd_link_hash_defweak)
   7198 			fdh = NULL;
   7199 		    }
   7200 
   7201 		  skip = (sym_sec->owner != ibfd
   7202 			  || sym_sec->output_section == bfd_abs_section_ptr);
   7203 		  if (skip)
   7204 		    {
   7205 		      if (fdh != NULL && sym_sec->owner == ibfd)
   7206 			{
   7207 			  /* Arrange for the function descriptor sym
   7208 			     to be dropped.  */
   7209 			  fdh->elf.root.u.def.value = 0;
   7210 			  fdh->elf.root.u.def.section = sym_sec;
   7211 			}
   7212 		      opd->adjust[rel->r_offset / 8] = -1;
   7213 		    }
   7214 		  else
   7215 		    {
   7216 		      /* We'll be keeping this opd entry.  */
   7217 
   7218 		      if (fdh != NULL)
   7219 			{
   7220 			  /* Redefine the function descriptor symbol to
   7221 			     this location in the opd section.  It is
   7222 			     necessary to update the value here rather
   7223 			     than using an array of adjustments as we do
   7224 			     for local symbols, because various places
   7225 			     in the generic ELF code use the value
   7226 			     stored in u.def.value.  */
   7227 			  fdh->elf.root.u.def.value = wptr - new_contents;
   7228 			  fdh->adjust_done = 1;
   7229 			}
   7230 
   7231 		      /* Local syms are a bit tricky.  We could
   7232 			 tweak them as they can be cached, but
   7233 			 we'd need to look through the local syms
   7234 			 for the function descriptor sym which we
   7235 			 don't have at the moment.  So keep an
   7236 			 array of adjustments.  */
   7237 		      opd->adjust[rel->r_offset / 8]
   7238 			= (wptr - new_contents) - (rptr - sec->contents);
   7239 
   7240 		      if (wptr != rptr)
   7241 			memcpy (wptr, rptr, opd_ent_size);
   7242 		      wptr += opd_ent_size;
   7243 		      if (add_aux_fields && opd_ent_size == 16)
   7244 			{
   7245 			  memset (wptr, '\0', 8);
   7246 			  wptr += 8;
   7247 			}
   7248 		    }
   7249 		  rptr += opd_ent_size;
   7250 		  offset += opd_ent_size;
   7251 		}
   7252 
   7253 	      if (skip)
   7254 		{
   7255 		  if (!NO_OPD_RELOCS
   7256 		      && !info->relocatable
   7257 		      && !dec_dynrel_count (rel->r_info, sec, info,
   7258 					    NULL, h, sym_sec))
   7259 		    goto error_ret;
   7260 		}
   7261 	      else
   7262 		{
   7263 		  /* We need to adjust any reloc offsets to point to the
   7264 		     new opd entries.  While we're at it, we may as well
   7265 		     remove redundant relocs.  */
   7266 		  rel->r_offset += opd->adjust[(offset - opd_ent_size) / 8];
   7267 		  if (write_rel != rel)
   7268 		    memcpy (write_rel, rel, sizeof (*rel));
   7269 		  ++write_rel;
   7270 		}
   7271 	    }
   7272 
   7273 	  sec->size = wptr - new_contents;
   7274 	  sec->reloc_count = write_rel - relstart;
   7275 	  if (add_aux_fields)
   7276 	    {
   7277 	      free (sec->contents);
   7278 	      sec->contents = new_contents;
   7279 	    }
   7280 
   7281 	  /* Fudge the header size too, as this is used later in
   7282 	     elf_bfd_final_link if we are emitting relocs.  */
   7283 	  rel_hdr = _bfd_elf_single_rel_hdr (sec);
   7284 	  rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
   7285 	  some_edited = TRUE;
   7286 	}
   7287       else if (elf_section_data (sec)->relocs != relstart)
   7288 	free (relstart);
   7289 
   7290       if (local_syms != NULL
   7291 	  && symtab_hdr->contents != (unsigned char *) local_syms)
   7292 	{
   7293 	  if (!info->keep_memory)
   7294 	    free (local_syms);
   7295 	  else
   7296 	    symtab_hdr->contents = (unsigned char *) local_syms;
   7297 	}
   7298     }
   7299 
   7300   if (some_edited)
   7301     elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
   7302 
   7303   /* If we are doing a final link and the last .opd entry is just 16 byte
   7304      long, add a 8 byte padding after it.  */
   7305   if (need_pad != NULL && !info->relocatable)
   7306     {
   7307       bfd_byte *p;
   7308 
   7309       if ((need_pad->flags & SEC_IN_MEMORY) == 0)
   7310 	{
   7311 	  BFD_ASSERT (need_pad->size > 0);
   7312 
   7313 	  p = bfd_malloc (need_pad->size + 8);
   7314 	  if (p == NULL)
   7315 	    return FALSE;
   7316 
   7317 	  if (! bfd_get_section_contents (need_pad->owner, need_pad,
   7318 					  p, 0, need_pad->size))
   7319 	    return FALSE;
   7320 
   7321 	  need_pad->contents = p;
   7322 	  need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
   7323 	}
   7324       else
   7325 	{
   7326 	  p = bfd_realloc (need_pad->contents, need_pad->size + 8);
   7327 	  if (p == NULL)
   7328 	    return FALSE;
   7329 
   7330 	  need_pad->contents = p;
   7331 	}
   7332 
   7333       memset (need_pad->contents + need_pad->size, 0, 8);
   7334       need_pad->size += 8;
   7335     }
   7336 
   7337   return TRUE;
   7338 }
   7339 
   7340 /* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
   7341 
   7342 asection *
   7343 ppc64_elf_tls_setup (struct bfd_link_info *info,
   7344 		     int no_tls_get_addr_opt,
   7345 		     int *no_multi_toc)
   7346 {
   7347   struct ppc_link_hash_table *htab;
   7348 
   7349   htab = ppc_hash_table (info);
   7350   if (htab == NULL)
   7351     return NULL;
   7352 
   7353   if (*no_multi_toc)
   7354     htab->do_multi_toc = 0;
   7355   else if (!htab->do_multi_toc)
   7356     *no_multi_toc = 1;
   7357 
   7358   htab->tls_get_addr = ((struct ppc_link_hash_entry *)
   7359 			elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
   7360 					      FALSE, FALSE, TRUE));
   7361   /* Move dynamic linking info to the function descriptor sym.  */
   7362   if (htab->tls_get_addr != NULL)
   7363     func_desc_adjust (&htab->tls_get_addr->elf, info);
   7364   htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
   7365 			   elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
   7366 						 FALSE, FALSE, TRUE));
   7367   if (!no_tls_get_addr_opt)
   7368     {
   7369       struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
   7370 
   7371       opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
   7372 				  FALSE, FALSE, TRUE);
   7373       if (opt != NULL)
   7374 	func_desc_adjust (opt, info);
   7375       opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
   7376 				     FALSE, FALSE, TRUE);
   7377       if (opt_fd != NULL
   7378 	  && (opt_fd->root.type == bfd_link_hash_defined
   7379 	      || opt_fd->root.type == bfd_link_hash_defweak))
   7380 	{
   7381 	  /* If glibc supports an optimized __tls_get_addr call stub,
   7382 	     signalled by the presence of __tls_get_addr_opt, and we'll
   7383 	     be calling __tls_get_addr via a plt call stub, then
   7384 	     make __tls_get_addr point to __tls_get_addr_opt.  */
   7385 	  tga_fd = &htab->tls_get_addr_fd->elf;
   7386 	  if (htab->elf.dynamic_sections_created
   7387 	      && tga_fd != NULL
   7388 	      && (tga_fd->type == STT_FUNC
   7389 		  || tga_fd->needs_plt)
   7390 	      && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
   7391 		   || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
   7392 		       && tga_fd->root.type == bfd_link_hash_undefweak)))
   7393 	    {
   7394 	      struct plt_entry *ent;
   7395 
   7396 	      for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
   7397 		if (ent->plt.refcount > 0)
   7398 		  break;
   7399 	      if (ent != NULL)
   7400 		{
   7401 		  tga_fd->root.type = bfd_link_hash_indirect;
   7402 		  tga_fd->root.u.i.link = &opt_fd->root;
   7403 		  ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
   7404 		  if (opt_fd->dynindx != -1)
   7405 		    {
   7406 		      /* Use __tls_get_addr_opt in dynamic relocations.  */
   7407 		      opt_fd->dynindx = -1;
   7408 		      _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
   7409 					      opt_fd->dynstr_index);
   7410 		      if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
   7411 			return NULL;
   7412 		    }
   7413 		  htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
   7414 		  tga = &htab->tls_get_addr->elf;
   7415 		  if (opt != NULL && tga != NULL)
   7416 		    {
   7417 		      tga->root.type = bfd_link_hash_indirect;
   7418 		      tga->root.u.i.link = &opt->root;
   7419 		      ppc64_elf_copy_indirect_symbol (info, opt, tga);
   7420 		      _bfd_elf_link_hash_hide_symbol (info, opt,
   7421 						      tga->forced_local);
   7422 		      htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
   7423 		    }
   7424 		  htab->tls_get_addr_fd->oh = htab->tls_get_addr;
   7425 		  htab->tls_get_addr_fd->is_func_descriptor = 1;
   7426 		  if (htab->tls_get_addr != NULL)
   7427 		    {
   7428 		      htab->tls_get_addr->oh = htab->tls_get_addr_fd;
   7429 		      htab->tls_get_addr->is_func = 1;
   7430 		    }
   7431 		}
   7432 	    }
   7433 	}
   7434       else
   7435 	no_tls_get_addr_opt = TRUE;
   7436     }
   7437   htab->no_tls_get_addr_opt = no_tls_get_addr_opt;
   7438   return _bfd_elf_tls_setup (info->output_bfd, info);
   7439 }
   7440 
   7441 /* Return TRUE iff REL is a branch reloc with a global symbol matching
   7442    HASH1 or HASH2.  */
   7443 
   7444 static bfd_boolean
   7445 branch_reloc_hash_match (const bfd *ibfd,
   7446 			 const Elf_Internal_Rela *rel,
   7447 			 const struct ppc_link_hash_entry *hash1,
   7448 			 const struct ppc_link_hash_entry *hash2)
   7449 {
   7450   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
   7451   enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
   7452   unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
   7453 
   7454   if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
   7455     {
   7456       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
   7457       struct elf_link_hash_entry *h;
   7458 
   7459       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   7460       h = elf_follow_link (h);
   7461       if (h == &hash1->elf || h == &hash2->elf)
   7462 	return TRUE;
   7463     }
   7464   return FALSE;
   7465 }
   7466 
   7467 /* Run through all the TLS relocs looking for optimization
   7468    opportunities.  The linker has been hacked (see ppc64elf.em) to do
   7469    a preliminary section layout so that we know the TLS segment
   7470    offsets.  We can't optimize earlier because some optimizations need
   7471    to know the tp offset, and we need to optimize before allocating
   7472    dynamic relocations.  */
   7473 
   7474 bfd_boolean
   7475 ppc64_elf_tls_optimize (struct bfd_link_info *info)
   7476 {
   7477   bfd *ibfd;
   7478   asection *sec;
   7479   struct ppc_link_hash_table *htab;
   7480   unsigned char *toc_ref;
   7481   int pass;
   7482 
   7483   if (info->relocatable || !info->executable)
   7484     return TRUE;
   7485 
   7486   htab = ppc_hash_table (info);
   7487   if (htab == NULL)
   7488     return FALSE;
   7489 
   7490   /* Make two passes over the relocs.  On the first pass, mark toc
   7491      entries involved with tls relocs, and check that tls relocs
   7492      involved in setting up a tls_get_addr call are indeed followed by
   7493      such a call.  If they are not, we can't do any tls optimization.
   7494      On the second pass twiddle tls_mask flags to notify
   7495      relocate_section that optimization can be done, and adjust got
   7496      and plt refcounts.  */
   7497   toc_ref = NULL;
   7498   for (pass = 0; pass < 2; ++pass)
   7499     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   7500       {
   7501 	Elf_Internal_Sym *locsyms = NULL;
   7502 	asection *toc = bfd_get_section_by_name (ibfd, ".toc");
   7503 
   7504 	for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   7505 	  if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
   7506 	    {
   7507 	      Elf_Internal_Rela *relstart, *rel, *relend;
   7508 	      bfd_boolean found_tls_get_addr_arg = 0;
   7509 
   7510 	      /* Read the relocations.  */
   7511 	      relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   7512 						    info->keep_memory);
   7513 	      if (relstart == NULL)
   7514 		return FALSE;
   7515 
   7516 	      relend = relstart + sec->reloc_count;
   7517 	      for (rel = relstart; rel < relend; rel++)
   7518 		{
   7519 		  enum elf_ppc64_reloc_type r_type;
   7520 		  unsigned long r_symndx;
   7521 		  struct elf_link_hash_entry *h;
   7522 		  Elf_Internal_Sym *sym;
   7523 		  asection *sym_sec;
   7524 		  unsigned char *tls_mask;
   7525 		  unsigned char tls_set, tls_clear, tls_type = 0;
   7526 		  bfd_vma value;
   7527 		  bfd_boolean ok_tprel, is_local;
   7528 		  long toc_ref_index = 0;
   7529 		  int expecting_tls_get_addr = 0;
   7530 		  bfd_boolean ret = FALSE;
   7531 
   7532 		  r_symndx = ELF64_R_SYM (rel->r_info);
   7533 		  if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
   7534 				  r_symndx, ibfd))
   7535 		    {
   7536 		    err_free_rel:
   7537 		      if (elf_section_data (sec)->relocs != relstart)
   7538 			free (relstart);
   7539 		      if (toc_ref != NULL)
   7540 			free (toc_ref);
   7541 		      if (locsyms != NULL
   7542 			  && (elf_symtab_hdr (ibfd).contents
   7543 			      != (unsigned char *) locsyms))
   7544 			free (locsyms);
   7545 		      return ret;
   7546 		    }
   7547 
   7548 		  if (h != NULL)
   7549 		    {
   7550 		      if (h->root.type == bfd_link_hash_defined
   7551 			  || h->root.type == bfd_link_hash_defweak)
   7552 			value = h->root.u.def.value;
   7553 		      else if (h->root.type == bfd_link_hash_undefweak)
   7554 			value = 0;
   7555 		      else
   7556 			{
   7557 			  found_tls_get_addr_arg = 0;
   7558 			  continue;
   7559 			}
   7560 		    }
   7561 		  else
   7562 		    /* Symbols referenced by TLS relocs must be of type
   7563 		       STT_TLS.  So no need for .opd local sym adjust.  */
   7564 		    value = sym->st_value;
   7565 
   7566 		  ok_tprel = FALSE;
   7567 		  is_local = FALSE;
   7568 		  if (h == NULL
   7569 		      || !h->def_dynamic)
   7570 		    {
   7571 		      is_local = TRUE;
   7572 		      if (h != NULL
   7573 			  && h->root.type == bfd_link_hash_undefweak)
   7574 			ok_tprel = TRUE;
   7575 		      else
   7576 			{
   7577 			  value += sym_sec->output_offset;
   7578 			  value += sym_sec->output_section->vma;
   7579 			  value -= htab->elf.tls_sec->vma;
   7580 			  ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
   7581 				      < (bfd_vma) 1 << 32);
   7582 			}
   7583 		    }
   7584 
   7585 		  r_type = ELF64_R_TYPE (rel->r_info);
   7586 		  /* If this section has old-style __tls_get_addr calls
   7587 		     without marker relocs, then check that each
   7588 		     __tls_get_addr call reloc is preceded by a reloc
   7589 		     that conceivably belongs to the __tls_get_addr arg
   7590 		     setup insn.  If we don't find matching arg setup
   7591 		     relocs, don't do any tls optimization.  */
   7592 		  if (pass == 0
   7593 		      && sec->has_tls_get_addr_call
   7594 		      && h != NULL
   7595 		      && (h == &htab->tls_get_addr->elf
   7596 			  || h == &htab->tls_get_addr_fd->elf)
   7597 		      && !found_tls_get_addr_arg
   7598 		      && is_branch_reloc (r_type))
   7599 		    {
   7600 		      info->callbacks->minfo (_("%C __tls_get_addr lost arg, "
   7601 						"TLS optimization disabled\n"),
   7602 					      ibfd, sec, rel->r_offset);
   7603 		      ret = TRUE;
   7604 		      goto err_free_rel;
   7605 		    }
   7606 
   7607 		  found_tls_get_addr_arg = 0;
   7608 		  switch (r_type)
   7609 		    {
   7610 		    case R_PPC64_GOT_TLSLD16:
   7611 		    case R_PPC64_GOT_TLSLD16_LO:
   7612 		      expecting_tls_get_addr = 1;
   7613 		      found_tls_get_addr_arg = 1;
   7614 		      /* Fall thru */
   7615 
   7616 		    case R_PPC64_GOT_TLSLD16_HI:
   7617 		    case R_PPC64_GOT_TLSLD16_HA:
   7618 		      /* These relocs should never be against a symbol
   7619 			 defined in a shared lib.  Leave them alone if
   7620 			 that turns out to be the case.  */
   7621 		      if (!is_local)
   7622 			continue;
   7623 
   7624 		      /* LD -> LE */
   7625 		      tls_set = 0;
   7626 		      tls_clear = TLS_LD;
   7627 		      tls_type = TLS_TLS | TLS_LD;
   7628 		      break;
   7629 
   7630 		    case R_PPC64_GOT_TLSGD16:
   7631 		    case R_PPC64_GOT_TLSGD16_LO:
   7632 		      expecting_tls_get_addr = 1;
   7633 		      found_tls_get_addr_arg = 1;
   7634 		      /* Fall thru */
   7635 
   7636 		    case R_PPC64_GOT_TLSGD16_HI:
   7637 		    case R_PPC64_GOT_TLSGD16_HA:
   7638 		      if (ok_tprel)
   7639 			/* GD -> LE */
   7640 			tls_set = 0;
   7641 		      else
   7642 			/* GD -> IE */
   7643 			tls_set = TLS_TLS | TLS_TPRELGD;
   7644 		      tls_clear = TLS_GD;
   7645 		      tls_type = TLS_TLS | TLS_GD;
   7646 		      break;
   7647 
   7648 		    case R_PPC64_GOT_TPREL16_DS:
   7649 		    case R_PPC64_GOT_TPREL16_LO_DS:
   7650 		    case R_PPC64_GOT_TPREL16_HI:
   7651 		    case R_PPC64_GOT_TPREL16_HA:
   7652 		      if (ok_tprel)
   7653 			{
   7654 			  /* IE -> LE */
   7655 			  tls_set = 0;
   7656 			  tls_clear = TLS_TPREL;
   7657 			  tls_type = TLS_TLS | TLS_TPREL;
   7658 			  break;
   7659 			}
   7660 		      continue;
   7661 
   7662 		    case R_PPC64_TLSGD:
   7663 		    case R_PPC64_TLSLD:
   7664 		      found_tls_get_addr_arg = 1;
   7665 		      /* Fall thru */
   7666 
   7667 		    case R_PPC64_TLS:
   7668 		    case R_PPC64_TOC16:
   7669 		    case R_PPC64_TOC16_LO:
   7670 		      if (sym_sec == NULL || sym_sec != toc)
   7671 			continue;
   7672 
   7673 		      /* Mark this toc entry as referenced by a TLS
   7674 			 code sequence.  We can do that now in the
   7675 			 case of R_PPC64_TLS, and after checking for
   7676 			 tls_get_addr for the TOC16 relocs.  */
   7677 		      if (toc_ref == NULL)
   7678 			toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
   7679 		      if (toc_ref == NULL)
   7680 			goto err_free_rel;
   7681 
   7682 		      if (h != NULL)
   7683 			value = h->root.u.def.value;
   7684 		      else
   7685 			value = sym->st_value;
   7686 		      value += rel->r_addend;
   7687 		      BFD_ASSERT (value < toc->size && value % 8 == 0);
   7688 		      toc_ref_index = (value + toc->output_offset) / 8;
   7689 		      if (r_type == R_PPC64_TLS
   7690 			  || r_type == R_PPC64_TLSGD
   7691 			  || r_type == R_PPC64_TLSLD)
   7692 			{
   7693 			  toc_ref[toc_ref_index] = 1;
   7694 			  continue;
   7695 			}
   7696 
   7697 		      if (pass != 0 && toc_ref[toc_ref_index] == 0)
   7698 			continue;
   7699 
   7700 		      tls_set = 0;
   7701 		      tls_clear = 0;
   7702 		      expecting_tls_get_addr = 2;
   7703 		      break;
   7704 
   7705 		    case R_PPC64_TPREL64:
   7706 		      if (pass == 0
   7707 			  || sec != toc
   7708 			  || toc_ref == NULL
   7709 			  || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
   7710 			continue;
   7711 		      if (ok_tprel)
   7712 			{
   7713 			  /* IE -> LE */
   7714 			  tls_set = TLS_EXPLICIT;
   7715 			  tls_clear = TLS_TPREL;
   7716 			  break;
   7717 			}
   7718 		      continue;
   7719 
   7720 		    case R_PPC64_DTPMOD64:
   7721 		      if (pass == 0
   7722 			  || sec != toc
   7723 			  || toc_ref == NULL
   7724 			  || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
   7725 			continue;
   7726 		      if (rel + 1 < relend
   7727 			  && (rel[1].r_info
   7728 			      == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
   7729 			  && rel[1].r_offset == rel->r_offset + 8)
   7730 			{
   7731 			  if (ok_tprel)
   7732 			    /* GD -> LE */
   7733 			    tls_set = TLS_EXPLICIT | TLS_GD;
   7734 			  else
   7735 			    /* GD -> IE */
   7736 			    tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
   7737 			  tls_clear = TLS_GD;
   7738 			}
   7739 		      else
   7740 			{
   7741 			  if (!is_local)
   7742 			    continue;
   7743 
   7744 			  /* LD -> LE */
   7745 			  tls_set = TLS_EXPLICIT;
   7746 			  tls_clear = TLS_LD;
   7747 			}
   7748 		      break;
   7749 
   7750 		    default:
   7751 		      continue;
   7752 		    }
   7753 
   7754 		  if (pass == 0)
   7755 		    {
   7756 		      if (!expecting_tls_get_addr
   7757 			  || !sec->has_tls_get_addr_call)
   7758 			continue;
   7759 
   7760 		      if (rel + 1 < relend
   7761 			  && branch_reloc_hash_match (ibfd, rel + 1,
   7762 						      htab->tls_get_addr,
   7763 						      htab->tls_get_addr_fd))
   7764 			{
   7765 			  if (expecting_tls_get_addr == 2)
   7766 			    {
   7767 			      /* Check for toc tls entries.  */
   7768 			      unsigned char *toc_tls;
   7769 			      int retval;
   7770 
   7771 			      retval = get_tls_mask (&toc_tls, NULL, NULL,
   7772 						     &locsyms,
   7773 						     rel, ibfd);
   7774 			      if (retval == 0)
   7775 				goto err_free_rel;
   7776 			      if (toc_tls != NULL)
   7777 				{
   7778 				  if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
   7779 				    found_tls_get_addr_arg = 1;
   7780 				  if (retval > 1)
   7781 				    toc_ref[toc_ref_index] = 1;
   7782 				}
   7783 			    }
   7784 			  continue;
   7785 			}
   7786 
   7787 		      if (expecting_tls_get_addr != 1)
   7788 			continue;
   7789 
   7790 		      /* Uh oh, we didn't find the expected call.  We
   7791 			 could just mark this symbol to exclude it
   7792 			 from tls optimization but it's safer to skip
   7793 			 the entire optimization.  */
   7794 		      info->callbacks->minfo (_("%C arg lost __tls_get_addr, "
   7795 						"TLS optimization disabled\n"),
   7796 					      ibfd, sec, rel->r_offset);
   7797 		      ret = TRUE;
   7798 		      goto err_free_rel;
   7799 		    }
   7800 
   7801 		  if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
   7802 		    {
   7803 		      struct plt_entry *ent;
   7804 		      for (ent = htab->tls_get_addr->elf.plt.plist;
   7805 			   ent != NULL;
   7806 			   ent = ent->next)
   7807 			if (ent->addend == 0)
   7808 			  {
   7809 			    if (ent->plt.refcount > 0)
   7810 			      {
   7811 				ent->plt.refcount -= 1;
   7812 				expecting_tls_get_addr = 0;
   7813 			      }
   7814 			    break;
   7815 			  }
   7816 		    }
   7817 
   7818 		  if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
   7819 		    {
   7820 		      struct plt_entry *ent;
   7821 		      for (ent = htab->tls_get_addr_fd->elf.plt.plist;
   7822 			   ent != NULL;
   7823 			   ent = ent->next)
   7824 			if (ent->addend == 0)
   7825 			  {
   7826 			    if (ent->plt.refcount > 0)
   7827 			      ent->plt.refcount -= 1;
   7828 			    break;
   7829 			  }
   7830 		    }
   7831 
   7832 		  if (tls_clear == 0)
   7833 		    continue;
   7834 
   7835 		  if ((tls_set & TLS_EXPLICIT) == 0)
   7836 		    {
   7837 		      struct got_entry *ent;
   7838 
   7839 		      /* Adjust got entry for this reloc.  */
   7840 		      if (h != NULL)
   7841 			ent = h->got.glist;
   7842 		      else
   7843 			ent = elf_local_got_ents (ibfd)[r_symndx];
   7844 
   7845 		      for (; ent != NULL; ent = ent->next)
   7846 			if (ent->addend == rel->r_addend
   7847 			    && ent->owner == ibfd
   7848 			    && ent->tls_type == tls_type)
   7849 			  break;
   7850 		      if (ent == NULL)
   7851 			abort ();
   7852 
   7853 		      if (tls_set == 0)
   7854 			{
   7855 			  /* We managed to get rid of a got entry.  */
   7856 			  if (ent->got.refcount > 0)
   7857 			    ent->got.refcount -= 1;
   7858 			}
   7859 		    }
   7860 		  else
   7861 		    {
   7862 		      /* If we got rid of a DTPMOD/DTPREL reloc pair then
   7863 			 we'll lose one or two dyn relocs.  */
   7864 		      if (!dec_dynrel_count (rel->r_info, sec, info,
   7865 					     NULL, h, sym_sec))
   7866 			return FALSE;
   7867 
   7868 		      if (tls_set == (TLS_EXPLICIT | TLS_GD))
   7869 			{
   7870 			  if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
   7871 						 NULL, h, sym_sec))
   7872 			    return FALSE;
   7873 			}
   7874 		    }
   7875 
   7876 		  *tls_mask |= tls_set;
   7877 		  *tls_mask &= ~tls_clear;
   7878 		}
   7879 
   7880 	      if (elf_section_data (sec)->relocs != relstart)
   7881 		free (relstart);
   7882 	    }
   7883 
   7884 	if (locsyms != NULL
   7885 	    && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
   7886 	  {
   7887 	    if (!info->keep_memory)
   7888 	      free (locsyms);
   7889 	    else
   7890 	      elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
   7891 	  }
   7892       }
   7893 
   7894   if (toc_ref != NULL)
   7895     free (toc_ref);
   7896   return TRUE;
   7897 }
   7898 
   7899 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
   7900    the values of any global symbols in a toc section that has been
   7901    edited.  Globals in toc sections should be a rarity, so this function
   7902    sets a flag if any are found in toc sections other than the one just
   7903    edited, so that futher hash table traversals can be avoided.  */
   7904 
   7905 struct adjust_toc_info
   7906 {
   7907   asection *toc;
   7908   unsigned long *skip;
   7909   bfd_boolean global_toc_syms;
   7910 };
   7911 
   7912 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
   7913 
   7914 static bfd_boolean
   7915 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
   7916 {
   7917   struct ppc_link_hash_entry *eh;
   7918   struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
   7919   unsigned long i;
   7920 
   7921   if (h->root.type == bfd_link_hash_indirect)
   7922     return TRUE;
   7923 
   7924   if (h->root.type == bfd_link_hash_warning)
   7925     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   7926 
   7927   if (h->root.type != bfd_link_hash_defined
   7928       && h->root.type != bfd_link_hash_defweak)
   7929     return TRUE;
   7930 
   7931   eh = (struct ppc_link_hash_entry *) h;
   7932   if (eh->adjust_done)
   7933     return TRUE;
   7934 
   7935   if (eh->elf.root.u.def.section == toc_inf->toc)
   7936     {
   7937       if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
   7938 	i = toc_inf->toc->rawsize >> 3;
   7939       else
   7940 	i = eh->elf.root.u.def.value >> 3;
   7941 
   7942       if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
   7943 	{
   7944 	  (*_bfd_error_handler)
   7945 	    (_("%s defined on removed toc entry"), eh->elf.root.root.string);
   7946 	  do
   7947 	    ++i;
   7948 	  while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
   7949 	  eh->elf.root.u.def.value = (bfd_vma) i << 3;
   7950 	}
   7951 
   7952       eh->elf.root.u.def.value -= toc_inf->skip[i];
   7953       eh->adjust_done = 1;
   7954     }
   7955   else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
   7956     toc_inf->global_toc_syms = TRUE;
   7957 
   7958   return TRUE;
   7959 }
   7960 
   7961 /* Examine all relocs referencing .toc sections in order to remove
   7962    unused .toc entries.  */
   7963 
   7964 bfd_boolean
   7965 ppc64_elf_edit_toc (struct bfd_link_info *info)
   7966 {
   7967   bfd *ibfd;
   7968   struct adjust_toc_info toc_inf;
   7969   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   7970 
   7971   htab->do_toc_opt = 1;
   7972   toc_inf.global_toc_syms = TRUE;
   7973   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   7974     {
   7975       asection *toc, *sec;
   7976       Elf_Internal_Shdr *symtab_hdr;
   7977       Elf_Internal_Sym *local_syms;
   7978       Elf_Internal_Rela *relstart, *rel, *toc_relocs;
   7979       unsigned long *skip, *drop;
   7980       unsigned char *used;
   7981       unsigned char *keep, last, some_unused;
   7982 
   7983       if (!is_ppc64_elf (ibfd))
   7984 	continue;
   7985 
   7986       toc = bfd_get_section_by_name (ibfd, ".toc");
   7987       if (toc == NULL
   7988 	  || toc->size == 0
   7989 	  || toc->sec_info_type == ELF_INFO_TYPE_JUST_SYMS
   7990 	  || elf_discarded_section (toc))
   7991 	continue;
   7992 
   7993       toc_relocs = NULL;
   7994       local_syms = NULL;
   7995       symtab_hdr = &elf_symtab_hdr (ibfd);
   7996 
   7997       /* Look at sections dropped from the final link.  */
   7998       skip = NULL;
   7999       relstart = NULL;
   8000       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   8001 	{
   8002 	  if (sec->reloc_count == 0
   8003 	      || !elf_discarded_section (sec)
   8004 	      || get_opd_info (sec)
   8005 	      || (sec->flags & SEC_ALLOC) == 0
   8006 	      || (sec->flags & SEC_DEBUGGING) != 0)
   8007 	    continue;
   8008 
   8009 	  relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
   8010 	  if (relstart == NULL)
   8011 	    goto error_ret;
   8012 
   8013 	  /* Run through the relocs to see which toc entries might be
   8014 	     unused.  */
   8015 	  for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
   8016 	    {
   8017 	      enum elf_ppc64_reloc_type r_type;
   8018 	      unsigned long r_symndx;
   8019 	      asection *sym_sec;
   8020 	      struct elf_link_hash_entry *h;
   8021 	      Elf_Internal_Sym *sym;
   8022 	      bfd_vma val;
   8023 
   8024 	      r_type = ELF64_R_TYPE (rel->r_info);
   8025 	      switch (r_type)
   8026 		{
   8027 		default:
   8028 		  continue;
   8029 
   8030 		case R_PPC64_TOC16:
   8031 		case R_PPC64_TOC16_LO:
   8032 		case R_PPC64_TOC16_HI:
   8033 		case R_PPC64_TOC16_HA:
   8034 		case R_PPC64_TOC16_DS:
   8035 		case R_PPC64_TOC16_LO_DS:
   8036 		  break;
   8037 		}
   8038 
   8039 	      r_symndx = ELF64_R_SYM (rel->r_info);
   8040 	      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   8041 			      r_symndx, ibfd))
   8042 		goto error_ret;
   8043 
   8044 	      if (sym_sec != toc)
   8045 		continue;
   8046 
   8047 	      if (h != NULL)
   8048 		val = h->root.u.def.value;
   8049 	      else
   8050 		val = sym->st_value;
   8051 	      val += rel->r_addend;
   8052 
   8053 	      if (val >= toc->size)
   8054 		continue;
   8055 
   8056 	      /* Anything in the toc ought to be aligned to 8 bytes.
   8057 		 If not, don't mark as unused.  */
   8058 	      if (val & 7)
   8059 		continue;
   8060 
   8061 	      if (skip == NULL)
   8062 		{
   8063 		  skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
   8064 		  if (skip == NULL)
   8065 		    goto error_ret;
   8066 		}
   8067 
   8068 	      skip[val >> 3] = ref_from_discarded;
   8069 	    }
   8070 
   8071 	  if (elf_section_data (sec)->relocs != relstart)
   8072 	    free (relstart);
   8073 	}
   8074 
   8075       /* For largetoc loads of address constants, we can convert
   8076 	 .  addis rx,2,addr@got@ha
   8077 	 .  ld ry,addr@got@l(rx)
   8078 	 to
   8079 	 .  addis rx,2,addr@toc@ha
   8080 	 .  addi ry,rx,addr@toc@l
   8081 	 when addr is within 2G of the toc pointer.  This then means
   8082 	 that the word storing "addr" in the toc is no longer needed.  */
   8083 
   8084       if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
   8085 	  && toc->output_section->rawsize < (bfd_vma) 1 << 31
   8086 	  && toc->reloc_count != 0)
   8087 	{
   8088 	  /* Read toc relocs.  */
   8089 	  toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
   8090 						  info->keep_memory);
   8091 	  if (toc_relocs == NULL)
   8092 	    goto error_ret;
   8093 
   8094 	  for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
   8095 	    {
   8096 	      enum elf_ppc64_reloc_type r_type;
   8097 	      unsigned long r_symndx;
   8098 	      asection *sym_sec;
   8099 	      struct elf_link_hash_entry *h;
   8100 	      Elf_Internal_Sym *sym;
   8101 	      bfd_vma val, addr;
   8102 
   8103 	      r_type = ELF64_R_TYPE (rel->r_info);
   8104 	      if (r_type != R_PPC64_ADDR64)
   8105 		continue;
   8106 
   8107 	      r_symndx = ELF64_R_SYM (rel->r_info);
   8108 	      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   8109 			      r_symndx, ibfd))
   8110 		goto error_ret;
   8111 
   8112 	      if (sym_sec == NULL
   8113 		  || elf_discarded_section (sym_sec))
   8114 		continue;
   8115 
   8116 	      if (!SYMBOL_CALLS_LOCAL (info, h))
   8117 		continue;
   8118 
   8119 	      if (h != NULL)
   8120 		{
   8121 		  if (h->type == STT_GNU_IFUNC)
   8122 		    continue;
   8123 		  val = h->root.u.def.value;
   8124 		}
   8125 	      else
   8126 		{
   8127 		  if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
   8128 		    continue;
   8129 		  val = sym->st_value;
   8130 		}
   8131 	      val += rel->r_addend;
   8132 	      val += sym_sec->output_section->vma + sym_sec->output_offset;
   8133 
   8134 	      /* We don't yet know the exact toc pointer value, but we
   8135 		 know it will be somewhere in the toc section.  Don't
   8136 		 optimize if the difference from any possible toc
   8137 		 pointer is outside [ff..f80008000, 7fff7fff].  */
   8138 	      addr = toc->output_section->vma + TOC_BASE_OFF;
   8139 	      if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
   8140 		continue;
   8141 
   8142 	      addr = toc->output_section->vma + toc->output_section->rawsize;
   8143 	      if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
   8144 		continue;
   8145 
   8146 	      if (skip == NULL)
   8147 		{
   8148 		  skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
   8149 		  if (skip == NULL)
   8150 		    goto error_ret;
   8151 		}
   8152 
   8153 	      skip[rel->r_offset >> 3]
   8154 		|= can_optimize | ((rel - toc_relocs) << 2);
   8155 	    }
   8156 	}
   8157 
   8158       if (skip == NULL)
   8159 	continue;
   8160 
   8161       used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
   8162       if (used == NULL)
   8163 	{
   8164 	error_ret:
   8165 	  if (local_syms != NULL
   8166 	      && symtab_hdr->contents != (unsigned char *) local_syms)
   8167 	    free (local_syms);
   8168 	  if (sec != NULL
   8169 	      && relstart != NULL
   8170 	      && elf_section_data (sec)->relocs != relstart)
   8171 	    free (relstart);
   8172 	  if (toc_relocs != NULL
   8173 	      && elf_section_data (toc)->relocs != toc_relocs)
   8174 	    free (toc_relocs);
   8175 	  if (skip != NULL)
   8176 	    free (skip);
   8177 	  return FALSE;
   8178 	}
   8179 
   8180       /* Now check all kept sections that might reference the toc.
   8181 	 Check the toc itself last.  */
   8182       for (sec = (ibfd->sections == toc && toc->next ? toc->next
   8183 		  : ibfd->sections);
   8184 	   sec != NULL;
   8185 	   sec = (sec == toc ? NULL
   8186 		  : sec->next == NULL ? toc
   8187 		  : sec->next == toc && toc->next ? toc->next
   8188 		  : sec->next))
   8189 	{
   8190 	  int repeat;
   8191 
   8192 	  if (sec->reloc_count == 0
   8193 	      || elf_discarded_section (sec)
   8194 	      || get_opd_info (sec)
   8195 	      || (sec->flags & SEC_ALLOC) == 0
   8196 	      || (sec->flags & SEC_DEBUGGING) != 0)
   8197 	    continue;
   8198 
   8199 	  relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   8200 						info->keep_memory);
   8201 	  if (relstart == NULL)
   8202 	    goto error_ret;
   8203 
   8204 	  /* Mark toc entries referenced as used.  */
   8205 	  repeat = 0;
   8206 	  do
   8207 	    for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
   8208 	      {
   8209 		enum elf_ppc64_reloc_type r_type;
   8210 		unsigned long r_symndx;
   8211 		asection *sym_sec;
   8212 		struct elf_link_hash_entry *h;
   8213 		Elf_Internal_Sym *sym;
   8214 		bfd_vma val;
   8215 
   8216 		r_type = ELF64_R_TYPE (rel->r_info);
   8217 		switch (r_type)
   8218 		  {
   8219 		  case R_PPC64_TOC16:
   8220 		  case R_PPC64_TOC16_LO:
   8221 		  case R_PPC64_TOC16_HI:
   8222 		  case R_PPC64_TOC16_HA:
   8223 		  case R_PPC64_TOC16_DS:
   8224 		  case R_PPC64_TOC16_LO_DS:
   8225 		    /* In case we're taking addresses of toc entries.  */
   8226 		  case R_PPC64_ADDR64:
   8227 		    break;
   8228 
   8229 		  default:
   8230 		    continue;
   8231 		  }
   8232 
   8233 		r_symndx = ELF64_R_SYM (rel->r_info);
   8234 		if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   8235 				r_symndx, ibfd))
   8236 		  {
   8237 		    free (used);
   8238 		    goto error_ret;
   8239 		  }
   8240 
   8241 		if (sym_sec != toc)
   8242 		  continue;
   8243 
   8244 		if (h != NULL)
   8245 		  val = h->root.u.def.value;
   8246 		else
   8247 		  val = sym->st_value;
   8248 		val += rel->r_addend;
   8249 
   8250 		if (val >= toc->size)
   8251 		  continue;
   8252 
   8253 		if ((skip[val >> 3] & can_optimize) != 0)
   8254 		  {
   8255 		    bfd_vma off;
   8256 		    unsigned char opc;
   8257 
   8258 		    switch (r_type)
   8259 		      {
   8260 		      case R_PPC64_TOC16_HA:
   8261 			break;
   8262 
   8263 		      case R_PPC64_TOC16_LO_DS:
   8264 			off = rel->r_offset + (bfd_big_endian (ibfd) ? -2 : 3);
   8265 			if (!bfd_get_section_contents (ibfd, sec, &opc, off, 1))
   8266 			  return FALSE;
   8267 			if ((opc & (0x3f << 2)) == (58u << 2))
   8268 			  break;
   8269 			/* Fall thru */
   8270 
   8271 		      default:
   8272 			/* Wrong sort of reloc, or not a ld.  We may
   8273 			   as well clear ref_from_discarded too.  */
   8274 			skip[val >> 3] = 0;
   8275 		      }
   8276 		  }
   8277 
   8278 		/* For the toc section, we only mark as used if
   8279 		   this entry itself isn't unused.  */
   8280 		if (sec == toc
   8281 		    && !used[val >> 3]
   8282 		    && (used[rel->r_offset >> 3]
   8283 			|| !(skip[rel->r_offset >> 3] & ref_from_discarded)))
   8284 		  /* Do all the relocs again, to catch reference
   8285 		     chains.  */
   8286 		  repeat = 1;
   8287 
   8288 		used[val >> 3] = 1;
   8289 	      }
   8290 	  while (repeat);
   8291 
   8292 	  if (elf_section_data (sec)->relocs != relstart)
   8293 	    free (relstart);
   8294 	}
   8295 
   8296       /* Merge the used and skip arrays.  Assume that TOC
   8297 	 doublewords not appearing as either used or unused belong
   8298 	 to to an entry more than one doubleword in size.  */
   8299       for (drop = skip, keep = used, last = 0, some_unused = 0;
   8300 	   drop < skip + (toc->size + 7) / 8;
   8301 	   ++drop, ++keep)
   8302 	{
   8303 	  if (*keep)
   8304 	    {
   8305 	      *drop &= ~ref_from_discarded;
   8306 	      if ((*drop & can_optimize) != 0)
   8307 		some_unused = 1;
   8308 	      last = 0;
   8309 	    }
   8310 	  else if (*drop)
   8311 	    {
   8312 	      some_unused = 1;
   8313 	      last = ref_from_discarded;
   8314 	    }
   8315 	  else
   8316 	    *drop = last;
   8317 	}
   8318 
   8319       free (used);
   8320 
   8321       if (some_unused)
   8322 	{
   8323 	  bfd_byte *contents, *src;
   8324 	  unsigned long off;
   8325 	  Elf_Internal_Sym *sym;
   8326 	  bfd_boolean local_toc_syms = FALSE;
   8327 
   8328 	  /* Shuffle the toc contents, and at the same time convert the
   8329 	     skip array from booleans into offsets.  */
   8330 	  if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
   8331 	    goto error_ret;
   8332 
   8333 	  elf_section_data (toc)->this_hdr.contents = contents;
   8334 
   8335 	  for (src = contents, off = 0, drop = skip;
   8336 	       src < contents + toc->size;
   8337 	       src += 8, ++drop)
   8338 	    {
   8339 	      if ((*drop & (can_optimize | ref_from_discarded)) != 0)
   8340 		off += 8;
   8341 	      else if (off != 0)
   8342 		{
   8343 		  *drop = off;
   8344 		  memcpy (src - off, src, 8);
   8345 		}
   8346 	    }
   8347 	  *drop = off;
   8348 	  toc->rawsize = toc->size;
   8349 	  toc->size = src - contents - off;
   8350 
   8351 	  /* Adjust addends for relocs against the toc section sym,
   8352 	     and optimize any accesses we can.  */
   8353 	  for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   8354 	    {
   8355 	      if (sec->reloc_count == 0
   8356 		  || elf_discarded_section (sec))
   8357 		continue;
   8358 
   8359 	      relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   8360 						    info->keep_memory);
   8361 	      if (relstart == NULL)
   8362 		goto error_ret;
   8363 
   8364 	      for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
   8365 		{
   8366 		  enum elf_ppc64_reloc_type r_type;
   8367 		  unsigned long r_symndx;
   8368 		  asection *sym_sec;
   8369 		  struct elf_link_hash_entry *h;
   8370 		  bfd_vma val;
   8371 
   8372 		  r_type = ELF64_R_TYPE (rel->r_info);
   8373 		  switch (r_type)
   8374 		    {
   8375 		    default:
   8376 		      continue;
   8377 
   8378 		    case R_PPC64_TOC16:
   8379 		    case R_PPC64_TOC16_LO:
   8380 		    case R_PPC64_TOC16_HI:
   8381 		    case R_PPC64_TOC16_HA:
   8382 		    case R_PPC64_TOC16_DS:
   8383 		    case R_PPC64_TOC16_LO_DS:
   8384 		    case R_PPC64_ADDR64:
   8385 		      break;
   8386 		    }
   8387 
   8388 		  r_symndx = ELF64_R_SYM (rel->r_info);
   8389 		  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   8390 				  r_symndx, ibfd))
   8391 		    goto error_ret;
   8392 
   8393 		  if (sym_sec != toc)
   8394 		    continue;
   8395 
   8396 		  if (h != NULL)
   8397 		    val = h->root.u.def.value;
   8398 		  else
   8399 		    {
   8400 		      val = sym->st_value;
   8401 		      if (val != 0)
   8402 			local_toc_syms = TRUE;
   8403 		    }
   8404 
   8405 		  val += rel->r_addend;
   8406 
   8407 		  if (val > toc->rawsize)
   8408 		    val = toc->rawsize;
   8409 		  else if ((skip[val >> 3] & ref_from_discarded) != 0)
   8410 		    continue;
   8411 		  else if ((skip[val >> 3] & can_optimize) != 0)
   8412 		    {
   8413 		      Elf_Internal_Rela *tocrel
   8414 			= toc_relocs + (skip[val >> 3] >> 2);
   8415 		      unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
   8416 
   8417 		      switch (r_type)
   8418 			{
   8419 			case R_PPC64_TOC16_HA:
   8420 			  rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
   8421 			  break;
   8422 
   8423 			case R_PPC64_TOC16_LO_DS:
   8424 			  rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
   8425 			  break;
   8426 
   8427 			default:
   8428 			  abort ();
   8429 			}
   8430 		      rel->r_addend = tocrel->r_addend;
   8431 		      elf_section_data (sec)->relocs = relstart;
   8432 		      continue;
   8433 		    }
   8434 
   8435 		  if (h != NULL || sym->st_value != 0)
   8436 		    continue;
   8437 
   8438 		  rel->r_addend -= skip[val >> 3];
   8439 		  elf_section_data (sec)->relocs = relstart;
   8440 		}
   8441 
   8442 	      if (elf_section_data (sec)->relocs != relstart)
   8443 		free (relstart);
   8444 	    }
   8445 
   8446 	  /* We shouldn't have local or global symbols defined in the TOC,
   8447 	     but handle them anyway.  */
   8448 	  if (local_syms != NULL)
   8449 	    for (sym = local_syms;
   8450 		 sym < local_syms + symtab_hdr->sh_info;
   8451 		 ++sym)
   8452 	      if (sym->st_value != 0
   8453 		  && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
   8454 		{
   8455 		  unsigned long i;
   8456 
   8457 		  if (sym->st_value > toc->rawsize)
   8458 		    i = toc->rawsize >> 3;
   8459 		  else
   8460 		    i = sym->st_value >> 3;
   8461 
   8462 		  if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
   8463 		    {
   8464 		      if (local_toc_syms)
   8465 			(*_bfd_error_handler)
   8466 			  (_("%s defined on removed toc entry"),
   8467 			   bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
   8468 		      do
   8469 			++i;
   8470 		      while ((skip[i] & (ref_from_discarded | can_optimize)));
   8471 		      sym->st_value = (bfd_vma) i << 3;
   8472 		    }
   8473 
   8474 		  sym->st_value -= skip[i];
   8475 		  symtab_hdr->contents = (unsigned char *) local_syms;
   8476 		}
   8477 
   8478 	  /* Adjust any global syms defined in this toc input section.  */
   8479 	  if (toc_inf.global_toc_syms)
   8480 	    {
   8481 	      toc_inf.toc = toc;
   8482 	      toc_inf.skip = skip;
   8483 	      toc_inf.global_toc_syms = FALSE;
   8484 	      elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
   8485 				      &toc_inf);
   8486 	    }
   8487 
   8488 	  if (toc->reloc_count != 0)
   8489 	    {
   8490 	      Elf_Internal_Shdr *rel_hdr;
   8491 	      Elf_Internal_Rela *wrel;
   8492 	      bfd_size_type sz;
   8493 
   8494 	      /* Remove unused toc relocs, and adjust those we keep.  */
   8495 	      if (toc_relocs == NULL)
   8496 		toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
   8497 							info->keep_memory);
   8498 	      if (toc_relocs == NULL)
   8499 		goto error_ret;
   8500 
   8501 	      wrel = toc_relocs;
   8502 	      for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
   8503 		if ((skip[rel->r_offset >> 3]
   8504 		     & (ref_from_discarded | can_optimize)) == 0)
   8505 		  {
   8506 		    wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
   8507 		    wrel->r_info = rel->r_info;
   8508 		    wrel->r_addend = rel->r_addend;
   8509 		    ++wrel;
   8510 		  }
   8511 		else if (!dec_dynrel_count (rel->r_info, toc, info,
   8512 					    &local_syms, NULL, NULL))
   8513 		  goto error_ret;
   8514 
   8515 	      elf_section_data (toc)->relocs = toc_relocs;
   8516 	      toc->reloc_count = wrel - toc_relocs;
   8517 	      rel_hdr = _bfd_elf_single_rel_hdr (toc);
   8518 	      sz = rel_hdr->sh_entsize;
   8519 	      rel_hdr->sh_size = toc->reloc_count * sz;
   8520 	    }
   8521 	}
   8522       else if (toc_relocs != NULL
   8523 	       && elf_section_data (toc)->relocs != toc_relocs)
   8524 	free (toc_relocs);
   8525 
   8526       if (local_syms != NULL
   8527 	  && symtab_hdr->contents != (unsigned char *) local_syms)
   8528 	{
   8529 	  if (!info->keep_memory)
   8530 	    free (local_syms);
   8531 	  else
   8532 	    symtab_hdr->contents = (unsigned char *) local_syms;
   8533 	}
   8534       free (skip);
   8535     }
   8536 
   8537   return TRUE;
   8538 }
   8539 
   8540 /* Return true iff input section I references the TOC using
   8541    instructions limited to +/-32k offsets.  */
   8542 
   8543 bfd_boolean
   8544 ppc64_elf_has_small_toc_reloc (asection *i)
   8545 {
   8546   return (is_ppc64_elf (i->owner)
   8547 	  && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
   8548 }
   8549 
   8550 /* Allocate space for one GOT entry.  */
   8551 
   8552 static void
   8553 allocate_got (struct elf_link_hash_entry *h,
   8554 	      struct bfd_link_info *info,
   8555 	      struct got_entry *gent)
   8556 {
   8557   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   8558   bfd_boolean dyn;
   8559   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
   8560   int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
   8561 		 ? 16 : 8);
   8562   int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
   8563 		  ? 2 : 1) * sizeof (Elf64_External_Rela);
   8564   asection *got = ppc64_elf_tdata (gent->owner)->got;
   8565 
   8566   gent->got.offset = got->size;
   8567   got->size += entsize;
   8568 
   8569   dyn = htab->elf.dynamic_sections_created;
   8570   if ((info->shared
   8571        || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
   8572 	    && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   8573 		|| h->root.type != bfd_link_hash_undefweak))
   8574     {
   8575       asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
   8576       relgot->size += rentsize;
   8577     }
   8578   else if (h->type == STT_GNU_IFUNC)
   8579     {
   8580       asection *relgot = htab->reliplt;
   8581       relgot->size += rentsize;
   8582       htab->got_reli_size += rentsize;
   8583     }
   8584 }
   8585 
   8586 /* This function merges got entries in the same toc group.  */
   8587 
   8588 static void
   8589 merge_got_entries (struct got_entry **pent)
   8590 {
   8591   struct got_entry *ent, *ent2;
   8592 
   8593   for (ent = *pent; ent != NULL; ent = ent->next)
   8594     if (!ent->is_indirect)
   8595       for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
   8596 	if (!ent2->is_indirect
   8597 	    && ent2->addend == ent->addend
   8598 	    && ent2->tls_type == ent->tls_type
   8599 	    && elf_gp (ent2->owner) == elf_gp (ent->owner))
   8600 	  {
   8601 	    ent2->is_indirect = TRUE;
   8602 	    ent2->got.ent = ent;
   8603 	  }
   8604 }
   8605 
   8606 /* Allocate space in .plt, .got and associated reloc sections for
   8607    dynamic relocs.  */
   8608 
   8609 static bfd_boolean
   8610 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
   8611 {
   8612   struct bfd_link_info *info;
   8613   struct ppc_link_hash_table *htab;
   8614   asection *s;
   8615   struct ppc_link_hash_entry *eh;
   8616   struct ppc_dyn_relocs *p;
   8617   struct got_entry **pgent, *gent;
   8618 
   8619   if (h->root.type == bfd_link_hash_indirect)
   8620     return TRUE;
   8621 
   8622   if (h->root.type == bfd_link_hash_warning)
   8623     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   8624 
   8625   info = (struct bfd_link_info *) inf;
   8626   htab = ppc_hash_table (info);
   8627   if (htab == NULL)
   8628     return FALSE;
   8629 
   8630   if ((htab->elf.dynamic_sections_created
   8631        && h->dynindx != -1
   8632        && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
   8633       || h->type == STT_GNU_IFUNC)
   8634     {
   8635       struct plt_entry *pent;
   8636       bfd_boolean doneone = FALSE;
   8637       for (pent = h->plt.plist; pent != NULL; pent = pent->next)
   8638 	if (pent->plt.refcount > 0)
   8639 	  {
   8640 	    if (!htab->elf.dynamic_sections_created
   8641 		|| h->dynindx == -1)
   8642 	      {
   8643 		s = htab->iplt;
   8644 		pent->plt.offset = s->size;
   8645 		s->size += PLT_ENTRY_SIZE;
   8646 		s = htab->reliplt;
   8647 	      }
   8648 	    else
   8649 	      {
   8650 		/* If this is the first .plt entry, make room for the special
   8651 		   first entry.  */
   8652 		s = htab->plt;
   8653 		if (s->size == 0)
   8654 		  s->size += PLT_INITIAL_ENTRY_SIZE;
   8655 
   8656 		pent->plt.offset = s->size;
   8657 
   8658 		/* Make room for this entry.  */
   8659 		s->size += PLT_ENTRY_SIZE;
   8660 
   8661 		/* Make room for the .glink code.  */
   8662 		s = htab->glink;
   8663 		if (s->size == 0)
   8664 		  s->size += GLINK_CALL_STUB_SIZE;
   8665 		/* We need bigger stubs past index 32767.  */
   8666 		if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
   8667 		  s->size += 4;
   8668 		s->size += 2*4;
   8669 
   8670 		/* We also need to make an entry in the .rela.plt section.  */
   8671 		s = htab->relplt;
   8672 	      }
   8673 	    s->size += sizeof (Elf64_External_Rela);
   8674 	    doneone = TRUE;
   8675 	  }
   8676 	else
   8677 	  pent->plt.offset = (bfd_vma) -1;
   8678       if (!doneone)
   8679 	{
   8680 	  h->plt.plist = NULL;
   8681 	  h->needs_plt = 0;
   8682 	}
   8683     }
   8684   else
   8685     {
   8686       h->plt.plist = NULL;
   8687       h->needs_plt = 0;
   8688     }
   8689 
   8690   eh = (struct ppc_link_hash_entry *) h;
   8691   /* Run through the TLS GD got entries first if we're changing them
   8692      to TPREL.  */
   8693   if ((eh->tls_mask & TLS_TPRELGD) != 0)
   8694     for (gent = h->got.glist; gent != NULL; gent = gent->next)
   8695       if (gent->got.refcount > 0
   8696 	  && (gent->tls_type & TLS_GD) != 0)
   8697 	{
   8698 	  /* This was a GD entry that has been converted to TPREL.  If
   8699 	     there happens to be a TPREL entry we can use that one.  */
   8700 	  struct got_entry *ent;
   8701 	  for (ent = h->got.glist; ent != NULL; ent = ent->next)
   8702 	    if (ent->got.refcount > 0
   8703 		&& (ent->tls_type & TLS_TPREL) != 0
   8704 		&& ent->addend == gent->addend
   8705 		&& ent->owner == gent->owner)
   8706 	      {
   8707 		gent->got.refcount = 0;
   8708 		break;
   8709 	      }
   8710 
   8711 	  /* If not, then we'll be using our own TPREL entry.  */
   8712 	  if (gent->got.refcount != 0)
   8713 	    gent->tls_type = TLS_TLS | TLS_TPREL;
   8714 	}
   8715 
   8716   /* Remove any list entry that won't generate a word in the GOT before
   8717      we call merge_got_entries.  Otherwise we risk merging to empty
   8718      entries.  */
   8719   pgent = &h->got.glist;
   8720   while ((gent = *pgent) != NULL)
   8721     if (gent->got.refcount > 0)
   8722       {
   8723 	if ((gent->tls_type & TLS_LD) != 0
   8724 	    && !h->def_dynamic)
   8725 	  {
   8726 	    ppc64_tlsld_got (gent->owner)->got.refcount += 1;
   8727 	    *pgent = gent->next;
   8728 	  }
   8729 	else
   8730 	  pgent = &gent->next;
   8731       }
   8732     else
   8733       *pgent = gent->next;
   8734 
   8735   if (!htab->do_multi_toc)
   8736     merge_got_entries (&h->got.glist);
   8737 
   8738   for (gent = h->got.glist; gent != NULL; gent = gent->next)
   8739     if (!gent->is_indirect)
   8740       {
   8741 	/* Make sure this symbol is output as a dynamic symbol.
   8742 	   Undefined weak syms won't yet be marked as dynamic,
   8743 	   nor will all TLS symbols.  */
   8744 	if (h->dynindx == -1
   8745 	    && !h->forced_local
   8746 	    && h->type != STT_GNU_IFUNC
   8747 	    && htab->elf.dynamic_sections_created)
   8748 	  {
   8749 	    if (! bfd_elf_link_record_dynamic_symbol (info, h))
   8750 	      return FALSE;
   8751 	  }
   8752 
   8753 	if (!is_ppc64_elf (gent->owner))
   8754 	  abort ();
   8755 
   8756 	allocate_got (h, info, gent);
   8757       }
   8758 
   8759   if (eh->dyn_relocs == NULL
   8760       || (!htab->elf.dynamic_sections_created
   8761 	  && h->type != STT_GNU_IFUNC))
   8762     return TRUE;
   8763 
   8764   /* In the shared -Bsymbolic case, discard space allocated for
   8765      dynamic pc-relative relocs against symbols which turn out to be
   8766      defined in regular objects.  For the normal shared case, discard
   8767      space for relocs that have become local due to symbol visibility
   8768      changes.  */
   8769 
   8770   if (info->shared)
   8771     {
   8772       /* Relocs that use pc_count are those that appear on a call insn,
   8773 	 or certain REL relocs (see must_be_dyn_reloc) that can be
   8774 	 generated via assembly.  We want calls to protected symbols to
   8775 	 resolve directly to the function rather than going via the plt.
   8776 	 If people want function pointer comparisons to work as expected
   8777 	 then they should avoid writing weird assembly.  */
   8778       if (SYMBOL_CALLS_LOCAL (info, h))
   8779 	{
   8780 	  struct ppc_dyn_relocs **pp;
   8781 
   8782 	  for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
   8783 	    {
   8784 	      p->count -= p->pc_count;
   8785 	      p->pc_count = 0;
   8786 	      if (p->count == 0)
   8787 		*pp = p->next;
   8788 	      else
   8789 		pp = &p->next;
   8790 	    }
   8791 	}
   8792 
   8793       /* Also discard relocs on undefined weak syms with non-default
   8794 	 visibility.  */
   8795       if (eh->dyn_relocs != NULL
   8796 	  && h->root.type == bfd_link_hash_undefweak)
   8797 	{
   8798 	  if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
   8799 	    eh->dyn_relocs = NULL;
   8800 
   8801 	  /* Make sure this symbol is output as a dynamic symbol.
   8802 	     Undefined weak syms won't yet be marked as dynamic.  */
   8803 	  else if (h->dynindx == -1
   8804 		   && !h->forced_local)
   8805 	    {
   8806 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   8807 		return FALSE;
   8808 	    }
   8809 	}
   8810     }
   8811   else if (h->type == STT_GNU_IFUNC)
   8812     {
   8813       if (!h->non_got_ref)
   8814 	eh->dyn_relocs = NULL;
   8815     }
   8816   else if (ELIMINATE_COPY_RELOCS)
   8817     {
   8818       /* For the non-shared case, discard space for relocs against
   8819 	 symbols which turn out to need copy relocs or are not
   8820 	 dynamic.  */
   8821 
   8822       if (!h->non_got_ref
   8823 	  && !h->def_regular)
   8824 	{
   8825 	  /* Make sure this symbol is output as a dynamic symbol.
   8826 	     Undefined weak syms won't yet be marked as dynamic.  */
   8827 	  if (h->dynindx == -1
   8828 	      && !h->forced_local)
   8829 	    {
   8830 	      if (! bfd_elf_link_record_dynamic_symbol (info, h))
   8831 		return FALSE;
   8832 	    }
   8833 
   8834 	  /* If that succeeded, we know we'll be keeping all the
   8835 	     relocs.  */
   8836 	  if (h->dynindx != -1)
   8837 	    goto keep;
   8838 	}
   8839 
   8840       eh->dyn_relocs = NULL;
   8841 
   8842     keep: ;
   8843     }
   8844 
   8845   /* Finally, allocate space.  */
   8846   for (p = eh->dyn_relocs; p != NULL; p = p->next)
   8847     {
   8848       asection *sreloc = elf_section_data (p->sec)->sreloc;
   8849       if (!htab->elf.dynamic_sections_created)
   8850 	sreloc = htab->reliplt;
   8851       sreloc->size += p->count * sizeof (Elf64_External_Rela);
   8852     }
   8853 
   8854   return TRUE;
   8855 }
   8856 
   8857 /* Find any dynamic relocs that apply to read-only sections.  */
   8858 
   8859 static bfd_boolean
   8860 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
   8861 {
   8862   struct ppc_link_hash_entry *eh;
   8863   struct ppc_dyn_relocs *p;
   8864 
   8865   if (h->root.type == bfd_link_hash_warning)
   8866     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   8867 
   8868   eh = (struct ppc_link_hash_entry *) h;
   8869   for (p = eh->dyn_relocs; p != NULL; p = p->next)
   8870     {
   8871       asection *s = p->sec->output_section;
   8872 
   8873       if (s != NULL && (s->flags & SEC_READONLY) != 0)
   8874 	{
   8875 	  struct bfd_link_info *info = inf;
   8876 
   8877 	  info->flags |= DF_TEXTREL;
   8878 
   8879 	  /* Not an error, just cut short the traversal.  */
   8880 	  return FALSE;
   8881 	}
   8882     }
   8883   return TRUE;
   8884 }
   8885 
   8886 /* Set the sizes of the dynamic sections.  */
   8887 
   8888 static bfd_boolean
   8889 ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
   8890 				 struct bfd_link_info *info)
   8891 {
   8892   struct ppc_link_hash_table *htab;
   8893   bfd *dynobj;
   8894   asection *s;
   8895   bfd_boolean relocs;
   8896   bfd *ibfd;
   8897   struct got_entry *first_tlsld;
   8898 
   8899   htab = ppc_hash_table (info);
   8900   if (htab == NULL)
   8901     return FALSE;
   8902 
   8903   dynobj = htab->elf.dynobj;
   8904   if (dynobj == NULL)
   8905     abort ();
   8906 
   8907   if (htab->elf.dynamic_sections_created)
   8908     {
   8909       /* Set the contents of the .interp section to the interpreter.  */
   8910       if (info->executable)
   8911 	{
   8912 	  s = bfd_get_section_by_name (dynobj, ".interp");
   8913 	  if (s == NULL)
   8914 	    abort ();
   8915 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
   8916 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
   8917 	}
   8918     }
   8919 
   8920   /* Set up .got offsets for local syms, and space for local dynamic
   8921      relocs.  */
   8922   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   8923     {
   8924       struct got_entry **lgot_ents;
   8925       struct got_entry **end_lgot_ents;
   8926       struct plt_entry **local_plt;
   8927       struct plt_entry **end_local_plt;
   8928       unsigned char *lgot_masks;
   8929       bfd_size_type locsymcount;
   8930       Elf_Internal_Shdr *symtab_hdr;
   8931       asection *srel;
   8932 
   8933       if (!is_ppc64_elf (ibfd))
   8934 	continue;
   8935 
   8936       for (s = ibfd->sections; s != NULL; s = s->next)
   8937 	{
   8938 	  struct ppc_dyn_relocs *p;
   8939 
   8940 	  for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
   8941 	    {
   8942 	      if (!bfd_is_abs_section (p->sec)
   8943 		  && bfd_is_abs_section (p->sec->output_section))
   8944 		{
   8945 		  /* Input section has been discarded, either because
   8946 		     it is a copy of a linkonce section or due to
   8947 		     linker script /DISCARD/, so we'll be discarding
   8948 		     the relocs too.  */
   8949 		}
   8950 	      else if (p->count != 0)
   8951 		{
   8952 		  srel = elf_section_data (p->sec)->sreloc;
   8953 		  if (!htab->elf.dynamic_sections_created)
   8954 		    srel = htab->reliplt;
   8955 		  srel->size += p->count * sizeof (Elf64_External_Rela);
   8956 		  if ((p->sec->output_section->flags & SEC_READONLY) != 0)
   8957 		    info->flags |= DF_TEXTREL;
   8958 		}
   8959 	    }
   8960 	}
   8961 
   8962       lgot_ents = elf_local_got_ents (ibfd);
   8963       if (!lgot_ents)
   8964 	continue;
   8965 
   8966       symtab_hdr = &elf_symtab_hdr (ibfd);
   8967       locsymcount = symtab_hdr->sh_info;
   8968       end_lgot_ents = lgot_ents + locsymcount;
   8969       local_plt = (struct plt_entry **) end_lgot_ents;
   8970       end_local_plt = local_plt + locsymcount;
   8971       lgot_masks = (unsigned char *) end_local_plt;
   8972       s = ppc64_elf_tdata (ibfd)->got;
   8973       srel = ppc64_elf_tdata (ibfd)->relgot;
   8974       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
   8975 	{
   8976 	  struct got_entry **pent, *ent;
   8977 
   8978 	  pent = lgot_ents;
   8979 	  while ((ent = *pent) != NULL)
   8980 	    if (ent->got.refcount > 0)
   8981 	      {
   8982 		if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
   8983 		  {
   8984 		    ppc64_tlsld_got (ibfd)->got.refcount += 1;
   8985 		    *pent = ent->next;
   8986 		  }
   8987 		else
   8988 		  {
   8989 		    unsigned int num = 1;
   8990 		    ent->got.offset = s->size;
   8991 		    if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
   8992 		      num = 2;
   8993 		    s->size += num * 8;
   8994 		    if (info->shared)
   8995 		      srel->size += num * sizeof (Elf64_External_Rela);
   8996 		    else if ((*lgot_masks & PLT_IFUNC) != 0)
   8997 		      {
   8998 			htab->reliplt->size
   8999 			  += num * sizeof (Elf64_External_Rela);
   9000 			htab->got_reli_size
   9001 			  += num * sizeof (Elf64_External_Rela);
   9002 		      }
   9003 		    pent = &ent->next;
   9004 		  }
   9005 	      }
   9006 	    else
   9007 	      *pent = ent->next;
   9008 	}
   9009 
   9010       /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt.  */
   9011       for (; local_plt < end_local_plt; ++local_plt)
   9012 	{
   9013 	  struct plt_entry *ent;
   9014 
   9015 	  for (ent = *local_plt; ent != NULL; ent = ent->next)
   9016 	    if (ent->plt.refcount > 0)
   9017 	      {
   9018 		s = htab->iplt;
   9019 		ent->plt.offset = s->size;
   9020 		s->size += PLT_ENTRY_SIZE;
   9021 
   9022 		htab->reliplt->size += sizeof (Elf64_External_Rela);
   9023 	      }
   9024 	    else
   9025 	      ent->plt.offset = (bfd_vma) -1;
   9026 	}
   9027     }
   9028 
   9029   /* Allocate global sym .plt and .got entries, and space for global
   9030      sym dynamic relocs.  */
   9031   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
   9032 
   9033   first_tlsld = NULL;
   9034   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   9035     {
   9036       struct got_entry *ent;
   9037 
   9038       if (!is_ppc64_elf (ibfd))
   9039 	continue;
   9040 
   9041       ent = ppc64_tlsld_got (ibfd);
   9042       if (ent->got.refcount > 0)
   9043 	{
   9044 	  if (!htab->do_multi_toc && first_tlsld != NULL)
   9045 	    {
   9046 	      ent->is_indirect = TRUE;
   9047 	      ent->got.ent = first_tlsld;
   9048 	    }
   9049 	  else
   9050 	    {
   9051 	      if (first_tlsld == NULL)
   9052 		first_tlsld = ent;
   9053 	      s = ppc64_elf_tdata (ibfd)->got;
   9054 	      ent->got.offset = s->size;
   9055 	      ent->owner = ibfd;
   9056 	      s->size += 16;
   9057 	      if (info->shared)
   9058 		{
   9059 		  asection *srel = ppc64_elf_tdata (ibfd)->relgot;
   9060 		  srel->size += sizeof (Elf64_External_Rela);
   9061 		}
   9062 	    }
   9063 	}
   9064       else
   9065 	ent->got.offset = (bfd_vma) -1;
   9066     }
   9067 
   9068   /* We now have determined the sizes of the various dynamic sections.
   9069      Allocate memory for them.  */
   9070   relocs = FALSE;
   9071   for (s = dynobj->sections; s != NULL; s = s->next)
   9072     {
   9073       if ((s->flags & SEC_LINKER_CREATED) == 0)
   9074 	continue;
   9075 
   9076       if (s == htab->brlt || s == htab->relbrlt)
   9077 	/* These haven't been allocated yet;  don't strip.  */
   9078 	continue;
   9079       else if (s == htab->got
   9080 	       || s == htab->plt
   9081 	       || s == htab->iplt
   9082 	       || s == htab->glink
   9083 	       || s == htab->dynbss)
   9084 	{
   9085 	  /* Strip this section if we don't need it; see the
   9086 	     comment below.  */
   9087 	}
   9088       else if (CONST_STRNEQ (s->name, ".rela"))
   9089 	{
   9090 	  if (s->size != 0)
   9091 	    {
   9092 	      if (s != htab->relplt)
   9093 		relocs = TRUE;
   9094 
   9095 	      /* We use the reloc_count field as a counter if we need
   9096 		 to copy relocs into the output file.  */
   9097 	      s->reloc_count = 0;
   9098 	    }
   9099 	}
   9100       else
   9101 	{
   9102 	  /* It's not one of our sections, so don't allocate space.  */
   9103 	  continue;
   9104 	}
   9105 
   9106       if (s->size == 0)
   9107 	{
   9108 	  /* If we don't need this section, strip it from the
   9109 	     output file.  This is mostly to handle .rela.bss and
   9110 	     .rela.plt.  We must create both sections in
   9111 	     create_dynamic_sections, because they must be created
   9112 	     before the linker maps input sections to output
   9113 	     sections.  The linker does that before
   9114 	     adjust_dynamic_symbol is called, and it is that
   9115 	     function which decides whether anything needs to go
   9116 	     into these sections.  */
   9117 	  s->flags |= SEC_EXCLUDE;
   9118 	  continue;
   9119 	}
   9120 
   9121       if ((s->flags & SEC_HAS_CONTENTS) == 0)
   9122 	continue;
   9123 
   9124       /* Allocate memory for the section contents.  We use bfd_zalloc
   9125 	 here in case unused entries are not reclaimed before the
   9126 	 section's contents are written out.  This should not happen,
   9127 	 but this way if it does we get a R_PPC64_NONE reloc in .rela
   9128 	 sections instead of garbage.
   9129 	 We also rely on the section contents being zero when writing
   9130 	 the GOT.  */
   9131       s->contents = bfd_zalloc (dynobj, s->size);
   9132       if (s->contents == NULL)
   9133 	return FALSE;
   9134     }
   9135 
   9136   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   9137     {
   9138       if (!is_ppc64_elf (ibfd))
   9139 	continue;
   9140 
   9141       s = ppc64_elf_tdata (ibfd)->got;
   9142       if (s != NULL && s != htab->got)
   9143 	{
   9144 	  if (s->size == 0)
   9145 	    s->flags |= SEC_EXCLUDE;
   9146 	  else
   9147 	    {
   9148 	      s->contents = bfd_zalloc (ibfd, s->size);
   9149 	      if (s->contents == NULL)
   9150 		return FALSE;
   9151 	    }
   9152 	}
   9153       s = ppc64_elf_tdata (ibfd)->relgot;
   9154       if (s != NULL)
   9155 	{
   9156 	  if (s->size == 0)
   9157 	    s->flags |= SEC_EXCLUDE;
   9158 	  else
   9159 	    {
   9160 	      s->contents = bfd_zalloc (ibfd, s->size);
   9161 	      if (s->contents == NULL)
   9162 		return FALSE;
   9163 	      relocs = TRUE;
   9164 	      s->reloc_count = 0;
   9165 	    }
   9166 	}
   9167     }
   9168 
   9169   if (htab->elf.dynamic_sections_created)
   9170     {
   9171       /* Add some entries to the .dynamic section.  We fill in the
   9172 	 values later, in ppc64_elf_finish_dynamic_sections, but we
   9173 	 must add the entries now so that we get the correct size for
   9174 	 the .dynamic section.  The DT_DEBUG entry is filled in by the
   9175 	 dynamic linker and used by the debugger.  */
   9176 #define add_dynamic_entry(TAG, VAL) \
   9177   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
   9178 
   9179       if (info->executable)
   9180 	{
   9181 	  if (!add_dynamic_entry (DT_DEBUG, 0))
   9182 	    return FALSE;
   9183 	}
   9184 
   9185       if (htab->plt != NULL && htab->plt->size != 0)
   9186 	{
   9187 	  if (!add_dynamic_entry (DT_PLTGOT, 0)
   9188 	      || !add_dynamic_entry (DT_PLTRELSZ, 0)
   9189 	      || !add_dynamic_entry (DT_PLTREL, DT_RELA)
   9190 	      || !add_dynamic_entry (DT_JMPREL, 0)
   9191 	      || !add_dynamic_entry (DT_PPC64_GLINK, 0))
   9192 	    return FALSE;
   9193 	}
   9194 
   9195       if (NO_OPD_RELOCS)
   9196 	{
   9197 	  if (!add_dynamic_entry (DT_PPC64_OPD, 0)
   9198 	      || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
   9199 	    return FALSE;
   9200 	}
   9201 
   9202       if (!htab->no_tls_get_addr_opt
   9203 	  && htab->tls_get_addr_fd != NULL
   9204 	  && htab->tls_get_addr_fd->elf.plt.plist != NULL
   9205 	  && !add_dynamic_entry (DT_PPC64_TLSOPT, 0))
   9206 	return FALSE;
   9207 
   9208       if (relocs)
   9209 	{
   9210 	  if (!add_dynamic_entry (DT_RELA, 0)
   9211 	      || !add_dynamic_entry (DT_RELASZ, 0)
   9212 	      || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
   9213 	    return FALSE;
   9214 
   9215 	  /* If any dynamic relocs apply to a read-only section,
   9216 	     then we need a DT_TEXTREL entry.  */
   9217 	  if ((info->flags & DF_TEXTREL) == 0)
   9218 	    elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
   9219 
   9220 	  if ((info->flags & DF_TEXTREL) != 0)
   9221 	    {
   9222 	      if (!add_dynamic_entry (DT_TEXTREL, 0))
   9223 		return FALSE;
   9224 	    }
   9225 	}
   9226     }
   9227 #undef add_dynamic_entry
   9228 
   9229   return TRUE;
   9230 }
   9231 
   9232 /* Determine the type of stub needed, if any, for a call.  */
   9233 
   9234 static inline enum ppc_stub_type
   9235 ppc_type_of_stub (asection *input_sec,
   9236 		  const Elf_Internal_Rela *rel,
   9237 		  struct ppc_link_hash_entry **hash,
   9238 		  struct plt_entry **plt_ent,
   9239 		  bfd_vma destination)
   9240 {
   9241   struct ppc_link_hash_entry *h = *hash;
   9242   bfd_vma location;
   9243   bfd_vma branch_offset;
   9244   bfd_vma max_branch_offset;
   9245   enum elf_ppc64_reloc_type r_type;
   9246 
   9247   if (h != NULL)
   9248     {
   9249       struct plt_entry *ent;
   9250       struct ppc_link_hash_entry *fdh = h;
   9251       if (h->oh != NULL
   9252 	  && h->oh->is_func_descriptor)
   9253 	{
   9254 	  fdh = ppc_follow_link (h->oh);
   9255 	  *hash = fdh;
   9256 	}
   9257 
   9258       for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
   9259 	if (ent->addend == rel->r_addend
   9260 	    && ent->plt.offset != (bfd_vma) -1)
   9261 	  {
   9262 	    *plt_ent = ent;
   9263 	    return ppc_stub_plt_call;
   9264 	  }
   9265 
   9266       /* Here, we know we don't have a plt entry.  If we don't have a
   9267 	 either a defined function descriptor or a defined entry symbol
   9268 	 in a regular object file, then it is pointless trying to make
   9269 	 any other type of stub.  */
   9270       if (!is_static_defined (&fdh->elf)
   9271 	  && !is_static_defined (&h->elf))
   9272 	return ppc_stub_none;
   9273     }
   9274   else if (elf_local_got_ents (input_sec->owner) != NULL)
   9275     {
   9276       Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
   9277       struct plt_entry **local_plt = (struct plt_entry **)
   9278 	elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
   9279       unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
   9280 
   9281       if (local_plt[r_symndx] != NULL)
   9282 	{
   9283 	  struct plt_entry *ent;
   9284 
   9285 	  for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
   9286 	    if (ent->addend == rel->r_addend
   9287 		&& ent->plt.offset != (bfd_vma) -1)
   9288 	      {
   9289 		*plt_ent = ent;
   9290 		return ppc_stub_plt_call;
   9291 	      }
   9292 	}
   9293     }
   9294 
   9295   /* Determine where the call point is.  */
   9296   location = (input_sec->output_offset
   9297 	      + input_sec->output_section->vma
   9298 	      + rel->r_offset);
   9299 
   9300   branch_offset = destination - location;
   9301   r_type = ELF64_R_TYPE (rel->r_info);
   9302 
   9303   /* Determine if a long branch stub is needed.  */
   9304   max_branch_offset = 1 << 25;
   9305   if (r_type != R_PPC64_REL24)
   9306     max_branch_offset = 1 << 15;
   9307 
   9308   if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
   9309     /* We need a stub.  Figure out whether a long_branch or plt_branch
   9310        is needed later.  */
   9311     return ppc_stub_long_branch;
   9312 
   9313   return ppc_stub_none;
   9314 }
   9315 
   9316 /* Build a .plt call stub.  */
   9317 
   9318 static inline bfd_byte *
   9319 build_plt_stub (bfd *obfd, bfd_byte *p, int offset, Elf_Internal_Rela *r)
   9320 {
   9321 #define PPC_LO(v) ((v) & 0xffff)
   9322 #define PPC_HI(v) (((v) >> 16) & 0xffff)
   9323 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
   9324 
   9325   if (PPC_HA (offset) != 0)
   9326     {
   9327       if (r != NULL)
   9328 	{
   9329 	  r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
   9330 	  r[1].r_offset = r[0].r_offset + 8;
   9331 	  r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
   9332 	  r[1].r_addend = r[0].r_addend;
   9333 	  if (PPC_HA (offset + 16) != PPC_HA (offset))
   9334 	    {
   9335 	      r[2].r_offset = r[1].r_offset + 4;
   9336 	      r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
   9337 	      r[2].r_addend = r[0].r_addend;
   9338 	    }
   9339 	  else
   9340 	    {
   9341 	      r[2].r_offset = r[1].r_offset + 8;
   9342 	      r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
   9343 	      r[2].r_addend = r[0].r_addend + 8;
   9344 	      r[3].r_offset = r[2].r_offset + 4;
   9345 	      r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
   9346 	      r[3].r_addend = r[0].r_addend + 16;
   9347 	    }
   9348 	}
   9349       bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p),	p += 4;
   9350       bfd_put_32 (obfd, STD_R2_40R1, p),			p += 4;
   9351       bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p),	p += 4;
   9352       if (PPC_HA (offset + 16) != PPC_HA (offset))
   9353 	{
   9354 	  bfd_put_32 (obfd, ADDI_R12_R12 | PPC_LO (offset), p),	p += 4;
   9355 	  offset = 0;
   9356 	}
   9357       bfd_put_32 (obfd, MTCTR_R11, p),				p += 4;
   9358       bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset + 8), p),	p += 4;
   9359       bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset + 16), p),	p += 4;
   9360       bfd_put_32 (obfd, BCTR, p),				p += 4;
   9361     }
   9362   else
   9363     {
   9364       if (r != NULL)
   9365 	{
   9366 	  r[0].r_offset += 4;
   9367 	  r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
   9368 	  if (PPC_HA (offset + 16) != PPC_HA (offset))
   9369 	    {
   9370 	      r[1].r_offset = r[0].r_offset + 4;
   9371 	      r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
   9372 	      r[1].r_addend = r[0].r_addend;
   9373 	    }
   9374 	  else
   9375 	    {
   9376 	      r[1].r_offset = r[0].r_offset + 8;
   9377 	      r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
   9378 	      r[1].r_addend = r[0].r_addend + 16;
   9379 	      r[2].r_offset = r[1].r_offset + 4;
   9380 	      r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
   9381 	      r[2].r_addend = r[0].r_addend + 8;
   9382 	    }
   9383 	}
   9384       bfd_put_32 (obfd, STD_R2_40R1, p),			p += 4;
   9385       bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset), p),	p += 4;
   9386       if (PPC_HA (offset + 16) != PPC_HA (offset))
   9387 	{
   9388 	  bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p),	p += 4;
   9389 	  offset = 0;
   9390 	}
   9391       bfd_put_32 (obfd, MTCTR_R11, p),				p += 4;
   9392       bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p),	p += 4;
   9393       bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p),	p += 4;
   9394       bfd_put_32 (obfd, BCTR, p),				p += 4;
   9395     }
   9396   return p;
   9397 }
   9398 
   9399 /* Build a special .plt call stub for __tls_get_addr.  */
   9400 
   9401 #define LD_R11_0R3	0xe9630000
   9402 #define LD_R12_0R3	0xe9830000
   9403 #define MR_R0_R3	0x7c601b78
   9404 #define CMPDI_R11_0	0x2c2b0000
   9405 #define ADD_R3_R12_R13	0x7c6c6a14
   9406 #define BEQLR		0x4d820020
   9407 #define MR_R3_R0	0x7c030378
   9408 #define MFLR_R11	0x7d6802a6
   9409 #define STD_R11_0R1	0xf9610000
   9410 #define BCTRL		0x4e800421
   9411 #define LD_R11_0R1	0xe9610000
   9412 #define LD_R2_0R1	0xe8410000
   9413 #define MTLR_R11	0x7d6803a6
   9414 
   9415 static inline bfd_byte *
   9416 build_tls_get_addr_stub (bfd *obfd, bfd_byte *p, int offset,
   9417 			 Elf_Internal_Rela *r)
   9418 {
   9419   bfd_put_32 (obfd, LD_R11_0R3 + 0, p),		p += 4;
   9420   bfd_put_32 (obfd, LD_R12_0R3 + 8, p),		p += 4;
   9421   bfd_put_32 (obfd, MR_R0_R3, p),		p += 4;
   9422   bfd_put_32 (obfd, CMPDI_R11_0, p),		p += 4;
   9423   bfd_put_32 (obfd, ADD_R3_R12_R13, p),		p += 4;
   9424   bfd_put_32 (obfd, BEQLR, p),			p += 4;
   9425   bfd_put_32 (obfd, MR_R3_R0, p),		p += 4;
   9426   bfd_put_32 (obfd, MFLR_R11, p),		p += 4;
   9427   bfd_put_32 (obfd, STD_R11_0R1 + 32, p),	p += 4;
   9428 
   9429   if (r != NULL)
   9430     r[0].r_offset += 9 * 4;
   9431   p = build_plt_stub (obfd, p, offset, r);
   9432   bfd_put_32 (obfd, BCTRL, p - 4);
   9433 
   9434   bfd_put_32 (obfd, LD_R11_0R1 + 32, p),	p += 4;
   9435   bfd_put_32 (obfd, LD_R2_0R1 + 40, p),		p += 4;
   9436   bfd_put_32 (obfd, MTLR_R11, p),		p += 4;
   9437   bfd_put_32 (obfd, BLR, p),			p += 4;
   9438 
   9439   return p;
   9440 }
   9441 
   9442 static Elf_Internal_Rela *
   9443 get_relocs (asection *sec, int count)
   9444 {
   9445   Elf_Internal_Rela *relocs;
   9446   struct bfd_elf_section_data *elfsec_data;
   9447 
   9448   elfsec_data = elf_section_data (sec);
   9449   relocs = elfsec_data->relocs;
   9450   if (relocs == NULL)
   9451     {
   9452       bfd_size_type relsize;
   9453       relsize = sec->reloc_count * sizeof (*relocs);
   9454       relocs = bfd_alloc (sec->owner, relsize);
   9455       if (relocs == NULL)
   9456 	return NULL;
   9457       elfsec_data->relocs = relocs;
   9458       elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
   9459 					  sizeof (Elf_Internal_Shdr));
   9460       if (elfsec_data->rela.hdr == NULL)
   9461 	return NULL;
   9462       elfsec_data->rela.hdr->sh_size = (sec->reloc_count
   9463 					* sizeof (Elf64_External_Rela));
   9464       elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
   9465       sec->reloc_count = 0;
   9466     }
   9467   relocs += sec->reloc_count;
   9468   sec->reloc_count += count;
   9469   return relocs;
   9470 }
   9471 
   9472 static bfd_vma
   9473 get_r2off (struct ppc_link_hash_table *htab,
   9474 	   struct ppc_stub_hash_entry *stub_entry)
   9475 {
   9476   bfd_vma r2off = htab->stub_group[stub_entry->target_section->id].toc_off;
   9477 
   9478   if (r2off == 0)
   9479     {
   9480       /* Support linking -R objects.  Get the toc pointer from the
   9481 	 opd entry.  */
   9482       char buf[8];
   9483       asection *opd = stub_entry->h->elf.root.u.def.section;
   9484       bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
   9485 
   9486       if (strcmp (opd->name, ".opd") != 0
   9487 	  || opd->reloc_count != 0)
   9488 	{
   9489 	  (*_bfd_error_handler) (_("cannot find opd entry toc for %s"),
   9490 				 stub_entry->h->elf.root.root.string);
   9491 	  bfd_set_error (bfd_error_bad_value);
   9492 	  return 0;
   9493 	}
   9494       if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
   9495 	return 0;
   9496       r2off = bfd_get_64 (opd->owner, buf);
   9497       r2off -= elf_gp (stub_entry->id_sec->output_section->owner);
   9498     }
   9499   r2off -= htab->stub_group[stub_entry->id_sec->id].toc_off;
   9500   return r2off;
   9501 }
   9502 
   9503 static bfd_boolean
   9504 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
   9505 {
   9506   struct ppc_stub_hash_entry *stub_entry;
   9507   struct ppc_branch_hash_entry *br_entry;
   9508   struct bfd_link_info *info;
   9509   struct ppc_link_hash_table *htab;
   9510   bfd_byte *loc;
   9511   bfd_byte *p;
   9512   bfd_vma dest, off;
   9513   int size;
   9514   Elf_Internal_Rela *r;
   9515   asection *plt;
   9516 
   9517   /* Massage our args to the form they really have.  */
   9518   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
   9519   info = in_arg;
   9520 
   9521   htab = ppc_hash_table (info);
   9522   if (htab == NULL)
   9523     return FALSE;
   9524 
   9525   /* Make a note of the offset within the stubs for this entry.  */
   9526   stub_entry->stub_offset = stub_entry->stub_sec->size;
   9527   loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
   9528 
   9529   htab->stub_count[stub_entry->stub_type - 1] += 1;
   9530   switch (stub_entry->stub_type)
   9531     {
   9532     case ppc_stub_long_branch:
   9533     case ppc_stub_long_branch_r2off:
   9534       /* Branches are relative.  This is where we are going to.  */
   9535       off = dest = (stub_entry->target_value
   9536 		    + stub_entry->target_section->output_offset
   9537 		    + stub_entry->target_section->output_section->vma);
   9538 
   9539       /* And this is where we are coming from.  */
   9540       off -= (stub_entry->stub_offset
   9541 	      + stub_entry->stub_sec->output_offset
   9542 	      + stub_entry->stub_sec->output_section->vma);
   9543 
   9544       size = 4;
   9545       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
   9546 	{
   9547 	  bfd_vma r2off = get_r2off (htab, stub_entry);
   9548 
   9549 	  if (r2off == 0)
   9550 	    {
   9551 	      htab->stub_error = TRUE;
   9552 	      return FALSE;
   9553 	    }
   9554 	  bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
   9555 	  loc += 4;
   9556 	  size = 12;
   9557 	  if (PPC_HA (r2off) != 0)
   9558 	    {
   9559 	      size = 16;
   9560 	      bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
   9561 	      loc += 4;
   9562 	    }
   9563 	  bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
   9564 	  loc += 4;
   9565 	  off -= size - 4;
   9566 	}
   9567       bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
   9568 
   9569       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
   9570 	{
   9571 	  (*_bfd_error_handler) (_("long branch stub `%s' offset overflow"),
   9572 				 stub_entry->root.string);
   9573 	  htab->stub_error = TRUE;
   9574 	  return FALSE;
   9575 	}
   9576 
   9577       if (info->emitrelocations)
   9578 	{
   9579 	  r = get_relocs (stub_entry->stub_sec, 1);
   9580 	  if (r == NULL)
   9581 	    return FALSE;
   9582 	  r->r_offset = loc - stub_entry->stub_sec->contents;
   9583 	  r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
   9584 	  r->r_addend = dest;
   9585 	  if (stub_entry->h != NULL)
   9586 	    {
   9587 	      struct elf_link_hash_entry **hashes;
   9588 	      unsigned long symndx;
   9589 	      struct ppc_link_hash_entry *h;
   9590 
   9591 	      hashes = elf_sym_hashes (htab->stub_bfd);
   9592 	      if (hashes == NULL)
   9593 		{
   9594 		  bfd_size_type hsize;
   9595 
   9596 		  hsize = (htab->stub_globals + 1) * sizeof (*hashes);
   9597 		  hashes = bfd_zalloc (htab->stub_bfd, hsize);
   9598 		  if (hashes == NULL)
   9599 		    return FALSE;
   9600 		  elf_sym_hashes (htab->stub_bfd) = hashes;
   9601 		  htab->stub_globals = 1;
   9602 		}
   9603 	      symndx = htab->stub_globals++;
   9604 	      h = stub_entry->h;
   9605 	      hashes[symndx] = &h->elf;
   9606 	      r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
   9607 	      if (h->oh != NULL && h->oh->is_func)
   9608 		h = ppc_follow_link (h->oh);
   9609 	      if (h->elf.root.u.def.section != stub_entry->target_section)
   9610 		/* H is an opd symbol.  The addend must be zero.  */
   9611 		r->r_addend = 0;
   9612 	      else
   9613 		{
   9614 		  off = (h->elf.root.u.def.value
   9615 			 + h->elf.root.u.def.section->output_offset
   9616 			 + h->elf.root.u.def.section->output_section->vma);
   9617 		  r->r_addend -= off;
   9618 		}
   9619 	    }
   9620 	}
   9621       break;
   9622 
   9623     case ppc_stub_plt_branch:
   9624     case ppc_stub_plt_branch_r2off:
   9625       br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
   9626 					 stub_entry->root.string + 9,
   9627 					 FALSE, FALSE);
   9628       if (br_entry == NULL)
   9629 	{
   9630 	  (*_bfd_error_handler) (_("can't find branch stub `%s'"),
   9631 				 stub_entry->root.string);
   9632 	  htab->stub_error = TRUE;
   9633 	  return FALSE;
   9634 	}
   9635 
   9636       dest = (stub_entry->target_value
   9637 	      + stub_entry->target_section->output_offset
   9638 	      + stub_entry->target_section->output_section->vma);
   9639 
   9640       bfd_put_64 (htab->brlt->owner, dest,
   9641 		  htab->brlt->contents + br_entry->offset);
   9642 
   9643       if (br_entry->iter == htab->stub_iteration)
   9644 	{
   9645 	  br_entry->iter = 0;
   9646 
   9647 	  if (htab->relbrlt != NULL)
   9648 	    {
   9649 	      /* Create a reloc for the branch lookup table entry.  */
   9650 	      Elf_Internal_Rela rela;
   9651 	      bfd_byte *rl;
   9652 
   9653 	      rela.r_offset = (br_entry->offset
   9654 			       + htab->brlt->output_offset
   9655 			       + htab->brlt->output_section->vma);
   9656 	      rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   9657 	      rela.r_addend = dest;
   9658 
   9659 	      rl = htab->relbrlt->contents;
   9660 	      rl += (htab->relbrlt->reloc_count++
   9661 		     * sizeof (Elf64_External_Rela));
   9662 	      bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
   9663 	    }
   9664 	  else if (info->emitrelocations)
   9665 	    {
   9666 	      r = get_relocs (htab->brlt, 1);
   9667 	      if (r == NULL)
   9668 		return FALSE;
   9669 	      /* brlt, being SEC_LINKER_CREATED does not go through the
   9670 		 normal reloc processing.  Symbols and offsets are not
   9671 		 translated from input file to output file form, so
   9672 		 set up the offset per the output file.  */
   9673 	      r->r_offset = (br_entry->offset
   9674 			     + htab->brlt->output_offset
   9675 			     + htab->brlt->output_section->vma);
   9676 	      r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   9677 	      r->r_addend = dest;
   9678 	    }
   9679 	}
   9680 
   9681       dest = (br_entry->offset
   9682 	      + htab->brlt->output_offset
   9683 	      + htab->brlt->output_section->vma);
   9684 
   9685       off = (dest
   9686 	     - elf_gp (htab->brlt->output_section->owner)
   9687 	     - htab->stub_group[stub_entry->id_sec->id].toc_off);
   9688 
   9689       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
   9690 	{
   9691 	  (*_bfd_error_handler)
   9692 	    (_("linkage table error against `%s'"),
   9693 	     stub_entry->root.string);
   9694 	  bfd_set_error (bfd_error_bad_value);
   9695 	  htab->stub_error = TRUE;
   9696 	  return FALSE;
   9697 	}
   9698 
   9699       if (info->emitrelocations)
   9700 	{
   9701 	  r = get_relocs (stub_entry->stub_sec, 1 + (PPC_HA (off) != 0));
   9702 	  if (r == NULL)
   9703 	    return FALSE;
   9704 	  r[0].r_offset = loc - stub_entry->stub_sec->contents;
   9705 	  if (bfd_big_endian (info->output_bfd))
   9706 	    r[0].r_offset += 2;
   9707 	  if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
   9708 	    r[0].r_offset += 4;
   9709 	  r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
   9710 	  r[0].r_addend = dest;
   9711 	  if (PPC_HA (off) != 0)
   9712 	    {
   9713 	      r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
   9714 	      r[1].r_offset = r[0].r_offset + 4;
   9715 	      r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
   9716 	      r[1].r_addend = r[0].r_addend;
   9717 	    }
   9718 	}
   9719 
   9720       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
   9721 	{
   9722 	  if (PPC_HA (off) != 0)
   9723 	    {
   9724 	      size = 16;
   9725 	      bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
   9726 	      loc += 4;
   9727 	      bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
   9728 	    }
   9729 	  else
   9730 	    {
   9731 	      size = 12;
   9732 	      bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
   9733 	    }
   9734 	}
   9735       else
   9736 	{
   9737 	  bfd_vma r2off = get_r2off (htab, stub_entry);
   9738 
   9739 	  if (r2off == 0)
   9740 	    {
   9741 	      htab->stub_error = TRUE;
   9742 	      return FALSE;
   9743 	    }
   9744 
   9745 	  bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
   9746 	  loc += 4;
   9747 	  size = 20;
   9748 	  if (PPC_HA (off) != 0)
   9749 	    {
   9750 	      size += 4;
   9751 	      bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
   9752 	      loc += 4;
   9753 	      bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
   9754 	      loc += 4;
   9755 	    }
   9756 	  else
   9757 	    {
   9758 	      bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
   9759 	      loc += 4;
   9760 	    }
   9761 
   9762 	  if (PPC_HA (r2off) != 0)
   9763 	    {
   9764 	      size += 4;
   9765 	      bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
   9766 	      loc += 4;
   9767 	    }
   9768 	  bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
   9769 	}
   9770       loc += 4;
   9771       bfd_put_32 (htab->stub_bfd, MTCTR_R11, loc);
   9772       loc += 4;
   9773       bfd_put_32 (htab->stub_bfd, BCTR, loc);
   9774       break;
   9775 
   9776     case ppc_stub_plt_call:
   9777       if (stub_entry->h != NULL
   9778 	  && stub_entry->h->is_func_descriptor
   9779 	  && stub_entry->h->oh != NULL)
   9780 	{
   9781 	  struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
   9782 
   9783 	  /* If the old-ABI "dot-symbol" is undefined make it weak so
   9784 	     we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.
   9785 	     FIXME: We used to define the symbol on one of the call
   9786 	     stubs instead, which is why we test symbol section id
   9787 	     against htab->top_id in various places.  Likely all
   9788 	     these checks could now disappear.  */
   9789 	  if (fh->elf.root.type == bfd_link_hash_undefined)
   9790 	    fh->elf.root.type = bfd_link_hash_undefweak;
   9791 	}
   9792 
   9793       /* Now build the stub.  */
   9794       dest = stub_entry->plt_ent->plt.offset & ~1;
   9795       if (dest >= (bfd_vma) -2)
   9796 	abort ();
   9797 
   9798       plt = htab->plt;
   9799       if (!htab->elf.dynamic_sections_created
   9800 	  || stub_entry->h == NULL
   9801 	  || stub_entry->h->elf.dynindx == -1)
   9802 	plt = htab->iplt;
   9803 
   9804       dest += plt->output_offset + plt->output_section->vma;
   9805 
   9806       if (stub_entry->h == NULL
   9807 	  && (stub_entry->plt_ent->plt.offset & 1) == 0)
   9808 	{
   9809 	  Elf_Internal_Rela rela;
   9810 	  bfd_byte *rl;
   9811 
   9812 	  rela.r_offset = dest;
   9813 	  rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
   9814 	  rela.r_addend = (stub_entry->target_value
   9815 			   + stub_entry->target_section->output_offset
   9816 			   + stub_entry->target_section->output_section->vma);
   9817 
   9818 	  rl = (htab->reliplt->contents
   9819 		+ (htab->reliplt->reloc_count++
   9820 		   * sizeof (Elf64_External_Rela)));
   9821 	  bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
   9822 	  stub_entry->plt_ent->plt.offset |= 1;
   9823 	}
   9824 
   9825       off = (dest
   9826 	     - elf_gp (plt->output_section->owner)
   9827 	     - htab->stub_group[stub_entry->id_sec->id].toc_off);
   9828 
   9829       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
   9830 	{
   9831 	  (*_bfd_error_handler)
   9832 	    (_("linkage table error against `%s'"),
   9833 	     stub_entry->h != NULL
   9834 	     ? stub_entry->h->elf.root.root.string
   9835 	     : "<local sym>");
   9836 	  bfd_set_error (bfd_error_bad_value);
   9837 	  htab->stub_error = TRUE;
   9838 	  return FALSE;
   9839 	}
   9840 
   9841       r = NULL;
   9842       if (info->emitrelocations)
   9843 	{
   9844 	  r = get_relocs (stub_entry->stub_sec,
   9845 			  (2 + (PPC_HA (off) != 0)
   9846 			   + (PPC_HA (off + 16) == PPC_HA (off))));
   9847 	  if (r == NULL)
   9848 	    return FALSE;
   9849 	  r[0].r_offset = loc - stub_entry->stub_sec->contents;
   9850 	  if (bfd_big_endian (info->output_bfd))
   9851 	    r[0].r_offset += 2;
   9852 	  r[0].r_addend = dest;
   9853 	}
   9854       if (stub_entry->h != NULL
   9855 	  && (stub_entry->h == htab->tls_get_addr_fd
   9856 	      || stub_entry->h == htab->tls_get_addr)
   9857 	  && !htab->no_tls_get_addr_opt)
   9858 	p = build_tls_get_addr_stub (htab->stub_bfd, loc, off, r);
   9859       else
   9860 	p = build_plt_stub (htab->stub_bfd, loc, off, r);
   9861       size = p - loc;
   9862       break;
   9863 
   9864     default:
   9865       BFD_FAIL ();
   9866       return FALSE;
   9867     }
   9868 
   9869   stub_entry->stub_sec->size += size;
   9870 
   9871   if (htab->emit_stub_syms)
   9872     {
   9873       struct elf_link_hash_entry *h;
   9874       size_t len1, len2;
   9875       char *name;
   9876       const char *const stub_str[] = { "long_branch",
   9877 				       "long_branch_r2off",
   9878 				       "plt_branch",
   9879 				       "plt_branch_r2off",
   9880 				       "plt_call" };
   9881 
   9882       len1 = strlen (stub_str[stub_entry->stub_type - 1]);
   9883       len2 = strlen (stub_entry->root.string);
   9884       name = bfd_malloc (len1 + len2 + 2);
   9885       if (name == NULL)
   9886 	return FALSE;
   9887       memcpy (name, stub_entry->root.string, 9);
   9888       memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
   9889       memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
   9890       h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
   9891       if (h == NULL)
   9892 	return FALSE;
   9893       if (h->root.type == bfd_link_hash_new)
   9894 	{
   9895 	  h->root.type = bfd_link_hash_defined;
   9896 	  h->root.u.def.section = stub_entry->stub_sec;
   9897 	  h->root.u.def.value = stub_entry->stub_offset;
   9898 	  h->ref_regular = 1;
   9899 	  h->def_regular = 1;
   9900 	  h->ref_regular_nonweak = 1;
   9901 	  h->forced_local = 1;
   9902 	  h->non_elf = 0;
   9903 	}
   9904     }
   9905 
   9906   return TRUE;
   9907 }
   9908 
   9909 /* As above, but don't actually build the stub.  Just bump offset so
   9910    we know stub section sizes, and select plt_branch stubs where
   9911    long_branch stubs won't do.  */
   9912 
   9913 static bfd_boolean
   9914 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
   9915 {
   9916   struct ppc_stub_hash_entry *stub_entry;
   9917   struct bfd_link_info *info;
   9918   struct ppc_link_hash_table *htab;
   9919   bfd_vma off;
   9920   int size;
   9921 
   9922   /* Massage our args to the form they really have.  */
   9923   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
   9924   info = in_arg;
   9925 
   9926   htab = ppc_hash_table (info);
   9927   if (htab == NULL)
   9928     return FALSE;
   9929 
   9930   if (stub_entry->stub_type == ppc_stub_plt_call)
   9931     {
   9932       asection *plt;
   9933       off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
   9934       if (off >= (bfd_vma) -2)
   9935 	abort ();
   9936       plt = htab->plt;
   9937       if (!htab->elf.dynamic_sections_created
   9938 	  || stub_entry->h == NULL
   9939 	  || stub_entry->h->elf.dynindx == -1)
   9940 	plt = htab->iplt;
   9941       off += (plt->output_offset
   9942 	      + plt->output_section->vma
   9943 	      - elf_gp (plt->output_section->owner)
   9944 	      - htab->stub_group[stub_entry->id_sec->id].toc_off);
   9945 
   9946       size = PLT_CALL_STUB_SIZE;
   9947       if (PPC_HA (off) == 0)
   9948 	size -= 4;
   9949       if (PPC_HA (off + 16) != PPC_HA (off))
   9950 	size += 4;
   9951       if (stub_entry->h != NULL
   9952 	  && (stub_entry->h == htab->tls_get_addr_fd
   9953 	      || stub_entry->h == htab->tls_get_addr)
   9954 	  && !htab->no_tls_get_addr_opt)
   9955 	size += 13 * 4;
   9956       if (info->emitrelocations)
   9957 	{
   9958 	  stub_entry->stub_sec->reloc_count
   9959 	    += 2 + (PPC_HA (off) != 0) + (PPC_HA (off + 16) == PPC_HA (off));
   9960 	  stub_entry->stub_sec->flags |= SEC_RELOC;
   9961 	}
   9962     }
   9963   else
   9964     {
   9965       /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
   9966 	 variants.  */
   9967       bfd_vma r2off = 0;
   9968 
   9969       off = (stub_entry->target_value
   9970 	     + stub_entry->target_section->output_offset
   9971 	     + stub_entry->target_section->output_section->vma);
   9972       off -= (stub_entry->stub_sec->size
   9973 	      + stub_entry->stub_sec->output_offset
   9974 	      + stub_entry->stub_sec->output_section->vma);
   9975 
   9976       /* Reset the stub type from the plt variant in case we now
   9977 	 can reach with a shorter stub.  */
   9978       if (stub_entry->stub_type >= ppc_stub_plt_branch)
   9979 	stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
   9980 
   9981       size = 4;
   9982       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
   9983 	{
   9984 	  r2off = get_r2off (htab, stub_entry);
   9985 	  if (r2off == 0)
   9986 	    {
   9987 	      htab->stub_error = TRUE;
   9988 	      return FALSE;
   9989 	    }
   9990 	  size = 12;
   9991 	  if (PPC_HA (r2off) != 0)
   9992 	    size = 16;
   9993 	  off -= size - 4;
   9994 	}
   9995 
   9996       /* If the branch offset if too big, use a ppc_stub_plt_branch.  */
   9997       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
   9998 	{
   9999 	  struct ppc_branch_hash_entry *br_entry;
   10000 
   10001 	  br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
   10002 					     stub_entry->root.string + 9,
   10003 					     TRUE, FALSE);
   10004 	  if (br_entry == NULL)
   10005 	    {
   10006 	      (*_bfd_error_handler) (_("can't build branch stub `%s'"),
   10007 				     stub_entry->root.string);
   10008 	      htab->stub_error = TRUE;
   10009 	      return FALSE;
   10010 	    }
   10011 
   10012 	  if (br_entry->iter != htab->stub_iteration)
   10013 	    {
   10014 	      br_entry->iter = htab->stub_iteration;
   10015 	      br_entry->offset = htab->brlt->size;
   10016 	      htab->brlt->size += 8;
   10017 
   10018 	      if (htab->relbrlt != NULL)
   10019 		htab->relbrlt->size += sizeof (Elf64_External_Rela);
   10020 	      else if (info->emitrelocations)
   10021 		{
   10022 		  htab->brlt->reloc_count += 1;
   10023 		  htab->brlt->flags |= SEC_RELOC;
   10024 		}
   10025 	    }
   10026 
   10027 	  stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
   10028 	  off = (br_entry->offset
   10029 		 + htab->brlt->output_offset
   10030 		 + htab->brlt->output_section->vma
   10031 		 - elf_gp (htab->brlt->output_section->owner)
   10032 		 - htab->stub_group[stub_entry->id_sec->id].toc_off);
   10033 
   10034 	  if (info->emitrelocations)
   10035 	    {
   10036 	      stub_entry->stub_sec->reloc_count += 1 + (PPC_HA (off) != 0);
   10037 	      stub_entry->stub_sec->flags |= SEC_RELOC;
   10038 	    }
   10039 
   10040 	  if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
   10041 	    {
   10042 	      size = 12;
   10043 	      if (PPC_HA (off) != 0)
   10044 		size = 16;
   10045 	    }
   10046 	  else
   10047 	    {
   10048 	      size = 20;
   10049 	      if (PPC_HA (off) != 0)
   10050 		size += 4;
   10051 
   10052 	      if (PPC_HA (r2off) != 0)
   10053 		size += 4;
   10054 	    }
   10055 	}
   10056       else if (info->emitrelocations)
   10057 	{
   10058 	  stub_entry->stub_sec->reloc_count += 1;
   10059 	  stub_entry->stub_sec->flags |= SEC_RELOC;
   10060 	}
   10061     }
   10062 
   10063   stub_entry->stub_sec->size += size;
   10064   return TRUE;
   10065 }
   10066 
   10067 /* Set up various things so that we can make a list of input sections
   10068    for each output section included in the link.  Returns -1 on error,
   10069    0 when no stubs will be needed, and 1 on success.  */
   10070 
   10071 int
   10072 ppc64_elf_setup_section_lists
   10073   (struct bfd_link_info *info,
   10074    asection *(*add_stub_section) (const char *, asection *),
   10075    void (*layout_sections_again) (void))
   10076 {
   10077   bfd *input_bfd;
   10078   int top_id, top_index, id;
   10079   asection *section;
   10080   asection **input_list;
   10081   bfd_size_type amt;
   10082   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   10083 
   10084   if (htab == NULL)
   10085     return -1;
   10086   /* Stash our params away.  */
   10087   htab->add_stub_section = add_stub_section;
   10088   htab->layout_sections_again = layout_sections_again;
   10089 
   10090   if (htab->brlt == NULL)
   10091     return 0;
   10092 
   10093   /* Find the top input section id.  */
   10094   for (input_bfd = info->input_bfds, top_id = 3;
   10095        input_bfd != NULL;
   10096        input_bfd = input_bfd->link_next)
   10097     {
   10098       for (section = input_bfd->sections;
   10099 	   section != NULL;
   10100 	   section = section->next)
   10101 	{
   10102 	  if (top_id < section->id)
   10103 	    top_id = section->id;
   10104 	}
   10105     }
   10106 
   10107   htab->top_id = top_id;
   10108   amt = sizeof (struct map_stub) * (top_id + 1);
   10109   htab->stub_group = bfd_zmalloc (amt);
   10110   if (htab->stub_group == NULL)
   10111     return -1;
   10112 
   10113   /* Set toc_off for com, und, abs and ind sections.  */
   10114   for (id = 0; id < 3; id++)
   10115     htab->stub_group[id].toc_off = TOC_BASE_OFF;
   10116 
   10117   /* We can't use output_bfd->section_count here to find the top output
   10118      section index as some sections may have been removed, and
   10119      strip_excluded_output_sections doesn't renumber the indices.  */
   10120   for (section = info->output_bfd->sections, top_index = 0;
   10121        section != NULL;
   10122        section = section->next)
   10123     {
   10124       if (top_index < section->index)
   10125 	top_index = section->index;
   10126     }
   10127 
   10128   htab->top_index = top_index;
   10129   amt = sizeof (asection *) * (top_index + 1);
   10130   input_list = bfd_zmalloc (amt);
   10131   htab->input_list = input_list;
   10132   if (input_list == NULL)
   10133     return -1;
   10134 
   10135   return 1;
   10136 }
   10137 
   10138 /* Set up for first pass at multitoc partitioning.  */
   10139 
   10140 void
   10141 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
   10142 {
   10143   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   10144 
   10145   elf_gp (info->output_bfd) = ppc64_elf_toc (info->output_bfd);
   10146   htab->toc_curr = elf_gp (info->output_bfd);
   10147   htab->toc_bfd = NULL;
   10148   htab->toc_first_sec = NULL;
   10149 }
   10150 
   10151 /* The linker repeatedly calls this function for each TOC input section
   10152    and linker generated GOT section.  Group input bfds such that the toc
   10153    within a group is less than 64k in size.  */
   10154 
   10155 bfd_boolean
   10156 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
   10157 {
   10158   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   10159   bfd_vma addr, off, limit;
   10160 
   10161   if (htab == NULL)
   10162     return FALSE;
   10163 
   10164   if (!htab->second_toc_pass)
   10165     {
   10166       /* Keep track of the first .toc or .got section for this input bfd.  */
   10167       if (htab->toc_bfd != isec->owner)
   10168 	{
   10169 	  htab->toc_bfd = isec->owner;
   10170 	  htab->toc_first_sec = isec;
   10171 	}
   10172 
   10173       addr = isec->output_offset + isec->output_section->vma;
   10174       off = addr - htab->toc_curr;
   10175       limit = 0x80008000;
   10176       if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
   10177 	limit = 0x10000;
   10178       if (off + isec->size > limit)
   10179 	{
   10180 	  addr = (htab->toc_first_sec->output_offset
   10181 		  + htab->toc_first_sec->output_section->vma);
   10182 	  htab->toc_curr = addr;
   10183 	}
   10184 
   10185       /* toc_curr is the base address of this toc group.  Set elf_gp
   10186 	 for the input section to be the offset relative to the
   10187 	 output toc base plus 0x8000.  Making the input elf_gp an
   10188 	 offset allows us to move the toc as a whole without
   10189 	 recalculating input elf_gp.  */
   10190       off = htab->toc_curr - elf_gp (isec->output_section->owner);
   10191       off += TOC_BASE_OFF;
   10192 
   10193       /* Die if someone uses a linker script that doesn't keep input
   10194 	 file .toc and .got together.  */
   10195       if (elf_gp (isec->owner) != 0
   10196 	  && elf_gp (isec->owner) != off)
   10197 	return FALSE;
   10198 
   10199       elf_gp (isec->owner) = off;
   10200       return TRUE;
   10201     }
   10202 
   10203   /* During the second pass toc_first_sec points to the start of
   10204      a toc group, and toc_curr is used to track the old elf_gp.
   10205      We use toc_bfd to ensure we only look at each bfd once.  */
   10206   if (htab->toc_bfd == isec->owner)
   10207     return TRUE;
   10208   htab->toc_bfd = isec->owner;
   10209 
   10210   if (htab->toc_first_sec == NULL
   10211       || htab->toc_curr != elf_gp (isec->owner))
   10212     {
   10213       htab->toc_curr = elf_gp (isec->owner);
   10214       htab->toc_first_sec = isec;
   10215     }
   10216   addr = (htab->toc_first_sec->output_offset
   10217 	  + htab->toc_first_sec->output_section->vma);
   10218   off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
   10219   elf_gp (isec->owner) = off;
   10220 
   10221   return TRUE;
   10222 }
   10223 
   10224 /* Called via elf_link_hash_traverse to merge GOT entries for global
   10225    symbol H.  */
   10226 
   10227 static bfd_boolean
   10228 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
   10229 {
   10230   if (h->root.type == bfd_link_hash_indirect)
   10231     return TRUE;
   10232 
   10233   if (h->root.type == bfd_link_hash_warning)
   10234     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   10235 
   10236   merge_got_entries (&h->got.glist);
   10237 
   10238   return TRUE;
   10239 }
   10240 
   10241 /* Called via elf_link_hash_traverse to allocate GOT entries for global
   10242    symbol H.  */
   10243 
   10244 static bfd_boolean
   10245 reallocate_got (struct elf_link_hash_entry *h, void *inf)
   10246 {
   10247   struct got_entry *gent;
   10248 
   10249   if (h->root.type == bfd_link_hash_indirect)
   10250     return TRUE;
   10251 
   10252   if (h->root.type == bfd_link_hash_warning)
   10253     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   10254 
   10255   for (gent = h->got.glist; gent != NULL; gent = gent->next)
   10256     if (!gent->is_indirect)
   10257       allocate_got (h, (struct bfd_link_info *) inf, gent);
   10258   return TRUE;
   10259 }
   10260 
   10261 /* Called on the first multitoc pass after the last call to
   10262    ppc64_elf_next_toc_section.  This function removes duplicate GOT
   10263    entries.  */
   10264 
   10265 bfd_boolean
   10266 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
   10267 {
   10268   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   10269   struct bfd *ibfd, *ibfd2;
   10270   bfd_boolean done_something;
   10271 
   10272   htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
   10273 
   10274   if (!htab->do_multi_toc)
   10275     return FALSE;
   10276 
   10277   /* Merge global sym got entries within a toc group.  */
   10278   elf_link_hash_traverse (&htab->elf, merge_global_got, info);
   10279 
   10280   /* And tlsld_got.  */
   10281   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   10282     {
   10283       struct got_entry *ent, *ent2;
   10284 
   10285       if (!is_ppc64_elf (ibfd))
   10286 	continue;
   10287 
   10288       ent = ppc64_tlsld_got (ibfd);
   10289       if (!ent->is_indirect
   10290 	  && ent->got.offset != (bfd_vma) -1)
   10291 	{
   10292 	  for (ibfd2 = ibfd->link_next; ibfd2 != NULL; ibfd2 = ibfd2->link_next)
   10293 	    {
   10294 	      if (!is_ppc64_elf (ibfd2))
   10295 		continue;
   10296 
   10297 	      ent2 = ppc64_tlsld_got (ibfd2);
   10298 	      if (!ent2->is_indirect
   10299 		  && ent2->got.offset != (bfd_vma) -1
   10300 		  && elf_gp (ibfd2) == elf_gp (ibfd))
   10301 		{
   10302 		  ent2->is_indirect = TRUE;
   10303 		  ent2->got.ent = ent;
   10304 		}
   10305 	    }
   10306 	}
   10307     }
   10308 
   10309   /* Zap sizes of got sections.  */
   10310   htab->reliplt->rawsize = htab->reliplt->size;
   10311   htab->reliplt->size -= htab->got_reli_size;
   10312   htab->got_reli_size = 0;
   10313 
   10314   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   10315     {
   10316       asection *got, *relgot;
   10317 
   10318       if (!is_ppc64_elf (ibfd))
   10319 	continue;
   10320 
   10321       got = ppc64_elf_tdata (ibfd)->got;
   10322       if (got != NULL)
   10323 	{
   10324 	  got->rawsize = got->size;
   10325 	  got->size = 0;
   10326 	  relgot = ppc64_elf_tdata (ibfd)->relgot;
   10327 	  relgot->rawsize = relgot->size;
   10328 	  relgot->size = 0;
   10329 	}
   10330     }
   10331 
   10332   /* Now reallocate the got, local syms first.  We don't need to
   10333      allocate section contents again since we never increase size.  */
   10334   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   10335     {
   10336       struct got_entry **lgot_ents;
   10337       struct got_entry **end_lgot_ents;
   10338       struct plt_entry **local_plt;
   10339       struct plt_entry **end_local_plt;
   10340       unsigned char *lgot_masks;
   10341       bfd_size_type locsymcount;
   10342       Elf_Internal_Shdr *symtab_hdr;
   10343       asection *s, *srel;
   10344 
   10345       if (!is_ppc64_elf (ibfd))
   10346 	continue;
   10347 
   10348       lgot_ents = elf_local_got_ents (ibfd);
   10349       if (!lgot_ents)
   10350 	continue;
   10351 
   10352       symtab_hdr = &elf_symtab_hdr (ibfd);
   10353       locsymcount = symtab_hdr->sh_info;
   10354       end_lgot_ents = lgot_ents + locsymcount;
   10355       local_plt = (struct plt_entry **) end_lgot_ents;
   10356       end_local_plt = local_plt + locsymcount;
   10357       lgot_masks = (unsigned char *) end_local_plt;
   10358       s = ppc64_elf_tdata (ibfd)->got;
   10359       srel = ppc64_elf_tdata (ibfd)->relgot;
   10360       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
   10361 	{
   10362 	  struct got_entry *ent;
   10363 
   10364 	  for (ent = *lgot_ents; ent != NULL; ent = ent->next)
   10365 	    {
   10366 	      unsigned int num = 1;
   10367 	      ent->got.offset = s->size;
   10368 	      if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
   10369 		num = 2;
   10370 	      s->size += num * 8;
   10371 	      if (info->shared)
   10372 		srel->size += num * sizeof (Elf64_External_Rela);
   10373 	      else if ((*lgot_masks & PLT_IFUNC) != 0)
   10374 		{
   10375 		  htab->reliplt->size
   10376 		    += num * sizeof (Elf64_External_Rela);
   10377 		  htab->got_reli_size
   10378 		    += num * sizeof (Elf64_External_Rela);
   10379 		}
   10380 	    }
   10381 	}
   10382     }
   10383 
   10384   elf_link_hash_traverse (&htab->elf, reallocate_got, info);
   10385 
   10386   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   10387     {
   10388       struct got_entry *ent;
   10389 
   10390       if (!is_ppc64_elf (ibfd))
   10391 	continue;
   10392 
   10393       ent = ppc64_tlsld_got (ibfd);
   10394       if (!ent->is_indirect
   10395 	  && ent->got.offset != (bfd_vma) -1)
   10396 	{
   10397 	  asection *s = ppc64_elf_tdata (ibfd)->got;
   10398 	  ent->got.offset = s->size;
   10399 	  s->size += 16;
   10400 	  if (info->shared)
   10401 	    {
   10402 	      asection *srel = ppc64_elf_tdata (ibfd)->relgot;
   10403 	      srel->size += sizeof (Elf64_External_Rela);
   10404 	    }
   10405 	}
   10406     }
   10407 
   10408   done_something = htab->reliplt->rawsize != htab->reliplt->size;
   10409   if (!done_something)
   10410     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
   10411       {
   10412 	asection *got;
   10413 
   10414 	if (!is_ppc64_elf (ibfd))
   10415 	  continue;
   10416 
   10417 	got = ppc64_elf_tdata (ibfd)->got;
   10418 	if (got != NULL)
   10419 	  {
   10420 	    done_something = got->rawsize != got->size;
   10421 	    if (done_something)
   10422 	      break;
   10423 	  }
   10424       }
   10425 
   10426   if (done_something)
   10427     (*htab->layout_sections_again) ();
   10428 
   10429   /* Set up for second pass over toc sections to recalculate elf_gp
   10430      on input sections.  */
   10431   htab->toc_bfd = NULL;
   10432   htab->toc_first_sec = NULL;
   10433   htab->second_toc_pass = TRUE;
   10434   return done_something;
   10435 }
   10436 
   10437 /* Called after second pass of multitoc partitioning.  */
   10438 
   10439 void
   10440 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
   10441 {
   10442   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   10443 
   10444   /* After the second pass, toc_curr tracks the TOC offset used
   10445      for code sections below in ppc64_elf_next_input_section.  */
   10446   htab->toc_curr = TOC_BASE_OFF;
   10447 }
   10448 
   10449 /* No toc references were found in ISEC.  If the code in ISEC makes no
   10450    calls, then there's no need to use toc adjusting stubs when branching
   10451    into ISEC.  Actually, indirect calls from ISEC are OK as they will
   10452    load r2.  Returns -1 on error, 0 for no stub needed, 1 for stub
   10453    needed, and 2 if a cyclical call-graph was found but no other reason
   10454    for a stub was detected.  If called from the top level, a return of
   10455    2 means the same as a return of 0.  */
   10456 
   10457 static int
   10458 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
   10459 {
   10460   int ret;
   10461 
   10462   /* Mark this section as checked.  */
   10463   isec->call_check_done = 1;
   10464 
   10465   /* We know none of our code bearing sections will need toc stubs.  */
   10466   if ((isec->flags & SEC_LINKER_CREATED) != 0)
   10467     return 0;
   10468 
   10469   if (isec->size == 0)
   10470     return 0;
   10471 
   10472   if (isec->output_section == NULL)
   10473     return 0;
   10474 
   10475   ret = 0;
   10476   if (isec->reloc_count != 0)
   10477     {
   10478       Elf_Internal_Rela *relstart, *rel;
   10479       Elf_Internal_Sym *local_syms;
   10480       struct ppc_link_hash_table *htab;
   10481 
   10482       relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
   10483 					    info->keep_memory);
   10484       if (relstart == NULL)
   10485 	return -1;
   10486 
   10487       /* Look for branches to outside of this section.  */
   10488       local_syms = NULL;
   10489       htab = ppc_hash_table (info);
   10490       if (htab == NULL)
   10491 	return -1;
   10492 
   10493       for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
   10494 	{
   10495 	  enum elf_ppc64_reloc_type r_type;
   10496 	  unsigned long r_symndx;
   10497 	  struct elf_link_hash_entry *h;
   10498 	  struct ppc_link_hash_entry *eh;
   10499 	  Elf_Internal_Sym *sym;
   10500 	  asection *sym_sec;
   10501 	  struct _opd_sec_data *opd;
   10502 	  bfd_vma sym_value;
   10503 	  bfd_vma dest;
   10504 
   10505 	  r_type = ELF64_R_TYPE (rel->r_info);
   10506 	  if (r_type != R_PPC64_REL24
   10507 	      && r_type != R_PPC64_REL14
   10508 	      && r_type != R_PPC64_REL14_BRTAKEN
   10509 	      && r_type != R_PPC64_REL14_BRNTAKEN)
   10510 	    continue;
   10511 
   10512 	  r_symndx = ELF64_R_SYM (rel->r_info);
   10513 	  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
   10514 			  isec->owner))
   10515 	    {
   10516 	      ret = -1;
   10517 	      break;
   10518 	    }
   10519 
   10520 	  /* Calls to dynamic lib functions go through a plt call stub
   10521 	     that uses r2.  */
   10522 	  eh = (struct ppc_link_hash_entry *) h;
   10523 	  if (eh != NULL
   10524 	      && (eh->elf.plt.plist != NULL
   10525 		  || (eh->oh != NULL
   10526 		      && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
   10527 	    {
   10528 	      ret = 1;
   10529 	      break;
   10530 	    }
   10531 
   10532 	  if (sym_sec == NULL)
   10533 	    /* Ignore other undefined symbols.  */
   10534 	    continue;
   10535 
   10536 	  /* Assume branches to other sections not included in the
   10537 	     link need stubs too, to cover -R and absolute syms.  */
   10538 	  if (sym_sec->output_section == NULL)
   10539 	    {
   10540 	      ret = 1;
   10541 	      break;
   10542 	    }
   10543 
   10544 	  if (h == NULL)
   10545 	    sym_value = sym->st_value;
   10546 	  else
   10547 	    {
   10548 	      if (h->root.type != bfd_link_hash_defined
   10549 		  && h->root.type != bfd_link_hash_defweak)
   10550 		abort ();
   10551 	      sym_value = h->root.u.def.value;
   10552 	    }
   10553 	  sym_value += rel->r_addend;
   10554 
   10555 	  /* If this branch reloc uses an opd sym, find the code section.  */
   10556 	  opd = get_opd_info (sym_sec);
   10557 	  if (opd != NULL)
   10558 	    {
   10559 	      if (h == NULL && opd->adjust != NULL)
   10560 		{
   10561 		  long adjust;
   10562 
   10563 		  adjust = opd->adjust[sym->st_value / 8];
   10564 		  if (adjust == -1)
   10565 		    /* Assume deleted functions won't ever be called.  */
   10566 		    continue;
   10567 		  sym_value += adjust;
   10568 		}
   10569 
   10570 	      dest = opd_entry_value (sym_sec, sym_value, &sym_sec, NULL);
   10571 	      if (dest == (bfd_vma) -1)
   10572 		continue;
   10573 	    }
   10574 	  else
   10575 	    dest = (sym_value
   10576 		    + sym_sec->output_offset
   10577 		    + sym_sec->output_section->vma);
   10578 
   10579 	  /* Ignore branch to self.  */
   10580 	  if (sym_sec == isec)
   10581 	    continue;
   10582 
   10583 	  /* If the called function uses the toc, we need a stub.  */
   10584 	  if (sym_sec->has_toc_reloc
   10585 	      || sym_sec->makes_toc_func_call)
   10586 	    {
   10587 	      ret = 1;
   10588 	      break;
   10589 	    }
   10590 
   10591 	  /* Assume any branch that needs a long branch stub might in fact
   10592 	     need a plt_branch stub.  A plt_branch stub uses r2.  */
   10593 	  else if (dest - (isec->output_offset
   10594 			   + isec->output_section->vma
   10595 			   + rel->r_offset) + (1 << 25) >= (2 << 25))
   10596 	    {
   10597 	      ret = 1;
   10598 	      break;
   10599 	    }
   10600 
   10601 	  /* If calling back to a section in the process of being
   10602 	     tested, we can't say for sure that no toc adjusting stubs
   10603 	     are needed, so don't return zero.  */
   10604 	  else if (sym_sec->call_check_in_progress)
   10605 	    ret = 2;
   10606 
   10607 	  /* Branches to another section that itself doesn't have any TOC
   10608 	     references are OK.  Recursively call ourselves to check.  */
   10609 	  else if (!sym_sec->call_check_done)
   10610 	    {
   10611 	      int recur;
   10612 
   10613 	      /* Mark current section as indeterminate, so that other
   10614 		 sections that call back to current won't be marked as
   10615 		 known.  */
   10616 	      isec->call_check_in_progress = 1;
   10617 	      recur = toc_adjusting_stub_needed (info, sym_sec);
   10618 	      isec->call_check_in_progress = 0;
   10619 
   10620 	      if (recur != 0)
   10621 		{
   10622 		  ret = recur;
   10623 		  if (recur != 2)
   10624 		    break;
   10625 		}
   10626 	    }
   10627 	}
   10628 
   10629       if (local_syms != NULL
   10630 	  && (elf_symtab_hdr (isec->owner).contents
   10631 	      != (unsigned char *) local_syms))
   10632 	free (local_syms);
   10633       if (elf_section_data (isec)->relocs != relstart)
   10634 	free (relstart);
   10635     }
   10636 
   10637   if ((ret & 1) == 0
   10638       && isec->map_head.s != NULL
   10639       && (strcmp (isec->output_section->name, ".init") == 0
   10640 	  || strcmp (isec->output_section->name, ".fini") == 0))
   10641     {
   10642       if (isec->map_head.s->has_toc_reloc
   10643 	  || isec->map_head.s->makes_toc_func_call)
   10644 	ret = 1;
   10645       else if (!isec->map_head.s->call_check_done)
   10646 	{
   10647 	  int recur;
   10648 	  isec->call_check_in_progress = 1;
   10649 	  recur = toc_adjusting_stub_needed (info, isec->map_head.s);
   10650 	  isec->call_check_in_progress = 0;
   10651 	  if (recur != 0)
   10652 	    ret = recur;
   10653 	}
   10654     }
   10655 
   10656   if (ret == 1)
   10657     isec->makes_toc_func_call = 1;
   10658 
   10659   return ret;
   10660 }
   10661 
   10662 /* The linker repeatedly calls this function for each input section,
   10663    in the order that input sections are linked into output sections.
   10664    Build lists of input sections to determine groupings between which
   10665    we may insert linker stubs.  */
   10666 
   10667 bfd_boolean
   10668 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
   10669 {
   10670   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   10671 
   10672   if (htab == NULL)
   10673     return FALSE;
   10674 
   10675   if ((isec->output_section->flags & SEC_CODE) != 0
   10676       && isec->output_section->index <= htab->top_index)
   10677     {
   10678       asection **list = htab->input_list + isec->output_section->index;
   10679       /* Steal the link_sec pointer for our list.  */
   10680 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
   10681       /* This happens to make the list in reverse order,
   10682 	 which is what we want.  */
   10683       PREV_SEC (isec) = *list;
   10684       *list = isec;
   10685     }
   10686 
   10687   if (htab->multi_toc_needed)
   10688     {
   10689       /* If a code section has a function that uses the TOC then we need
   10690 	 to use the right TOC (obviously).  Also, make sure that .opd gets
   10691 	 the correct TOC value for R_PPC64_TOC relocs that don't have or
   10692 	 can't find their function symbol (shouldn't ever happen now).
   10693 	 Also specially treat .fixup for the linux kernel.  .fixup
   10694 	 contains branches, but only back to the function that hit an
   10695 	 exception.  */
   10696       if (isec->has_toc_reloc
   10697 	  || (isec->flags & SEC_CODE) == 0
   10698 	  || strcmp (isec->name, ".fixup") == 0)
   10699 	{
   10700 	  if (elf_gp (isec->owner) != 0)
   10701 	    htab->toc_curr = elf_gp (isec->owner);
   10702 	}
   10703       else
   10704 	{
   10705 	  if (!isec->call_check_done
   10706 	      && toc_adjusting_stub_needed (info, isec) < 0)
   10707 	    return FALSE;
   10708 	  /* If we make a local call from this section, ie. a branch
   10709 	     without a following nop, then we have no place to put a
   10710 	     toc restoring insn.  We must use the same toc group as
   10711 	     the callee.
   10712 	     Testing makes_toc_func_call actually tests for *any*
   10713 	     calls to functions that need a good toc pointer.  A more
   10714 	     precise test would be better, as this one will set
   10715 	     incorrect values for pasted .init/.fini fragments.
   10716 	     (Fixed later in check_pasted_section.)  */
   10717 	  if (isec->makes_toc_func_call
   10718 	      && elf_gp (isec->owner) != 0)
   10719 	    htab->toc_curr = elf_gp (isec->owner);
   10720 	}
   10721     }
   10722 
   10723   /* Functions that don't use the TOC can belong in any TOC group.
   10724      Use the last TOC base.  */
   10725   htab->stub_group[isec->id].toc_off = htab->toc_curr;
   10726   return TRUE;
   10727 }
   10728 
   10729 /* Check that all .init and .fini sections use the same toc, if they
   10730    have toc relocs.  */
   10731 
   10732 static bfd_boolean
   10733 check_pasted_section (struct bfd_link_info *info, const char *name)
   10734 {
   10735   asection *o = bfd_get_section_by_name (info->output_bfd, name);
   10736 
   10737   if (o != NULL)
   10738     {
   10739       struct ppc_link_hash_table *htab = ppc_hash_table (info);
   10740       bfd_vma toc_off = 0;
   10741       asection *i;
   10742 
   10743       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   10744 	if (i->has_toc_reloc)
   10745 	  {
   10746 	    if (toc_off == 0)
   10747 	      toc_off = htab->stub_group[i->id].toc_off;
   10748 	    else if (toc_off != htab->stub_group[i->id].toc_off)
   10749 	      return FALSE;
   10750 	  }
   10751 
   10752       if (toc_off == 0)
   10753 	for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   10754 	  if (i->makes_toc_func_call)
   10755 	    {
   10756 	      toc_off = htab->stub_group[i->id].toc_off;
   10757 	      break;
   10758 	    }
   10759 
   10760       /* Make sure the whole pasted function uses the same toc offset.  */
   10761       if (toc_off != 0)
   10762 	for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   10763 	  htab->stub_group[i->id].toc_off = toc_off;
   10764     }
   10765   return TRUE;
   10766 }
   10767 
   10768 bfd_boolean
   10769 ppc64_elf_check_init_fini (struct bfd_link_info *info)
   10770 {
   10771   return (check_pasted_section (info, ".init")
   10772 	  & check_pasted_section (info, ".fini"));
   10773 }
   10774 
   10775 /* See whether we can group stub sections together.  Grouping stub
   10776    sections may result in fewer stubs.  More importantly, we need to
   10777    put all .init* and .fini* stubs at the beginning of the .init or
   10778    .fini output sections respectively, because glibc splits the
   10779    _init and _fini functions into multiple parts.  Putting a stub in
   10780    the middle of a function is not a good idea.  */
   10781 
   10782 static void
   10783 group_sections (struct ppc_link_hash_table *htab,
   10784 		bfd_size_type stub_group_size,
   10785 		bfd_boolean stubs_always_before_branch)
   10786 {
   10787   asection **list;
   10788   bfd_size_type stub14_group_size;
   10789   bfd_boolean suppress_size_errors;
   10790 
   10791   suppress_size_errors = FALSE;
   10792   stub14_group_size = stub_group_size;
   10793   if (stub_group_size == 1)
   10794     {
   10795       /* Default values.  */
   10796       if (stubs_always_before_branch)
   10797 	{
   10798 	  stub_group_size = 0x1e00000;
   10799 	  stub14_group_size = 0x7800;
   10800 	}
   10801       else
   10802 	{
   10803 	  stub_group_size = 0x1c00000;
   10804 	  stub14_group_size = 0x7000;
   10805 	}
   10806       suppress_size_errors = TRUE;
   10807     }
   10808 
   10809   list = htab->input_list + htab->top_index;
   10810   do
   10811     {
   10812       asection *tail = *list;
   10813       while (tail != NULL)
   10814 	{
   10815 	  asection *curr;
   10816 	  asection *prev;
   10817 	  bfd_size_type total;
   10818 	  bfd_boolean big_sec;
   10819 	  bfd_vma curr_toc;
   10820 
   10821 	  curr = tail;
   10822 	  total = tail->size;
   10823 	  big_sec = total > (ppc64_elf_section_data (tail) != NULL
   10824 			     && ppc64_elf_section_data (tail)->has_14bit_branch
   10825 			     ? stub14_group_size : stub_group_size);
   10826 	  if (big_sec && !suppress_size_errors)
   10827 	    (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
   10828 				     tail->owner, tail);
   10829 	  curr_toc = htab->stub_group[tail->id].toc_off;
   10830 
   10831 	  while ((prev = PREV_SEC (curr)) != NULL
   10832 		 && ((total += curr->output_offset - prev->output_offset)
   10833 		     < (ppc64_elf_section_data (prev) != NULL
   10834 			&& ppc64_elf_section_data (prev)->has_14bit_branch
   10835 			? stub14_group_size : stub_group_size))
   10836 		 && htab->stub_group[prev->id].toc_off == curr_toc)
   10837 	    curr = prev;
   10838 
   10839 	  /* OK, the size from the start of CURR to the end is less
   10840 	     than stub_group_size and thus can be handled by one stub
   10841 	     section.  (or the tail section is itself larger than
   10842 	     stub_group_size, in which case we may be toast.)  We
   10843 	     should really be keeping track of the total size of stubs
   10844 	     added here, as stubs contribute to the final output
   10845 	     section size.  That's a little tricky, and this way will
   10846 	     only break if stubs added make the total size more than
   10847 	     2^25, ie. for the default stub_group_size, if stubs total
   10848 	     more than 2097152 bytes, or nearly 75000 plt call stubs.  */
   10849 	  do
   10850 	    {
   10851 	      prev = PREV_SEC (tail);
   10852 	      /* Set up this stub group.  */
   10853 	      htab->stub_group[tail->id].link_sec = curr;
   10854 	    }
   10855 	  while (tail != curr && (tail = prev) != NULL);
   10856 
   10857 	  /* But wait, there's more!  Input sections up to stub_group_size
   10858 	     bytes before the stub section can be handled by it too.
   10859 	     Don't do this if we have a really large section after the
   10860 	     stubs, as adding more stubs increases the chance that
   10861 	     branches may not reach into the stub section.  */
   10862 	  if (!stubs_always_before_branch && !big_sec)
   10863 	    {
   10864 	      total = 0;
   10865 	      while (prev != NULL
   10866 		     && ((total += tail->output_offset - prev->output_offset)
   10867 			 < (ppc64_elf_section_data (prev) != NULL
   10868 			    && ppc64_elf_section_data (prev)->has_14bit_branch
   10869 			    ? stub14_group_size : stub_group_size))
   10870 		     && htab->stub_group[prev->id].toc_off == curr_toc)
   10871 		{
   10872 		  tail = prev;
   10873 		  prev = PREV_SEC (tail);
   10874 		  htab->stub_group[tail->id].link_sec = curr;
   10875 		}
   10876 	    }
   10877 	  tail = prev;
   10878 	}
   10879     }
   10880   while (list-- != htab->input_list);
   10881   free (htab->input_list);
   10882 #undef PREV_SEC
   10883 }
   10884 
   10885 /* Determine and set the size of the stub section for a final link.
   10886 
   10887    The basic idea here is to examine all the relocations looking for
   10888    PC-relative calls to a target that is unreachable with a "bl"
   10889    instruction.  */
   10890 
   10891 bfd_boolean
   10892 ppc64_elf_size_stubs (struct bfd_link_info *info, bfd_signed_vma group_size)
   10893 {
   10894   bfd_size_type stub_group_size;
   10895   bfd_boolean stubs_always_before_branch;
   10896   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   10897 
   10898   if (htab == NULL)
   10899     return FALSE;
   10900 
   10901   stubs_always_before_branch = group_size < 0;
   10902   if (group_size < 0)
   10903     stub_group_size = -group_size;
   10904   else
   10905     stub_group_size = group_size;
   10906 
   10907   group_sections (htab, stub_group_size, stubs_always_before_branch);
   10908 
   10909   while (1)
   10910     {
   10911       bfd *input_bfd;
   10912       unsigned int bfd_indx;
   10913       asection *stub_sec;
   10914 
   10915       htab->stub_iteration += 1;
   10916 
   10917       for (input_bfd = info->input_bfds, bfd_indx = 0;
   10918 	   input_bfd != NULL;
   10919 	   input_bfd = input_bfd->link_next, bfd_indx++)
   10920 	{
   10921 	  Elf_Internal_Shdr *symtab_hdr;
   10922 	  asection *section;
   10923 	  Elf_Internal_Sym *local_syms = NULL;
   10924 
   10925 	  if (!is_ppc64_elf (input_bfd))
   10926 	    continue;
   10927 
   10928 	  /* We'll need the symbol table in a second.  */
   10929 	  symtab_hdr = &elf_symtab_hdr (input_bfd);
   10930 	  if (symtab_hdr->sh_info == 0)
   10931 	    continue;
   10932 
   10933 	  /* Walk over each section attached to the input bfd.  */
   10934 	  for (section = input_bfd->sections;
   10935 	       section != NULL;
   10936 	       section = section->next)
   10937 	    {
   10938 	      Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
   10939 
   10940 	      /* If there aren't any relocs, then there's nothing more
   10941 		 to do.  */
   10942 	      if ((section->flags & SEC_RELOC) == 0
   10943 		  || (section->flags & SEC_ALLOC) == 0
   10944 		  || (section->flags & SEC_LOAD) == 0
   10945 		  || (section->flags & SEC_CODE) == 0
   10946 		  || section->reloc_count == 0)
   10947 		continue;
   10948 
   10949 	      /* If this section is a link-once section that will be
   10950 		 discarded, then don't create any stubs.  */
   10951 	      if (section->output_section == NULL
   10952 		  || section->output_section->owner != info->output_bfd)
   10953 		continue;
   10954 
   10955 	      /* Get the relocs.  */
   10956 	      internal_relocs
   10957 		= _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
   10958 					     info->keep_memory);
   10959 	      if (internal_relocs == NULL)
   10960 		goto error_ret_free_local;
   10961 
   10962 	      /* Now examine each relocation.  */
   10963 	      irela = internal_relocs;
   10964 	      irelaend = irela + section->reloc_count;
   10965 	      for (; irela < irelaend; irela++)
   10966 		{
   10967 		  enum elf_ppc64_reloc_type r_type;
   10968 		  unsigned int r_indx;
   10969 		  enum ppc_stub_type stub_type;
   10970 		  struct ppc_stub_hash_entry *stub_entry;
   10971 		  asection *sym_sec, *code_sec;
   10972 		  bfd_vma sym_value, code_value;
   10973 		  bfd_vma destination;
   10974 		  bfd_boolean ok_dest;
   10975 		  struct ppc_link_hash_entry *hash;
   10976 		  struct ppc_link_hash_entry *fdh;
   10977 		  struct elf_link_hash_entry *h;
   10978 		  Elf_Internal_Sym *sym;
   10979 		  char *stub_name;
   10980 		  const asection *id_sec;
   10981 		  struct _opd_sec_data *opd;
   10982 		  struct plt_entry *plt_ent;
   10983 
   10984 		  r_type = ELF64_R_TYPE (irela->r_info);
   10985 		  r_indx = ELF64_R_SYM (irela->r_info);
   10986 
   10987 		  if (r_type >= R_PPC64_max)
   10988 		    {
   10989 		      bfd_set_error (bfd_error_bad_value);
   10990 		      goto error_ret_free_internal;
   10991 		    }
   10992 
   10993 		  /* Only look for stubs on branch instructions.  */
   10994 		  if (r_type != R_PPC64_REL24
   10995 		      && r_type != R_PPC64_REL14
   10996 		      && r_type != R_PPC64_REL14_BRTAKEN
   10997 		      && r_type != R_PPC64_REL14_BRNTAKEN)
   10998 		    continue;
   10999 
   11000 		  /* Now determine the call target, its name, value,
   11001 		     section.  */
   11002 		  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   11003 				  r_indx, input_bfd))
   11004 		    goto error_ret_free_internal;
   11005 		  hash = (struct ppc_link_hash_entry *) h;
   11006 
   11007 		  ok_dest = FALSE;
   11008 		  fdh = NULL;
   11009 		  sym_value = 0;
   11010 		  if (hash == NULL)
   11011 		    {
   11012 		      sym_value = sym->st_value;
   11013 		      ok_dest = TRUE;
   11014 		    }
   11015 		  else if (hash->elf.root.type == bfd_link_hash_defined
   11016 			   || hash->elf.root.type == bfd_link_hash_defweak)
   11017 		    {
   11018 		      sym_value = hash->elf.root.u.def.value;
   11019 		      if (sym_sec->output_section != NULL)
   11020 			ok_dest = TRUE;
   11021 		    }
   11022 		  else if (hash->elf.root.type == bfd_link_hash_undefweak
   11023 			   || hash->elf.root.type == bfd_link_hash_undefined)
   11024 		    {
   11025 		      /* Recognise an old ABI func code entry sym, and
   11026 			 use the func descriptor sym instead if it is
   11027 			 defined.  */
   11028 		      if (hash->elf.root.root.string[0] == '.'
   11029 			  && (fdh = lookup_fdh (hash, htab)) != NULL)
   11030 			{
   11031 			  if (fdh->elf.root.type == bfd_link_hash_defined
   11032 			      || fdh->elf.root.type == bfd_link_hash_defweak)
   11033 			    {
   11034 			      sym_sec = fdh->elf.root.u.def.section;
   11035 			      sym_value = fdh->elf.root.u.def.value;
   11036 			      if (sym_sec->output_section != NULL)
   11037 				ok_dest = TRUE;
   11038 			    }
   11039 			  else
   11040 			    fdh = NULL;
   11041 			}
   11042 		    }
   11043 		  else
   11044 		    {
   11045 		      bfd_set_error (bfd_error_bad_value);
   11046 		      goto error_ret_free_internal;
   11047 		    }
   11048 
   11049 		  destination = 0;
   11050 		  if (ok_dest)
   11051 		    {
   11052 		      sym_value += irela->r_addend;
   11053 		      destination = (sym_value
   11054 				     + sym_sec->output_offset
   11055 				     + sym_sec->output_section->vma);
   11056 		    }
   11057 
   11058 		  code_sec = sym_sec;
   11059 		  code_value = sym_value;
   11060 		  opd = get_opd_info (sym_sec);
   11061 		  if (opd != NULL)
   11062 		    {
   11063 		      bfd_vma dest;
   11064 
   11065 		      if (hash == NULL && opd->adjust != NULL)
   11066 			{
   11067 			  long adjust = opd->adjust[sym_value / 8];
   11068 			  if (adjust == -1)
   11069 			    continue;
   11070 			  code_value += adjust;
   11071 			  sym_value += adjust;
   11072 			}
   11073 		      dest = opd_entry_value (sym_sec, sym_value,
   11074 					      &code_sec, &code_value);
   11075 		      if (dest != (bfd_vma) -1)
   11076 			{
   11077 			  destination = dest;
   11078 			  if (fdh != NULL)
   11079 			    {
   11080 			      /* Fixup old ABI sym to point at code
   11081 				 entry.  */
   11082 			      hash->elf.root.type = bfd_link_hash_defweak;
   11083 			      hash->elf.root.u.def.section = code_sec;
   11084 			      hash->elf.root.u.def.value = code_value;
   11085 			    }
   11086 			}
   11087 		    }
   11088 
   11089 		  /* Determine what (if any) linker stub is needed.  */
   11090 		  plt_ent = NULL;
   11091 		  stub_type = ppc_type_of_stub (section, irela, &hash,
   11092 						&plt_ent, destination);
   11093 
   11094 		  if (stub_type != ppc_stub_plt_call)
   11095 		    {
   11096 		      /* Check whether we need a TOC adjusting stub.
   11097 			 Since the linker pastes together pieces from
   11098 			 different object files when creating the
   11099 			 _init and _fini functions, it may be that a
   11100 			 call to what looks like a local sym is in
   11101 			 fact a call needing a TOC adjustment.  */
   11102 		      if (code_sec != NULL
   11103 			  && code_sec->output_section != NULL
   11104 			  && (htab->stub_group[code_sec->id].toc_off
   11105 			      != htab->stub_group[section->id].toc_off)
   11106 			  && (code_sec->has_toc_reloc
   11107 			      || code_sec->makes_toc_func_call))
   11108 			stub_type = ppc_stub_long_branch_r2off;
   11109 		    }
   11110 
   11111 		  if (stub_type == ppc_stub_none)
   11112 		    continue;
   11113 
   11114 		  /* __tls_get_addr calls might be eliminated.  */
   11115 		  if (stub_type != ppc_stub_plt_call
   11116 		      && hash != NULL
   11117 		      && (hash == htab->tls_get_addr
   11118 			  || hash == htab->tls_get_addr_fd)
   11119 		      && section->has_tls_reloc
   11120 		      && irela != internal_relocs)
   11121 		    {
   11122 		      /* Get tls info.  */
   11123 		      unsigned char *tls_mask;
   11124 
   11125 		      if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
   11126 					 irela - 1, input_bfd))
   11127 			goto error_ret_free_internal;
   11128 		      if (*tls_mask != 0)
   11129 			continue;
   11130 		    }
   11131 
   11132 		  /* Support for grouping stub sections.  */
   11133 		  id_sec = htab->stub_group[section->id].link_sec;
   11134 
   11135 		  /* Get the name of this stub.  */
   11136 		  stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
   11137 		  if (!stub_name)
   11138 		    goto error_ret_free_internal;
   11139 
   11140 		  stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
   11141 						     stub_name, FALSE, FALSE);
   11142 		  if (stub_entry != NULL)
   11143 		    {
   11144 		      /* The proper stub has already been created.  */
   11145 		      free (stub_name);
   11146 		      continue;
   11147 		    }
   11148 
   11149 		  stub_entry = ppc_add_stub (stub_name, section, htab);
   11150 		  if (stub_entry == NULL)
   11151 		    {
   11152 		      free (stub_name);
   11153 		    error_ret_free_internal:
   11154 		      if (elf_section_data (section)->relocs == NULL)
   11155 			free (internal_relocs);
   11156 		    error_ret_free_local:
   11157 		      if (local_syms != NULL
   11158 			  && (symtab_hdr->contents
   11159 			      != (unsigned char *) local_syms))
   11160 			free (local_syms);
   11161 		      return FALSE;
   11162 		    }
   11163 
   11164 		  stub_entry->stub_type = stub_type;
   11165 		  if (stub_type != ppc_stub_plt_call)
   11166 		    {
   11167 		      stub_entry->target_value = code_value;
   11168 		      stub_entry->target_section = code_sec;
   11169 		    }
   11170 		  else
   11171 		    {
   11172 		      stub_entry->target_value = sym_value;
   11173 		      stub_entry->target_section = sym_sec;
   11174 		    }
   11175 		  stub_entry->h = hash;
   11176 		  stub_entry->plt_ent = plt_ent;
   11177 		  stub_entry->addend = irela->r_addend;
   11178 
   11179 		  if (stub_entry->h != NULL)
   11180 		    htab->stub_globals += 1;
   11181 		}
   11182 
   11183 	      /* We're done with the internal relocs, free them.  */
   11184 	      if (elf_section_data (section)->relocs != internal_relocs)
   11185 		free (internal_relocs);
   11186 	    }
   11187 
   11188 	  if (local_syms != NULL
   11189 	      && symtab_hdr->contents != (unsigned char *) local_syms)
   11190 	    {
   11191 	      if (!info->keep_memory)
   11192 		free (local_syms);
   11193 	      else
   11194 		symtab_hdr->contents = (unsigned char *) local_syms;
   11195 	    }
   11196 	}
   11197 
   11198       /* We may have added some stubs.  Find out the new size of the
   11199 	 stub sections.  */
   11200       for (stub_sec = htab->stub_bfd->sections;
   11201 	   stub_sec != NULL;
   11202 	   stub_sec = stub_sec->next)
   11203 	if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
   11204 	  {
   11205 	    stub_sec->rawsize = stub_sec->size;
   11206 	    stub_sec->size = 0;
   11207 	    stub_sec->reloc_count = 0;
   11208 	    stub_sec->flags &= ~SEC_RELOC;
   11209 	  }
   11210 
   11211       htab->brlt->size = 0;
   11212       htab->brlt->reloc_count = 0;
   11213       htab->brlt->flags &= ~SEC_RELOC;
   11214       if (htab->relbrlt != NULL)
   11215 	htab->relbrlt->size = 0;
   11216 
   11217       bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
   11218 
   11219       if (info->emitrelocations
   11220 	  && htab->glink != NULL && htab->glink->size != 0)
   11221 	{
   11222 	  htab->glink->reloc_count = 1;
   11223 	  htab->glink->flags |= SEC_RELOC;
   11224 	}
   11225 
   11226       for (stub_sec = htab->stub_bfd->sections;
   11227 	   stub_sec != NULL;
   11228 	   stub_sec = stub_sec->next)
   11229 	if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
   11230 	    && stub_sec->rawsize != stub_sec->size)
   11231 	  break;
   11232 
   11233       /* Exit from this loop when no stubs have been added, and no stubs
   11234 	 have changed size.  */
   11235       if (stub_sec == NULL)
   11236 	break;
   11237 
   11238       /* Ask the linker to do its stuff.  */
   11239       (*htab->layout_sections_again) ();
   11240     }
   11241 
   11242   /* It would be nice to strip htab->brlt from the output if the
   11243      section is empty, but it's too late.  If we strip sections here,
   11244      the dynamic symbol table is corrupted since the section symbol
   11245      for the stripped section isn't written.  */
   11246 
   11247   return TRUE;
   11248 }
   11249 
   11250 /* Called after we have determined section placement.  If sections
   11251    move, we'll be called again.  Provide a value for TOCstart.  */
   11252 
   11253 bfd_vma
   11254 ppc64_elf_toc (bfd *obfd)
   11255 {
   11256   asection *s;
   11257   bfd_vma TOCstart;
   11258 
   11259   /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
   11260      order.  The TOC starts where the first of these sections starts.  */
   11261   s = bfd_get_section_by_name (obfd, ".got");
   11262   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
   11263     s = bfd_get_section_by_name (obfd, ".toc");
   11264   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
   11265     s = bfd_get_section_by_name (obfd, ".tocbss");
   11266   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
   11267     s = bfd_get_section_by_name (obfd, ".plt");
   11268   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
   11269     {
   11270       /* This may happen for
   11271 	 o  references to TOC base (SYM@toc / TOC[tc0]) without a
   11272 	 .toc directive
   11273 	 o  bad linker script
   11274 	 o --gc-sections and empty TOC sections
   11275 
   11276 	 FIXME: Warn user?  */
   11277 
   11278       /* Look for a likely section.  We probably won't even be
   11279 	 using TOCstart.  */
   11280       for (s = obfd->sections; s != NULL; s = s->next)
   11281 	if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
   11282 			 | SEC_EXCLUDE))
   11283 	    == (SEC_ALLOC | SEC_SMALL_DATA))
   11284 	  break;
   11285       if (s == NULL)
   11286 	for (s = obfd->sections; s != NULL; s = s->next)
   11287 	  if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
   11288 	      == (SEC_ALLOC | SEC_SMALL_DATA))
   11289 	    break;
   11290       if (s == NULL)
   11291 	for (s = obfd->sections; s != NULL; s = s->next)
   11292 	  if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
   11293 	      == SEC_ALLOC)
   11294 	    break;
   11295       if (s == NULL)
   11296 	for (s = obfd->sections; s != NULL; s = s->next)
   11297 	  if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
   11298 	    break;
   11299     }
   11300 
   11301   TOCstart = 0;
   11302   if (s != NULL)
   11303     TOCstart = s->output_section->vma + s->output_offset;
   11304 
   11305   return TOCstart;
   11306 }
   11307 
   11308 /* Build all the stubs associated with the current output file.
   11309    The stubs are kept in a hash table attached to the main linker
   11310    hash table.  This function is called via gldelf64ppc_finish.  */
   11311 
   11312 bfd_boolean
   11313 ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
   11314 		       struct bfd_link_info *info,
   11315 		       char **stats)
   11316 {
   11317   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   11318   asection *stub_sec;
   11319   bfd_byte *p;
   11320   int stub_sec_count = 0;
   11321 
   11322   if (htab == NULL)
   11323     return FALSE;
   11324 
   11325   htab->emit_stub_syms = emit_stub_syms;
   11326 
   11327   /* Allocate memory to hold the linker stubs.  */
   11328   for (stub_sec = htab->stub_bfd->sections;
   11329        stub_sec != NULL;
   11330        stub_sec = stub_sec->next)
   11331     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
   11332 	&& stub_sec->size != 0)
   11333       {
   11334 	stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
   11335 	if (stub_sec->contents == NULL)
   11336 	  return FALSE;
   11337 	/* We want to check that built size is the same as calculated
   11338 	   size.  rawsize is a convenient location to use.  */
   11339 	stub_sec->rawsize = stub_sec->size;
   11340 	stub_sec->size = 0;
   11341       }
   11342 
   11343   if (htab->glink != NULL && htab->glink->size != 0)
   11344     {
   11345       unsigned int indx;
   11346       bfd_vma plt0;
   11347 
   11348       /* Build the .glink plt call stub.  */
   11349       if (htab->emit_stub_syms)
   11350 	{
   11351 	  struct elf_link_hash_entry *h;
   11352 	  h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
   11353 				    TRUE, FALSE, FALSE);
   11354 	  if (h == NULL)
   11355 	    return FALSE;
   11356 	  if (h->root.type == bfd_link_hash_new)
   11357 	    {
   11358 	      h->root.type = bfd_link_hash_defined;
   11359 	      h->root.u.def.section = htab->glink;
   11360 	      h->root.u.def.value = 8;
   11361 	      h->ref_regular = 1;
   11362 	      h->def_regular = 1;
   11363 	      h->ref_regular_nonweak = 1;
   11364 	      h->forced_local = 1;
   11365 	      h->non_elf = 0;
   11366 	    }
   11367 	}
   11368       plt0 = htab->plt->output_section->vma + htab->plt->output_offset - 16;
   11369       if (info->emitrelocations)
   11370 	{
   11371 	  Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
   11372 	  if (r == NULL)
   11373 	    return FALSE;
   11374 	  r->r_offset = (htab->glink->output_offset
   11375 			 + htab->glink->output_section->vma);
   11376 	  r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
   11377 	  r->r_addend = plt0;
   11378 	}
   11379       p = htab->glink->contents;
   11380       plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
   11381       bfd_put_64 (htab->glink->owner, plt0, p);
   11382       p += 8;
   11383       bfd_put_32 (htab->glink->owner, MFLR_R12, p);
   11384       p += 4;
   11385       bfd_put_32 (htab->glink->owner, BCL_20_31, p);
   11386       p += 4;
   11387       bfd_put_32 (htab->glink->owner, MFLR_R11, p);
   11388       p += 4;
   11389       bfd_put_32 (htab->glink->owner, LD_R2_M16R11, p);
   11390       p += 4;
   11391       bfd_put_32 (htab->glink->owner, MTLR_R12, p);
   11392       p += 4;
   11393       bfd_put_32 (htab->glink->owner, ADD_R12_R2_R11, p);
   11394       p += 4;
   11395       bfd_put_32 (htab->glink->owner, LD_R11_0R12, p);
   11396       p += 4;
   11397       bfd_put_32 (htab->glink->owner, LD_R2_0R12 | 8, p);
   11398       p += 4;
   11399       bfd_put_32 (htab->glink->owner, MTCTR_R11, p);
   11400       p += 4;
   11401       bfd_put_32 (htab->glink->owner, LD_R11_0R12 | 16, p);
   11402       p += 4;
   11403       bfd_put_32 (htab->glink->owner, BCTR, p);
   11404       p += 4;
   11405       while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
   11406 	{
   11407 	  bfd_put_32 (htab->glink->owner, NOP, p);
   11408 	  p += 4;
   11409 	}
   11410 
   11411       /* Build the .glink lazy link call stubs.  */
   11412       indx = 0;
   11413       while (p < htab->glink->contents + htab->glink->size)
   11414 	{
   11415 	  if (indx < 0x8000)
   11416 	    {
   11417 	      bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
   11418 	      p += 4;
   11419 	    }
   11420 	  else
   11421 	    {
   11422 	      bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
   11423 	      p += 4;
   11424 	      bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
   11425 	      p += 4;
   11426 	    }
   11427 	  bfd_put_32 (htab->glink->owner,
   11428 		      B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
   11429 	  indx++;
   11430 	  p += 4;
   11431 	}
   11432       htab->glink->rawsize = p - htab->glink->contents;
   11433     }
   11434 
   11435   if (htab->brlt->size != 0)
   11436     {
   11437       htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
   11438 					 htab->brlt->size);
   11439       if (htab->brlt->contents == NULL)
   11440 	return FALSE;
   11441     }
   11442   if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
   11443     {
   11444       htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
   11445 					    htab->relbrlt->size);
   11446       if (htab->relbrlt->contents == NULL)
   11447 	return FALSE;
   11448     }
   11449 
   11450   /* Build the stubs as directed by the stub hash table.  */
   11451   bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
   11452 
   11453   if (htab->relbrlt != NULL)
   11454     htab->relbrlt->reloc_count = 0;
   11455 
   11456   for (stub_sec = htab->stub_bfd->sections;
   11457        stub_sec != NULL;
   11458        stub_sec = stub_sec->next)
   11459     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
   11460       {
   11461 	stub_sec_count += 1;
   11462 	if (stub_sec->rawsize != stub_sec->size)
   11463 	  break;
   11464       }
   11465 
   11466   if (stub_sec != NULL
   11467       || htab->glink->rawsize != htab->glink->size)
   11468     {
   11469       htab->stub_error = TRUE;
   11470       (*_bfd_error_handler) (_("stubs don't match calculated size"));
   11471     }
   11472 
   11473   if (htab->stub_error)
   11474     return FALSE;
   11475 
   11476   if (stats != NULL)
   11477     {
   11478       *stats = bfd_malloc (500);
   11479       if (*stats == NULL)
   11480 	return FALSE;
   11481 
   11482       sprintf (*stats, _("linker stubs in %u group%s\n"
   11483 			 "  branch       %lu\n"
   11484 			 "  toc adjust   %lu\n"
   11485 			 "  long branch  %lu\n"
   11486 			 "  long toc adj %lu\n"
   11487 			 "  plt call     %lu"),
   11488 	       stub_sec_count,
   11489 	       stub_sec_count == 1 ? "" : "s",
   11490 	       htab->stub_count[ppc_stub_long_branch - 1],
   11491 	       htab->stub_count[ppc_stub_long_branch_r2off - 1],
   11492 	       htab->stub_count[ppc_stub_plt_branch - 1],
   11493 	       htab->stub_count[ppc_stub_plt_branch_r2off - 1],
   11494 	       htab->stub_count[ppc_stub_plt_call - 1]);
   11495     }
   11496   return TRUE;
   11497 }
   11498 
   11499 /* This function undoes the changes made by add_symbol_adjust.  */
   11500 
   11501 static bfd_boolean
   11502 undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
   11503 {
   11504   struct ppc_link_hash_entry *eh;
   11505 
   11506   if (h->root.type == bfd_link_hash_indirect)
   11507     return TRUE;
   11508 
   11509   if (h->root.type == bfd_link_hash_warning)
   11510     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   11511 
   11512   eh = (struct ppc_link_hash_entry *) h;
   11513   if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
   11514     return TRUE;
   11515 
   11516   eh->elf.root.type = bfd_link_hash_undefined;
   11517   return TRUE;
   11518 }
   11519 
   11520 void
   11521 ppc64_elf_restore_symbols (struct bfd_link_info *info)
   11522 {
   11523   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   11524 
   11525   if (htab != NULL)
   11526     elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
   11527 }
   11528 
   11529 /* What to do when ld finds relocations against symbols defined in
   11530    discarded sections.  */
   11531 
   11532 static unsigned int
   11533 ppc64_elf_action_discarded (asection *sec)
   11534 {
   11535   if (strcmp (".opd", sec->name) == 0)
   11536     return 0;
   11537 
   11538   if (strcmp (".toc", sec->name) == 0)
   11539     return 0;
   11540 
   11541   if (strcmp (".toc1", sec->name) == 0)
   11542     return 0;
   11543 
   11544   return _bfd_elf_default_action_discarded (sec);
   11545 }
   11546 
   11547 /* REL points to a low-part reloc on a largetoc instruction sequence.
   11548    Find the matching high-part reloc instruction and verify that it
   11549    is addis REG,x,imm.  If so, set *REG to x and return a pointer to
   11550    the high-part reloc.  */
   11551 
   11552 static const Elf_Internal_Rela *
   11553 ha_reloc_match (const Elf_Internal_Rela *relocs,
   11554 		const Elf_Internal_Rela *rel,
   11555 		unsigned int *reg,
   11556 		bfd_boolean match_addend,
   11557 		const bfd *input_bfd,
   11558 		const bfd_byte *contents)
   11559 {
   11560   enum elf_ppc64_reloc_type r_type, r_type_ha;
   11561   bfd_vma r_info_ha, r_addend;
   11562 
   11563   r_type = ELF64_R_TYPE (rel->r_info);
   11564   switch (r_type)
   11565     {
   11566     case R_PPC64_GOT_TLSLD16_LO:
   11567     case R_PPC64_GOT_TLSGD16_LO:
   11568     case R_PPC64_GOT_TPREL16_LO_DS:
   11569     case R_PPC64_GOT_DTPREL16_LO_DS:
   11570     case R_PPC64_GOT16_LO:
   11571     case R_PPC64_TOC16_LO:
   11572       r_type_ha = r_type + 2;
   11573       break;
   11574     case R_PPC64_GOT16_LO_DS:
   11575       r_type_ha = R_PPC64_GOT16_HA;
   11576       break;
   11577     case R_PPC64_TOC16_LO_DS:
   11578       r_type_ha = R_PPC64_TOC16_HA;
   11579       break;
   11580     default:
   11581       abort ();
   11582     }
   11583   r_info_ha = ELF64_R_INFO (ELF64_R_SYM (rel->r_info), r_type_ha);
   11584   r_addend = rel->r_addend;
   11585 
   11586   while (--rel >= relocs)
   11587     if (rel->r_info == r_info_ha
   11588 	&& (!match_addend
   11589 	    || rel->r_addend == r_addend))
   11590       {
   11591 	const bfd_byte *p = contents + (rel->r_offset & ~3);
   11592 	unsigned int insn = bfd_get_32 (input_bfd, p);
   11593 	if ((insn & (0x3f << 26)) == (15u << 26) /* addis rt,x,imm */
   11594 	    && (insn & (0x1f << 21)) == (*reg << 21))
   11595 	  {
   11596 	    *reg = (insn >> 16) & 0x1f;
   11597 	    return rel;
   11598 	  }
   11599 	break;
   11600       }
   11601   return NULL;
   11602 }
   11603 
   11604 /* The RELOCATE_SECTION function is called by the ELF backend linker
   11605    to handle the relocations for a section.
   11606 
   11607    The relocs are always passed as Rela structures; if the section
   11608    actually uses Rel structures, the r_addend field will always be
   11609    zero.
   11610 
   11611    This function is responsible for adjust the section contents as
   11612    necessary, and (if using Rela relocs and generating a
   11613    relocatable output file) adjusting the reloc addend as
   11614    necessary.
   11615 
   11616    This function does not have to worry about setting the reloc
   11617    address or the reloc symbol index.
   11618 
   11619    LOCAL_SYMS is a pointer to the swapped in local symbols.
   11620 
   11621    LOCAL_SECTIONS is an array giving the section in the input file
   11622    corresponding to the st_shndx field of each local symbol.
   11623 
   11624    The global hash table entry for the global symbols can be found
   11625    via elf_sym_hashes (input_bfd).
   11626 
   11627    When generating relocatable output, this function must handle
   11628    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
   11629    going to be the section symbol corresponding to the output
   11630    section, which means that the addend must be adjusted
   11631    accordingly.  */
   11632 
   11633 static bfd_boolean
   11634 ppc64_elf_relocate_section (bfd *output_bfd,
   11635 			    struct bfd_link_info *info,
   11636 			    bfd *input_bfd,
   11637 			    asection *input_section,
   11638 			    bfd_byte *contents,
   11639 			    Elf_Internal_Rela *relocs,
   11640 			    Elf_Internal_Sym *local_syms,
   11641 			    asection **local_sections)
   11642 {
   11643   struct ppc_link_hash_table *htab;
   11644   Elf_Internal_Shdr *symtab_hdr;
   11645   struct elf_link_hash_entry **sym_hashes;
   11646   Elf_Internal_Rela *rel;
   11647   Elf_Internal_Rela *relend;
   11648   Elf_Internal_Rela outrel;
   11649   bfd_byte *loc;
   11650   struct got_entry **local_got_ents;
   11651   unsigned char *ha_opt;
   11652   bfd_vma TOCstart;
   11653   bfd_boolean no_ha_opt;
   11654   bfd_boolean ret = TRUE;
   11655   bfd_boolean is_opd;
   11656   /* Disabled until we sort out how ld should choose 'y' vs 'at'.  */
   11657   bfd_boolean is_power4 = FALSE;
   11658   bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
   11659 
   11660   /* Initialize howto table if needed.  */
   11661   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
   11662     ppc_howto_init ();
   11663 
   11664   htab = ppc_hash_table (info);
   11665   if (htab == NULL)
   11666     return FALSE;
   11667 
   11668   /* Don't relocate stub sections.  */
   11669   if (input_section->owner == htab->stub_bfd)
   11670     return TRUE;
   11671 
   11672   BFD_ASSERT (is_ppc64_elf (input_bfd));
   11673 
   11674   local_got_ents = elf_local_got_ents (input_bfd);
   11675   TOCstart = elf_gp (output_bfd);
   11676   symtab_hdr = &elf_symtab_hdr (input_bfd);
   11677   sym_hashes = elf_sym_hashes (input_bfd);
   11678   is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
   11679   ha_opt = NULL;
   11680   no_ha_opt = FALSE;
   11681 
   11682   rel = relocs;
   11683   relend = relocs + input_section->reloc_count;
   11684   for (; rel < relend; rel++)
   11685     {
   11686       enum elf_ppc64_reloc_type r_type;
   11687       bfd_vma addend, orig_addend;
   11688       bfd_reloc_status_type r;
   11689       Elf_Internal_Sym *sym;
   11690       asection *sec;
   11691       struct elf_link_hash_entry *h_elf;
   11692       struct ppc_link_hash_entry *h;
   11693       struct ppc_link_hash_entry *fdh;
   11694       const char *sym_name;
   11695       unsigned long r_symndx, toc_symndx;
   11696       bfd_vma toc_addend;
   11697       unsigned char tls_mask, tls_gd, tls_type;
   11698       unsigned char sym_type;
   11699       bfd_vma relocation;
   11700       bfd_boolean unresolved_reloc;
   11701       bfd_boolean warned;
   11702       unsigned int insn;
   11703       unsigned int mask;
   11704       struct ppc_stub_hash_entry *stub_entry;
   11705       bfd_vma max_br_offset;
   11706       bfd_vma from;
   11707 
   11708       r_type = ELF64_R_TYPE (rel->r_info);
   11709       r_symndx = ELF64_R_SYM (rel->r_info);
   11710 
   11711       /* For old style R_PPC64_TOC relocs with a zero symbol, use the
   11712 	 symbol of the previous ADDR64 reloc.  The symbol gives us the
   11713 	 proper TOC base to use.  */
   11714       if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
   11715 	  && rel != relocs
   11716 	  && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
   11717 	  && is_opd)
   11718 	r_symndx = ELF64_R_SYM (rel[-1].r_info);
   11719 
   11720       sym = NULL;
   11721       sec = NULL;
   11722       h_elf = NULL;
   11723       sym_name = NULL;
   11724       unresolved_reloc = FALSE;
   11725       warned = FALSE;
   11726       orig_addend = rel->r_addend;
   11727 
   11728       if (r_symndx < symtab_hdr->sh_info)
   11729 	{
   11730 	  /* It's a local symbol.  */
   11731 	  struct _opd_sec_data *opd;
   11732 
   11733 	  sym = local_syms + r_symndx;
   11734 	  sec = local_sections[r_symndx];
   11735 	  sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
   11736 	  sym_type = ELF64_ST_TYPE (sym->st_info);
   11737 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
   11738 	  opd = get_opd_info (sec);
   11739 	  if (opd != NULL && opd->adjust != NULL)
   11740 	    {
   11741 	      long adjust = opd->adjust[(sym->st_value + rel->r_addend) / 8];
   11742 	      if (adjust == -1)
   11743 		relocation = 0;
   11744 	      else
   11745 		{
   11746 		  /* If this is a relocation against the opd section sym
   11747 		     and we have edited .opd, adjust the reloc addend so
   11748 		     that ld -r and ld --emit-relocs output is correct.
   11749 		     If it is a reloc against some other .opd symbol,
   11750 		     then the symbol value will be adjusted later.  */
   11751 		  if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
   11752 		    rel->r_addend += adjust;
   11753 		  else
   11754 		    relocation += adjust;
   11755 		}
   11756 	    }
   11757 	}
   11758       else
   11759 	{
   11760 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
   11761 				   r_symndx, symtab_hdr, sym_hashes,
   11762 				   h_elf, sec, relocation,
   11763 				   unresolved_reloc, warned);
   11764 	  sym_name = h_elf->root.root.string;
   11765 	  sym_type = h_elf->type;
   11766 	}
   11767       h = (struct ppc_link_hash_entry *) h_elf;
   11768 
   11769       if (sec != NULL && elf_discarded_section (sec))
   11770 	RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
   11771 					 rel, relend,
   11772 					 ppc64_elf_howto_table[r_type],
   11773 					 contents);
   11774 
   11775       if (info->relocatable)
   11776 	continue;
   11777 
   11778       /* TLS optimizations.  Replace instruction sequences and relocs
   11779 	 based on information we collected in tls_optimize.  We edit
   11780 	 RELOCS so that --emit-relocs will output something sensible
   11781 	 for the final instruction stream.  */
   11782       tls_mask = 0;
   11783       tls_gd = 0;
   11784       toc_symndx = 0;
   11785       if (h != NULL)
   11786 	tls_mask = h->tls_mask;
   11787       else if (local_got_ents != NULL)
   11788 	{
   11789 	  struct plt_entry **local_plt = (struct plt_entry **)
   11790 	    (local_got_ents + symtab_hdr->sh_info);
   11791 	  unsigned char *lgot_masks = (unsigned char *)
   11792 	    (local_plt + symtab_hdr->sh_info);
   11793 	  tls_mask = lgot_masks[r_symndx];
   11794 	}
   11795       if (tls_mask == 0
   11796 	  && (r_type == R_PPC64_TLS
   11797 	      || r_type == R_PPC64_TLSGD
   11798 	      || r_type == R_PPC64_TLSLD))
   11799 	{
   11800 	  /* Check for toc tls entries.  */
   11801 	  unsigned char *toc_tls;
   11802 
   11803 	  if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
   11804 			     &local_syms, rel, input_bfd))
   11805 	    return FALSE;
   11806 
   11807 	  if (toc_tls)
   11808 	    tls_mask = *toc_tls;
   11809 	}
   11810 
   11811       /* Check that tls relocs are used with tls syms, and non-tls
   11812 	 relocs are used with non-tls syms.  */
   11813       if (r_symndx != STN_UNDEF
   11814 	  && r_type != R_PPC64_NONE
   11815 	  && (h == NULL
   11816 	      || h->elf.root.type == bfd_link_hash_defined
   11817 	      || h->elf.root.type == bfd_link_hash_defweak)
   11818 	  && (IS_PPC64_TLS_RELOC (r_type)
   11819 	      != (sym_type == STT_TLS
   11820 		  || (sym_type == STT_SECTION
   11821 		      && (sec->flags & SEC_THREAD_LOCAL) != 0))))
   11822 	{
   11823 	  if (tls_mask != 0
   11824 	      && (r_type == R_PPC64_TLS
   11825 		  || r_type == R_PPC64_TLSGD
   11826 		  || r_type == R_PPC64_TLSLD))
   11827 	    /* R_PPC64_TLS is OK against a symbol in the TOC.  */
   11828 	    ;
   11829 	  else
   11830 	    (*_bfd_error_handler)
   11831 	      (!IS_PPC64_TLS_RELOC (r_type)
   11832 	       ? _("%B(%A+0x%lx): %s used with TLS symbol %s")
   11833 	       : _("%B(%A+0x%lx): %s used with non-TLS symbol %s"),
   11834 	       input_bfd,
   11835 	       input_section,
   11836 	       (long) rel->r_offset,
   11837 	       ppc64_elf_howto_table[r_type]->name,
   11838 	       sym_name);
   11839 	}
   11840 
   11841       /* Ensure reloc mapping code below stays sane.  */
   11842       if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
   11843 	  || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
   11844 	  || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TLSGD16 & 3)
   11845 	  || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
   11846 	  || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
   11847 	  || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
   11848 	  || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TPREL16_DS & 3)
   11849 	  || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
   11850 	  || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
   11851 	  || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
   11852 	abort ();
   11853 
   11854       switch (r_type)
   11855 	{
   11856 	default:
   11857 	  break;
   11858 
   11859 	case R_PPC64_LO_DS_OPT:
   11860 	  insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
   11861 	  if ((insn & (0x3f << 26)) != 58u << 26)
   11862 	    abort ();
   11863 	  insn += (14u << 26) - (58u << 26);
   11864 	  bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
   11865 	  r_type = R_PPC64_TOC16_LO;
   11866 	  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   11867 	  break;
   11868 
   11869 	case R_PPC64_TOC16:
   11870 	case R_PPC64_TOC16_LO:
   11871 	case R_PPC64_TOC16_DS:
   11872 	case R_PPC64_TOC16_LO_DS:
   11873 	  {
   11874 	    /* Check for toc tls entries.  */
   11875 	    unsigned char *toc_tls;
   11876 	    int retval;
   11877 
   11878 	    retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
   11879 				   &local_syms, rel, input_bfd);
   11880 	    if (retval == 0)
   11881 	      return FALSE;
   11882 
   11883 	    if (toc_tls)
   11884 	      {
   11885 		tls_mask = *toc_tls;
   11886 		if (r_type == R_PPC64_TOC16_DS
   11887 		    || r_type == R_PPC64_TOC16_LO_DS)
   11888 		  {
   11889 		    if (tls_mask != 0
   11890 			&& (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
   11891 		      goto toctprel;
   11892 		  }
   11893 		else
   11894 		  {
   11895 		    /* If we found a GD reloc pair, then we might be
   11896 		       doing a GD->IE transition.  */
   11897 		    if (retval == 2)
   11898 		      {
   11899 			tls_gd = TLS_TPRELGD;
   11900 			if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
   11901 			  goto tls_ldgd_opt;
   11902 		      }
   11903 		    else if (retval == 3)
   11904 		      {
   11905 			if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
   11906 			  goto tls_ldgd_opt;
   11907 		      }
   11908 		  }
   11909 	      }
   11910 	  }
   11911 	  break;
   11912 
   11913 	case R_PPC64_GOT_TPREL16_HI:
   11914 	case R_PPC64_GOT_TPREL16_HA:
   11915 	  if (tls_mask != 0
   11916 	      && (tls_mask & TLS_TPREL) == 0)
   11917 	    {
   11918 	      rel->r_offset -= d_offset;
   11919 	      bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
   11920 	      r_type = R_PPC64_NONE;
   11921 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   11922 	    }
   11923 	  break;
   11924 
   11925 	case R_PPC64_GOT_TPREL16_DS:
   11926 	case R_PPC64_GOT_TPREL16_LO_DS:
   11927 	  if (tls_mask != 0
   11928 	      && (tls_mask & TLS_TPREL) == 0)
   11929 	    {
   11930 	    toctprel:
   11931 	      insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
   11932 	      insn &= 31 << 21;
   11933 	      insn |= 0x3c0d0000;	/* addis 0,13,0 */
   11934 	      bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
   11935 	      r_type = R_PPC64_TPREL16_HA;
   11936 	      if (toc_symndx != 0)
   11937 		{
   11938 		  rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
   11939 		  rel->r_addend = toc_addend;
   11940 		  /* We changed the symbol.  Start over in order to
   11941 		     get h, sym, sec etc. right.  */
   11942 		  rel--;
   11943 		  continue;
   11944 		}
   11945 	      else
   11946 		rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   11947 	    }
   11948 	  break;
   11949 
   11950 	case R_PPC64_TLS:
   11951 	  if (tls_mask != 0
   11952 	      && (tls_mask & TLS_TPREL) == 0)
   11953 	    {
   11954 	      insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
   11955 	      insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
   11956 	      if (insn == 0)
   11957 		abort ();
   11958 	      bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
   11959 	      /* Was PPC64_TLS which sits on insn boundary, now
   11960 		 PPC64_TPREL16_LO which is at low-order half-word.  */
   11961 	      rel->r_offset += d_offset;
   11962 	      r_type = R_PPC64_TPREL16_LO;
   11963 	      if (toc_symndx != 0)
   11964 		{
   11965 		  rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
   11966 		  rel->r_addend = toc_addend;
   11967 		  /* We changed the symbol.  Start over in order to
   11968 		     get h, sym, sec etc. right.  */
   11969 		  rel--;
   11970 		  continue;
   11971 		}
   11972 	      else
   11973 		rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   11974 	    }
   11975 	  break;
   11976 
   11977 	case R_PPC64_GOT_TLSGD16_HI:
   11978 	case R_PPC64_GOT_TLSGD16_HA:
   11979 	  tls_gd = TLS_TPRELGD;
   11980 	  if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
   11981 	    goto tls_gdld_hi;
   11982 	  break;
   11983 
   11984 	case R_PPC64_GOT_TLSLD16_HI:
   11985 	case R_PPC64_GOT_TLSLD16_HA:
   11986 	  if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
   11987 	    {
   11988 	    tls_gdld_hi:
   11989 	      if ((tls_mask & tls_gd) != 0)
   11990 		r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
   11991 			  + R_PPC64_GOT_TPREL16_DS);
   11992 	      else
   11993 		{
   11994 		  rel->r_offset -= d_offset;
   11995 		  bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
   11996 		  r_type = R_PPC64_NONE;
   11997 		}
   11998 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   11999 	    }
   12000 	  break;
   12001 
   12002 	case R_PPC64_GOT_TLSGD16:
   12003 	case R_PPC64_GOT_TLSGD16_LO:
   12004 	  tls_gd = TLS_TPRELGD;
   12005 	  if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
   12006 	    goto tls_ldgd_opt;
   12007 	  break;
   12008 
   12009 	case R_PPC64_GOT_TLSLD16:
   12010 	case R_PPC64_GOT_TLSLD16_LO:
   12011 	  if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
   12012 	    {
   12013 	      unsigned int insn1, insn2, insn3;
   12014 	      bfd_vma offset;
   12015 
   12016 	    tls_ldgd_opt:
   12017 	      offset = (bfd_vma) -1;
   12018 	      /* If not using the newer R_PPC64_TLSGD/LD to mark
   12019 		 __tls_get_addr calls, we must trust that the call
   12020 		 stays with its arg setup insns, ie. that the next
   12021 		 reloc is the __tls_get_addr call associated with
   12022 		 the current reloc.  Edit both insns.  */
   12023 	      if (input_section->has_tls_get_addr_call
   12024 		  && rel + 1 < relend
   12025 		  && branch_reloc_hash_match (input_bfd, rel + 1,
   12026 					      htab->tls_get_addr,
   12027 					      htab->tls_get_addr_fd))
   12028 		offset = rel[1].r_offset;
   12029 	      if ((tls_mask & tls_gd) != 0)
   12030 		{
   12031 		  /* IE */
   12032 		  insn1 = bfd_get_32 (output_bfd,
   12033 				      contents + rel->r_offset - d_offset);
   12034 		  insn1 &= (1 << 26) - (1 << 2);
   12035 		  insn1 |= 58 << 26;	/* ld */
   12036 		  insn2 = 0x7c636a14;	/* add 3,3,13 */
   12037 		  if (offset != (bfd_vma) -1)
   12038 		    rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
   12039 		  if ((tls_mask & TLS_EXPLICIT) == 0)
   12040 		    r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
   12041 			      + R_PPC64_GOT_TPREL16_DS);
   12042 		  else
   12043 		    r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
   12044 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12045 		}
   12046 	      else
   12047 		{
   12048 		  /* LE */
   12049 		  insn1 = 0x3c6d0000;	/* addis 3,13,0 */
   12050 		  insn2 = 0x38630000;	/* addi 3,3,0 */
   12051 		  if (tls_gd == 0)
   12052 		    {
   12053 		      /* Was an LD reloc.  */
   12054 		      if (toc_symndx)
   12055 			sec = local_sections[toc_symndx];
   12056 		      for (r_symndx = 0;
   12057 			   r_symndx < symtab_hdr->sh_info;
   12058 			   r_symndx++)
   12059 			if (local_sections[r_symndx] == sec)
   12060 			  break;
   12061 		      if (r_symndx >= symtab_hdr->sh_info)
   12062 			r_symndx = STN_UNDEF;
   12063 		      rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
   12064 		      if (r_symndx != STN_UNDEF)
   12065 			rel->r_addend -= (local_syms[r_symndx].st_value
   12066 					  + sec->output_offset
   12067 					  + sec->output_section->vma);
   12068 		    }
   12069 		  else if (toc_symndx != 0)
   12070 		    {
   12071 		      r_symndx = toc_symndx;
   12072 		      rel->r_addend = toc_addend;
   12073 		    }
   12074 		  r_type = R_PPC64_TPREL16_HA;
   12075 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12076 		  if (offset != (bfd_vma) -1)
   12077 		    {
   12078 		      rel[1].r_info = ELF64_R_INFO (r_symndx,
   12079 						    R_PPC64_TPREL16_LO);
   12080 		      rel[1].r_offset = offset + d_offset;
   12081 		      rel[1].r_addend = rel->r_addend;
   12082 		    }
   12083 		}
   12084 	      bfd_put_32 (output_bfd, insn1,
   12085 			  contents + rel->r_offset - d_offset);
   12086 	      if (offset != (bfd_vma) -1)
   12087 		{
   12088 		  insn3 = bfd_get_32 (output_bfd,
   12089 				      contents + offset + 4);
   12090 		  if (insn3 == NOP
   12091 		      || insn3 == CROR_151515 || insn3 == CROR_313131)
   12092 		    {
   12093 		      rel[1].r_offset += 4;
   12094 		      bfd_put_32 (output_bfd, insn2, contents + offset + 4);
   12095 		      insn2 = NOP;
   12096 		    }
   12097 		  bfd_put_32 (output_bfd, insn2, contents + offset);
   12098 		}
   12099 	      if ((tls_mask & tls_gd) == 0
   12100 		  && (tls_gd == 0 || toc_symndx != 0))
   12101 		{
   12102 		  /* We changed the symbol.  Start over in order
   12103 		     to get h, sym, sec etc. right.  */
   12104 		  rel--;
   12105 		  continue;
   12106 		}
   12107 	    }
   12108 	  break;
   12109 
   12110 	case R_PPC64_TLSGD:
   12111 	  if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
   12112 	    {
   12113 	      unsigned int insn2, insn3;
   12114 	      bfd_vma offset = rel->r_offset;
   12115 
   12116 	      if ((tls_mask & TLS_TPRELGD) != 0)
   12117 		{
   12118 		  /* IE */
   12119 		  r_type = R_PPC64_NONE;
   12120 		  insn2 = 0x7c636a14;	/* add 3,3,13 */
   12121 		}
   12122 	      else
   12123 		{
   12124 		  /* LE */
   12125 		  if (toc_symndx != 0)
   12126 		    {
   12127 		      r_symndx = toc_symndx;
   12128 		      rel->r_addend = toc_addend;
   12129 		    }
   12130 		  r_type = R_PPC64_TPREL16_LO;
   12131 		  rel->r_offset = offset + d_offset;
   12132 		  insn2 = 0x38630000;	/* addi 3,3,0 */
   12133 		}
   12134 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12135 	      /* Zap the reloc on the _tls_get_addr call too.  */
   12136 	      BFD_ASSERT (offset == rel[1].r_offset);
   12137 	      rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
   12138 	      insn3 = bfd_get_32 (output_bfd,
   12139 				  contents + offset + 4);
   12140 	      if (insn3 == NOP
   12141 		  || insn3 == CROR_151515 || insn3 == CROR_313131)
   12142 		{
   12143 		  rel->r_offset += 4;
   12144 		  bfd_put_32 (output_bfd, insn2, contents + offset + 4);
   12145 		  insn2 = NOP;
   12146 		}
   12147 	      bfd_put_32 (output_bfd, insn2, contents + offset);
   12148 	      if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
   12149 		{
   12150 		  rel--;
   12151 		  continue;
   12152 		}
   12153 	    }
   12154 	  break;
   12155 
   12156 	case R_PPC64_TLSLD:
   12157 	  if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
   12158 	    {
   12159 	      unsigned int insn2, insn3;
   12160 	      bfd_vma offset = rel->r_offset;
   12161 
   12162 	      if (toc_symndx)
   12163 		sec = local_sections[toc_symndx];
   12164 	      for (r_symndx = 0;
   12165 		   r_symndx < symtab_hdr->sh_info;
   12166 		   r_symndx++)
   12167 		if (local_sections[r_symndx] == sec)
   12168 		  break;
   12169 	      if (r_symndx >= symtab_hdr->sh_info)
   12170 		r_symndx = STN_UNDEF;
   12171 	      rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
   12172 	      if (r_symndx != STN_UNDEF)
   12173 		rel->r_addend -= (local_syms[r_symndx].st_value
   12174 				  + sec->output_offset
   12175 				  + sec->output_section->vma);
   12176 
   12177 	      r_type = R_PPC64_TPREL16_LO;
   12178 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12179 	      rel->r_offset = offset + d_offset;
   12180 	      /* Zap the reloc on the _tls_get_addr call too.  */
   12181 	      BFD_ASSERT (offset == rel[1].r_offset);
   12182 	      rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
   12183 	      insn2 = 0x38630000;	/* addi 3,3,0 */
   12184 	      insn3 = bfd_get_32 (output_bfd,
   12185 				  contents + offset + 4);
   12186 	      if (insn3 == NOP
   12187 		  || insn3 == CROR_151515 || insn3 == CROR_313131)
   12188 		{
   12189 		  rel->r_offset += 4;
   12190 		  bfd_put_32 (output_bfd, insn2, contents + offset + 4);
   12191 		  insn2 = NOP;
   12192 		}
   12193 	      bfd_put_32 (output_bfd, insn2, contents + offset);
   12194 	      rel--;
   12195 	      continue;
   12196 	    }
   12197 	  break;
   12198 
   12199 	case R_PPC64_DTPMOD64:
   12200 	  if (rel + 1 < relend
   12201 	      && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
   12202 	      && rel[1].r_offset == rel->r_offset + 8)
   12203 	    {
   12204 	      if ((tls_mask & TLS_GD) == 0)
   12205 		{
   12206 		  rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
   12207 		  if ((tls_mask & TLS_TPRELGD) != 0)
   12208 		    r_type = R_PPC64_TPREL64;
   12209 		  else
   12210 		    {
   12211 		      bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
   12212 		      r_type = R_PPC64_NONE;
   12213 		    }
   12214 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12215 		}
   12216 	    }
   12217 	  else
   12218 	    {
   12219 	      if ((tls_mask & TLS_LD) == 0)
   12220 		{
   12221 		  bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
   12222 		  r_type = R_PPC64_NONE;
   12223 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12224 		}
   12225 	    }
   12226 	  break;
   12227 
   12228 	case R_PPC64_TPREL64:
   12229 	  if ((tls_mask & TLS_TPREL) == 0)
   12230 	    {
   12231 	      r_type = R_PPC64_NONE;
   12232 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   12233 	    }
   12234 	  break;
   12235 	}
   12236 
   12237       /* Handle other relocations that tweak non-addend part of insn.  */
   12238       insn = 0;
   12239       max_br_offset = 1 << 25;
   12240       addend = rel->r_addend;
   12241       switch (r_type)
   12242 	{
   12243 	default:
   12244 	  break;
   12245 
   12246 	  /* Branch taken prediction relocations.  */
   12247 	case R_PPC64_ADDR14_BRTAKEN:
   12248 	case R_PPC64_REL14_BRTAKEN:
   12249 	  insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
   12250 	  /* Fall thru.  */
   12251 
   12252 	  /* Branch not taken prediction relocations.  */
   12253 	case R_PPC64_ADDR14_BRNTAKEN:
   12254 	case R_PPC64_REL14_BRNTAKEN:
   12255 	  insn |= bfd_get_32 (output_bfd,
   12256 			      contents + rel->r_offset) & ~(0x01 << 21);
   12257 	  /* Fall thru.  */
   12258 
   12259 	case R_PPC64_REL14:
   12260 	  max_br_offset = 1 << 15;
   12261 	  /* Fall thru.  */
   12262 
   12263 	case R_PPC64_REL24:
   12264 	  /* Calls to functions with a different TOC, such as calls to
   12265 	     shared objects, need to alter the TOC pointer.  This is
   12266 	     done using a linkage stub.  A REL24 branching to these
   12267 	     linkage stubs needs to be followed by a nop, as the nop
   12268 	     will be replaced with an instruction to restore the TOC
   12269 	     base pointer.  */
   12270 	  fdh = h;
   12271 	  if (h != NULL
   12272 	      && h->oh != NULL
   12273 	      && h->oh->is_func_descriptor)
   12274 	    fdh = ppc_follow_link (h->oh);
   12275 	  stub_entry = ppc_get_stub_entry (input_section, sec, fdh, rel, htab);
   12276 	  if (stub_entry != NULL
   12277 	      && (stub_entry->stub_type == ppc_stub_plt_call
   12278 		  || stub_entry->stub_type == ppc_stub_plt_branch_r2off
   12279 		  || stub_entry->stub_type == ppc_stub_long_branch_r2off))
   12280 	    {
   12281 	      bfd_boolean can_plt_call = FALSE;
   12282 
   12283 	      if (rel->r_offset + 8 <= input_section->size)
   12284 		{
   12285 		  unsigned long nop;
   12286 		  nop = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
   12287 		  if (nop == NOP
   12288 		      || nop == CROR_151515 || nop == CROR_313131)
   12289 		    {
   12290 		      if (h != NULL
   12291 			  && (h == htab->tls_get_addr_fd
   12292 			      || h == htab->tls_get_addr)
   12293 			  && !htab->no_tls_get_addr_opt)
   12294 			{
   12295 			  /* Special stub used, leave nop alone.  */
   12296 			}
   12297 		      else
   12298 			bfd_put_32 (input_bfd, LD_R2_40R1,
   12299 				    contents + rel->r_offset + 4);
   12300 		      can_plt_call = TRUE;
   12301 		    }
   12302 		}
   12303 
   12304 	      if (!can_plt_call)
   12305 		{
   12306 		  if (stub_entry->stub_type == ppc_stub_plt_call)
   12307 		    {
   12308 		      /* If this is a plain branch rather than a branch
   12309 			 and link, don't require a nop.  However, don't
   12310 			 allow tail calls in a shared library as they
   12311 			 will result in r2 being corrupted.  */
   12312 		      unsigned long br;
   12313 		      br = bfd_get_32 (input_bfd, contents + rel->r_offset);
   12314 		      if (info->executable && (br & 1) == 0)
   12315 			can_plt_call = TRUE;
   12316 		      else
   12317 			stub_entry = NULL;
   12318 		    }
   12319 		  else if (h != NULL
   12320 			   && strcmp (h->elf.root.root.string,
   12321 				      ".__libc_start_main") == 0)
   12322 		    {
   12323 		      /* Allow crt1 branch to go via a toc adjusting stub.  */
   12324 		      can_plt_call = TRUE;
   12325 		    }
   12326 		  else
   12327 		    {
   12328 		      if (strcmp (input_section->output_section->name,
   12329 				  ".init") == 0
   12330 			  || strcmp (input_section->output_section->name,
   12331 				     ".fini") == 0)
   12332 			(*_bfd_error_handler)
   12333 			  (_("%B(%A+0x%lx): automatic multiple TOCs "
   12334 			     "not supported using your crt files; "
   12335 			     "recompile with -mminimal-toc or upgrade gcc"),
   12336 			   input_bfd,
   12337 			   input_section,
   12338 			   (long) rel->r_offset);
   12339 		      else
   12340 			(*_bfd_error_handler)
   12341 			  (_("%B(%A+0x%lx): sibling call optimization to `%s' "
   12342 			     "does not allow automatic multiple TOCs; "
   12343 			     "recompile with -mminimal-toc or "
   12344 			     "-fno-optimize-sibling-calls, "
   12345 			     "or make `%s' extern"),
   12346 			   input_bfd,
   12347 			   input_section,
   12348 			   (long) rel->r_offset,
   12349 			   sym_name,
   12350 			   sym_name);
   12351 		      bfd_set_error (bfd_error_bad_value);
   12352 		      ret = FALSE;
   12353 		    }
   12354 		}
   12355 
   12356 	      if (can_plt_call
   12357 		  && stub_entry->stub_type == ppc_stub_plt_call)
   12358 		unresolved_reloc = FALSE;
   12359 	    }
   12360 
   12361 	  if ((stub_entry == NULL
   12362 	       || stub_entry->stub_type == ppc_stub_long_branch
   12363 	       || stub_entry->stub_type == ppc_stub_plt_branch)
   12364 	      && get_opd_info (sec) != NULL)
   12365 	    {
   12366 	      /* The branch destination is the value of the opd entry. */
   12367 	      bfd_vma off = (relocation + addend
   12368 			     - sec->output_section->vma
   12369 			     - sec->output_offset);
   12370 	      bfd_vma dest = opd_entry_value (sec, off, NULL, NULL);
   12371 	      if (dest != (bfd_vma) -1)
   12372 		{
   12373 		  relocation = dest;
   12374 		  addend = 0;
   12375 		}
   12376 	    }
   12377 
   12378 	  /* If the branch is out of reach we ought to have a long
   12379 	     branch stub.  */
   12380 	  from = (rel->r_offset
   12381 		  + input_section->output_offset
   12382 		  + input_section->output_section->vma);
   12383 
   12384 	  if (stub_entry != NULL
   12385 	      && (stub_entry->stub_type == ppc_stub_long_branch
   12386 		  || stub_entry->stub_type == ppc_stub_plt_branch)
   12387 	      && (r_type == R_PPC64_ADDR14_BRTAKEN
   12388 		  || r_type == R_PPC64_ADDR14_BRNTAKEN
   12389 		  || (relocation + addend - from + max_br_offset
   12390 		      < 2 * max_br_offset)))
   12391 	    /* Don't use the stub if this branch is in range.  */
   12392 	    stub_entry = NULL;
   12393 
   12394 	  if (stub_entry != NULL)
   12395 	    {
   12396 	      /* Munge up the value and addend so that we call the stub
   12397 		 rather than the procedure directly.  */
   12398 	      relocation = (stub_entry->stub_offset
   12399 			    + stub_entry->stub_sec->output_offset
   12400 			    + stub_entry->stub_sec->output_section->vma);
   12401 	      addend = 0;
   12402 	    }
   12403 
   12404 	  if (insn != 0)
   12405 	    {
   12406 	      if (is_power4)
   12407 		{
   12408 		  /* Set 'a' bit.  This is 0b00010 in BO field for branch
   12409 		     on CR(BI) insns (BO == 001at or 011at), and 0b01000
   12410 		     for branch on CTR insns (BO == 1a00t or 1a01t).  */
   12411 		  if ((insn & (0x14 << 21)) == (0x04 << 21))
   12412 		    insn |= 0x02 << 21;
   12413 		  else if ((insn & (0x14 << 21)) == (0x10 << 21))
   12414 		    insn |= 0x08 << 21;
   12415 		  else
   12416 		    break;
   12417 		}
   12418 	      else
   12419 		{
   12420 		  /* Invert 'y' bit if not the default.  */
   12421 		  if ((bfd_signed_vma) (relocation + addend - from) < 0)
   12422 		    insn ^= 0x01 << 21;
   12423 		}
   12424 
   12425 	      bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
   12426 	    }
   12427 
   12428 	  /* NOP out calls to undefined weak functions.
   12429 	     We can thus call a weak function without first
   12430 	     checking whether the function is defined.  */
   12431 	  else if (h != NULL
   12432 		   && h->elf.root.type == bfd_link_hash_undefweak
   12433 		   && h->elf.dynindx == -1
   12434 		   && r_type == R_PPC64_REL24
   12435 		   && relocation == 0
   12436 		   && addend == 0)
   12437 	    {
   12438 	      bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
   12439 	      continue;
   12440 	    }
   12441 	  break;
   12442 	}
   12443 
   12444       /* Set `addend'.  */
   12445       tls_type = 0;
   12446       switch (r_type)
   12447 	{
   12448 	default:
   12449 	  (*_bfd_error_handler)
   12450 	    (_("%B: unknown relocation type %d for symbol %s"),
   12451 	     input_bfd, (int) r_type, sym_name);
   12452 
   12453 	  bfd_set_error (bfd_error_bad_value);
   12454 	  ret = FALSE;
   12455 	  continue;
   12456 
   12457 	case R_PPC64_NONE:
   12458 	case R_PPC64_TLS:
   12459 	case R_PPC64_TLSGD:
   12460 	case R_PPC64_TLSLD:
   12461 	case R_PPC64_GNU_VTINHERIT:
   12462 	case R_PPC64_GNU_VTENTRY:
   12463 	  continue;
   12464 
   12465 	  /* GOT16 relocations.  Like an ADDR16 using the symbol's
   12466 	     address in the GOT as relocation value instead of the
   12467 	     symbol's value itself.  Also, create a GOT entry for the
   12468 	     symbol and put the symbol value there.  */
   12469 	case R_PPC64_GOT_TLSGD16:
   12470 	case R_PPC64_GOT_TLSGD16_LO:
   12471 	case R_PPC64_GOT_TLSGD16_HI:
   12472 	case R_PPC64_GOT_TLSGD16_HA:
   12473 	  tls_type = TLS_TLS | TLS_GD;
   12474 	  goto dogot;
   12475 
   12476 	case R_PPC64_GOT_TLSLD16:
   12477 	case R_PPC64_GOT_TLSLD16_LO:
   12478 	case R_PPC64_GOT_TLSLD16_HI:
   12479 	case R_PPC64_GOT_TLSLD16_HA:
   12480 	  tls_type = TLS_TLS | TLS_LD;
   12481 	  goto dogot;
   12482 
   12483 	case R_PPC64_GOT_TPREL16_DS:
   12484 	case R_PPC64_GOT_TPREL16_LO_DS:
   12485 	case R_PPC64_GOT_TPREL16_HI:
   12486 	case R_PPC64_GOT_TPREL16_HA:
   12487 	  tls_type = TLS_TLS | TLS_TPREL;
   12488 	  goto dogot;
   12489 
   12490 	case R_PPC64_GOT_DTPREL16_DS:
   12491 	case R_PPC64_GOT_DTPREL16_LO_DS:
   12492 	case R_PPC64_GOT_DTPREL16_HI:
   12493 	case R_PPC64_GOT_DTPREL16_HA:
   12494 	  tls_type = TLS_TLS | TLS_DTPREL;
   12495 	  goto dogot;
   12496 
   12497 	case R_PPC64_GOT16:
   12498 	case R_PPC64_GOT16_LO:
   12499 	case R_PPC64_GOT16_HI:
   12500 	case R_PPC64_GOT16_HA:
   12501 	case R_PPC64_GOT16_DS:
   12502 	case R_PPC64_GOT16_LO_DS:
   12503 	dogot:
   12504 	  {
   12505 	    /* Relocation is to the entry for this symbol in the global
   12506 	       offset table.  */
   12507 	    asection *got;
   12508 	    bfd_vma *offp;
   12509 	    bfd_vma off;
   12510 	    unsigned long indx = 0;
   12511 	    struct got_entry *ent;
   12512 
   12513 	    if (tls_type == (TLS_TLS | TLS_LD)
   12514 		&& (h == NULL
   12515 		    || !h->elf.def_dynamic))
   12516 	      ent = ppc64_tlsld_got (input_bfd);
   12517 	    else
   12518 	      {
   12519 
   12520 		if (h != NULL)
   12521 		  {
   12522 		    bfd_boolean dyn = htab->elf.dynamic_sections_created;
   12523 		    if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
   12524 							  &h->elf)
   12525 			|| (info->shared
   12526 			    && SYMBOL_CALLS_LOCAL (info, &h->elf)))
   12527 		      /* This is actually a static link, or it is a
   12528 			 -Bsymbolic link and the symbol is defined
   12529 			 locally, or the symbol was forced to be local
   12530 			 because of a version file.  */
   12531 		      ;
   12532 		    else
   12533 		      {
   12534 			indx = h->elf.dynindx;
   12535 			unresolved_reloc = FALSE;
   12536 		      }
   12537 		    ent = h->elf.got.glist;
   12538 		  }
   12539 		else
   12540 		  {
   12541 		    if (local_got_ents == NULL)
   12542 		      abort ();
   12543 		    ent = local_got_ents[r_symndx];
   12544 		  }
   12545 
   12546 		for (; ent != NULL; ent = ent->next)
   12547 		  if (ent->addend == orig_addend
   12548 		      && ent->owner == input_bfd
   12549 		      && ent->tls_type == tls_type)
   12550 		    break;
   12551 	      }
   12552 
   12553 	    if (ent == NULL)
   12554 	      abort ();
   12555 	    if (ent->is_indirect)
   12556 	      ent = ent->got.ent;
   12557 	    offp = &ent->got.offset;
   12558 	    got = ppc64_elf_tdata (ent->owner)->got;
   12559 	    if (got == NULL)
   12560 	      abort ();
   12561 
   12562 	    /* The offset must always be a multiple of 8.  We use the
   12563 	       least significant bit to record whether we have already
   12564 	       processed this entry.  */
   12565 	    off = *offp;
   12566 	    if ((off & 1) != 0)
   12567 	      off &= ~1;
   12568 	    else
   12569 	      {
   12570 		/* Generate relocs for the dynamic linker, except in
   12571 		   the case of TLSLD where we'll use one entry per
   12572 		   module.  */
   12573 		asection *relgot;
   12574 		bfd_boolean ifunc;
   12575 
   12576 		*offp = off | 1;
   12577 		relgot = NULL;
   12578 		ifunc = (h != NULL
   12579 			 ? h->elf.type == STT_GNU_IFUNC
   12580 			 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
   12581 		if ((info->shared || indx != 0)
   12582 		    && (h == NULL
   12583 			|| (tls_type == (TLS_TLS | TLS_LD)
   12584 			    && !h->elf.def_dynamic)
   12585 			|| ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
   12586 			|| h->elf.root.type != bfd_link_hash_undefweak))
   12587 		  relgot = ppc64_elf_tdata (ent->owner)->relgot;
   12588 		else if (ifunc)
   12589 		  relgot = htab->reliplt;
   12590 		if (relgot != NULL)
   12591 		  {
   12592 		    outrel.r_offset = (got->output_section->vma
   12593 				       + got->output_offset
   12594 				       + off);
   12595 		    outrel.r_addend = addend;
   12596 		    if (tls_type & (TLS_LD | TLS_GD))
   12597 		      {
   12598 			outrel.r_addend = 0;
   12599 			outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
   12600 			if (tls_type == (TLS_TLS | TLS_GD))
   12601 			  {
   12602 			    loc = relgot->contents;
   12603 			    loc += (relgot->reloc_count++
   12604 				    * sizeof (Elf64_External_Rela));
   12605 			    bfd_elf64_swap_reloca_out (output_bfd,
   12606 						       &outrel, loc);
   12607 			    outrel.r_offset += 8;
   12608 			    outrel.r_addend = addend;
   12609 			    outrel.r_info
   12610 			      = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
   12611 			  }
   12612 		      }
   12613 		    else if (tls_type == (TLS_TLS | TLS_DTPREL))
   12614 		      outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
   12615 		    else if (tls_type == (TLS_TLS | TLS_TPREL))
   12616 		      outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
   12617 		    else if (indx != 0)
   12618 		      outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
   12619 		    else
   12620 		      {
   12621 			if (ifunc)
   12622 			  outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
   12623 			else
   12624 			  outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   12625 
   12626 			/* Write the .got section contents for the sake
   12627 			   of prelink.  */
   12628 			loc = got->contents + off;
   12629 			bfd_put_64 (output_bfd, outrel.r_addend + relocation,
   12630 				    loc);
   12631 		      }
   12632 
   12633 		    if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
   12634 		      {
   12635 			outrel.r_addend += relocation;
   12636 			if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
   12637 			  outrel.r_addend -= htab->elf.tls_sec->vma;
   12638 		      }
   12639 		    loc = relgot->contents;
   12640 		    loc += (relgot->reloc_count++
   12641 			    * sizeof (Elf64_External_Rela));
   12642 		    bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
   12643 		  }
   12644 
   12645 		/* Init the .got section contents here if we're not
   12646 		   emitting a reloc.  */
   12647 		else
   12648 		  {
   12649 		    relocation += addend;
   12650 		    if (tls_type == (TLS_TLS | TLS_LD))
   12651 		      relocation = 1;
   12652 		    else if (tls_type != 0)
   12653 		      {
   12654 			relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
   12655 			if (tls_type == (TLS_TLS | TLS_TPREL))
   12656 			  relocation += DTP_OFFSET - TP_OFFSET;
   12657 
   12658 			if (tls_type == (TLS_TLS | TLS_GD))
   12659 			  {
   12660 			    bfd_put_64 (output_bfd, relocation,
   12661 					got->contents + off + 8);
   12662 			    relocation = 1;
   12663 			  }
   12664 		      }
   12665 
   12666 		    bfd_put_64 (output_bfd, relocation,
   12667 				got->contents + off);
   12668 		  }
   12669 	      }
   12670 
   12671 	    if (off >= (bfd_vma) -2)
   12672 	      abort ();
   12673 
   12674 	    relocation = got->output_section->vma + got->output_offset + off;
   12675 	    addend = -(TOCstart + htab->stub_group[input_section->id].toc_off);
   12676 	  }
   12677 	  break;
   12678 
   12679 	case R_PPC64_PLT16_HA:
   12680 	case R_PPC64_PLT16_HI:
   12681 	case R_PPC64_PLT16_LO:
   12682 	case R_PPC64_PLT32:
   12683 	case R_PPC64_PLT64:
   12684 	  /* Relocation is to the entry for this symbol in the
   12685 	     procedure linkage table.  */
   12686 
   12687 	  /* Resolve a PLT reloc against a local symbol directly,
   12688 	     without using the procedure linkage table.  */
   12689 	  if (h == NULL)
   12690 	    break;
   12691 
   12692 	  /* It's possible that we didn't make a PLT entry for this
   12693 	     symbol.  This happens when statically linking PIC code,
   12694 	     or when using -Bsymbolic.  Go find a match if there is a
   12695 	     PLT entry.  */
   12696 	  if (htab->plt != NULL)
   12697 	    {
   12698 	      struct plt_entry *ent;
   12699 	      for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
   12700 		if (ent->addend == orig_addend
   12701 		    && ent->plt.offset != (bfd_vma) -1)
   12702 		  {
   12703 		    relocation = (htab->plt->output_section->vma
   12704 				  + htab->plt->output_offset
   12705 				  + ent->plt.offset);
   12706 		    unresolved_reloc = FALSE;
   12707 		  }
   12708 	    }
   12709 	  break;
   12710 
   12711 	case R_PPC64_TOC:
   12712 	  /* Relocation value is TOC base.  */
   12713 	  relocation = TOCstart;
   12714 	  if (r_symndx == STN_UNDEF)
   12715 	    relocation += htab->stub_group[input_section->id].toc_off;
   12716 	  else if (unresolved_reloc)
   12717 	    ;
   12718 	  else if (sec != NULL && sec->id <= htab->top_id)
   12719 	    relocation += htab->stub_group[sec->id].toc_off;
   12720 	  else
   12721 	    unresolved_reloc = TRUE;
   12722 	  goto dodyn;
   12723 
   12724 	  /* TOC16 relocs.  We want the offset relative to the TOC base,
   12725 	     which is the address of the start of the TOC plus 0x8000.
   12726 	     The TOC consists of sections .got, .toc, .tocbss, and .plt,
   12727 	     in this order.  */
   12728 	case R_PPC64_TOC16:
   12729 	case R_PPC64_TOC16_LO:
   12730 	case R_PPC64_TOC16_HI:
   12731 	case R_PPC64_TOC16_DS:
   12732 	case R_PPC64_TOC16_LO_DS:
   12733 	case R_PPC64_TOC16_HA:
   12734 	  addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
   12735 	  break;
   12736 
   12737 	  /* Relocate against the beginning of the section.  */
   12738 	case R_PPC64_SECTOFF:
   12739 	case R_PPC64_SECTOFF_LO:
   12740 	case R_PPC64_SECTOFF_HI:
   12741 	case R_PPC64_SECTOFF_DS:
   12742 	case R_PPC64_SECTOFF_LO_DS:
   12743 	case R_PPC64_SECTOFF_HA:
   12744 	  if (sec != NULL)
   12745 	    addend -= sec->output_section->vma;
   12746 	  break;
   12747 
   12748 	case R_PPC64_REL16:
   12749 	case R_PPC64_REL16_LO:
   12750 	case R_PPC64_REL16_HI:
   12751 	case R_PPC64_REL16_HA:
   12752 	  break;
   12753 
   12754 	case R_PPC64_REL14:
   12755 	case R_PPC64_REL14_BRNTAKEN:
   12756 	case R_PPC64_REL14_BRTAKEN:
   12757 	case R_PPC64_REL24:
   12758 	  break;
   12759 
   12760 	case R_PPC64_TPREL16:
   12761 	case R_PPC64_TPREL16_LO:
   12762 	case R_PPC64_TPREL16_HI:
   12763 	case R_PPC64_TPREL16_HA:
   12764 	case R_PPC64_TPREL16_DS:
   12765 	case R_PPC64_TPREL16_LO_DS:
   12766 	case R_PPC64_TPREL16_HIGHER:
   12767 	case R_PPC64_TPREL16_HIGHERA:
   12768 	case R_PPC64_TPREL16_HIGHEST:
   12769 	case R_PPC64_TPREL16_HIGHESTA:
   12770 	  if (h != NULL
   12771 	      && h->elf.root.type == bfd_link_hash_undefweak
   12772 	      && h->elf.dynindx == -1)
   12773 	    {
   12774 	      /* Make this relocation against an undefined weak symbol
   12775 		 resolve to zero.  This is really just a tweak, since
   12776 		 code using weak externs ought to check that they are
   12777 		 defined before using them.  */
   12778 	      bfd_byte *p = contents + rel->r_offset - d_offset;
   12779 
   12780 	      insn = bfd_get_32 (output_bfd, p);
   12781 	      insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
   12782 	      if (insn != 0)
   12783 		bfd_put_32 (output_bfd, insn, p);
   12784 	      break;
   12785 	    }
   12786 	  addend -= htab->elf.tls_sec->vma + TP_OFFSET;
   12787 	  if (info->shared)
   12788 	    /* The TPREL16 relocs shouldn't really be used in shared
   12789 	       libs as they will result in DT_TEXTREL being set, but
   12790 	       support them anyway.  */
   12791 	    goto dodyn;
   12792 	  break;
   12793 
   12794 	case R_PPC64_DTPREL16:
   12795 	case R_PPC64_DTPREL16_LO:
   12796 	case R_PPC64_DTPREL16_HI:
   12797 	case R_PPC64_DTPREL16_HA:
   12798 	case R_PPC64_DTPREL16_DS:
   12799 	case R_PPC64_DTPREL16_LO_DS:
   12800 	case R_PPC64_DTPREL16_HIGHER:
   12801 	case R_PPC64_DTPREL16_HIGHERA:
   12802 	case R_PPC64_DTPREL16_HIGHEST:
   12803 	case R_PPC64_DTPREL16_HIGHESTA:
   12804 	  addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
   12805 	  break;
   12806 
   12807 	case R_PPC64_DTPMOD64:
   12808 	  relocation = 1;
   12809 	  addend = 0;
   12810 	  goto dodyn;
   12811 
   12812 	case R_PPC64_TPREL64:
   12813 	  addend -= htab->elf.tls_sec->vma + TP_OFFSET;
   12814 	  goto dodyn;
   12815 
   12816 	case R_PPC64_DTPREL64:
   12817 	  addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
   12818 	  /* Fall thru */
   12819 
   12820 	  /* Relocations that may need to be propagated if this is a
   12821 	     dynamic object.  */
   12822 	case R_PPC64_REL30:
   12823 	case R_PPC64_REL32:
   12824 	case R_PPC64_REL64:
   12825 	case R_PPC64_ADDR14:
   12826 	case R_PPC64_ADDR14_BRNTAKEN:
   12827 	case R_PPC64_ADDR14_BRTAKEN:
   12828 	case R_PPC64_ADDR16:
   12829 	case R_PPC64_ADDR16_DS:
   12830 	case R_PPC64_ADDR16_HA:
   12831 	case R_PPC64_ADDR16_HI:
   12832 	case R_PPC64_ADDR16_HIGHER:
   12833 	case R_PPC64_ADDR16_HIGHERA:
   12834 	case R_PPC64_ADDR16_HIGHEST:
   12835 	case R_PPC64_ADDR16_HIGHESTA:
   12836 	case R_PPC64_ADDR16_LO:
   12837 	case R_PPC64_ADDR16_LO_DS:
   12838 	case R_PPC64_ADDR24:
   12839 	case R_PPC64_ADDR32:
   12840 	case R_PPC64_ADDR64:
   12841 	case R_PPC64_UADDR16:
   12842 	case R_PPC64_UADDR32:
   12843 	case R_PPC64_UADDR64:
   12844 	dodyn:
   12845 	  if ((input_section->flags & SEC_ALLOC) == 0)
   12846 	    break;
   12847 
   12848 	  if (NO_OPD_RELOCS && is_opd)
   12849 	    break;
   12850 
   12851 	  if ((info->shared
   12852 	       && (h == NULL
   12853 		   || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
   12854 		   || h->elf.root.type != bfd_link_hash_undefweak)
   12855 	       && (must_be_dyn_reloc (info, r_type)
   12856 		   || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
   12857 	      || (ELIMINATE_COPY_RELOCS
   12858 		  && !info->shared
   12859 		  && h != NULL
   12860 		  && h->elf.dynindx != -1
   12861 		  && !h->elf.non_got_ref
   12862 		  && !h->elf.def_regular)
   12863 	      || (!info->shared
   12864 		  && (h != NULL
   12865 		      ? h->elf.type == STT_GNU_IFUNC
   12866 		      : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
   12867 	    {
   12868 	      bfd_boolean skip, relocate;
   12869 	      asection *sreloc;
   12870 	      bfd_vma out_off;
   12871 
   12872 	      /* When generating a dynamic object, these relocations
   12873 		 are copied into the output file to be resolved at run
   12874 		 time.  */
   12875 
   12876 	      skip = FALSE;
   12877 	      relocate = FALSE;
   12878 
   12879 	      out_off = _bfd_elf_section_offset (output_bfd, info,
   12880 						 input_section, rel->r_offset);
   12881 	      if (out_off == (bfd_vma) -1)
   12882 		skip = TRUE;
   12883 	      else if (out_off == (bfd_vma) -2)
   12884 		skip = TRUE, relocate = TRUE;
   12885 	      out_off += (input_section->output_section->vma
   12886 			  + input_section->output_offset);
   12887 	      outrel.r_offset = out_off;
   12888 	      outrel.r_addend = rel->r_addend;
   12889 
   12890 	      /* Optimize unaligned reloc use.  */
   12891 	      if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
   12892 		  || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
   12893 		r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
   12894 	      else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
   12895 		       || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
   12896 		r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
   12897 	      else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
   12898 		       || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
   12899 		r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
   12900 
   12901 	      if (skip)
   12902 		memset (&outrel, 0, sizeof outrel);
   12903 	      else if (!SYMBOL_CALLS_LOCAL (info, &h->elf)
   12904 		       && !is_opd
   12905 		       && r_type != R_PPC64_TOC)
   12906 		outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
   12907 	      else
   12908 		{
   12909 		  /* This symbol is local, or marked to become local,
   12910 		     or this is an opd section reloc which must point
   12911 		     at a local function.  */
   12912 		  outrel.r_addend += relocation;
   12913 		  if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
   12914 		    {
   12915 		      if (is_opd && h != NULL)
   12916 			{
   12917 			  /* Lie about opd entries.  This case occurs
   12918 			     when building shared libraries and we
   12919 			     reference a function in another shared
   12920 			     lib.  The same thing happens for a weak
   12921 			     definition in an application that's
   12922 			     overridden by a strong definition in a
   12923 			     shared lib.  (I believe this is a generic
   12924 			     bug in binutils handling of weak syms.)
   12925 			     In these cases we won't use the opd
   12926 			     entry in this lib.  */
   12927 			  unresolved_reloc = FALSE;
   12928 			}
   12929 		      if (!is_opd
   12930 			  && r_type == R_PPC64_ADDR64
   12931 			  && (h != NULL
   12932 			      ? h->elf.type == STT_GNU_IFUNC
   12933 			      : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
   12934 			outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
   12935 		      else
   12936 			{
   12937 			  outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   12938 
   12939 			  /* We need to relocate .opd contents for ld.so.
   12940 			     Prelink also wants simple and consistent rules
   12941 			     for relocs.  This make all RELATIVE relocs have
   12942 			     *r_offset equal to r_addend.  */
   12943 			  relocate = TRUE;
   12944 			}
   12945 		    }
   12946 		  else
   12947 		    {
   12948 		      long indx = 0;
   12949 
   12950 		      if (h != NULL
   12951 			  ? h->elf.type == STT_GNU_IFUNC
   12952 			  : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
   12953 			{
   12954 			  (*_bfd_error_handler)
   12955 			    (_("%B(%A+0x%lx): relocation %s for indirect "
   12956 			       "function %s unsupported"),
   12957 			     input_bfd,
   12958 			     input_section,
   12959 			     (long) rel->r_offset,
   12960 			     ppc64_elf_howto_table[r_type]->name,
   12961 			     sym_name);
   12962 			  ret = FALSE;
   12963 			}
   12964 		      else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
   12965 			;
   12966 		      else if (sec == NULL || sec->owner == NULL)
   12967 			{
   12968 			  bfd_set_error (bfd_error_bad_value);
   12969 			  return FALSE;
   12970 			}
   12971 		      else
   12972 			{
   12973 			  asection *osec;
   12974 
   12975 			  osec = sec->output_section;
   12976 			  indx = elf_section_data (osec)->dynindx;
   12977 
   12978 			  if (indx == 0)
   12979 			    {
   12980 			      if ((osec->flags & SEC_READONLY) == 0
   12981 				  && htab->elf.data_index_section != NULL)
   12982 				osec = htab->elf.data_index_section;
   12983 			      else
   12984 				osec = htab->elf.text_index_section;
   12985 			      indx = elf_section_data (osec)->dynindx;
   12986 			    }
   12987 			  BFD_ASSERT (indx != 0);
   12988 
   12989 			  /* We are turning this relocation into one
   12990 			     against a section symbol, so subtract out
   12991 			     the output section's address but not the
   12992 			     offset of the input section in the output
   12993 			     section.  */
   12994 			  outrel.r_addend -= osec->vma;
   12995 			}
   12996 
   12997 		      outrel.r_info = ELF64_R_INFO (indx, r_type);
   12998 		    }
   12999 		}
   13000 
   13001 	      sreloc = elf_section_data (input_section)->sreloc;
   13002 	      if (!htab->elf.dynamic_sections_created)
   13003 		sreloc = htab->reliplt;
   13004 	      if (sreloc == NULL)
   13005 		abort ();
   13006 
   13007 	      if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
   13008 		  >= sreloc->size)
   13009 		abort ();
   13010 	      loc = sreloc->contents;
   13011 	      loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
   13012 	      bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
   13013 
   13014 	      /* If this reloc is against an external symbol, it will
   13015 		 be computed at runtime, so there's no need to do
   13016 		 anything now.  However, for the sake of prelink ensure
   13017 		 that the section contents are a known value.  */
   13018 	      if (! relocate)
   13019 		{
   13020 		  unresolved_reloc = FALSE;
   13021 		  /* The value chosen here is quite arbitrary as ld.so
   13022 		     ignores section contents except for the special
   13023 		     case of .opd where the contents might be accessed
   13024 		     before relocation.  Choose zero, as that won't
   13025 		     cause reloc overflow.  */
   13026 		  relocation = 0;
   13027 		  addend = 0;
   13028 		  /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
   13029 		     to improve backward compatibility with older
   13030 		     versions of ld.  */
   13031 		  if (r_type == R_PPC64_ADDR64)
   13032 		    addend = outrel.r_addend;
   13033 		  /* Adjust pc_relative relocs to have zero in *r_offset.  */
   13034 		  else if (ppc64_elf_howto_table[r_type]->pc_relative)
   13035 		    addend = (input_section->output_section->vma
   13036 			      + input_section->output_offset
   13037 			      + rel->r_offset);
   13038 		}
   13039 	    }
   13040 	  break;
   13041 
   13042 	case R_PPC64_COPY:
   13043 	case R_PPC64_GLOB_DAT:
   13044 	case R_PPC64_JMP_SLOT:
   13045 	case R_PPC64_JMP_IREL:
   13046 	case R_PPC64_RELATIVE:
   13047 	  /* We shouldn't ever see these dynamic relocs in relocatable
   13048 	     files.  */
   13049 	  /* Fall through.  */
   13050 
   13051 	case R_PPC64_PLTGOT16:
   13052 	case R_PPC64_PLTGOT16_DS:
   13053 	case R_PPC64_PLTGOT16_HA:
   13054 	case R_PPC64_PLTGOT16_HI:
   13055 	case R_PPC64_PLTGOT16_LO:
   13056 	case R_PPC64_PLTGOT16_LO_DS:
   13057 	case R_PPC64_PLTREL32:
   13058 	case R_PPC64_PLTREL64:
   13059 	  /* These ones haven't been implemented yet.  */
   13060 
   13061 	  (*_bfd_error_handler)
   13062 	    (_("%B: relocation %s is not supported for symbol %s."),
   13063 	     input_bfd,
   13064 	     ppc64_elf_howto_table[r_type]->name, sym_name);
   13065 
   13066 	  bfd_set_error (bfd_error_invalid_operation);
   13067 	  ret = FALSE;
   13068 	  continue;
   13069 	}
   13070 
   13071       /* Multi-instruction sequences that access the TOC can be
   13072 	 optimized, eg. addis ra,r2,0; addi rb,ra,x;
   13073 	 to             nop;           addi rb,r2,x;  */
   13074       switch (r_type)
   13075 	{
   13076 	default:
   13077 	  break;
   13078 
   13079 	case R_PPC64_GOT_TLSLD16_HI:
   13080 	case R_PPC64_GOT_TLSGD16_HI:
   13081 	case R_PPC64_GOT_TPREL16_HI:
   13082 	case R_PPC64_GOT_DTPREL16_HI:
   13083 	case R_PPC64_GOT16_HI:
   13084 	case R_PPC64_TOC16_HI:
   13085 	  /* These relocs would only be useful if building up an
   13086 	     offset to later add to r2, perhaps in an indexed
   13087 	     addressing mode instruction.  Don't try to optimize.
   13088 	     Unfortunately, the possibility of someone building up an
   13089 	     offset like this or even with the HA relocs, means that
   13090 	     we need to check the high insn when optimizing the low
   13091 	     insn.  */
   13092 	  break;
   13093 
   13094 	case R_PPC64_GOT_TLSLD16_HA:
   13095 	case R_PPC64_GOT_TLSGD16_HA:
   13096 	case R_PPC64_GOT_TPREL16_HA:
   13097 	case R_PPC64_GOT_DTPREL16_HA:
   13098 	case R_PPC64_GOT16_HA:
   13099 	case R_PPC64_TOC16_HA:
   13100 	  /* nop is done later.  */
   13101 	  break;
   13102 
   13103 	case R_PPC64_GOT_TLSLD16_LO:
   13104 	case R_PPC64_GOT_TLSGD16_LO:
   13105 	case R_PPC64_GOT_TPREL16_LO_DS:
   13106 	case R_PPC64_GOT_DTPREL16_LO_DS:
   13107 	case R_PPC64_GOT16_LO:
   13108 	case R_PPC64_GOT16_LO_DS:
   13109 	case R_PPC64_TOC16_LO:
   13110 	case R_PPC64_TOC16_LO_DS:
   13111 	  if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000)
   13112 	    {
   13113 	      bfd_byte *p = contents + (rel->r_offset & ~3);
   13114 	      insn = bfd_get_32 (input_bfd, p);
   13115 	      if ((insn & (0x3f << 26)) == 14u << 26 /* addi */
   13116 		  || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
   13117 		  || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
   13118 		  || (insn & (0x3f << 26)) == 36u << 26 /* stw */
   13119 		  || (insn & (0x3f << 26)) == 38u << 26 /* stb */
   13120 		  || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
   13121 		  || (insn & (0x3f << 26)) == 42u << 26 /* lha */
   13122 		  || (insn & (0x3f << 26)) == 44u << 26 /* sth */
   13123 		  || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
   13124 		  || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
   13125 		  || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
   13126 		  || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
   13127 		  || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
   13128 		  || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
   13129 		  || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
   13130 		      && (insn & 3) != 1)
   13131 		  || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
   13132 		      && ((insn & 3) == 0 || (insn & 3) == 3)))
   13133 		{
   13134 		  unsigned int reg = (insn >> 16) & 0x1f;
   13135 		  const Elf_Internal_Rela *ha;
   13136 		  bfd_boolean match_addend;
   13137 
   13138 		  match_addend = (sym != NULL
   13139 				  && ELF_ST_TYPE (sym->st_info) == STT_SECTION);
   13140 		  ha = ha_reloc_match (relocs, rel, &reg, match_addend,
   13141 				       input_bfd, contents);
   13142 		  if (ha != NULL)
   13143 		    {
   13144 		      insn &= ~(0x1f << 16);
   13145 		      insn |= reg << 16;
   13146 		      bfd_put_32 (input_bfd, insn, p);
   13147 		      if (ha_opt == NULL)
   13148 			{
   13149 			  ha_opt = bfd_zmalloc (input_section->reloc_count);
   13150 			  if (ha_opt == NULL)
   13151 			    return FALSE;
   13152 			}
   13153 		      ha_opt[ha - relocs] = 1;
   13154 		    }
   13155 		  else
   13156 		    /* If we don't find a matching high part insn,
   13157 		       something is fishy.  Refuse to nop any high
   13158 		       part insn in this section.  */
   13159 		    no_ha_opt = TRUE;
   13160 		}
   13161 	    }
   13162 	  break;
   13163 	}
   13164 
   13165       /* Do any further special processing.  */
   13166       switch (r_type)
   13167 	{
   13168 	default:
   13169 	  break;
   13170 
   13171 	case R_PPC64_ADDR16_HA:
   13172 	case R_PPC64_REL16_HA:
   13173 	case R_PPC64_ADDR16_HIGHERA:
   13174 	case R_PPC64_ADDR16_HIGHESTA:
   13175 	case R_PPC64_TOC16_HA:
   13176 	case R_PPC64_SECTOFF_HA:
   13177 	case R_PPC64_TPREL16_HA:
   13178 	case R_PPC64_DTPREL16_HA:
   13179 	case R_PPC64_TPREL16_HIGHER:
   13180 	case R_PPC64_TPREL16_HIGHERA:
   13181 	case R_PPC64_TPREL16_HIGHEST:
   13182 	case R_PPC64_TPREL16_HIGHESTA:
   13183 	case R_PPC64_DTPREL16_HIGHER:
   13184 	case R_PPC64_DTPREL16_HIGHERA:
   13185 	case R_PPC64_DTPREL16_HIGHEST:
   13186 	case R_PPC64_DTPREL16_HIGHESTA:
   13187 	  /* It's just possible that this symbol is a weak symbol
   13188 	     that's not actually defined anywhere. In that case,
   13189 	     'sec' would be NULL, and we should leave the symbol
   13190 	     alone (it will be set to zero elsewhere in the link).  */
   13191 	  if (sec == NULL)
   13192 	    break;
   13193 	  /* Fall thru */
   13194 
   13195 	case R_PPC64_GOT16_HA:
   13196 	case R_PPC64_PLTGOT16_HA:
   13197 	case R_PPC64_PLT16_HA:
   13198 	case R_PPC64_GOT_TLSGD16_HA:
   13199 	case R_PPC64_GOT_TLSLD16_HA:
   13200 	case R_PPC64_GOT_TPREL16_HA:
   13201 	case R_PPC64_GOT_DTPREL16_HA:
   13202 	  /* Add 0x10000 if sign bit in 0:15 is set.
   13203 	     Bits 0:15 are not used.  */
   13204 	  addend += 0x8000;
   13205 	  break;
   13206 
   13207 	case R_PPC64_ADDR16_DS:
   13208 	case R_PPC64_ADDR16_LO_DS:
   13209 	case R_PPC64_GOT16_DS:
   13210 	case R_PPC64_GOT16_LO_DS:
   13211 	case R_PPC64_PLT16_LO_DS:
   13212 	case R_PPC64_SECTOFF_DS:
   13213 	case R_PPC64_SECTOFF_LO_DS:
   13214 	case R_PPC64_TOC16_DS:
   13215 	case R_PPC64_TOC16_LO_DS:
   13216 	case R_PPC64_PLTGOT16_DS:
   13217 	case R_PPC64_PLTGOT16_LO_DS:
   13218 	case R_PPC64_GOT_TPREL16_DS:
   13219 	case R_PPC64_GOT_TPREL16_LO_DS:
   13220 	case R_PPC64_GOT_DTPREL16_DS:
   13221 	case R_PPC64_GOT_DTPREL16_LO_DS:
   13222 	case R_PPC64_TPREL16_DS:
   13223 	case R_PPC64_TPREL16_LO_DS:
   13224 	case R_PPC64_DTPREL16_DS:
   13225 	case R_PPC64_DTPREL16_LO_DS:
   13226 	  insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
   13227 	  mask = 3;
   13228 	  /* If this reloc is against an lq insn, then the value must be
   13229 	     a multiple of 16.  This is somewhat of a hack, but the
   13230 	     "correct" way to do this by defining _DQ forms of all the
   13231 	     _DS relocs bloats all reloc switches in this file.  It
   13232 	     doesn't seem to make much sense to use any of these relocs
   13233 	     in data, so testing the insn should be safe.  */
   13234 	  if ((insn & (0x3f << 26)) == (56u << 26))
   13235 	    mask = 15;
   13236 	  if (((relocation + addend) & mask) != 0)
   13237 	    {
   13238 	      (*_bfd_error_handler)
   13239 		(_("%B(%A+0x%lx): error: %s not a multiple of %u"),
   13240 		 input_bfd, input_section, (long) rel->r_offset,
   13241 		 ppc64_elf_howto_table[r_type]->name,
   13242 		 mask + 1);
   13243 	      bfd_set_error (bfd_error_bad_value);
   13244 	      ret = FALSE;
   13245 	      continue;
   13246 	    }
   13247 	  break;
   13248 	}
   13249 
   13250       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
   13251 	 because such sections are not SEC_ALLOC and thus ld.so will
   13252 	 not process them.  */
   13253       if (unresolved_reloc
   13254 	  && !((input_section->flags & SEC_DEBUGGING) != 0
   13255 	       && h->elf.def_dynamic))
   13256 	{
   13257 	  (*_bfd_error_handler)
   13258 	    (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
   13259 	     input_bfd,
   13260 	     input_section,
   13261 	     (long) rel->r_offset,
   13262 	     ppc64_elf_howto_table[(int) r_type]->name,
   13263 	     h->elf.root.root.string);
   13264 	  ret = FALSE;
   13265 	}
   13266 
   13267       r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
   13268 				    input_bfd,
   13269 				    input_section,
   13270 				    contents,
   13271 				    rel->r_offset,
   13272 				    relocation,
   13273 				    addend);
   13274 
   13275       if (r != bfd_reloc_ok)
   13276 	{
   13277 	  if (sym_name == NULL)
   13278 	    sym_name = "(null)";
   13279 	  if (r == bfd_reloc_overflow)
   13280 	    {
   13281 	      if (warned)
   13282 		continue;
   13283 	      if (h != NULL
   13284 		  && h->elf.root.type == bfd_link_hash_undefweak
   13285 		  && ppc64_elf_howto_table[r_type]->pc_relative)
   13286 		{
   13287 		  /* Assume this is a call protected by other code that
   13288 		     detects the symbol is undefined.  If this is the case,
   13289 		     we can safely ignore the overflow.  If not, the
   13290 		     program is hosed anyway, and a little warning isn't
   13291 		     going to help.  */
   13292 
   13293 		  continue;
   13294 		}
   13295 
   13296 	      if (!((*info->callbacks->reloc_overflow)
   13297 		    (info, (h ? &h->elf.root : NULL), sym_name,
   13298 		     ppc64_elf_howto_table[r_type]->name,
   13299 		     orig_addend, input_bfd, input_section, rel->r_offset)))
   13300 		return FALSE;
   13301 	    }
   13302 	  else
   13303 	    {
   13304 	      (*_bfd_error_handler)
   13305 		(_("%B(%A+0x%lx): %s reloc against `%s': error %d"),
   13306 		 input_bfd,
   13307 		 input_section,
   13308 		 (long) rel->r_offset,
   13309 		 ppc64_elf_howto_table[r_type]->name,
   13310 		 sym_name,
   13311 		 (int) r);
   13312 	      ret = FALSE;
   13313 	    }
   13314 	}
   13315     }
   13316 
   13317   if (ha_opt != NULL)
   13318     {
   13319       if (!no_ha_opt)
   13320 	{
   13321 	  unsigned char *opt = ha_opt;
   13322 	  rel = relocs;
   13323 	  relend = relocs + input_section->reloc_count;
   13324 	  for (; rel < relend; opt++, rel++)
   13325 	    if (*opt != 0)
   13326 	      {
   13327 		bfd_byte *p = contents + (rel->r_offset & ~3);
   13328 		bfd_put_32 (input_bfd, NOP, p);
   13329 	      }
   13330 	}
   13331       free (ha_opt);
   13332     }
   13333 
   13334   /* If we're emitting relocations, then shortly after this function
   13335      returns, reloc offsets and addends for this section will be
   13336      adjusted.  Worse, reloc symbol indices will be for the output
   13337      file rather than the input.  Save a copy of the relocs for
   13338      opd_entry_value.  */
   13339   if (is_opd && (info->emitrelocations || info->relocatable))
   13340     {
   13341       bfd_size_type amt;
   13342       amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
   13343       rel = bfd_alloc (input_bfd, amt);
   13344       BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd_relocs == NULL);
   13345       ppc64_elf_tdata (input_bfd)->opd_relocs = rel;
   13346       if (rel == NULL)
   13347 	return FALSE;
   13348       memcpy (rel, relocs, amt);
   13349     }
   13350   return ret;
   13351 }
   13352 
   13353 /* Adjust the value of any local symbols in opd sections.  */
   13354 
   13355 static int
   13356 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
   13357 			      const char *name ATTRIBUTE_UNUSED,
   13358 			      Elf_Internal_Sym *elfsym,
   13359 			      asection *input_sec,
   13360 			      struct elf_link_hash_entry *h)
   13361 {
   13362   struct _opd_sec_data *opd;
   13363   long adjust;
   13364   bfd_vma value;
   13365 
   13366   if (h != NULL)
   13367     return 1;
   13368 
   13369   opd = get_opd_info (input_sec);
   13370   if (opd == NULL || opd->adjust == NULL)
   13371     return 1;
   13372 
   13373   value = elfsym->st_value - input_sec->output_offset;
   13374   if (!info->relocatable)
   13375     value -= input_sec->output_section->vma;
   13376 
   13377   adjust = opd->adjust[value / 8];
   13378   if (adjust == -1)
   13379     return 2;
   13380 
   13381   elfsym->st_value += adjust;
   13382   return 1;
   13383 }
   13384 
   13385 /* Finish up dynamic symbol handling.  We set the contents of various
   13386    dynamic sections here.  */
   13387 
   13388 static bfd_boolean
   13389 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
   13390 				 struct bfd_link_info *info,
   13391 				 struct elf_link_hash_entry *h,
   13392 				 Elf_Internal_Sym *sym)
   13393 {
   13394   struct ppc_link_hash_table *htab;
   13395   struct plt_entry *ent;
   13396   Elf_Internal_Rela rela;
   13397   bfd_byte *loc;
   13398 
   13399   htab = ppc_hash_table (info);
   13400   if (htab == NULL)
   13401     return FALSE;
   13402 
   13403   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
   13404     if (ent->plt.offset != (bfd_vma) -1)
   13405       {
   13406 	/* This symbol has an entry in the procedure linkage
   13407 	   table.  Set it up.  */
   13408 	if (!htab->elf.dynamic_sections_created
   13409 	    || h->dynindx == -1)
   13410 	  {
   13411 	    BFD_ASSERT (h->type == STT_GNU_IFUNC
   13412 			&& h->def_regular
   13413 			&& (h->root.type == bfd_link_hash_defined
   13414 			    || h->root.type == bfd_link_hash_defweak));
   13415 	    rela.r_offset = (htab->iplt->output_section->vma
   13416 			     + htab->iplt->output_offset
   13417 			     + ent->plt.offset);
   13418 	    rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
   13419 	    rela.r_addend = (h->root.u.def.value
   13420 			     + h->root.u.def.section->output_offset
   13421 			     + h->root.u.def.section->output_section->vma
   13422 			     + ent->addend);
   13423 	    loc = (htab->reliplt->contents
   13424 		   + (htab->reliplt->reloc_count++
   13425 		      * sizeof (Elf64_External_Rela)));
   13426 	  }
   13427 	else
   13428 	  {
   13429 	    rela.r_offset = (htab->plt->output_section->vma
   13430 			     + htab->plt->output_offset
   13431 			     + ent->plt.offset);
   13432 	    rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
   13433 	    rela.r_addend = ent->addend;
   13434 	    loc = (htab->relplt->contents
   13435 		   + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE)
   13436 		      / (PLT_ENTRY_SIZE / sizeof (Elf64_External_Rela))));
   13437 	  }
   13438 	bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
   13439       }
   13440 
   13441   if (h->needs_copy)
   13442     {
   13443       /* This symbol needs a copy reloc.  Set it up.  */
   13444 
   13445       if (h->dynindx == -1
   13446 	  || (h->root.type != bfd_link_hash_defined
   13447 	      && h->root.type != bfd_link_hash_defweak)
   13448 	  || htab->relbss == NULL)
   13449 	abort ();
   13450 
   13451       rela.r_offset = (h->root.u.def.value
   13452 		       + h->root.u.def.section->output_section->vma
   13453 		       + h->root.u.def.section->output_offset);
   13454       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
   13455       rela.r_addend = 0;
   13456       loc = htab->relbss->contents;
   13457       loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
   13458       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
   13459     }
   13460 
   13461   /* Mark some specially defined symbols as absolute.  */
   13462   if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
   13463     sym->st_shndx = SHN_ABS;
   13464 
   13465   return TRUE;
   13466 }
   13467 
   13468 /* Used to decide how to sort relocs in an optimal manner for the
   13469    dynamic linker, before writing them out.  */
   13470 
   13471 static enum elf_reloc_type_class
   13472 ppc64_elf_reloc_type_class (const Elf_Internal_Rela *rela)
   13473 {
   13474   enum elf_ppc64_reloc_type r_type;
   13475 
   13476   r_type = ELF64_R_TYPE (rela->r_info);
   13477   switch (r_type)
   13478     {
   13479     case R_PPC64_RELATIVE:
   13480       return reloc_class_relative;
   13481     case R_PPC64_JMP_SLOT:
   13482       return reloc_class_plt;
   13483     case R_PPC64_COPY:
   13484       return reloc_class_copy;
   13485     default:
   13486       return reloc_class_normal;
   13487     }
   13488 }
   13489 
   13490 /* Finish up the dynamic sections.  */
   13491 
   13492 static bfd_boolean
   13493 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
   13494 				   struct bfd_link_info *info)
   13495 {
   13496   struct ppc_link_hash_table *htab;
   13497   bfd *dynobj;
   13498   asection *sdyn;
   13499 
   13500   htab = ppc_hash_table (info);
   13501   if (htab == NULL)
   13502     return FALSE;
   13503 
   13504   dynobj = htab->elf.dynobj;
   13505   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
   13506 
   13507   if (htab->elf.dynamic_sections_created)
   13508     {
   13509       Elf64_External_Dyn *dyncon, *dynconend;
   13510 
   13511       if (sdyn == NULL || htab->got == NULL)
   13512 	abort ();
   13513 
   13514       dyncon = (Elf64_External_Dyn *) sdyn->contents;
   13515       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
   13516       for (; dyncon < dynconend; dyncon++)
   13517 	{
   13518 	  Elf_Internal_Dyn dyn;
   13519 	  asection *s;
   13520 
   13521 	  bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
   13522 
   13523 	  switch (dyn.d_tag)
   13524 	    {
   13525 	    default:
   13526 	      continue;
   13527 
   13528 	    case DT_PPC64_GLINK:
   13529 	      s = htab->glink;
   13530 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   13531 	      /* We stupidly defined DT_PPC64_GLINK to be the start
   13532 		 of glink rather than the first entry point, which is
   13533 		 what ld.so needs, and now have a bigger stub to
   13534 		 support automatic multiple TOCs.  */
   13535 	      dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
   13536 	      break;
   13537 
   13538 	    case DT_PPC64_OPD:
   13539 	      s = bfd_get_section_by_name (output_bfd, ".opd");
   13540 	      if (s == NULL)
   13541 		continue;
   13542 	      dyn.d_un.d_ptr = s->vma;
   13543 	      break;
   13544 
   13545 	    case DT_PPC64_OPDSZ:
   13546 	      s = bfd_get_section_by_name (output_bfd, ".opd");
   13547 	      if (s == NULL)
   13548 		continue;
   13549 	      dyn.d_un.d_val = s->size;
   13550 	      break;
   13551 
   13552 	    case DT_PLTGOT:
   13553 	      s = htab->plt;
   13554 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   13555 	      break;
   13556 
   13557 	    case DT_JMPREL:
   13558 	      s = htab->relplt;
   13559 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   13560 	      break;
   13561 
   13562 	    case DT_PLTRELSZ:
   13563 	      dyn.d_un.d_val = htab->relplt->size;
   13564 	      break;
   13565 
   13566 	    case DT_RELASZ:
   13567 	      /* Don't count procedure linkage table relocs in the
   13568 		 overall reloc count.  */
   13569 	      s = htab->relplt;
   13570 	      if (s == NULL)
   13571 		continue;
   13572 	      dyn.d_un.d_val -= s->size;
   13573 	      break;
   13574 
   13575 	    case DT_RELA:
   13576 	      /* We may not be using the standard ELF linker script.
   13577 		 If .rela.plt is the first .rela section, we adjust
   13578 		 DT_RELA to not include it.  */
   13579 	      s = htab->relplt;
   13580 	      if (s == NULL)
   13581 		continue;
   13582 	      if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
   13583 		continue;
   13584 	      dyn.d_un.d_ptr += s->size;
   13585 	      break;
   13586 	    }
   13587 
   13588 	  bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
   13589 	}
   13590     }
   13591 
   13592   if (htab->got != NULL && htab->got->size != 0)
   13593     {
   13594       /* Fill in the first entry in the global offset table.
   13595 	 We use it to hold the link-time TOCbase.  */
   13596       bfd_put_64 (output_bfd,
   13597 		  elf_gp (output_bfd) + TOC_BASE_OFF,
   13598 		  htab->got->contents);
   13599 
   13600       /* Set .got entry size.  */
   13601       elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
   13602     }
   13603 
   13604   if (htab->plt != NULL && htab->plt->size != 0)
   13605     {
   13606       /* Set .plt entry size.  */
   13607       elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
   13608 	= PLT_ENTRY_SIZE;
   13609     }
   13610 
   13611   /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
   13612      brlt ourselves if emitrelocations.  */
   13613   if (htab->brlt != NULL
   13614       && htab->brlt->reloc_count != 0
   13615       && !_bfd_elf_link_output_relocs (output_bfd,
   13616 				       htab->brlt,
   13617 				       elf_section_data (htab->brlt)->rela.hdr,
   13618 				       elf_section_data (htab->brlt)->relocs,
   13619 				       NULL))
   13620     return FALSE;
   13621 
   13622   if (htab->glink != NULL
   13623       && htab->glink->reloc_count != 0
   13624       && !_bfd_elf_link_output_relocs (output_bfd,
   13625 				       htab->glink,
   13626 				       elf_section_data (htab->glink)->rela.hdr,
   13627 				       elf_section_data (htab->glink)->relocs,
   13628 				       NULL))
   13629     return FALSE;
   13630 
   13631   /* We need to handle writing out multiple GOT sections ourselves,
   13632      since we didn't add them to DYNOBJ.  We know dynobj is the first
   13633      bfd.  */
   13634   while ((dynobj = dynobj->link_next) != NULL)
   13635     {
   13636       asection *s;
   13637 
   13638       if (!is_ppc64_elf (dynobj))
   13639 	continue;
   13640 
   13641       s = ppc64_elf_tdata (dynobj)->got;
   13642       if (s != NULL
   13643 	  && s->size != 0
   13644 	  && s->output_section != bfd_abs_section_ptr
   13645 	  && !bfd_set_section_contents (output_bfd, s->output_section,
   13646 					s->contents, s->output_offset,
   13647 					s->size))
   13648 	return FALSE;
   13649       s = ppc64_elf_tdata (dynobj)->relgot;
   13650       if (s != NULL
   13651 	  && s->size != 0
   13652 	  && s->output_section != bfd_abs_section_ptr
   13653 	  && !bfd_set_section_contents (output_bfd, s->output_section,
   13654 					s->contents, s->output_offset,
   13655 					s->size))
   13656 	return FALSE;
   13657     }
   13658 
   13659   return TRUE;
   13660 }
   13661 
   13662 #include "elf64-target.h"
   13663